Merge branch 'master' of https://scm.cri.ensmp.fr/git/Faustine
[Faustine.git] / interpretor / faust-0.9.47mr3 / compiler / generator / Text.cpp
1 /************************************************************************
2 ************************************************************************
3 FAUST compiler
4 Copyright (C) 2003-2004 GRAME, Centre National de Creation Musicale
5 ---------------------------------------------------------------------
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 ************************************************************************
20 ************************************************************************/
21
22
23
24 #include <math.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include "Text.hh"
29 #include "compatibility.hh"
30 #include <string>
31 #include <vector>
32 #include <iostream>
33 #include <sstream>
34 #include <assert.h>
35
36 #include "floats.hh"
37
38 extern bool gInternDoubleSwitch;
39
40 static string substitution (const string& model, const vector<string>& args);
41
42 /**
43 * Text substitution. Creates a string by replacing all the $n
44 * occurences in the model string, with the corresponding arguments.
45 * Example :
46 * subst("float $0 = $1;", "var", T(10.2))
47 */
48 string subst (const string& model, const vector<string>& args)
49 {
50 return substitution(model, args);
51 }
52
53 string subst (const string& model, const string& a0)
54 {
55 vector<string> args(10);
56 args[0] = a0;
57 return substitution (model, args);
58 }
59
60 string subst (const string& model, const string& a0, const string& a1)
61 {
62 vector<string> args(10);
63 args[0] = a0;
64 args[1] = a1;
65
66 return substitution (model, args);
67 }
68
69 string subst (const string& model, const string& a0, const string& a1, const string& a2)
70 {
71 vector<string> args(10);
72
73 args[0] = a0;
74 args[1] = a1;
75 args[2] = a2;
76
77 return substitution (model, args);
78 }
79
80 string subst (const string& model, const string& a0, const string& a1, const string& a2, const string& a3)
81 {
82 vector<string> args(10);
83
84 args[0] = a0;
85 args[1] = a1;
86 args[2] = a2;
87 args[3] = a3;
88
89 return substitution (model, args);
90 }
91
92 string subst (const string& model, const string& a0, const string& a1, const string& a2, const string& a3, const string& a4)
93 {
94 vector<string> args(10);
95
96 args[0] = a0;
97 args[1] = a1;
98 args[2] = a2;
99 args[3] = a3;
100 args[4] = a4;
101
102 return substitution (model, args);
103 }
104
105 string subst (const string& model, const string& a0, const string& a1, const string& a2, const string& a3, const string& a4, const string& a5)
106 {
107 vector<string> args(10);
108
109 args[0] = a0;
110 args[1] = a1;
111 args[2] = a2;
112 args[3] = a3;
113 args[4] = a4;
114 args[5] = a5;
115
116 return substitution (model, args);
117 }
118
119 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)
120 {
121 vector<string> args(10);
122
123 args[0] = a0;
124 args[1] = a1;
125 args[2] = a2;
126 args[3] = a3;
127 args[4] = a4;
128 args[5] = a5;
129 args[6] = a6;
130
131 return substitution (model, args);
132 }
133
134
135 static string substitution (const string& model, const vector<string>& args)
136 {
137 char c;
138 int i=0, ilast = model.length()-1;
139 string result;
140
141 while (i < ilast) {
142 c = model[i++];
143 if (c != '$') {
144 result += c;
145 } else {
146 c = model[i++];
147 if (c >= '0' && c <= '9') {
148 result += args[c - '0'];
149 } else {
150 result += c;
151 }
152 }
153 }
154 if (i == ilast) result += model[i];
155 return result;
156 }
157
158
159 string T (char* c) { return string(c); }
160 string T (int n) { char c[64]; snprintf(c, 63, "%d",n); return string(c); }
161 string T (long n) { char c[64]; snprintf(c, 63, "%ld",n); return string(c); }
162
163
164 /**
165 * If needed add a trailing '.0' to the
166 * the textual representation of a floating point number
167 * to avoid confusions with an int.
168 */
169 static void ensureFloat(char* c)
170 {
171 bool isInt = true;
172 while (*c != 0) {
173 if ((*c == '.') | (*c == 'e')) isInt = false;
174 c++;
175 }
176
177 if (isInt) {
178 *c++ = '.';
179 *c++ = '0';
180 *c = 0;
181 }
182 }
183
184 /**
185 * Convert a double-precision float into a string.
186 * Adjusts the precision p to the needs. Add a trailing
187 * f if single-precision is required.
188 */
189 string T(double n)
190 {
191 char c[64];
192 int p = 1;
193 if (isfinite(n)) {
194 do { snprintf(c, 32, "%.*g", p++, n); } while (atof(c) != n);
195 ensureFloat(c);
196 return string(c)+inumix();
197 } else {
198 snprintf(c, 32, "%g", n);
199 return string(c);
200 }
201 }
202
203
204 /**
205 * remove quotes from a string
206 */
207 string unquote(const string& s)
208 {
209 assert(s.size() >= 2);
210 assert(s[0] == '"');
211 assert(s[s.size()-1] == '"');
212 return s.substr(1, s.size()-2);
213 }
214
215
216 /**
217 * add quotes to a string
218 */
219 string quote(const string& s)
220 {
221 string q("\"");
222 q += s;
223 q += "\"";
224 return q;
225 }
226
227 string rmWhiteSpaces(const string& s)
228 {
229 size_t i = s.find_first_not_of(" \t");
230 size_t j = s.find_last_not_of(" \t");
231
232 if ( (i != string::npos) & (j != string::npos) ) {
233 return s.substr(i, 1+j-i);
234 } else {
235 return "";
236 }
237 }