Main Page   Class Hierarchy   Alphabetical List   Compound List   Examples  
utils.h
00001 /***************************************************************************
00002     copyright            : (C) 2002-2008 by Stefano Barbato
00003     email                : stefano@codesink.org
00004 
00005     $Id: utils.h,v 1.23 2008-10-07 11:06:26 tat Exp $
00006  ***************************************************************************/
00007 #ifndef _MIMETIC_UTILS_H_
00008 #define _MIMETIC_UTILS_H_
00009 #include <iostream>
00010 #include <string>
00011 #include <ctype.h>
00012 #include <mimetic/libconfig.h>
00013 #include <mimetic/strutils.h>
00014 
00015 namespace mimetic
00016 {
00017 
00018 std::ostream& crlf(std::ostream&);
00019 std::ostream& nl(std::ostream&);
00020 
00021 #ifndef isblank
00022 inline int isblank(char c)
00023 {
00024     return c == ' ' || c == '\t';
00025 }
00026 #endif
00027 
00028 namespace utils
00029 {
00030 
00031 /// returns the filename out of the fqn (fully qualified name) 
00032 std::string extractFilename(const std::string&);
00033 
00034 /// returns a string representation of \p n
00035 std::string int2str(int n);
00036 
00037 /// return true if the string contains just blanks (space and tabs)
00038 bool string_is_blank(const std::string&);
00039 
00040 /// returns the integer value represented by \p s
00041 int str2int(const std::string& s);
00042 
00043 /// returns a string hexadecimal representation of \p n
00044 std::string int2hex(unsigned int n);
00045 
00046 // find_bm specialization for random access iterators
00047 template<typename Iterator>
00048 Iterator find_bm(Iterator bit, Iterator eit, const std::string& word, const std::random_access_iterator_tag&)
00049 {
00050     int bLen = word.length();
00051     const char* pWord = word.c_str();
00052     int i, t, shift[256];
00053     unsigned char c;
00054 
00055     for(i = 0; i < 256; ++i)  
00056         shift[i] = bLen;
00057 
00058     for(i = 0; i < bLen; ++i)
00059         shift[ (unsigned char) pWord[i] ] = bLen -i - 1;
00060 
00061     for(i = t = bLen-1; t >= 0; --i, --t)
00062     {
00063         if((bit + i) >= eit)
00064             return eit; 
00065 
00066         while((c = *(bit + i)) != pWord[t]) 
00067         {
00068             i += std::max(bLen-t, shift[c]);
00069             if((bit + i) >= eit) return eit; 
00070             t = bLen-1;
00071         }
00072     }
00073 
00074     return bit + i + 1;
00075 }
00076 
00077 // boyer-moore find 
00078 /**
00079  * find the first occurrence of \p word in (\p bit, \p eit]
00080  *
00081  * returns an Iterator pointing at the first character of the found pattern
00082  * or \p eit if the search fails
00083  */
00084 template<typename Iterator>
00085 Iterator find_bm(Iterator bit, Iterator eit, const std::string& word)
00086 {
00087     return find_bm(bit, eit, word, 
00088         typename std::iterator_traits<Iterator>::iterator_category());
00089 }
00090 
00091 
00092 
00093 } // ns utils
00094 
00095 }
00096 
00097 #endif