/*************************************************************************** * Copyright (C) 2005 by Sebastian Beck * * sebastian.bw@freenet.de * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ /* This function makes a unsigned long int value (32 bits) contaning the numbers * in the same order like in the QString. If no point is in the string, * precision (default=2) is filled with 0 . * Example: * bool OK * QString s="1234,5678"; //can also be ".2"->20, "0012"->1200 * unsigned long int result = StringToLong( s, &OK, 3 ); // = 1234567 * if( ! OK ) error(); * Be aware that unsigned long int can hold the last 10 numbers not above 4294967296 (2 exp 32 ). * *ok is true if the numbers laying behind each other are one of "1234567890" or nothing(0 , empty char) * and can include at one time one of ".," else 0 is returned*/ unsigned long int StringToNum( QString string, bool* ok, char precision=2 ) { unsigned long int li = 0; uchar e = string.length(); bool point = FALSE; uint n; *ok = TRUE; for( n=0; n < e; n++ ) { QChar c = string.at(n); switch( c ) { case '0' : case 0 : li = li * 10; break; case '1' : li = li * 10 + 1; break; case '2' : li = li * 10 + 2; break; case '3' : li = li * 10 + 3; break; case '4' : li = li * 10 + 4; break; case '5' : li = li * 10 + 5; break; case '6' : li = li * 10 + 6; break; case '7' : li = li * 10 + 7; break; case '8' : li = li * 10 + 8; break; case '9' : li = li * 10 + 9; break; case '.' : case ',' : if( ! point ) { point = TRUE; e = n+precision+1; break; } else { *ok = FALSE; return 0; } default : *ok = FALSE; return 0; } } if( ! point ) for( n=0; n