src/Utils/Utils.h

Go to the documentation of this file.
00001 /*
00002 * Copyright (C) 2005 Norman Casagrande
00003 * For informations write to nova77@gmail.com
00004 *
00005 * This library is free software; you can redistribute it and/or
00006 * modify it under the terms of the GNU Lesser General Public
00007 * License as published by the Free Software Foundation; either
00008 * version 2.1 of the License, or (at your option) any later version.
00009 *
00010 * This library is distributed in the hope that it will be useful,
00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 * Lesser General Public License for more details.
00014 *
00015 * You should have received a copy of the GNU Lesser General Public
00016 * License along with this library; if not, write to the Free Software
00017 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018 *
00019 */
00020 
00025 #ifndef __UTILS_H
00026 #define __UTILS_H
00027 
00028 #include <cstring>
00029 #include <locale>
00030 #include <algorithm>
00031 #include <cctype> // for isspace
00032 #include <sstream>
00033 
00034 #include <iostream>
00035 
00036 using namespace std;
00037 
00040 
00041 namespace nor_utils
00042 {
00043 
00044 // ----------------------------------------------------------------
00045 
00054 struct white_tab : ctype<char>
00055 {
00060    white_tab(): ctype<char>(init()) {}
00061    ~white_tab() { if (_rc) delete _rc; } 
00062 
00068    ctype_base::mask const* init()
00069    {
00070       _rc = new ctype_base::mask[ctype<char>::table_size];
00071 
00072       // not using fill_n because I don't wanna get bothered by the warning
00073       // of the VC8 compiler. This does not need to be secured! :P
00074       for (size_t i = 0; i < ctype<char>::table_size; ++i)
00075          _rc[i] = ctype_base::mask();
00076 
00077       _rc['\t'] = ctype_base::space;
00078       _rc['\n'] = ctype_base::space;
00079 
00080       return _rc;
00081    }
00082 
00089    ctype_base::mask const* get_table() {  return _rc;  }
00090 
00091 private:
00092    ctype_base::mask* _rc; 
00093 
00094 };
00095 
00096 // ----------------------------------------------------------------
00097 
00104 inline void skip_line(istream& inFile, const int nLines = 1)
00105 {
00106    for (int i = 0; i < nLines; ++i)
00107       while ( inFile.get() != '\n' && !inFile.eof() );
00108 }
00109 
00110 // ----------------------------------------------------------------
00111 
00126 inline string addAndCheckExtension(const string& file, const string& extension)
00127 {
00128    // if file smaller than the extension
00129    // or
00130    // if the file has no extension whatsoever
00131    // then return it with the extension
00132    if ( file.length() <= extension.length() ||
00133         file.rfind('.') == string::npos )
00134       return file + "." + extension;
00135 
00136    // get the extension
00137    size_t pos = file.rfind('.') + 1;
00138    string fileExt = file.substr(pos, file.length() - pos);
00139 
00140    if (fileExt == extension)
00141       return file; // extension is the same. Just return the file name
00142    else
00143       return file + "." + extension; // return filename plus extension
00144 }
00145 
00146 
00147 // ----------------------------------------------------------------
00148 
00155 inline string trim(const string& str)
00156 {
00157    size_t beg, end;
00158 
00159    string::const_iterator fIt = str.begin();
00160    string::const_reverse_iterator rIt = str.rbegin();
00161 
00162    for (beg = 0; isspace( *(fIt++) ); ++fIt);
00163    for (end = str.length(); isspace( *(rIt++) ); --end);
00164 
00165    return str.substr(beg, end-beg);  
00166 }
00167 
00168 // ----------------------------------------------------------------
00169 
00170 // 
00178 inline bool cmp_nocase(const string &s1, const string &s2)
00179 {
00180    string::const_iterator p1 = s1.begin();
00181    string::const_iterator p2 = s2.begin();
00182 
00183    if (s1.length() != s2.length())
00184       return false;
00185 
00186    while (p1 != s1.end() && p2 != s2.end() )
00187    {
00188       if (toupper(*p1) != toupper(*p2) )
00189          return false;
00190 
00191       ++p1;
00192       ++p2;
00193    }
00194 
00195    return true;
00196 }
00197 
00198 // ----------------------------------------------------------------
00199 
00207 inline size_t count_columns(istream& in)
00208 {
00209    size_t nCols = 0;
00210    bool inCol = false;
00211    int c = 0;
00212 
00213    while( !in.eof() )
00214    {
00215       c = in.get();
00216 
00217       if ( c == '\n' || c == '\r' )
00218          break;
00219 
00220       if (isspace(c))
00221       {
00222          if (inCol)
00223             inCol = false;
00224       }
00225       else
00226       {
00227          if (!inCol)
00228          {
00229             inCol = true;
00230             ++nCols;
00231          }
00232       }
00233 
00234    }
00235 
00236    return nCols;
00237 }
00238 
00239 // ----------------------------------------------------------------
00240 // ----------------------------------------------------------------
00241 
00265 template < typename T1, typename T2, typename Pred >
00266 bool comparePairOnSecond( const pair<T1, T2>& el1, const pair<T1, T2>& el2 )
00267 { return Pred()(el1.second, el2.second); }
00268 
00269 
00270 // ----------------------------------------------------------------
00271 
00281 template < typename T >
00282 bool is_zero( const T val, const T smallVal = 1E-10 )
00283 {
00284    if ( val <= smallVal &&
00285         val >= -smallVal)
00286       return true;
00287    else
00288       return false;
00289 }
00290 
00291 // ----------------------------------------------------------------
00292 
00293 } // end of namespace nor_utils
00294 
00295 #endif // __UTILS_H

Generated on Mon Nov 28 21:43:46 2005 for MultiBoost by  doxygen 1.4.5