- 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")) if (id.length() > 6 && id.substr(0, 6) == Glib::ustring("reskey"))
{ {
key = id.substr(6); 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")); pos = parameters.find(Glib::ustring("arrival-time"));
if (pos != parameters.end()) if (pos != parameters.end())
{ {
string_to_int(pos->second, arrival_time); arrival_time = string_to<int>(pos->second);
} }
// read "how-many" property // read "how-many" property
pos = parameters.find(Glib::ustring("how-many")); pos = parameters.find(Glib::ustring("how-many"));
if (pos != parameters.end()) 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")); pos = parameters.find(Glib::ustring("arrival-time"));
if (pos != parameters.end()) if (pos != parameters.end())
{ {
string_to_int(pos->second, arrival_time); arrival_time = string_to<int>(pos->second);
} }
// read "priority" property // read "priority" property
pos = parameters.find(Glib::ustring("priority")); pos = parameters.find(Glib::ustring("priority"));
if (pos != parameters.end()) 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); _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")); pos = parameters.find(Glib::ustring("arrival-delta"));
if (pos != parameters.end()) if (pos != parameters.end())
{ {
string_to_int(pos->second, arrival_time); arrival_time = string_to<int>(pos->second);
} }
// read "priority" property // read "priority" property
pos = parameters.find(Glib::ustring("priority")); pos = parameters.find(Glib::ustring("priority"));
if (pos != parameters.end()) if (pos != parameters.end())
{ {
string_to_int(pos->second, priority); priority = string_to<int>(pos->second);
} }
// read "priority" property // read "priority" property
pos = parameters.find(Glib::ustring("lasts-for")); pos = parameters.find(Glib::ustring("lasts-for"));
if (pos != parameters.end()) 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); _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")); pos = parameters.find(Glib::ustring("arrival-time"));
if (pos != parameters.end()) 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); _last_request = &_hist->add_request(*_last_thread, arrival_time);
@ -259,7 +259,7 @@ XMLSerializerFactory::create_subrequest(Parameters& parameters)
pos = parameters.find(Glib::ustring("resource")); pos = parameters.find(Glib::ustring("resource"));
if (pos != parameters.end()) if (pos != parameters.end())
{ {
string_to_int(pos->second, old_key); old_key = string_to<int>(pos->second);
TempMap::iterator temp_pos; TempMap::iterator temp_pos;
temp_pos = _temp_map.find((resource_key_t)old_key); 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")); pos = parameters.find(Glib::ustring("lasts-for"));
if (pos != parameters.end()) 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); 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); //XMLVisitor xvisit(resources_node);
Glib::ustring key; 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); //xvisit.from_resource(*((*iter).second), key);
from_resource(resources_node, *((*iter).second), key); from_resource(resources_node, *((*iter).second), key);
iter++; iter++;
@ -164,7 +164,7 @@ void XMLVisitor::from_resource(xmlNodePtr parent, const Resource& obj, const Gli
Glib::ustring strPreemptible("false"); // fixed?? Glib::ustring strPreemptible("false"); // fixed??
Glib::ustring strArrivalTime("0"); // fixed?? Glib::ustring strArrivalTime("0"); // fixed??
Glib::ustring strPlaces; 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); xmlNodePtr process_node = xmlNewChild(parent, NULL, (const xmlChar *) "resource", NULL);
xmlNewProp(process_node, (const xmlChar *) "name", (const xmlChar *) obj.get_name().c_str()); 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 strPriority;
Glib::ustring strArrivalTime; Glib::ustring strArrivalTime;
int_to_string(obj.get_base_priority(), strPriority); to_string<int>(obj.get_base_priority(), strPriority);
int_to_string(obj.get_arrival_time(), strArrivalTime); to_string<int>(obj.get_arrival_time(), strArrivalTime);
xmlNodePtr process_node = xmlNewChild(parent, NULL, (const xmlChar *) "process", NULL); xmlNodePtr process_node = xmlNewChild(parent, NULL, (const xmlChar *) "process", NULL);
xmlNewProp(process_node, (const xmlChar *) "name", (const xmlChar *) obj.get_name().c_str()); 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 strPriority;
Glib::ustring strArrivalTime; Glib::ustring strArrivalTime;
Glib::ustring strLastsTime; Glib::ustring strLastsTime;
int_to_string(obj.get_base_priority(), strPriority); to_string<int>(obj.get_base_priority(), strPriority);
int_to_string(obj.get_arrival_time(), strArrivalTime); to_string<int>(obj.get_arrival_time(), strArrivalTime);
int_to_string(obj.get_elapsed_time(), strLastsTime); to_string<int>(obj.get_elapsed_time(), strLastsTime);
// get_elapsed_time() or get_total_cpu_time() ??? // get_elapsed_time() or get_total_cpu_time() ???
xmlNodePtr thread_node = xmlNewChild(parent, NULL, (const xmlChar *) "thread", NULL); xmlNodePtr thread_node = xmlNewChild(parent, NULL, (const xmlChar *) "thread", NULL);
xmlNewProp(thread_node, (const xmlChar *) "name", (const xmlChar *) obj.get_name().c_str()); 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; 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); xmlNodePtr request_node = xmlNewChild(parent, NULL, (const xmlChar *) "request", NULL);
xmlNewProp(request_node, (const xmlChar *) "arrival-time", (const xmlChar *) strArrivalTime.c_str()); 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 strResource;
Glib::ustring strLastsFor; Glib::ustring strLastsFor;
int_to_string(obj.get_resource_key(), strResource); to_string<int>(obj.get_resource_key(), strResource);
int_to_string(obj.get_length(), strLastsFor); to_string<int>(obj.get_length(), strLastsFor);
xmlNodePtr subrequest_node = xmlNewChild(parent, NULL, (const xmlChar *) "subrequest", NULL); xmlNodePtr subrequest_node = xmlNewChild(parent, NULL, (const xmlChar *) "subrequest", NULL);
xmlNewProp(subrequest_node, (const xmlChar *) "resource", (const xmlChar *) strResource.c_str()); 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) 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(); ActiveIterator act_it = _active_policies.begin();
while (act_it != _active_policies.end()) while (act_it != _active_policies.end())
{ {
if (act_it->second == *avail_it) if (act_it->second == *avail_it)
{ {
ActiveIterator removable = act_it++; act_it->second->deactivate();
removable->second->deactivate(); // Please note the postfix increment
_active_policies.erase(removable); // (operating on the old, now invalidated by
// erase, iterator object):
_active_policies.erase(act_it++);
} }
else else
act_it++; ++act_it;
} }
} //~ for(avail_it) } //~ for(avail_it)
} }

