1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
|
/************************************************************************
************************************************************************
FAUST compiler
Copyright (C) 2003-2018 GRAME, Centre National de Creation Musicale
---------------------------------------------------------------------
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
************************************************************************
************************************************************************/
#ifndef _Text_H
#define _Text_H
#include <string.h>
#include <fstream>
#include <iostream>
#include <list>
#include <sstream>
#include <string>
#include <vector>
#include <cmath>
using namespace std;
string subst(const string& m, const string& a0);
string subst(const string& m, const vector<string>& vargs);
string subst(const string& m, const string& a0, const string& a1);
string subst(const string& m, const string& a0, const string& a1, const string& a2);
string subst(const string& model, const string& a0, const string& a1, const string& a2, const string& a3);
string subst(const string& model, const string& a0, const string& a1, const string& a2, const string& a3,
const string& a4);
string subst(const string& model, const string& a0, const string& a1, const string& a2, const string& a3,
const string& a4, const string& a5);
string subst(const string& model, const string& a0, const string& a1, const string& a2, const string& a3,
const string& a4, const string& a5, const string& a6);
string T(char* c);
string T(int n);
string T(long n);
string T(float n);
string T(double n);
// add and remove quotes of a string
string unquote(const string& s);
string quote(const string& s);
void tab(int n, ostream& fout);
void back(int n, ostream& fout);
void printlines(int n, list<string>& lines, ostream& fout, const string& sep = "");
string rmWhiteSpaces(const string& s);
inline string checkFloat(float val)
{
return (std::isinf(val)) ? "INFINITY" : T(val);
}
inline string checkDouble(double val)
{
return (std::isinf(val)) ? "INFINITY" : T(val);
}
string checkReal(double val);
string indent(string const& str, int tabs);
string replaceChar(string str, char ch1, char ch2);
string replaceCharList(string str, const vector<char>& ch1, char ch2);
inline bool checkMin(const string& str)
{
return ((str == "min") || (str == "min_i") || (str == "min_f") || (str == "min_") || (str == "min_l"));
}
inline bool checkMax(const string& str)
{
return ((str == "max") || (str == "max_i") || (str == "max_f") || (str == "max_") || (str == "max_l"));
}
inline bool checkMinMax(const string& str)
{
return checkMin(str) || checkMax(str);
}
inline bool startWith(const string& str, const string& prefix)
{
return (str.substr(0, prefix.size()) == prefix);
}
inline bool endWith(const string& str, const string& suffix)
{
size_t i = str.rfind(suffix);
return (i != string::npos) && (i == (str.length() - suffix.length()));
}
inline string startWithRes(const string& str, const string& prefix)
{
return (str.substr(0, prefix.size()) == prefix) ? str.substr(prefix.size()) : "";
}
inline bool startWithRes(const string& str, const string& prefix, string& res)
{
if (str.substr(0, prefix.size()) == prefix) {
res = str.substr(prefix.size());
return true;
} else {
return false;
}
}
inline string removeChar(const string& str, char c)
{
string res;
res.reserve(str.size()); // optional, avoids buffer reallocations in the loop
for (size_t i = 0; i < str.size(); ++i) {
if (str[i] != c) res += str[i];
}
return res;
}
inline bool replaceExtension(const string& str, const string& term, string& res)
{
size_t pos = str.rfind('.');
if (pos != string::npos) {
res = str.substr(0, pos) + term;
return true;
} else {
res = str;
return false;
}
}
inline string pathToContent(const string& path)
{
ifstream file(path.c_str(), ifstream::binary);
file.seekg(0, file.end);
int size = int(file.tellg());
file.seekg(0, file.beg);
// And allocate buffer to that a single line can be read...
char* buffer = new char[size + 1];
file.read(buffer, size);
// Terminate the string
buffer[size] = 0;
string result = buffer;
file.close();
delete[] buffer;
return result;
}
// For soundfile : remove spaces between filenames and possibly
// put a unique file in a {...} list
inline string prepareURL(const string& url)
{
bool in_str = false;
stringstream dst;
for (size_t i = 0; i < url.size(); i++) {
switch (url[i]) {
case '\n':
case '\t':
case '\r':
break;
case '\'':
in_str = !in_str;
dst << url[i];
break;
case ' ':
// Do not remove spaces in path ('....')
if (in_str) dst << url[i];
break;
default:
dst << url[i];
break;
}
}
string res = dst.str();
// If unique file, create a list with it
return (res[0] != '{') ? "{'" + res + "'}" : res;
}
inline string flatten(const string& src)
{
string dst;
for (size_t i = 0; i < src.size(); i++) {
switch (src[i]) {
case '\n':
case '\t':
case '\r':
break;
case ' ':
if (!(i + 1 < src.size() && src[i + 1] == ' ')) {
dst += src[i];
}
break;
default:
dst += src[i];
break;
}
}
return dst;
}
// To be used for WASM or SOUL
inline string flattenJSON(const string& src)
{
string dst;
for (size_t i = 0; i < src.size(); i++) {
switch (src[i]) {
case '"':
dst += "\\\"";
break;
case '\\':
dst += "\\";
break;
case '\'':
dst += "\\'";
break;
default:
dst += src[i];
break;
}
}
return dst;
}
// To be used for JavaScript
inline string flattenJSON1(const string& src)
{
string dst;
for (size_t i = 0; i < src.size(); i++) {
switch (src[i]) {
case '\\':
dst += "\\";
break;
case '\'':
dst += "\\'";
break;
default:
dst += src[i];
break;
}
}
return dst;
}
// To filter compilation arguments in 'createDSPFactoryFromString' and 'createInterpreterDSPFactoryFromString'
inline bool testArg(const char* arg)
{
vector<const char*> filter_argv = { "-tg", "-sg", "-ps", "-svg", "-mdoc", "-mdlang", "-stripdoc", "-sd", "-xml", "-json" };
for (size_t i = 0; i < filter_argv.size(); i++) {
if (strcmp(filter_argv[i], arg) == 0) return true;
}
return false;
}
vector<string> tokenizeString(const string& str, char sep);
#endif
|