src/misc.cpp

changeset 508
7ace3537a560
parent 507
fc76d38c3530
child 538
2f85d4d286e5
equal deleted inserted replaced
507:fc76d38c3530 508:7ace3537a560
126 126
127 return out; 127 return out;
128 } 128 }
129 129
130 // ============================================================================= 130 // =============================================================================
131 // Float to string. Removes trailing zeroes and is locale-independant. 131 // -----------------------------------------------------------------------------
132 // TODO: Replace with QString::number() 132 bool numeric (const str& tok)
133 // -----------------------------------------------------------------------------
134 str ftoa (double num)
135 { // Disable the locale first so that the decimal point will not
136 // turn into anything weird (like commas)
137 setlocale (LC_NUMERIC, "C");
138
139 str rep;
140 rep.sprintf ("%f", num);
141
142 // Remove trailing zeroes
143 while (rep.right (1) == "0")
144 rep.chop (1);
145
146 // If there were only zeroes in the decimal place, remove
147 // the decimal point now.
148 if (rep.right (1) == ".")
149 rep.chop (1);
150
151 return rep;
152 }
153
154 // =============================================================================
155 // TODO: I guess Qt must have something like this stashed somewhere?
156 // -----------------------------------------------------------------------------
157 bool isNumber (const str& tok)
158 { bool gotDot = false; 133 { bool gotDot = false;
159 134
160 for (int i = 0; i < tok.length(); ++i) 135 for (int i = 0; i < tok.length(); ++i)
161 { const qchar c = tok[i]; 136 { const qchar c = tok[i];
162 137
278 253
279 for (const StringFormatArg& arg : vals) 254 for (const StringFormatArg& arg : vals)
280 list << arg.value(); 255 list << arg.value();
281 256
282 return list.join (delim); 257 return list.join (delim);
283 }
284
285 // =============================================================================
286 // TODO: I'm quite sure Qt has this covered as well.
287 // -----------------------------------------------------------------------------
288 double atof (str val)
289 { // Disable the locale while parsing the line or atof's behavior changes
290 // between locales (i.e. fails to read decimals properly). That is
291 // quite undesired...
292 setlocale (LC_NUMERIC, "C");
293
294 char* buf = new char[val.length()];
295 char* bufptr = &buf[0];
296
297 for (QChar& c : val)
298 *bufptr++ = c.toLatin1();
299
300 *bufptr = '\0';
301
302 double fval = atof (buf);
303 delete[] buf;
304 return fval;
305 } 258 }
306 259
307 // ============================================================================= 260 // =============================================================================
308 // This is the main algorithm of the ring finder. It tries to use math to find 261 // This is the main algorithm of the ring finder. It tries to use math to find
309 // the one ring between r0 and r1. If it fails (the ring number is non-integral), 262 // the one ring between r0 and r1. If it fails (the ring number is non-integral),

mercurial