bincimap

Log | Files | Refs | LICENSE

convert.h (8958B)


      1 /* -*- Mode: c++; -*- */
      2 /*  --------------------------------------------------------------------
      3  *  Filename:
      4  *    src/util/convert.h
      5  *  
      6  *  Description:
      7  *    Declaration of miscellaneous convertion functions.
      8  *
      9  *  Authors:
     10  *    Andreas Aardal Hanssen <andreas-binc curly bincimap spot org>
     11  *
     12  *  Bugs:
     13  *
     14  *  ChangeLog:
     15  *
     16  *  --------------------------------------------------------------------
     17  *  Copyright 2002-2005 Andreas Aardal Hanssen
     18  *
     19  *  This program is free software; you can redistribute it and/or modify
     20  *  it under the terms of the GNU General Public License as published by
     21  *  the Free Software Foundation; either version 2 of the License, or
     22  *  (at your option) any later version.
     23  *
     24  *  This program is distributed in the hope that it will be useful,
     25  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     26  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     27  *  GNU General Public License for more details.
     28  *
     29  *  You should have received a copy of the GNU General Public License
     30  *  along with this program; if not, write to the Free Software
     31  *  Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA.
     32  *  --------------------------------------------------------------------
     33  */
     34 #ifdef HAVE_CONFIG_H
     35 #include <config.h>
     36 #endif
     37 
     38 #ifndef convert_h_included
     39 #define convert_h_included
     40 #include <cstdlib>
     41 #include <string>
     42 #include <vector>
     43 #include <iomanip>
     44 #include <iostream>
     45 
     46 #include <stdio.h>
     47 #include <sys/stat.h>
     48 
     49 #include "address.h"
     50 #include "depot.h"
     51 
     52 #include <cstring>
     53 
     54 namespace Binc {
     55 
     56   //----------------------------------------------------------------------
     57   inline std::string toString(int i_in)
     58   {
     59     char intbuf[16];
     60     snprintf(intbuf, sizeof(intbuf), "%d", i_in);
     61     return std::string(intbuf);
     62   }
     63 
     64   //----------------------------------------------------------------------
     65   inline std::string toString(unsigned int i_in)
     66   {
     67     char intbuf[16];
     68     snprintf(intbuf, sizeof(intbuf), "%u", i_in);
     69     return std::string(intbuf);
     70   }
     71 
     72   //----------------------------------------------------------------------
     73   inline std::string toString(unsigned long i_in)
     74   {
     75     char longbuf[40];
     76     snprintf(longbuf, sizeof(longbuf), "%lu", i_in);
     77     return std::string(longbuf);
     78   }
     79 
     80   //----------------------------------------------------------------------
     81   inline std::string toString(const char *i_in)
     82   {
     83     return std::string(i_in);
     84   }
     85 
     86   //----------------------------------------------------------------------
     87   inline int atoi(const std::string &s_in)
     88   {
     89     return ::atoi(s_in.c_str());
     90   }
     91 
     92   //----------------------------------------------------------------------
     93   inline std::string toHex(const std::string &s)
     94   {
     95     const char hexchars[] = "0123456789abcdef";
     96     std::string tmp;
     97     for (std::string::const_iterator i = s.begin(); i != s.end(); ++i) {
     98       unsigned char c = (unsigned char)*i;
     99       tmp += hexchars[((c & 0xf0) >> 4)];
    100       tmp += hexchars[c & 0x0f];
    101     }
    102     
    103     return tmp;
    104   }
    105 
    106   //----------------------------------------------------------------------
    107   inline std::string fromHex(const std::string &s)
    108   {
    109     const char hexchars[] = "0123456789abcdef";
    110     std::string tmp;
    111     for (std::string::const_iterator i = s.begin();
    112 	 i != s.end() && i + 1 != s.end(); i += 2) {
    113       int n;
    114       unsigned char c = *i;
    115       unsigned char d = *(i + 1);
    116       
    117       const char *t;
    118       if ((t = strchr(hexchars, c)) == 0)
    119 	return "out of range";
    120       n = (t - hexchars) << 4;
    121       
    122       
    123       if ((t = strchr(hexchars, d)) == 0)
    124 	return "out of range";
    125       n += (t - hexchars);
    126       
    127       if (n >= 0 && n <= 255)
    128 	tmp += (char) n;
    129       else
    130 	return "out of range";
    131     }
    132     
    133     return tmp;
    134   }
    135   
    136   //----------------------------------------------------------------------
    137   inline std::string toImapString(const std::string &s_in)
    138   {
    139     for (std::string::const_iterator i = s_in.begin(); i != s_in.end(); ++i) {
    140       unsigned char c = (unsigned char)*i;
    141       if (c <= 31 || c >= 127 || c == '\"' || c == '\\')
    142 	return "{" + toString(s_in.length()) + "}\r\n" + s_in;
    143     }
    144     
    145     return "\"" + s_in + "\"";
    146   }
    147 
    148   //----------------------------------------------------------------------
    149   inline void uppercase(std::string &input)
    150   {
    151     for (std::string::iterator i = input.begin(); i != input.end(); ++i)
    152       *i = toupper(*i);
    153   }
    154 
    155   //----------------------------------------------------------------------
    156   inline void lowercase(std::string &input)
    157   {
    158     for (std::string::iterator i = input.begin(); i != input.end(); ++i)
    159       *i = tolower(*i);
    160   }
    161 
    162   //----------------------------------------------------------------------
    163   inline void chomp(std::string &s_in, const std::string &chars = " \t\r\n")
    164   {
    165     int n = s_in.length();
    166     while (n > 1 && chars.find(s_in[n - 1]) != std::string::npos)
    167       s_in.resize(n-- - 1);
    168   }
    169 
    170   //----------------------------------------------------------------------
    171   inline void trim(std::string &s_in, const std::string &chars = " \t\r\n")
    172   {
    173     while (s_in != "" && chars.find(s_in[0]) != std::string::npos)
    174       s_in = s_in.substr(1);
    175     chomp(s_in, chars);
    176   }
    177 
    178   //----------------------------------------------------------------------
    179   inline const std::string unfold(const std::string &a, 
    180 				  bool removecomment = true)
    181   {
    182     std::string tmp;
    183     bool incomment = false;
    184     bool inquotes = false;
    185     for (std::string::const_iterator i = a.begin(); i != a.end(); ++i) {
    186       unsigned char c = (unsigned char)*i;
    187       if (!inquotes && removecomment) {
    188 	if (c == '(') {
    189 	  incomment = true; 
    190 	  tmp += " ";
    191 	} else if (c == ')') {
    192 	  incomment = false; 
    193 	} else if (c != 0x0a && c != 0x0d) {
    194 	  tmp += *i;
    195         }
    196       } else if (c != 0x0a && c != 0x0d) {
    197 	tmp += *i;
    198       }
    199 
    200       if (!incomment) {
    201         if (*i == '\"') 
    202           inquotes = !inquotes;
    203       }
    204     }
    205 
    206     trim(tmp);
    207     return tmp;
    208   }
    209   
    210   //----------------------------------------------------------------------
    211   inline void split(const std::string &s_in, const std::string &delim, 
    212 	     std::vector<std::string> &dest, bool skipempty = true)
    213   {
    214     std::string token;
    215     for (std::string::const_iterator i = s_in.begin(); i != s_in.end(); ++i) {
    216       if (delim.find(*i) != std::string::npos) {
    217 	if (!skipempty || token != "")
    218 	  dest.push_back(token);
    219 	token = "";
    220       } else
    221 	token += *i;
    222     }
    223 
    224     if (token != "")
    225       dest.push_back(token);
    226   }
    227 
    228   //----------------------------------------------------------------------
    229   inline void splitAddr(const std::string &s_in,
    230 			std::vector<std::string> &dest, bool skipempty = true)
    231   {
    232     static const std::string delim = ",";
    233     std::string token;
    234     bool inquote = false;
    235     for (std::string::const_iterator i = s_in.begin(); i != s_in.end(); ++i) {
    236       if (inquote && *i == '\"') inquote = false;
    237       else if (!inquote && *i == '\"') inquote = true;
    238 
    239       if (!inquote && delim.find(*i) != std::string::npos) {
    240 	if (!skipempty || token != "")
    241 	  dest.push_back(token);
    242 	token = "";
    243       } else
    244 	token += *i;
    245     }
    246     if (token != "")
    247       dest.push_back(token);
    248   }
    249 
    250   //----------------------------------------------------------------------
    251   inline std::string toCanonMailbox(const std::string &s_in)
    252   {
    253     if (s_in.find("..") != std::string::npos) return "";
    254 
    255     if (s_in.length() >= 5) {
    256       std::string a = s_in.substr(0, 5);
    257       uppercase(a);
    258       return a == "INBOX" ?
    259 	a + (s_in.length() > 5 ? s_in.substr(5) : "") : s_in;
    260     }
    261     
    262     return s_in;
    263   }
    264 
    265   //------------------------------------------------------------------------
    266   inline std::string toRegex(const std::string &s_in, char delimiter)
    267   {
    268     std::string regex = "^";
    269     for (std::string::const_iterator i = s_in.begin(); i != s_in.end(); ++i) {
    270       if (*i == '.' || *i == '[' || *i == ']' || *i == '{' || *i == '}' ||
    271 	  *i == '(' || *i == ')' || *i == '^' || *i == '$' || *i == '?' ||
    272 	  *i == '+' || *i == '\\') {
    273 	regex += "\\";
    274 	regex += *i;
    275       } else if (*i == '*')
    276 	regex += ".*?";
    277        else if (*i == '%') {
    278         regex += "(\\";
    279 	regex += delimiter;
    280 	regex += "){0,1}";
    281 	regex += "[^\\";
    282 	regex += delimiter;
    283 	regex += "]*?";
    284       } else regex += *i;
    285     }
    286     
    287     if (regex[regex.length() - 1] == '?')
    288       regex[regex.length() - 1] = '$';
    289     else
    290       regex += "$";
    291 
    292     return regex;
    293   }
    294 
    295   //------------------------------------------------------------------------
    296   class BincStream {
    297   private:
    298     std::string nstr;
    299 
    300   public:
    301 
    302     //--
    303     BincStream &operator << (std::ostream&(*)(std::ostream&));
    304     BincStream &operator << (const std::string &t);
    305     BincStream &operator << (unsigned int t);
    306     BincStream &operator << (int t);
    307     BincStream &operator << (char t);
    308 
    309     //--
    310     const std::string &str(void) const;
    311 
    312     //--
    313     int getSize(void) const;
    314 
    315     //--
    316     void clear(void);
    317 
    318     //--
    319     BincStream(void);
    320     ~BincStream(void);
    321   };
    322 }
    323 
    324 #endif