00001
00002
00003
00004
00005
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
00032 std::string extractFilename(const std::string&);
00033
00034
00035 std::string int2str(int n);
00036
00037
00038 bool string_is_blank(const std::string&);
00039
00040
00041 int str2int(const std::string& s);
00042
00043
00044 std::string int2hex(unsigned int n);
00045
00046
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
00078
00079
00080
00081
00082
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 }
00094
00095 }
00096
00097 #endif