115 lines
2.6 KiB
C++
115 lines
2.6 KiB
C++
// src/backend/string_utils.cc - Copyright 2005, 2006, University
|
|
// of Padova, dept. of Pure and Applied
|
|
// Mathematics
|
|
//
|
|
// This file is part of SGPEMv2.
|
|
//
|
|
// This 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.
|
|
//
|
|
// SGPEMv2 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 SGPEMv2; if not, write to the Free Software
|
|
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
#include "string_utils.hh"
|
|
|
|
using namespace std;
|
|
using Glib::ustring;
|
|
|
|
/**
|
|
\brief A function that converts a Unicode string to an integer value
|
|
|
|
The string can contain ONLY digits and the "minus" character.
|
|
|
|
\returns TRUE if the string is well formatted
|
|
\returns FALSE otherwise
|
|
*/
|
|
bool
|
|
string_to_int(const ustring& str, int& num)
|
|
{
|
|
static const ustring allvalid = "0123456789-";
|
|
static const ustring digits = "0123456789";
|
|
|
|
// the string can't be empty
|
|
if (str.length() == 0 || (str.length() == 1 && str[0] == '-'))
|
|
return false;
|
|
|
|
//checks if the string contains only digits
|
|
if (str.find_first_not_of(allvalid) < str.length())
|
|
return false;
|
|
if (str.substr(1).find_first_not_of(digits) < str.length()-1)
|
|
return false;
|
|
|
|
num=0;
|
|
int multiplier = 1, val;
|
|
int start; //the position of the biggest digit
|
|
if (str[0] == '-')
|
|
start = 1;
|
|
else
|
|
start = 0;
|
|
for (int pos = str.length() - 1; pos >= start ; pos--)
|
|
{
|
|
val = str[pos] - 48; //the INTEGER value of the digit
|
|
num += val*multiplier;
|
|
multiplier *= 10;
|
|
}
|
|
//if there is the minus then multiply for -1
|
|
if (start == 1)
|
|
num *= -1;
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
\brief A function that converts an integer value to an Unicode string
|
|
*/
|
|
void
|
|
int_to_string(const int& num, ustring& str)
|
|
{
|
|
if (num == 0)
|
|
{
|
|
str = '0';
|
|
return;
|
|
}
|
|
|
|
str = "";
|
|
int val = num;
|
|
bool negative = (val < 0)? true : false;
|
|
if (negative) val *= -1;
|
|
|
|
while (true)
|
|
{
|
|
str = char(val % 10 + 48) + str;
|
|
if (val > 1 && val / 10 != 0)
|
|
val /= 10;
|
|
else
|
|
break;
|
|
}
|
|
if (negative)
|
|
str = '-' + str;
|
|
}
|
|
|
|
void
|
|
float_to_string(const float& f, Glib::ustring& str)
|
|
{
|
|
stringstream ss;
|
|
ss << f;
|
|
char p[20];
|
|
ss.getline(p,20);
|
|
str = p;
|
|
}
|
|
|
|
void
|
|
string_to_float(const Glib::ustring& str, float& f)
|
|
{
|
|
stringstream ss;
|
|
ss << str;
|
|
ss >> f;
|
|
}
|