FAUST compiler  0.9.9.6b8
Text.cpp
Go to the documentation of this file.
00001 /************************************************************************
00002  ************************************************************************
00003     FAUST compiler
00004     Copyright (C) 2003-2004 GRAME, Centre National de Creation Musicale
00005     ---------------------------------------------------------------------
00006     This program is free software; you can redistribute it and/or modify
00007     it under the terms of the GNU General Public License as published by
00008     the Free Software Foundation; either version 2 of the License, or
00009     (at your option) any later version.
00010 
00011     This program is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014     GNU General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License
00017     along with this program; if not, write to the Free Software
00018     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00019  ************************************************************************
00020  ************************************************************************/
00021 
00022 
00023 
00024 #include <stdio.h>
00025 #include <stdlib.h>
00026 #include <string.h>
00027 #include "Text.hh"
00028 #include "compatibility.hh"
00029 #include <string>
00030 #include <vector>
00031 #include <iostream>
00032 #include <sstream>
00033 #include <assert.h>
00034 
00035 #include "floats.hh"
00036 
00037 extern bool gInternDoubleSwitch;
00038 
00039 static string substitution (const string& model, const vector<string>& args);
00040 
00047 string subst (const string& model, const vector<string>& args)
00048 {
00049     return substitution(model, args);
00050 }
00051 
00052 string subst (const string& model, const string& a0)
00053 {
00054     vector<string> args(10);
00055     args[0] = a0;
00056     return substitution (model, args);
00057 }
00058 
00059 string subst (const string& model, const string& a0, const string& a1)
00060 {
00061     vector<string> args(10);
00062     args[0] = a0;
00063     args[1] = a1;
00064 
00065     return substitution (model, args);
00066 }
00067 
00068 string subst (const string& model, const string& a0, const string& a1, const string& a2)
00069 {
00070     vector<string> args(10);
00071 
00072     args[0] = a0;
00073     args[1] = a1;
00074     args[2] = a2;
00075 
00076     return substitution (model, args);
00077 }
00078 
00079 string subst (const string& model, const string& a0, const string& a1, const string& a2, const string& a3)
00080 {
00081     vector<string> args(10);
00082 
00083     args[0] = a0;
00084     args[1] = a1;
00085     args[2] = a2;
00086     args[3] = a3;
00087 
00088     return substitution (model, args);
00089 }
00090 
00091 string subst (const string& model, const string& a0, const string& a1, const string& a2, const string& a3, const string& a4)
00092 {
00093     vector<string> args(10);
00094 
00095     args[0] = a0;
00096     args[1] = a1;
00097     args[2] = a2;
00098     args[3] = a3;
00099     args[4] = a4;
00100 
00101     return substitution (model, args);
00102 }
00103 
00104 string subst (const string& model, const string& a0, const string& a1, const string& a2, const string& a3, const string& a4, const string& a5)
00105 {
00106     vector<string> args(10);
00107 
00108     args[0] = a0;
00109     args[1] = a1;
00110     args[2] = a2;
00111     args[3] = a3;
00112     args[4] = a4;
00113     args[5] = a5;
00114 
00115     return substitution (model, args);
00116 }
00117 
00118 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)
00119 {
00120     vector<string> args(10);
00121 
00122     args[0] = a0;
00123     args[1] = a1;
00124     args[2] = a2;
00125     args[3] = a3;
00126     args[4] = a4;
00127     args[5] = a5;
00128     args[6] = a6;
00129 
00130     return substitution (model, args);
00131 }
00132 
00133 
00134 static string substitution (const string& model, const vector<string>& args)
00135 {
00136     char    c;
00137     int     i=0, ilast = model.length()-1;
00138     string  result;
00139 
00140     while (i < ilast) {
00141         c = model[i++];
00142         if (c != '$') {
00143             result += c;
00144         } else {
00145             c = model[i++];
00146             if (c >= '0' && c <= '9') {
00147                 result += args[c - '0'];
00148             } else {
00149                 result += c;
00150             }
00151         }
00152     }
00153     if (i == ilast) result += model[i];
00154     return result;
00155 }
00156 
00157 
00158 string T (char* c)  { return string(c); }
00159 string T (int n)    { char c[64]; snprintf(c, 63, "%d",n);  return string(c); }
00160 string T (long n)   { char c[64]; snprintf(c, 63, "%ld",n); return string(c); }
00161 
00162 
00168 static void ensureFloat(char* c)
00169 {
00170     bool isInt = true;
00171     while (*c != 0) {
00172         if ((*c == '.') | (*c == 'e'))  isInt = false;
00173         c++;
00174     }
00175 
00176     if (isInt) {
00177         *c++ = '.';
00178         *c++ = '0';
00179         *c   = 0;
00180     }
00181 }
00182 
00188 string T(double n)
00189 {
00190     char    c[64];
00191     int     p = 1;
00192 
00193     do { snprintf(c, 32, "%.*g", p++, n); } while (atof(c) != n);
00194     ensureFloat(c);
00195     return string(c)+inumix();
00196 }
00197 
00198 
00202 string unquote(const string& s)
00203 {
00204     assert(s.size() >= 2);
00205     assert(s[0] == '"');
00206     assert(s[s.size()-1] == '"');
00207     return s.substr(1, s.size()-2);
00208 }
00209 
00210 
00214 string quote(const string& s)
00215 {
00216     string q("\"");
00217     q += s;
00218     q += "\"";
00219     return q;
00220 }
00221 
00222 string rmWhiteSpaces(const string& s)
00223 {
00224     size_t i = s.find_first_not_of(" \t");
00225     size_t j = s.find_last_not_of(" \t");
00226 
00227     if ( (i != string::npos) & (j != string::npos) ) {
00228         return s.substr(i, 1+j-i);
00229     } else {
00230         return "";
00231     }
00232 }