- Unify interface of string_utils. I dropped most of the exception

handling, hoping it's unnecessary. Code should be slightly more
maintainable now.


git-svn-id: svn://svn.gna.org/svn/sgpemv2/trunk@943 3ecf2c5c-341e-0410-92b4-d18e462d057c
This commit is contained in:
tchernobog 2006-08-26 10:45:04 +00:00
parent 696c513ed2
commit 231662825d
8 changed files with 55 additions and 185 deletions

View file

@ -144,20 +144,20 @@ CPUPoliciesGatekeeper::deactivate_policies(CPUPolicyManager* manager)
for (; avail_it != avail_end; ++avail_it)
{
// TODO isn't there a way to write more compact code by using
// library utilities?
ActiveIterator act_it = _active_policies.begin();
while (act_it != _active_policies.end())
{
if (act_it->second == *avail_it)
{
ActiveIterator removable = act_it++;
removable->second->deactivate();
_active_policies.erase(removable);
act_it->second->deactivate();
// Please note the postfix increment
// (operating on the old, now invalidated by
// erase, iterator object):
_active_policies.erase(act_it++);
}
else
act_it++;
++act_it;
}
} //~ for(avail_it)
}

View file

@ -155,11 +155,11 @@ GlobalPreferences::key_file_read(KeyFile& kf)
_pol_dirs.clear();
// read speed
{
int new_speed;
int new_speed = 1000; // use a default value
const Glib::ustring* val = kf.search_value(Glib::ustring("speed"));
if (val)
{
string_to_int(*val, new_speed);
new_speed = string_to<int>(*val);
set_speed(new_speed);
}
}
@ -219,7 +219,7 @@ GlobalPreferences::key_file_write(KeyFile& kf)
{
Glib::ustring key("speed");
Glib::ustring value;
int_to_string(_speed, value);
to_string<int>(_speed, value);
kf.insert_key_value(key, value);
}

View file

@ -109,15 +109,12 @@ ResourcePoliciesGatekeeper::deactivate_policies(const ResourcePolicyManager& man
for (AvailableIt avail_it = policies.begin(); avail_it != policies.end(); ++avail_it)
{
for (PolicyIterator it = _active_policies.begin(); it != _active_policies.end();)
{
// NOTE we use postfix ++ because I'm not sure if it's
// safe to increment an invalid iterator (set::erase() makes the iterator
// invalid)
if (it->second == *avail_it)
// Please note the postfix increment (operating
// on the old iterator, now invalidated by erase)
_active_policies.erase(it++);
else
++it;
}
++it;
} //~ for(avail_it)
}

View file

@ -30,168 +30,65 @@ namespace sgpem
{
/* Force template instantiation to allow visibility outside this DSO */
template SG_DLLEXPORT int string_to<int>(const Glib::ustring&);
template SG_DLLEXPORT float string_to<float>(const Glib::ustring&);
template SG_DLLEXPORT int string_to<int>(const Glib::ustring&) throw(domain_error);
template SG_DLLEXPORT float string_to<float>(const Glib::ustring&) throw(domain_error);
template SG_DLLEXPORT double string_to<double>(const Glib::ustring&) throw(domain_error);
template SG_DLLEXPORT void to_string<int>(const int val, Glib::ustring&);
template SG_DLLEXPORT void to_string<float>(const float val, Glib::ustring&);
template SG_DLLEXPORT void to_string<double>(const double val, Glib::ustring&);
// Specialized further down in this file:
// template SG_DLLEXPORT bool string_to<bool>(const 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;
}
template <typename T>
T
string_to(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);
T value;
T result;
try
{
iss >> value;
iss >> result;
}
catch (ios_base::failure e)
{
throw domain_error(e.what());
}
// disable exceptions, otherwise peek() will throw them!
// how useless!!!
iss.exceptions(ios_base::goodbit);
if (iss.peek() != istringstream::traits_type::eof())
throw domain_error(_("incorrect number format"));
return value;
return result;
}
template <>
SG_DLLEXPORT bool
SG_DLLEXPORT bool
string_to<bool>(const Glib::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);
bool value;
try
{
iss >> boolalpha >> value;
}
catch (ios_base::failure e)
{
throw domain_error(e.what());
}
// disable exceptions, otherwise peek() will throw them!
// how useless!!!
iss.exceptions(ios_base::goodbit);
if (iss.peek() != istringstream::traits_type::eof())
throw domain_error(_("incorrect boolean"));
iss >> value;
return value;
}
/**
\brief A function that converts an integer value to an Unicode string
*/
template<typename T>
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)
to_string(const T val, Glib::ustring& str)
{
stringstream ss;
ss << f;
char p[20];
ss.getline(p, 20);
str = p;
ss << val;
ss >> str;
}
void
string_to_float(const Glib::ustring& str, float& f)
{
stringstream ss;
ss << str;
ss >> f;
}
// helper function for tokenize()
// helper function for tokenize()
static void
add_token(Tokens& tokens, const ustring& token)
{

View file

@ -32,41 +32,17 @@ namespace sgpem
{
typedef std::vector<Glib::ustring> Tokens;
/**\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).
\returns TRUE if ths string represent a valid integer number
\returns FALSE otherwise
*/
bool SG_DLLEXPORT string_to_int(const Glib::ustring&, int&);
template <typename T>
T SG_DLLEXPORT string_to(const Glib::ustring&) throw(std::domain_error);
/**\brief This function converts an integer value into a string.
/**\brief This function converts a value into a string, if possible.
There is no return value because this function always succeeds.
*/
void SG_DLLEXPORT int_to_string(const int&, Glib::ustring&);
template<typename T>
void SG_DLLEXPORT to_string(const T val, Glib::ustring&);
/**\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&);
/**\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.
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 SG_DLLEXPORT tokenize(const Glib::ustring& str);
}

View file

@ -288,7 +288,7 @@ SimulationWidget::draw_grid(cairo_t* ctx)
top_margin + graph_height);
cairo_rel_line_to(ctx, 0, 0.5 * _y_unit);
Glib::ustring val;
int_to_string(t, val);
to_string<int>(t, val);
cairo_move_to(ctx, left_graph_margin + (t+1)*_x_unit,
top_margin + graph_height + 2.0 * _y_unit);
cairo_show_text(ctx,val.c_str());