00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef ARCHON_UTILITIES_TEXT_H
00021 #define ARCHON_UTILITIES_TEXT_H
00022
00023 #include <string.h>
00024 #include <string>
00025 #include <vector>
00026
00027 #include <archon/util/unicode.H>
00028
00029 #include <archon/util/exception.H>
00030
00031 namespace Archon
00032 {
00033 namespace Utilities
00034 {
00035 namespace Text
00036 {
00037 using namespace std;
00038
00039 string toString(void *);
00040 string toString(bool);
00041 string toString(short);
00042 string toString(unsigned short);
00043 string toString(int);
00044 string toString(unsigned);
00045 string toString(long);
00046 string toString(unsigned long);
00047 string toString(float);
00048 string toString(double);
00049 string toString(long double);
00050 inline string toString(string v) { return v; }
00051 inline string toString(ustring v) { return Unicode::encodeUtf8(v); }
00052
00053 bool isPrefixOf(string prefix, string, bool ignoreCase=false);
00054 bool isSuffixOf(string suffix, string, bool ignoreCase=false);
00055
00056 string toLowerCase(string);
00057 string toUpperCase(string);
00058 string initCap(string);
00059
00063 string trim(string);
00064
00072 string lineTrim(string);
00073
00078 int compareIgnoreCase(const string &a, const string &b);
00079
00080 string escapeNonprintable(const string &);
00081 ustring escapeNonprintable(const ustring &);
00082
00083 class StringTokenizer
00084 {
00085 bool returnDelims;
00086 char *delims;
00087 char *str;
00088 char *pos;
00089
00090 public:
00091 StringTokenizer(string str, const char *delims,
00092 bool returnDelims = false):
00093 returnDelims(returnDelims)
00094 {
00095 this->delims = new char[strlen(delims)+1];
00096 strcpy(this->delims, delims);
00097 const unsigned size = str.size();
00098 this->str = new char[size+1];
00099 str.copy(this->str, size);
00100 this->str[size] = 0;
00101 pos = this->str;
00102 if(!returnDelims) pos += strspn(pos, delims);
00103 }
00104
00105 ~StringTokenizer()
00106 {{
00107 delete[] str;
00108 delete[] delims;
00109 }}
00110
00111 bool hasMoreElements()
00112 {
00113 return *pos;
00114 }
00115
00116 string nextToken()
00117 {
00118 if(!*pos) return 0;
00119 int n = strcspn(pos, delims);
00120 if(n == 0) n = 1;
00121 string s(pos, n);
00122 pos += n;
00123 if(!returnDelims) pos += strspn(pos, delims);
00124 return s;
00125 }
00126 };
00127
00162 string format(string, int width = 0);
00163
00164 template<class T>
00165 class Table
00166 {
00167 vector<T> elements;
00168 int rows, cols;
00169
00170 public:
00171 vector<double> columnWidthFractions;
00172
00173 Table(int rows, vector<double> columnWidthFractions):
00174 rows(rows), cols(columnWidthFractions.size()),
00175 columnWidthFractions(columnWidthFractions)
00176 {
00177 if(rows < 0) ARCHON_THROW1(ArgumentException,
00178 "Number of rows out of bound");
00179 if(cols < 1) ARCHON_THROW1(ArgumentException,
00180 "Number of columns out of bound");
00181 elements = vector<T>(rows*cols);
00182 }
00183
00184 T &operator()(int r, int c)
00185 {
00186 if(r < 0 || r >= rows) ARCHON_THROW1(ArgumentException,
00187 "Row index out of bound");
00188 if(c < 0 || c >= cols) ARCHON_THROW1(ArgumentException,
00189 "Column index out of bound");
00190 return elements[r*cols+c];
00191 }
00192
00193 T operator()(int r, int c) const
00194 {
00195 if(r < 0 || r >= rows) ARCHON_THROW1(ArgumentException,
00196 "Row index out of bound");
00197 if(c < 0 || c >= cols) ARCHON_THROW1(ArgumentException,
00198 "Column index out of bound");
00199 return elements[r*cols+c];
00200 }
00201
00202 int getCols() const { return cols; }
00203 int getRows() const { return rows; }
00204 };
00205
00206 string format(Table<string> &table, int maxTotalWidth = 0, int columnSpacing = 2, bool header = false);
00207 }
00208 }
00209 }
00210
00211 #endif // ARCHON_UTILITIES_TEXT_H