- 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

@ -104,7 +104,7 @@ XMLSerializerFactory::create_resource(Parameters& parameters)
if (id.length() > 6 && id.substr(0, 6) == Glib::ustring("reskey"))
{
key = id.substr(6);
string_to_int(key, old_key);
old_key = string_to<int>(key);
}
}
@ -119,14 +119,14 @@ XMLSerializerFactory::create_resource(Parameters& parameters)
pos = parameters.find(Glib::ustring("arrival-time"));
if (pos != parameters.end())
{
string_to_int(pos->second, arrival_time);
arrival_time = string_to<int>(pos->second);
}
// read "how-many" property
pos = parameters.find(Glib::ustring("how-many"));
if (pos != parameters.end())
{
string_to_int(pos->second, how_many);
how_many = string_to<int>(pos->second);
}
@ -160,14 +160,14 @@ XMLSerializerFactory::create_process(Parameters& parameters)
pos = parameters.find(Glib::ustring("arrival-time"));
if (pos != parameters.end())
{
string_to_int(pos->second, arrival_time);
arrival_time = string_to<int>(pos->second);
}
// read "priority" property
pos = parameters.find(Glib::ustring("priority"));
if (pos != parameters.end())
{
string_to_int(pos->second, priority);
priority = string_to<int>(pos->second);
}
_last_process = &_hist->add_process(name, arrival_time, priority);
@ -200,21 +200,21 @@ XMLSerializerFactory::create_thread(Parameters& parameters)
pos = parameters.find(Glib::ustring("arrival-delta"));
if (pos != parameters.end())
{
string_to_int(pos->second, arrival_time);
arrival_time = string_to<int>(pos->second);
}
// read "priority" property
pos = parameters.find(Glib::ustring("priority"));
if (pos != parameters.end())
{
string_to_int(pos->second, priority);
priority = string_to<int>(pos->second);
}
// read "priority" property
pos = parameters.find(Glib::ustring("lasts-for"));
if (pos != parameters.end())
{
string_to_int(pos->second, lasts_for);
lasts_for = string_to<int>(pos->second);
}
_last_thread = &_hist->add_thread(name, *_last_process, lasts_for, arrival_time, priority);
@ -235,7 +235,7 @@ XMLSerializerFactory::create_request(Parameters& parameters)
pos = parameters.find(Glib::ustring("arrival-time"));
if (pos != parameters.end())
{
string_to_int(pos->second, arrival_time);
arrival_time = string_to<int>(pos->second);
}
_last_request = &_hist->add_request(*_last_thread, arrival_time);
@ -259,7 +259,7 @@ XMLSerializerFactory::create_subrequest(Parameters& parameters)
pos = parameters.find(Glib::ustring("resource"));
if (pos != parameters.end())
{
string_to_int(pos->second, old_key);
old_key = string_to<int>(pos->second);
TempMap::iterator temp_pos;
temp_pos = _temp_map.find((resource_key_t)old_key);
@ -274,7 +274,7 @@ XMLSerializerFactory::create_subrequest(Parameters& parameters)
pos = parameters.find(Glib::ustring("lasts-for"));
if (pos != parameters.end())
{
string_to_int(pos->second, lasts_for);
lasts_for = string_to<int>(pos->second);
}
return _hist->add_subrequest(*_last_request, new_key, (History::time_t)lasts_for);

View File

@ -129,7 +129,7 @@ void XMLVisitor::from_environment(xmlNodePtr parent, const Environment& env) thr
{
//XMLVisitor xvisit(resources_node);
Glib::ustring key;
int_to_string((int)(*iter).first, key);
to_string<int>(static_cast<int>((*iter).first), key);
//xvisit.from_resource(*((*iter).second), key);
from_resource(resources_node, *((*iter).second), key);
iter++;
@ -164,7 +164,7 @@ void XMLVisitor::from_resource(xmlNodePtr parent, const Resource& obj, const Gli
Glib::ustring strPreemptible("false"); // fixed??
Glib::ustring strArrivalTime("0"); // fixed??
Glib::ustring strPlaces;
int_to_string((int)obj.get_places(), strPlaces);
to_string<int>(static_cast<int>(obj.get_places()), strPlaces);
xmlNodePtr process_node = xmlNewChild(parent, NULL, (const xmlChar *) "resource", NULL);
xmlNewProp(process_node, (const xmlChar *) "name", (const xmlChar *) obj.get_name().c_str());
@ -186,8 +186,8 @@ void XMLVisitor::from_process(xmlNodePtr parent, const Process& obj) throw(Seria
{
Glib::ustring strPriority;
Glib::ustring strArrivalTime;
int_to_string(obj.get_base_priority(), strPriority);
int_to_string(obj.get_arrival_time(), strArrivalTime);
to_string<int>(obj.get_base_priority(), strPriority);
to_string<int>(obj.get_arrival_time(), strArrivalTime);
xmlNodePtr process_node = xmlNewChild(parent, NULL, (const xmlChar *) "process", NULL);
xmlNewProp(process_node, (const xmlChar *) "name", (const xmlChar *) obj.get_name().c_str());
@ -226,9 +226,9 @@ void XMLVisitor::from_thread(xmlNodePtr parent, const Thread& obj) throw(Seriali
Glib::ustring strPriority;
Glib::ustring strArrivalTime;
Glib::ustring strLastsTime;
int_to_string(obj.get_base_priority(), strPriority);
int_to_string(obj.get_arrival_time(), strArrivalTime);
int_to_string(obj.get_elapsed_time(), strLastsTime);
to_string<int>(obj.get_base_priority(), strPriority);
to_string<int>(obj.get_arrival_time(), strArrivalTime);
to_string<int>(obj.get_elapsed_time(), strLastsTime);
// get_elapsed_time() or get_total_cpu_time() ???
xmlNodePtr thread_node = xmlNewChild(parent, NULL, (const xmlChar *) "thread", NULL);
xmlNewProp(thread_node, (const xmlChar *) "name", (const xmlChar *) obj.get_name().c_str());
@ -265,7 +265,7 @@ void XMLVisitor::from_request(xmlNodePtr parent, const Request& obj) throw(Seria
{
Glib::ustring strArrivalTime;
int_to_string(obj.get_instant(), strArrivalTime);
to_string<int>(obj.get_instant(), strArrivalTime);
xmlNodePtr request_node = xmlNewChild(parent, NULL, (const xmlChar *) "request", NULL);
xmlNewProp(request_node, (const xmlChar *) "arrival-time", (const xmlChar *) strArrivalTime.c_str());
@ -299,8 +299,8 @@ void XMLVisitor::from_subrequest(xmlNodePtr parent, const SubRequest& obj) throw
Glib::ustring strResource;
Glib::ustring strLastsFor;
int_to_string(obj.get_resource_key(), strResource);
int_to_string(obj.get_length(), strLastsFor);
to_string<int>(obj.get_resource_key(), strResource);
to_string<int>(obj.get_length(), strLastsFor);
xmlNodePtr subrequest_node = xmlNewChild(parent, NULL, (const xmlChar *) "subrequest", NULL);
xmlNewProp(subrequest_node, (const xmlChar *) "resource", (const xmlChar *) strResource.c_str());

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());