00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00040 #ifndef MSTRING_H
00041 #define MSTRING_H
00042
00043 #include <list>
00044
00045
00048 namespace mstring {
00049
00050
00059 #ifndef DLLEXPORT
00060 #ifdef _DLL //For Windows DLLs (Visual Studio)
00061 #define DLLEXPORT __declspec(dllexport)
00062 #else //Default: No export
00063 #define DLLEXPORT
00064 #endif
00065 #endif //DLLEXPORT
00066
00076 #ifndef MSTRING_LOCALE
00077 #define MSTRING_LOCALE 1
00078 #endif //MSTRING_LOCALE
00079
00109 #ifndef foreach
00110 #define foreach(VARIABLE, ITERATOR, STLCONTAINER) \
00111 for(ITERATOR = STLCONTAINER.begin(); \
00112 (ITERATOR != STLCONTAINER.end()) && ((VARIABLE = *ITERATOR) || true); \
00113 ITERATOR++)
00114 #endif //foreach
00115
00116
00117
00118 typedef unsigned int uint;
00119 typedef unsigned long ulong;
00120
00121
00132 struct DLLEXPORT MBoolStrings
00133 {
00141 const char* locale;
00143 const char* strTrue;
00145 const char* strFalse;
00147 const char* strYes;
00149 const char* strNo;
00150 };
00151
00162 struct DLLEXPORT MTimeStrings
00163 {
00171 const char* locale;
00183 char* formatDate;
00196 char* formatTime;
00197 };
00198
00206 DLLEXPORT enum NumericBase {Bin = 2, Oct = 8, Dec = 10, Hex = 16};
00207
00224 DLLEXPORT enum NumericFormat {DefaultFormat = 0x00, AlignSign = 0x01,
00225 ShowPlus = 0x02, ShowBase = 0x04};
00226
00237 DLLEXPORT enum TimeFormat {Date, Time, Date_Time};
00238
00239
00240
00241 class MStringList;
00242
00243
00255 class DLLEXPORT MString
00256 {
00257 public:
00259 MString();
00262 MString(const char* str);
00264 MString(const MString& str);
00266 MString(char c, uint len);
00269 MString(const char* str, uint n);
00271 virtual ~MString();
00272
00273
00275 MString& operator=(const char* str);
00277 MString& operator=(const MString& str);
00280 bool operator<(const char* str) const;
00283 bool operator<=(const char* str) const;
00286 bool operator==(const char* str) const;
00289 bool operator!=(const char* str) const;
00292 bool operator>=(const char* str) const;
00295 bool operator>(const char* str) const;
00298 MString operator+(const char* str) const;
00301 MString operator+(const MString& str) const;
00303 MString& operator+=(const char* str);
00305 MString& operator+=(const MString& str);
00306
00308 MString operator*(uint n) const;
00311 MString& operator*=(uint n);
00314 operator const char *() const;
00329 const char& operator[](int pos) const;
00332 char& operator[](int pos);
00341 MString operator()(int start, int end) const;
00342
00344 void clear();
00347 const char* get() const;
00349 uint len() const;
00351 bool isEmpty() const;
00354 MString left(uint len) const;
00357 MString right(uint len) const;
00362 MString mid(uint start, uint len = 0xffffffff) const;
00365 MString trimLeft(const char c = '\0') const;
00368 MString trimRight(const char c = '\0') const;
00371 MString trimAll(const char c = '\0') const;
00375 MString padLeft(uint len, char c = ' ') const;
00379 MString padRight(uint len, char c = ' ') const;
00383 MString padCenter(uint len, char c = ' ') const;
00385 MString caseUpper() const;
00387 MString caseLower() const;
00390 bool startsWith(const char* str) const;
00393 bool endsWith(const char* str) const;
00394
00403 MString& toString(int val, int width = -1, char fillChar = ' ',
00404 NumericFormat format = DefaultFormat, NumericBase base = Dec);
00413 MString& toString(uint val, int width = -1, char fillChar = ' ',
00414 NumericFormat format = DefaultFormat, NumericBase base = Dec);
00423 MString& toString(long val, int width = -1, char fillChar = ' ',
00424 NumericFormat format = DefaultFormat, NumericBase base = Dec);
00433 MString& toString(ulong val, int width = -1, char fillChar = ' ',
00434 NumericFormat format = DefaultFormat, NumericBase base = Dec);
00443 MString& toString(float val, int precision = -1, int width = -1,
00444 char fillChar = ' ', NumericFormat format = DefaultFormat);
00453 MString& toString(double val, int precision = -1, int width = -1,
00454 char fillChar = ' ', NumericFormat format = DefaultFormat);
00466 MString& boolToString(bool val, bool yesNo = false,
00467 const MBoolStrings* format = NULL);
00479 MString& timeToString(time_t val, TimeFormat which = Date_Time,
00480 const MTimeStrings* format = NULL);
00481
00489 int toInt(NumericBase base = Dec) const;
00497 uint toUInt(NumericBase base = Dec) const;
00505 long toLong(NumericBase base = Dec) const;
00513 ulong toULong(NumericBase base = Dec) const;
00522 double toDouble() const;
00540 int toBool(const MBoolStrings* format = NULL) const;
00557 time_t toTime(TimeFormat which = Date_Time,
00558 const MTimeStrings* format = NULL) const;
00559
00563 uint occurs(const char* str) const;
00570 int find(const char* str, int pos = 0) const;
00576 int at(const char* str, int n = 1) const;
00581 MString section(const char* sep, int n) const;
00587 MStringList split(const char* sep, bool ignoreEmpty = false) const;
00590 MString& replace(const char* str, const char* byStr);
00591
00602 MString wrap(uint width, uint indent = 0, bool hanging = false) const;
00609 MString& append(const char* str, const char* sep = MString::NL);
00610
00611
00616 static MString version(bool verbose = true);
00624 static void setNumLocale(bool on = true);
00632 static void setTimeLocale(bool on = true);
00633
00638 static MString number(int val, int width = -1, char fillChar = ' ',
00639 NumericFormat format = DefaultFormat, NumericBase base = Dec);
00644 static MString number(uint val, int width = -1, char fillChar = ' ',
00645 NumericFormat format = DefaultFormat, NumericBase base = Dec);
00650 static MString number(long val, int width = -1, char fillChar = ' ',
00651 NumericFormat format = DefaultFormat, NumericBase base = Dec);
00656 static MString number(ulong val, int width = -1, char fillChar = ' ',
00657 NumericFormat format = DefaultFormat, NumericBase base = Dec);
00662 static MString number(float val, int precision = -1, int width = -1,
00663 char fillChar = ' ', NumericFormat format = DefaultFormat);
00668 static MString number(double val, int precision = -1, int width = -1,
00669 char fillChar = ' ', NumericFormat format = DefaultFormat);
00676 static MString boolean(bool val, bool yesNo = false,
00677 const MBoolStrings* format = NULL);
00683 static MString time(time_t val, TimeFormat which = Date_Time,
00684 const MTimeStrings* format = NULL);
00685
00686
00687
00691 static const MString NL;
00692
00693
00694 protected:
00695
00698 void initClass();
00702 void newString(uint len);
00705 void delString();
00708 virtual uint calcWidth() const;
00713 virtual MString constructIndent(uint width) const;
00714
00715
00717 char* _string;
00720 mutable uint stringLen;
00735 static const MBoolStrings BOOLLOCALES[];
00751 static const MTimeStrings TIMELOCALES[];
00752
00753
00754 private:
00755
00758 MString wrapLine(const MString& ms, uint width, uint indent,
00759 bool hanging) const;
00760 };
00761
00762
00771 #if defined(_WIN32) && defined(_MSC_VER) //Only MS Visual Studio
00772 template class DLLEXPORT std::list<MString>;
00773 #endif //_WIN32 && _MSC_VER
00774 class DLLEXPORT MStringList : public std::list<MString>
00775 {
00776 public:
00778 MStringList();
00780 MStringList(const std::list<MString>& msl);
00783 MStringList(const_iterator start, const_iterator end);
00784 ~MStringList();
00785
00788 MStringList operator+(const MStringList& msl) const;
00790 MStringList& operator+=(const MStringList& msl);
00801 const MString& operator[](int pos) const;
00804 MStringList& operator<<(const char* str);
00806 MString join(const char* sep) const;
00808 bool contains(const char* str, bool ignoreCase = false) const;
00810 MStringList intersect(const MStringList& msl,
00811 bool ignoreCase = false) const;
00815 MStringList minus(const MStringList& msl, bool ignoreCase = false) const;
00816 };
00817
00818
00819
00821 DLLEXPORT const MString operator+(const char* str, const MString& ms);
00823 DLLEXPORT std::ostream& operator<<(std::ostream& stream, const MString& ms);
00826 DLLEXPORT std::istream& operator>>(std::istream& stream, MString& ms);
00829 DLLEXPORT std::ostream& operator<<(std::ostream& stream, const MStringList& msl);
00830
00831 }
00832
00833 #endif //MSTRING_H