View File

@ -155,11 +155,11 @@ GlobalPreferences::key_file_read(KeyFile& kf)
_pol_dirs.clear(); _pol_dirs.clear();
// read speed // read speed
{ {
int new_speed; int new_speed = 1000; // use a default value
const Glib::ustring* val = kf.search_value(Glib::ustring("speed")); const Glib::ustring* val = kf.search_value(Glib::ustring("speed"));
if (val) if (val)
{ {
string_to_int(*val, new_speed); new_speed = string_to<int>(*val);
set_speed(new_speed); set_speed(new_speed);
} }
} }
@ -219,7 +219,7 @@ GlobalPreferences::key_file_write(KeyFile& kf)
{ {
Glib::ustring key("speed"); Glib::ustring key("speed");
Glib::ustring value; Glib::ustring value;
int_to_string(_speed, value); to_string<int>(_speed, value);
kf.insert_key_value(key, 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 (AvailableIt avail_it = policies.begin(); avail_it != policies.end(); ++avail_it)
{ {
for (PolicyIterator it = _active_policies.begin(); it != _active_policies.end();) 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) if (it->second == *avail_it)
// Please note the postfix increment (operating
// on the old iterator, now invalidated by erase)
_active_policies.erase(it++); _active_policies.erase(it++);
else else
++it; ++it;
}
} //~ for(avail_it) } //~ for(avail_it)
} }

View File

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

View File

@ -32,41 +32,17 @@ namespace sgpem
{ {
typedef std::vector<Glib::ustring> Tokens; 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> template <typename T>
T SG_DLLEXPORT string_to(const Glib::ustring&) throw(std::domain_error); 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. 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); Tokens SG_DLLEXPORT tokenize(const Glib::ustring& str);
} }

View File

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