tools.cpp

Go to the documentation of this file.
00001 /*
00002 #########################################################################
00003 #
00004 #  This file is part of trustyRC.
00005 #
00006 #  trustyRC, fully modular IRC robot 
00007 #  Copyright (C) 2006-2008 Nicoleau Fabien 
00008 #
00009 #  trustyRC is free software: you can redistribute it and/or modify
00010 #  it under the terms of the GNU General Public License as published by
00011 #  the Free Software Foundation, either version 3 of the License, or
00012 #  (at your option) any later version.
00013 #
00014 #  trustyRC is distributed in the hope that it will be useful,
00015 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 #  GNU General Public License for more details.
00018 #
00019 #  You should have received a copy of the GNU General Public License
00020 #  along with trustyRC.  If not, see <http://www.gnu.org/licenses/>.
00021 #
00022 #########################################################################
00023 */
00024 
00029 #include "tools.h"
00030 #include <iostream>
00031 using namespace std;
00032 
00036 Tools::Tools()
00037 {
00038 
00039 }
00040 
00044 Tools::~Tools()
00045 {
00046 
00047 }
00048 
00054 string Tools::asciiToHexa(string asciiStr)
00055 {
00056    ostringstream oss;
00057    for (unsigned int i = 0 ; i < asciiStr.length();i++) {
00058       oss << hex <<(int) asciiStr[i];
00059    }
00060    return oss.str();
00061 }
00062 
00068 string Tools::hexaToAscii(string hexaStr)
00069 {
00070    ostringstream oss;
00071    for (unsigned int i = 0 ; i < hexaStr.length();i++) {
00072       oss << dec <<(char) hexaStr[i];
00073    }
00074    return oss.str();
00075 }
00076 
00083 bool Tools::isInVector(vector<string> v,string str)
00084 {
00085    bool ret (false);
00086    for (unsigned int i = 0 ; i < v.size() ; i ++ )
00087    {
00088       if(v.at(i) == str)
00089       {
00090          ret = true;
00091          break;
00092       }   
00093    }
00094    return (ret);
00095 }
00096 
00102 void Tools::delStrFromVector(vector<string>*v,string str)
00103 {
00104    vector<string>::iterator it = v->begin();
00105    while ( (it != v->end()) && (v->size() > 0) )
00106    {
00107       if ( (*it) == str )
00108       {
00109          v->erase(it);
00110          break;
00111       }
00112       else
00113          it ++ ;
00114    }
00115 }
00116 
00122 string Tools::intToStr(int number)
00123 {
00124    ostringstream oss;
00125    oss << number;
00126    return oss.str();
00127 }
00128 
00134 string Tools::doubleToStr(double number)
00135 {
00136    ostringstream oss;
00137    oss << number;
00138    return oss.str();
00139 }
00140 
00147 double Tools::strToDouble(string str)
00148 {
00149    istringstream iss(str);
00150    double number;
00151    if( (iss >> number) && ( iss.eof() ) )
00152    {
00153       return number;   
00154    }
00155    else
00156    {
00157       return 0.0 ;
00158    }
00159 }
00160 
00167 int Tools::strToInt(string str)
00168 {
00169    istringstream iss(str);
00170    int number;
00171    if( (iss >> number) && ( iss.eof() ) )
00172    {
00173       return number;   
00174    }
00175    else
00176    {
00177       return 0 ;
00178    }
00179 }
00180 
00187 unsigned int Tools::strToUnsignedInt(string str)
00188 {
00189    istringstream iss(str);
00190    int number;
00191    if( (iss >> number) && ( iss.eof() ) )
00192    {
00193       return number;   
00194    }
00195    else
00196    {
00197       return 0 ;
00198    }
00199 }
00200 
00211 unsigned int Tools::strtimeToSeconds(string strtime) 
00212 {
00213    unsigned int days=0,hours=0,minuts=0,seconds=0;
00214    string currentValue = "";
00215    for(unsigned int i=0;i < strtime.length() ; i ++ ) {
00216       switch(tolower(strtime[i])) {
00217          case 'd': days += Tools::strToUnsignedInt(currentValue); currentValue = "" ;
00218          break;
00219          case 'h': hours += Tools::strToUnsignedInt(currentValue); currentValue = "" ;
00220          break;
00221          case 'm': minuts += Tools::strToUnsignedInt(currentValue); currentValue = "" ;
00222          break;
00223          case 's': seconds += Tools::strToUnsignedInt(currentValue); currentValue = "" ;
00224          break;
00225          default : currentValue += strtime[i] ;
00226       }
00227    }
00228    seconds += Tools::strToUnsignedInt(currentValue);
00229    return (seconds+(minuts*60)+(hours*3600)+(days*86400));
00230 }
00231 
00237 string Tools::to_lower(string str)
00238 {
00239    string ret = str;
00240    for ( unsigned int i = 0 ; i < ret.length() ; i ++ )
00241    {
00242       ret[i] = tolower(ret[i]);
00243    }
00244    return ret;
00245 }
00246 
00252 string Tools::to_upper(string str)
00253 {
00254    string ret = str;
00255    for ( unsigned int i = 0 ; i < ret.length() ; i ++ )
00256    {
00257       ret[i] = toupper(ret[i]);
00258    }
00259    return ret;
00260 }
00261 
00268 int Tools::random(int min,int max)
00269 {
00270    return (min+(int) (double(max-(min-1))*rand()/(RAND_MAX+1.0))) ;
00271 }
00272 
00280 string Tools::vectorToString(vector<string> vec,string separator,unsigned int start )
00281 {
00282    string ret = "";
00283    for (unsigned int i = 0 ; i < vec.size() ; i ++ )
00284    {
00285       if ( i >= start )
00286       {
00287          ret += vec[i];
00288          if ( i != (vec.size() -1) )
00289          {
00290             ret += separator;
00291          }   
00292       }
00293    }
00294    return ret;
00295 }
00296 
00304 vector<string> Tools::stringToVector(string str,string separator,unsigned int start)
00305 {
00306    vector<string> ret;
00307    ret.clear();
00308    string::size_type i = 0 ;
00309    unsigned int nbWords = 0;
00310    while ( i < str.length() )
00311    {
00312                 i = str.find(separator) ;
00313                 if ( i == string::npos ) {
00314                         ret.push_back(str);
00315                         i = str.length();
00316                 }
00317                 else {
00318                         if ( nbWords >= start) {
00319                                 ret.push_back(str.substr(0,i));
00320                         }
00321                         str = str.substr(i+separator.length());
00322                         nbWords++;
00323                         i = 0;
00324                 }
00325    }
00326    return ret ;
00327 }
00328 
00336 vector<string> Tools::gatherVectorElements(vector<string> v,string separator,unsigned int length)
00337 {
00338    vector<string> newVector;
00339    string buffer ;
00340    for (unsigned int i = 0 ; i < v.size() ; i+=length )
00341    {
00342       buffer = "";
00343       for ( unsigned int j = 0; (j < length) && (i+j<v.size()) ; j++ )
00344       {
00345          buffer+=(v[i+j]+separator);
00346       }
00347       newVector.push_back(buffer);
00348    }
00349    return newVector;
00350 }
00351 
00358 string Tools::escapeChar(string str,char c)
00359 {
00360    unsigned int i = 0 ;
00361    while ( i < str.length() )
00362    {
00363       if ( str[i] == c )
00364       {
00365          str.insert(i,"\\");
00366          i += 2;
00367       }
00368       else
00369       {
00370          i += 1;
00371       }
00372    }
00373    return str ;
00374 }
00375 
00383 void Tools::log(string fileName,string str,bool timestamp,bool truncate)
00384 {
00385    time_t now ;
00386    time(&now);
00387    char timeFormat[17];
00388    ofstream file;
00389    if ( truncate ) {
00390       file.open(fileName.c_str(), std::ios_base::trunc );
00391    }
00392    else {
00393       file.open(fileName.c_str(), std::ios_base::app );
00394    }
00395    if(file.is_open()) {
00396       if (timestamp) {
00397               strftime(timeFormat,18,"%y-%m-%d %X",localtime(&now));
00398          string time(timeFormat);
00399          str = time +" "+str ;
00400       }
00401       file << str << endl ;
00402       file.close();
00403    }
00404 }
00405 
00411 string Tools::urlencode(string str)
00412 {
00413    string::size_type pos ;
00414    string signs[44] = {"+", " ", "&", "é", "\"", "è", "ç", "à", "=", "°", ",", ";", ":", "ù", "^", "$", "µ", "¬", "¹", "#", "{", "[", "|", "`", "\\", "^", "@", "]", "}", "?", "/", "§", "£", "š", "â", "ê", "ô", "î", "û", "ä", "ï", "ö", "ë","ü"};
00415    string codes[44] = {"%2B", "+", "%26", "%E9", "%22", "%E8", "%E7", "%E0", "%3D", "%B0", "%2C", "%3B", "%3A", "%F9", "%5E", "%24", "%B5", "%AC", "%B9", "%23", "%7B", "%5B", "%7C", "%60", "%5C", "%5E", "%40", "%5D", "%7D", "%3F", "%2F", "%A7", "%A3", "%A8", "%E2", "%EA", "%F4", "%EE", "%FB", "%E4", "%EF", "%F6", "%EB", "%FC"};
00416    for ( unsigned int i = 0 ; i < 44 ; i ++ )
00417    {
00418       pos = str.find(signs[i]) ;
00419       while ( pos != string::npos )
00420       {
00421          str.replace(pos,signs[i].length(),codes[i]);
00422          pos = str.find(signs[i]) ;
00423       }
00424    }
00425    return str ;
00426 }
00427 
00433 string Tools::clearAccents(string str) 
00434 {
00435    string::size_type pos ;
00436    string accents[16] = {"é","è","ê","ë","ç","à","â","ä","ù","û","ü","ù","ô","ö","î","ï"} ;
00437    string clean[16]   = {"e","e","e","e","c","a","a","a","u","u","u","u","o","o","i","i"} ;
00438    for ( unsigned int i = 0 ; i < 16 ; i ++ )
00439    {
00440       pos = str.find(accents[i]) ;
00441       while ( pos != string::npos )
00442       {
00443          str.replace(pos,accents[i].length(),clean[i]);
00444          pos = str.find(accents[i]) ;
00445       }
00446    }
00447    return str ;
00448 }
00449 
00455 string Tools::cleanHTML(string str)
00456 {
00457    const unsigned int NB_DEL = 7;
00458    const unsigned int NB_REPLACE = 30;
00459    string::size_type pos ;
00460    string del[NB_DEL] = {"&middot;","<b>","</b>","<i>","</i>","<br>","<br />"} ;
00461    string codes[NB_REPLACE] = {"&nbsp;","&#39;","&#039;","&quot;","&eacute;","&egrave;","&#233;","&#232;","&gt;","&lt;","&#234;","&#224;","&agrave;","&ccedil;","&icirc;","&ecirc;","&ouml;","&ocirc;","&iuml;","&ugrave;","&Ccedil;","&acirc;","&laquo;","&raquo;","&ucirc;","&sup2;","&copy;","&#64;","&amp;","&deg;"};
00462    string signs[NB_REPLACE] = {" ","'","'","\"","é","è","é","è",">","<","ê","à","à","ç","î","ê","ö","ô","ï","ù","ç","â","«","»","û","²","©","@","&","°"};
00463    for ( unsigned int i = 0 ; i < NB_REPLACE ; i ++ )
00464    {
00465       pos = str.find(codes[i]) ;
00466       while ( pos != string::npos )
00467       {
00468          str.replace(pos,codes[i].length(),signs[i]);
00469          pos = str.find(codes[i]) ;
00470       }
00471    }
00472    for ( unsigned int i = 0 ; i < NB_DEL ; i++ )
00473    {
00474       pos = str.find(del[i]) ;
00475       while ( pos != string::npos )
00476       {
00477          str.erase(pos,del[i].length());
00478          pos = str.find(del[i]) ;
00479       }
00480    }
00481    return str ;
00482 }
00483 
00489 string Tools::parseQ3Colors(string raw) 
00490 {
00491         string parsedString = "";
00492         unsigned int i = 0;
00493         while (i < raw.length() )
00494         {
00495                 if ( raw[i] != '^' ) {
00496                         parsedString += raw[i];
00497                         i++;
00498                 }
00499                 else {
00500                         if ((i<(raw.length()-1)) && (raw[i+1] !='x') && (raw[i+1] !='X')) {
00501                                 i += 2;
00502                         }
00503                         else if ((i<(raw.length()-7)) && ((raw[i+1]=='x')||(raw[i+1]=='X'))) {
00504                                 i += 8;
00505                         }
00506                         else { //possible?
00507                                 i++;
00508                         }
00509                 }
00510                 
00511         }
00512         return parsedString;
00513 }
00514 
00521 bool Tools::ircMaskMatch(string request,string mask) {
00522    if (fnmatch(mask.c_str(),request.c_str(),0)==0 )
00523       return true;
00524    else
00525       return false;
00526    
00527 }
00528 
00536 int Tools::masksMatch(char*str1,char*str2)
00537 {
00538   if (*str1 == 0 && *str2 == 0)
00539     return (1);
00540   if (*str1 == '*')
00541     {
00542       if (*str2 == 0)
00543       return (masksMatch(str1 + 1, str2));
00544       else
00545       return (masksMatch(str1 + 1, str2) || masksMatch(str1, str2 + 1));
00546     }
00547   if (*str2 == '*')
00548     {
00549       if (*str1 == 0)
00550       return (masksMatch(str1, str2 + 1));
00551       else
00552       return (masksMatch(str1 + 1, str2) || masksMatch(str1, str2 + 1));
00553     }
00554   if (*str1 == *str2)
00555     return (masksMatch(str1 + 1, str2 + 1));
00556   return (0);
00557 }
00558 
00565 bool copyFile(string source,string destination) {
00566    char tmp;
00567    ifstream fi(source.c_str(), ios::in|ios::binary);
00568    if (!fi) return false;
00569    ofstream fo(destination.c_str(), ios::out|ios::binary);
00570    if (!fo) return false;
00571     
00572    while ( fo && fi.get(tmp) )
00573       fo.put(tmp);
00574    return fo.good() && fi.eof();
00575 }

Generated on Sun Aug 16 15:28:29 2009 for trustyRC by  doxygen 1.5.8