- Written some code for command processing. Still not tested it. Feedback is very much appreciated!

git-svn-id: svn://svn.gna.org/svn/sgpemv2/trunk@746 3ecf2c5c-341e-0410-92b4-d18e462d057c
This commit is contained in:
elvez 2006-07-09 16:27:16 +00:00
parent 0e79b163f3
commit 2ede92d6d1
4 changed files with 444 additions and 23 deletions

View file

@ -20,9 +20,15 @@
#include "string_utils.hh"
#include <sstream>
using namespace std;
//using namespace sgpem;
using Glib::ustring;
namespace sgpem
{
/**
\brief A function that converts a Unicode string to an integer value
@ -66,6 +72,30 @@ string_to_int(const ustring& str, int& num)
return true;
}
int
string_to_int(const ustring& str) throw(domain_error)
{
if(tokenize(str).size() != 1)
throw domain_error("too few or too many tokens");
istringstream iss(str);
iss.exceptions(ios_base::failbit | ios_base::badbit);
int value;
try
{
iss >> value;
}
catch(ios_base::failure& e)
{
throw domain_error(e.what());
}
return value;
}
/**
\brief A function that converts an integer value to an Unicode string
*/
@ -112,3 +142,49 @@ string_to_float(const Glib::ustring& str, float& f)
ss << str;
ss >> f;
}
// helper function for tokenize()
static void
add_token(Tokens& tokens, const ustring& token)
{
if(token.size() > 0)
tokens.push_back(token);
}
Tokens
tokenize(const ustring& str)
{
istringstream iss(str);
Tokens tokens;
while(iss)
{
ustring token;
iss >> token;
ustring::size_type index;
// for the SET command, parse the assigment symbol as
// a separate token
do
{
index = token.find('=');
if(index != ustring::npos)
{
add_token(tokens, token.substr(0, index));
add_token(tokens, "=");
// the second argument can safely be out of range
token = token.substr(index + 1, token.size());
}
}
while(index != ustring::npos);
add_token(tokens, token);
}
return tokens;
}
}

View file

@ -24,45 +24,54 @@
#include "config.h"
#include <sstream>
#include <iostream>
#include <vector>
#include <stdexcept>
#include <glibmm/ustring.h>
// FIXME : mark this file as deprecated.
#warning "This file is obsolete and deprecated."
#warning "It will be removed soon. Please update your code!"
namespace sgpem
{
typedef std::vector<Glib::ustring> Tokens;
/**\brief This function tries to convert a string into an integer value.
/**\brief This function tries to convert a string into an integer value.
The string can contain only digits and the minus character (for negative numbers).
The string can contain only digits and the minus character (for negative numbers).
\returns TRUE if ths string represent a valid integer number
\returns FALSE otherwise
*/
bool SG_DLLEXPORT string_to_int(const Glib::ustring&, int&);
\returns TRUE if ths string represent a valid integer number
\returns FALSE otherwise
*/
bool SG_DLLEXPORT string_to_int(const Glib::ustring&, int&);
/**\brief This function converts an integer value into a string.
int SG_DLLEXPORT string_to_int(const Glib::ustring&) throw(std::domain_error);
There is no return value because this function always succeeds.
*/
void SG_DLLEXPORT int_to_string(const int&, Glib::ustring&);
/**\brief This function converts an integer value into a string.
There is no return value because this function always succeeds.
*/
void SG_DLLEXPORT int_to_string(const int&, Glib::ustring&);
/**\brief This function converts a float value into a string.
/**\brief This function converts a float value into a string.
There is no return value because this function always succeeds.
*/
void SG_DLLEXPORT float_to_string(const float&, Glib::ustring&);
There is no return value because this function always succeeds.
*/
void SG_DLLEXPORT float_to_string(const float&, Glib::ustring&);
/**\brief This function tries to convert a string into a float value.
/**\brief This function tries to convert a string into a float value.
The string can contain only digits, the minus, plus and dot (-+.) characters. If not,
the value 0 is assigned.
The string can contain only digits, the minus, plus and dot (-+.) characters. If not,
the value 0 is assigned.
There is no return value because this function always succeeds, even if the string is badly formed.
*/
void SG_DLLEXPORT string_to_float(const Glib::ustring&, float&);
There is no return value because this function always succeeds, even if the string is badly formed.
*/
void SG_DLLEXPORT string_to_float(const Glib::ustring&, float&);
Tokens tokenize(const Glib::ustring& str);
}
#endif