String.h

Go to the documentation of this file.
00001 #ifndef __STRING_H__
00002 #define __STRING_H__
00003 
00004 /*
00005 
00006 $Header$
00007 
00008 */
00009 #include <stdarg.h>
00010 #include <string>
00011 
00012 // Messages.
00013 static const char *const MSG_STRING_CANNOT_CONVERT = "Cannot convert string '%s' to type '%s' (%s).";
00014 static const char *const MSG_STRING_MALFORMED_XML  = "Malformed XML near tag '%s'.";
00015 static const char *const MSG_STRING_TOO_BIG        = "String is too big. Requested size is %d, maximal size is %u.";
00016 static const char *const MSG_STRING_PRINTF         = "Unrecoverable printf error (%s).";
00017 
00018 #define COLOR_STRING(str, fgColor, bgColor) String::colorize(str, CONSOLE_COLOR_FG_##fgColor, CONSOLE_COLOR_BG_##bgColor)
00019 
00020 using namespace std;
00021 
00022 static const unsigned int STRING_DEFAULT_LINE_WIDTH = 65;
00023 
00024 static const unsigned int LIMIT_CSTRING_SIZE = 1024;
00025 
00026 static const unsigned int STRING_PRINTF_INIT_SIZE = 256;
00027 static const unsigned int STRING_PRINTF_MAX_SIZE  = 1048576;
00028 
00029 // Console colors.
00030 
00031 static const char *const CONSOLE_COLOR_ECHO_ESCAPE      = "";
00032 
00033 static const char *const CONSOLE_COLOR_FG_BLACK         = "\033[30m";
00034 static const char *const CONSOLE_COLOR_FG_RED           = "\033[31m";
00035 static const char *const CONSOLE_COLOR_FG_GREEN         = "\033[32m";
00036 static const char *const CONSOLE_COLOR_FG_ORANGE        = "\033[33m";
00037 static const char *const CONSOLE_COLOR_FG_BLUE          = "\033[34m";
00038 static const char *const CONSOLE_COLOR_FG_MAGENTA       = "\033[35m";
00039 static const char *const CONSOLE_COLOR_FG_CYAN          = "\033[36m";
00040 static const char *const CONSOLE_COLOR_FG_GRAY          = "\033[37m";
00041 static const char *const CONSOLE_COLOR_FG_LIGHT_BLACK   = "\033[30;1m";
00042 static const char *const CONSOLE_COLOR_FG_LIGHT_RED     = "\033[31;1m";
00043 static const char *const CONSOLE_COLOR_FG_LIGHT_GREEN   = "\033[32;1m";
00044 static const char *const CONSOLE_COLOR_FG_YELLOW        = "\033[33;1m";
00045 static const char *const CONSOLE_COLOR_FG_VIOLET        = "\033[34;1m";
00046 static const char *const CONSOLE_COLOR_FG_LIGHT_MAGENTA = "\033[35;1m";
00047 static const char *const CONSOLE_COLOR_FG_LIGHT_CYAN    = "\033[36;1m";
00048 static const char *const CONSOLE_COLOR_FG_WHITE         = "\033[37;1m";
00049 
00050 static const char *const CONSOLE_COLOR_BG_BLACK         = "\033[40m";
00051 static const char *const CONSOLE_COLOR_BG_RED           = "\033[41m";
00052 static const char *const CONSOLE_COLOR_BG_GREEN         = "\033[42m";
00053 static const char *const CONSOLE_COLOR_BG_ORANGE        = "\033[43m";
00054 static const char *const CONSOLE_COLOR_BG_BLUE          = "\033[44m";
00055 static const char *const CONSOLE_COLOR_BG_MAGENTA       = "\033[45m";
00056 static const char *const CONSOLE_COLOR_BG_CYAN          = "\033[46m";
00057 static const char *const CONSOLE_COLOR_BG_WHITE         = "\033[47m";
00058 static const char *const CONSOLE_COLOR_BG_NONE          = "";
00059 
00060 static const char *const CONSOLE_COLOR_REGULAR          = "\033[0m";
00061 
00062 typedef enum
00063 {
00064   ALIGN_LEFT    = 'L',
00065   ALIGN_RIGHT   = 'R',
00066   ALIGN_CENTER  = 'C',
00067   ALIGN_JUSTIFY = 'J'
00068 } Alignment;
00069 
00070 typedef enum
00071 {
00072   BASE_DEC = 'D',
00073   BASE_HEX = 'H',
00074   BASE_OCT = 'O'
00075 } NumberBase;
00076 
00077 typedef struct
00078 {
00079   const string name;
00080   const string color;
00081 } StringColorTag;
00082 
00083 static const StringColorTag STRING_COLOR_TAGS[] = {
00084   {"service",  CONSOLE_COLOR_FG_YELLOW},
00085   {"registry", CONSOLE_COLOR_FG_ORANGE},
00086   {"signal",   CONSOLE_COLOR_FG_CYAN},
00087   {"slot",     CONSOLE_COLOR_FG_CYAN},
00088   {"stack",    CONSOLE_COLOR_FG_MAGENTA},
00089   {"keyword",  CONSOLE_COLOR_FG_WHITE},
00090   {"word",     CONSOLE_COLOR_FG_RED},
00091   {"arg",      CONSOLE_COLOR_FG_CYAN},
00092   {"url",      CONSOLE_COLOR_FG_BLUE},
00093   {"value",    CONSOLE_COLOR_FG_GREEN},
00094   {"example",  CONSOLE_COLOR_FG_LIGHT_GREEN},
00095   // End of list.
00096   {"", ""}
00097 };
00098 
00099 class String : public string
00100 {
00101 
00102 public:
00103 
00104   String();
00105   String(const char *str);
00106   String(const string &str);
00107   String(const char *format, va_list args);
00108   String(const unsigned int size, const char fill);
00109   String(const char *data, unsigned int size);
00110   virtual ~String();
00111 
00112   String &toLower();
00113   static String toLower(const String &str);
00114 
00115   String &toUpper();
00116   static String toUpper(const String &str);
00117 
00118   String &md5();
00119   static String md5(const String &str);
00120 
00121   String &setNum(const int i, const NumberBase base = BASE_DEC);
00122   String &setNum(const unsigned int i, NumberBase base = BASE_DEC);
00123   String &setNum(const long long i, const NumberBase base = BASE_DEC);
00124   String &setNum(const unsigned long long i, const NumberBase base = BASE_DEC);
00125   String &setNum(const double d);
00126   String &setBool(const bool b);
00127   String &sprintf(const char *format, ...) __attribute__ ((format (printf, 2, 3)));
00128   String &vsprintf(const char *format, va_list args);
00129 
00130   bool isTrue() const;
00131   int  toInt(int &n) const;
00132   int  toUint(unsigned int &n) const;
00133   int  toLongLong(long long &n) const;
00134   int  toDouble(double &n) const;
00135 
00136   String &trim(const bool asciiOnly = false);
00137   String &replaceCtrlChars(const char replacement = ' ');
00138   String &replaceChar(const char oldChar, const char newChar = ' ');
00139   String &replaceSubStr(const string &oldStr, const string &newStr = "");
00140   String &align(const unsigned int width = STRING_DEFAULT_LINE_WIDTH, const Alignment al = ALIGN_JUSTIFY);
00141 
00142   bool contains(const String &str, const bool ignoreCase = false) const;
00143   String &intersection(const String &str, const bool ignoreCase = false);
00144   bool equals(const String &str, const bool ignoreCase = false) const;
00145 
00146   // Color stuff.
00147   String &colorize(const String &fgColor = CONSOLE_COLOR_FG_WHITE, const String &bgColor = CONSOLE_COLOR_BG_NONE);
00148   unsigned int colorLength() const;
00149   String &parseXml();
00150 
00151   static const char *eol();
00152 
00153   static String number(const int i, const NumberBase base = BASE_DEC);
00154   static String number(const unsigned int i, const NumberBase base = BASE_DEC);
00155   static String number(const long long i, const NumberBase base = BASE_DEC);
00156   static String number(const unsigned long long i, const NumberBase base = BASE_DEC);
00157   static String number(const double d);
00158   static String boolean(const bool b);
00159   static bool isTrue(const String &str);
00160   static String timestamp();
00161 
00162   // Color stuff.
00163   static String colorize(const String &str, const String &fgColor = CONSOLE_COLOR_FG_WHITE, const String &bgColor = CONSOLE_COLOR_BG_NONE);
00164   static int setColorEnabled(const bool yes = true);
00165   static String parseXml(const String &xmlStr);
00166   static unsigned int colorLength(const char *str);
00167 
00168 private:
00169 
00170   static bool _colorEnabled;
00171 };
00172 
00173 inline
00174 const char *CSTRING(const String &str)
00175 {
00176   return str.c_str();
00177 }
00178 
00179 #endif

Generated on Thu Sep 6 20:11:25 2007 for Pylon Application Platform by  doxygen 1.5.1