Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

Utilities Class Reference

A collection of utility functions. More...

#include <Utilities.h>

List of all members.

Static Public Methods

std::string lower_case (std::string s)
 Return the lower case equivalent of a string.

std::string to_string (bool value)
 Return the string equivalent of a boolean: "truie" or "false".

std::string to_string (int value)
 Return the string equivalent of an integer (base 10).

std::string to_string (float value)
 Return the string equivalent of a floating point number in form X...X.YY (two digits after the decimal).

std::string to_string (color)
 Return the string equivalent of a color, all lowercase.

std::string to_string (const Position &)
 Return the string equivalent to the input Position.

bool from_string (const std::string &, bool &out)
 Converts a string to a boolean.

bool from_string (const std::string &, int &out)
 Converts a string to a integer.

bool from_string (const std::string &, float &out)
 Converts a string to a float.

bool from_string (const std::string &, color &out)
 Converts a string to a color.

bool from_string (const std::string &, Position &out)
 Converts a string to a Position.


Detailed Description

A collection of utility functions.

This class does not have instances, but rather collects together a number of functions related to converting information to and from string format.


Member Function Documentation

bool Utilities::from_string const std::string &   ,
Position &    out
[static]
 

Converts a string to a Position.

Returns :
true if string parameter represents a Position, that is, is of the form float,float.
Parameters:
out  set to the Position (if any)

00200 {
00201   const char *s = input.c_str();
00202   const char *t = 0;
00203 
00204   float x = string_to_double(s,t);
00205   if (s == t) return false;
00206   // no X coordinate
00207 
00208   if (*t != ',') return false;
00209   s = t+1;
00210   // check for comma
00211 
00212   float y = string_to_double(s,t);
00213   if (s == t) return false;
00214   // no Y coordinate
00215 
00216   result = Position(x,y);
00217   return true;
00218 }

bool Utilities::from_string const std::string &   ,
color   out
[static]
 

Converts a string to a color.

Returns :
true if string parameter names an EZWindows color.
Parameters:
out  set to the color (if any)

00186 {
00187   string copy = lower_case(input);
00188   if (copy == "black") { result = Black; return true; }
00189   if (copy == "white") { result = White; return true; }
00190   if (copy == "red") { result = Red; return true; }
00191   if (copy == "green") { result = Green; return true; }
00192   if (copy == "blue") { result = Blue; return true; }
00193   if (copy == "yellow") { result = Yellow; return true; }
00194   if (copy == "cyan") { result = Cyan; return true; }
00195   if (copy == "magenta") { result = Magenta; return true; }
00196   return false;
00197 }

bool Utilities::from_string const std::string &   ,
float &    out
[static]
 

Converts a string to a float.

Returns :
true if string parameter represents a float.
Parameters:
out  set to the float (if any)

00173 {
00174   const char *s = input.c_str();
00175   const char *t = 0;
00176   double tmp = string_to_double(s,t);
00177 
00178   if (s == t || *t != '\0') return false;
00179   // didn't work.
00180 
00181   f = tmp;
00182   return true;
00183 }

bool Utilities::from_string const std::string &   ,
int &    out
[static]
 

Converts a string to a integer.

Returns :
true if string parameter represents a integer.
Parameters:
out  set to the integer (if any)

00138 {
00139   const char *s = input.c_str();
00140   int temp = 0;
00141   bool negative = false;
00142 
00143   // remove the negative sign, if there is one
00144   if (*s == '-') {
00145     ++s;
00146     negative = true;
00147   }
00148 
00149   if (!*s) return false;
00150   // not a number if stops before getting anywhere
00151 
00152   while ('0' <= *s && *s <= '9') {
00153     // if another digit
00154 
00155     temp *= 10;
00156     temp += *s - '0';
00157     // add it in;
00158 
00159     ++s;
00160     // continue;
00161   }
00162 
00163   if (*s) return false;
00164   // not a number if has trailing junk.
00165 
00166   result = negative ? -temp : temp;
00167   // set result according to whether minus sign found
00168 
00169   return true;
00170 }

bool Utilities::from_string const std::string &   ,
bool &    out
[static]
 

Converts a string to a boolean.

Returns :
true if string parameter represents a boolean.
Parameters:
out  set to the boolean (if any)

00120 {
00121   string copy = lower_case(input);
00122   if (copy == "true" || copy == "yes") {
00123     result = true;
00124     return true;
00125   } else if (copy == "false" || copy == "no") {
00126     result = false;
00127     return true;
00128   } else {
00129     return false;
00130   }
00131 }

string Utilities::lower_case std::string    s [static]
 

Return the lower case equivalent of a string.

00022 {
00023   for (int i = 0, s = output.size(); i < s; ++i)
00024     if (isupper(output[i]))
00025       output[i] = tolower(output[i]);
00026   return output;
00027 }

string Utilities::to_string const Position &    [static]
 

Return the string equivalent to the input Position.

Result will be in the form x,y

00111 {
00112   return to_string(x.GetXDistance()) + "," + to_string(x.GetYDistance());
00113 }

string Utilities::to_string color    value [static]
 

Return the string equivalent of a color, all lowercase.

00092 {
00093   switch (value) {
00094   case Black: return "black";
00095   case White: return "white";
00096   case Red: return "red"; 
00097   case Green: return "green";
00098   case Blue: return "blue";
00099   case Yellow: return "yellow";
00100   case Cyan: return "cyan";
00101   case Magenta: return "magenta";
00102   default:
00103     return "unknown color";
00104   }
00105 }

string Utilities::to_string float    value [static]
 

Return the string equivalent of a floating point number in form X...X.YY (two digits after the decimal).

If the number is exactly integral, no decimal point or fraction is produced.

00067 {
00068   int ivalue = (int)value;
00069   string intpart = to_string(ivalue);
00070   float remainder = value - ivalue;
00071 
00072   if (remainder == 0.0) return intpart;
00073 
00074   if (remainder < 0.0) {
00075     if (ivalue == 0) intpart = "-0";
00076     remainder = -remainder;
00077   }
00078   // if the number was negative, the negation is already handled
00079   // by intpart.
00080 
00081   int hundredths = int(remainder*100.0+0.5);
00082   return intpart
00083     + "."
00084     + char('0' + hundredths/10)
00085     + char('0' + hundredths%10);
00086 }

string Utilities::to_string int    value [static]
 

Return the string equivalent of an integer (base 10).

00041 {
00042   if (value == 0) return "0";
00043   // Zero is a special case.
00044 
00045   bool negative = value < 0;
00046   if (negative) value = 0 - value;
00047   // if the parameter value is negative, it must be inverted
00048 
00049   string output = "";
00050   do {
00051     output = char('0' + value % 10) + output;
00052     // append the rightmost digit to the head of the output string
00053     value /= 10;
00054     // drop that digit (shift over the digits),
00055   } while (value > 0); // until the value becomes 0.
00056 
00057   // if the original value was negative, prepend a '-' to the string to return.
00058   return (negative) ? ('-' + output) : output;
00059 }

string Utilities::to_string bool    value [static]
 

Return the string equivalent of a boolean: "truie" or "false".

00033 {
00034   return (value) ? "true" : "false";
00035 }


The documentation for this class was generated from the following files:
Generated on Fri Nov 8 10:52:33 2002 for Draw by doxygen1.2.17