1 /************************************************************************
2 ************************************************************************
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.
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.
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 ************************************************************************/
29 #include "compatibility.hh"
38 extern bool gInternDoubleSwitch
;
40 static string
substitution (const string
& model
, const vector
<string
>& args
);
43 * Text substitution. Creates a string by replacing all the $n
44 * occurences in the model string, with the corresponding arguments.
46 * subst("float $0 = $1;", "var", T(10.2))
48 string
subst (const string
& model
, const vector
<string
>& args
)
50 return substitution(model
, args
);
53 string
subst (const string
& model
, const string
& a0
)
55 vector
<string
> args(10);
57 return substitution (model
, args
);
60 string
subst (const string
& model
, const string
& a0
, const string
& a1
)
62 vector
<string
> args(10);
66 return substitution (model
, args
);
69 string
subst (const string
& model
, const string
& a0
, const string
& a1
, const string
& a2
)
71 vector
<string
> args(10);
77 return substitution (model
, args
);
80 string
subst (const string
& model
, const string
& a0
, const string
& a1
, const string
& a2
, const string
& a3
)
82 vector
<string
> args(10);
89 return substitution (model
, args
);
92 string
subst (const string
& model
, const string
& a0
, const string
& a1
, const string
& a2
, const string
& a3
, const string
& a4
)
94 vector
<string
> args(10);
102 return substitution (model
, args
);
105 string
subst (const string
& model
, const string
& a0
, const string
& a1
, const string
& a2
, const string
& a3
, const string
& a4
, const string
& a5
)
107 vector
<string
> args(10);
116 return substitution (model
, args
);
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
)
121 vector
<string
> args(10);
131 return substitution (model
, args
);
135 static string
substitution (const string
& model
, const vector
<string
>& args
)
138 int i
=0, ilast
= model
.length()-1;
147 if (c
>= '0' && c
<= '9') {
148 result
+= args
[c
- '0'];
154 if (i
== ilast
) result
+= model
[i
];
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
); }
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.
169 static void ensureFloat(char* c
)
173 if ((*c
== '.') | (*c
== 'e')) isInt
= false;
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.
194 do { snprintf(c
, 32, "%.*g", p
++, n
); } while (atof(c
) != n
);
196 return string(c
)+inumix();
198 snprintf(c
, 32, "%g", n
);
205 * remove quotes from a string
207 string
unquote(const string
& s
)
209 assert(s
.size() >= 2);
211 assert(s
[s
.size()-1] == '"');
212 return s
.substr(1, s
.size()-2);
217 * add quotes to a string
219 string
quote(const string
& s
)
227 string
rmWhiteSpaces(const string
& s
)
229 size_t i
= s
.find_first_not_of(" \t");
230 size_t j
= s
.find_last_not_of(" \t");
232 if ( (i
!= string::npos
) & (j
!= string::npos
) ) {
233 return s
.substr(i
, 1+j
-i
);