presage  0.8.7
utility.cpp
Go to the documentation of this file.
00001 
00002 /******************************************************
00003  *  Presage, an extensible predictive text entry system
00004  *  ---------------------------------------------------
00005  *
00006  *  Copyright (C) 2008  Matteo Vescovi <matteo.vescovi@yahoo.co.uk>
00007 
00008     This program is free software; you can redistribute it and/or modify
00009     it under the terms of the GNU General Public License as published by
00010     the Free Software Foundation; either version 2 of the License, or
00011     (at your option) any later version.
00012 
00013     This program is distributed in the hope that it will be useful,
00014     but WITHOUT ANY WARRANTY; without even the implied warranty of
00015     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016     GNU General Public License for more details.
00017 
00018     You should have received a copy of the GNU General Public License along
00019     with this program; if not, write to the Free Software Foundation, Inc.,
00020     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00021                                                                              *
00022                                                                 **********(*)*/
00023 
00024 
00025 #include "utility.h"
00026 #ifdef HAVE_STRING_H
00027 # include <string.h>
00028 #endif
00029 #ifdef HAVE_STDLIB_H
00030 # include <stdlib.h>
00031 #endif
00032 
00036 char* Utility::strtolower(char* str)
00037 {
00038     for(int i = 0; str[i] != '\0'; i++)
00039         str[i] = tolower (str[i]);
00040     
00041     return str;
00042 }
00043 
00044 
00048 std::string& Utility::strtolower(std::string& str)
00049 {
00050     for(std::string::iterator i = str.begin();
00051         i != str.end();
00052         i++ )
00053         *i = tolower( *i );
00054     
00055     return str;
00056 }
00057 
00061 std::string Utility::strtolower(const std::string& str)
00062 {
00063     std::string lowstr = str;
00064     for(std::string::iterator i = lowstr.begin();
00065         i != lowstr.end();
00066         i++ )
00067         *i = tolower( *i );
00068     
00069     return lowstr;
00070 }
00071 
00072 
00076 std::string Utility::strtoupper(const std::string& str)
00077 {
00078     std::string upstr = str;
00079     for(std::string::iterator i = upstr.begin();
00080         i != upstr.end();
00081         i++ )
00082         *i = toupper( *i );
00083     
00084     return upstr;
00085 }
00086 
00087 
00091 bool Utility::isTrueFalse(const char* str)
00092 {
00093     // TODO: manage yes/no with gettext?
00094 
00095     return (isTrue (str) || isFalse (str));
00096 }
00097 
00101 bool Utility::isTrue(const char* str)
00102 {
00103     bool result = false;
00104     
00105     char* workingStr = new char[strlen (str) + 1];
00106     strcpy (workingStr, str);
00107     
00108     std::string lowstr = strtolower (workingStr);
00109     if( lowstr == "true" ||
00110         lowstr == "1" )
00111         result = true;
00112 
00113     delete[] workingStr;
00114 
00115     return result;
00116 }
00117 
00118 
00122 bool Utility::isFalse(const char* str)
00123 {
00124     bool result = false;
00125 
00126     char* workingStr = new char[strlen(str) + 1];
00127     strcpy (workingStr, str);
00128 
00129     std::string lowstr = strtolower (workingStr);
00130     if( lowstr == "false" ||
00131         lowstr == "0" )
00132         result = true;
00133     
00134     delete[] workingStr;
00135 
00136     return result;
00137 }
00138 
00139 
00143 bool Utility::isTrue(const std::string& str)
00144 {
00145     return isTrue( str.c_str() );
00146 }
00147 
00148 
00152 bool Utility::isFalse(const std::string& str)
00153 {
00154     return isFalse( str.c_str() );
00155 }
00156 
00157 
00161 bool Utility::isTrueFalse( const std::string& str )
00162 {
00163     return isTrueFalse( str.c_str() );
00164 }
00165 
00166 
00170 bool Utility::isYesNo(const char* str)
00171 {
00172     return (isYes (str) || isNo (str));
00173 }
00174 
00175 
00179 bool Utility::isYes( const char* str )
00180 {
00181     bool result = false;
00182 
00183     char* workingStr = new char[strlen(str) + 1];
00184     strcpy (workingStr, str);
00185     
00186     std::string lowstr = strtolower (workingStr);
00187     if( lowstr == "yes" ||
00188         lowstr == "yeah" ||
00189         lowstr == "ye" ||
00190         lowstr == "true")
00191         result = true;
00192     
00193     delete[] workingStr;
00194     
00195     return result;
00196 }
00197 
00198 
00202 bool Utility::isNo( const char* str )
00203 {
00204     bool result = false;
00205 
00206     char* workingStr = new char[strlen(str) + 1];
00207     strcpy (workingStr, str);
00208 
00209     std::string lowstr = strtolower (workingStr);
00210     if( lowstr == "no" ||
00211         lowstr == "nope" ||
00212         lowstr == "nah" ||
00213         lowstr == "nay" ||
00214         lowstr == "false")
00215         result = true;
00216 
00217     delete[] workingStr;
00218 
00219     return result;
00220 }
00221 
00222 
00226 bool Utility::isYesNo(const std::string& str)
00227 {
00228     return isYesNo (str.c_str());
00229 }
00230 
00231 
00235 bool Utility::isYes( const std::string& str )
00236 {
00237     return isYes (str.c_str());
00238 }
00239 
00240 
00244 bool Utility::isNo(const std::string& str)
00245 {
00246     return isNo (str.c_str());
00247 }
00248 
00252 double Utility::toDouble(const std::string str)
00253 {
00254     return atof(str.c_str());
00255 }
00256 
00260 int Utility::toInt(const std::string str)
00261 {
00262     return atoi(str.c_str());
00263 }