Main Page   Class Hierarchy   Alphabetical List   Compound List   Examples  

utils.h

00001 /***************************************************************************
00002     copyright            : (C) 2002-2005 by Stefano Barbato
00003     email                : stefano@codesink.org
00004 
00005     $Id: utils.h,v 1.22 2006/04/10 08:42:44 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 /// returns the integer value represented by \p s
00047 int str2int(const std::string& s);
00048 
00049 /// returns a string hexadecimal representation of \p n
00050 std::string int2hex(unsigned int n);
00051 
00052 // find_bm specialization for random access iterators
00053 template<typename Iterator>
00054 Iterator find_bm(Iterator bit, Iterator eit, const std::string& word, const std::random_access_iterator_tag&)
00055 {
00056     int bLen = word.length();
00057     const char* pWord = word.c_str();
00058     int i, t, shift[256];
00059     unsigned char c;
00060 
00061     for(i = 0; i < 256; ++i)  
00062         shift[i] = bLen;
00063 
00064     for(i = 0; i < bLen; ++i)
00065         shift[ (int) pWord[i] ] = bLen -i - 1;
00066 
00067     for(i = t = bLen-1; t >= 0; --i, --t)
00068     {
00069         if((bit + i) >= eit)
00070             return eit; 
00071 
00072         while((c = *(bit + i)) != pWord[t]) 
00073         {
00074             i += std::max(bLen-t, shift[c]);
00075             if((bit + i) >= eit) return eit; 
00076             t = bLen-1;
00077         }
00078     }
00079 
00080     return bit + i + 1;
00081 }
00082 
00083 // boyer-moore find 
00084 /**
00085  * find the first occurrence of \p word in (\p bit, \p eit]
00086  *
00087  * returns an Iterator pointing at the first character of the found pattern
00088  * or \p eit if the search fails
00089  */
00090 template<typename Iterator>
00091 Iterator find_bm(Iterator bit, Iterator eit, const std::string& word)
00092 {
00093     return find_bm(bit, eit, word, 
00094         typename std::iterator_traits<Iterator>::iterator_category());
00095 }
00096 
00097 
00098 
00099 } // ns utils
00100 
00101 }
00102 
00103 #endif