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 
00008 /***************************************************************************
00009  *                                                                         *
00010  *   This program is free software; you can redistribute it and/or modify  *
00011  *   it under the terms of the GNU General Public License as published by  *
00012  *   the Free Software Foundation; either version 2 of the License, or     *
00013  *   (at your option) any later version.                                   *
00014  *                                                                         *
00015  ***************************************************************************/
00016 #ifndef _MIMETIC_UTILS_H_
00017 #define _MIMETIC_UTILS_H_
00018 #include <iostream>
00019 #include <string>
00020 #include <ctype.h>
00021 #include <mimetic/libconfig.h>
00022 #include <mimetic/strutils.h>
00023 
00024 namespace mimetic
00025 {
00026 
00027 std::ostream& crlf(std::ostream&);
00028 std::ostream& nl(std::ostream&);
00029 
00030 #ifndef isblank
00031 inline int isblank(char c)
00032 {
00033     return c == ' ' || c == '\t';
00034 }
00035 #endif
00036 
00037 namespace utils
00038 {
00039 
00040 /// returns the filename out of the fqn (fully qualified name) 
00041 std::string extractFilename(const std::string&);
00042 
00043 /// returns a string representation of \p n
00044 std::string int2str(int n);
00045 
00046 /// return true if the string contains just blanks (space and tabs)
00047 bool string_is_blank(const std::string&);
00048 
00049 /// returns the integer value represented by \p s
00050 int str2int(const std::string& s);
00051 
00052 /// returns a string hexadecimal representation of \p n
00053 std::string int2hex(unsigned int n);
00054 
00055 // find_bm specialization for random access iterators
00056 template<typename Iterator>
00057 Iterator find_bm(Iterator bit, Iterator eit, const std::string& word, const std::random_access_iterator_tag&)
00058 {
00059     int bLen = word.length();
00060     const char* pWord = word.c_str();
00061     int i, t, shift[256];
00062     unsigned char c;
00063 
00064     for(i = 0; i < 256; ++i)  
00065         shift[i] = bLen;
00066 
00067     for(i = 0; i < bLen; ++i)
00068         shift[ (int) pWord[i] ] = bLen -i - 1;
00069 
00070     for(i = t = bLen-1; t >= 0; --i, --t)
00071     {
00072         if((bit + i) >= eit)
00073             return eit; 
00074 
00075         while((c = *(bit + i)) != pWord[t]) 
00076         {
00077             i += std::max(bLen-t, shift[c]);
00078             if((bit + i) >= eit) return eit; 
00079             t = bLen-1;
00080         }
00081     }
00082 
00083     return bit + i + 1;
00084 }
00085 
00086 // boyer-moore find 
00087 /**
00088  * find the first occurrence of \p word in (\p bit, \p eit]
00089  *
00090  * returns an Iterator pointing at the first character of the found pattern
00091  * or \p eit if the search fails
00092  */
00093 template<typename Iterator>
00094 Iterator find_bm(Iterator bit, Iterator eit, const std::string& word)
00095 {
00096     return find_bm(bit, eit, word, 
00097         typename std::iterator_traits<Iterator>::iterator_category());
00098 }
00099 
00100 
00101 
00102 } // ns utils
00103 
00104 }
00105 
00106 #endif