00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <stdio.h>
00025 #include <string.h>
00026 #include "Text.hh"
00027 #include "compatibility.hh"
00028 #include <string>
00029 #include <vector>
00030
00031
00032 static string substitution (const string& model, const vector<string>& args)
00033 {
00034 char c;
00035 int i=0, ilast = model.length()-1;
00036 string result;
00037
00038 while (i < ilast) {
00039 c = model[i++];
00040 if (c != '$') {
00041 result += c;
00042 } else {
00043 c = model[i++];
00044 if (c >= '0' && c <= '9') {
00045 result += args[c - '0'];
00046 } else {
00047 result += c;
00048 }
00049 }
00050 }
00051 if (i == ilast) result += model[i];
00052 return result;
00053 }
00054 string subst (const string& model, const vector<string>& args)
00055 {
00056 return substitution(model, args);
00057 }
00058
00059 string subst (const string& model, const string& a0)
00060 {
00061 vector<string> args(10);
00062 args[0] = a0;
00063 return substitution (model, args);
00064 }
00065
00066 string subst (const string& model, const string& a0, const string& a1)
00067 {
00068 vector<string> args(10);
00069 args[0] = a0;
00070 args[1] = a1;
00071
00072 return substitution (model, args);
00073 }
00074
00075 string subst (const string& model, const string& a0, const string& a1, const string& a2)
00076 {
00077 vector<string> args(10);
00078
00079 args[0] = a0;
00080 args[1] = a1;
00081 args[2] = a2;
00082
00083 return substitution (model, args);
00084 }
00085
00086 string subst (const string& model, const string& a0, const string& a1, const string& a2, const string& a3)
00087 {
00088 vector<string> args(10);
00089
00090 args[0] = a0;
00091 args[1] = a1;
00092 args[2] = a2;
00093 args[3] = a3;
00094
00095 return substitution (model, args);
00096 }
00097
00098 string subst (const string& model, const string& a0, const string& a1, const string& a2, const string& a3, const string& a4)
00099 {
00100 vector<string> args(10);
00101
00102 args[0] = a0;
00103 args[1] = a1;
00104 args[2] = a2;
00105 args[3] = a3;
00106 args[4] = a4;
00107
00108 return substitution (model, args);
00109 }
00110
00111 string subst (const string& model, const string& a0, const string& a1, const string& a2, const string& a3, const string& a4, const string& a5)
00112 {
00113 vector<string> args(10);
00114
00115 args[0] = a0;
00116 args[1] = a1;
00117 args[2] = a2;
00118 args[3] = a3;
00119 args[4] = a4;
00120 args[5] = a5;
00121
00122 return substitution (model, args);
00123 }
00124
00125
00126
00127 static void zdel(char* c)
00128 {
00129 int l = strlen(c) - 1;
00130 while ( l>1 && c[l-1] != '.' && c[l] == '0') c[l--]=0;
00131 }
00132
00133 string T (char* c) { return string(c); }
00134 string T (int n) { char c[64]; snprintf(c, 63, "%d",n); return string(c); }
00135 string T (long n) { char c[64]; snprintf(c, 63, "%ld",n); return string(c); }
00136
00137 #if 1
00138 string T (float n)
00139 {
00140 char c[64];
00141 if (n < 0.1 && n > -0.1 && n != 0.0) {
00142 snprintf(c, 63, "%ef", n);
00143 } else {
00144 snprintf(c, 63, "%ff", n);
00145 zdel(c);
00146 }
00147 return string(c);
00148 }
00149
00150 string T (double n)
00151 {
00152 char c[64];
00153 if (n < 0.1 && n > -0.1) {
00154 snprintf(c, 63, "%ef", n);
00155 } else {
00156 snprintf(c, 63, "%ff", n);
00157 zdel(c);
00158 }
00159 return string(c);
00160 }
00161 #else
00162 string T (float n)
00163 {
00164 char c[64];
00165 if (n < 0.1 && n > -0.1 && n != 0.0) {
00166 snprintf(c, 63, "%e", n);
00167 } else {
00168 snprintf(c, 63, "%f", n);
00169 zdel(c);
00170 }
00171 return string(c);
00172 }
00173
00174 string T (double n)
00175 {
00176 char c[64];
00177 if (n < 0.1 && n > -0.1) {
00178 snprintf(c, 63, "%e", n);
00179 } else {
00180 snprintf(c, 63, "%f", n);
00181 zdel(c);
00182 }
00183 return string(c);
00184 }
00185 #endif