00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include <sys/types.h>
00012 #include "table.h"
00013 #include "strdup.h"
00014 #include <stdio.h>
00015 #include <string.h>
00016 #include <minmax.h>
00017
00018 using std::endl;
00019
00020 bool Table::format = true;
00021
00022 Table::Table(unsigned int columns, const char* title)
00023 : c(columns), a(columns)
00024 {
00025 t = strDup(title);
00026 for (unsigned int i=0; i<c; i++)
00027 a[i] = left;
00028 }
00029
00030 Table::~Table()
00031 {
00032 for (unsigned int i=0; i<v.size(); i++)
00033 free(v[i]);
00034 free(t);
00035 }
00036
00037 void Table::align(unsigned int col, Align al)
00038 {
00039 a[col] = al;
00040 }
00041
00042 Table::Align Table::align(int col) const
00043 {
00044 return a[col];
00045 }
00046
00047 void Table::clean()
00048 {
00049 for (unsigned int i=0; i<v.size(); i++)
00050 free(v[i]);
00051 v.erase(v.begin(),v.end());
00052 }
00053
00054 Table& Table::operator<<(const char* s)
00055 {
00056 v.push_back(strDup(s));
00057 return *this;
00058 }
00059
00060 Table& Table::operator<<(int i)
00061 {
00062 char buf[32];
00063 sprintf(buf, "%d", i);
00064 v.push_back(strDup(buf));
00065 return *this;
00066 }
00067
00068 const char* Table::item(unsigned int i) const
00069 {
00070 if (i < v.size())
00071 return "";
00072 else
00073 return v[i];
00074 }
00075
00076 const char* Table::item(unsigned int row, unsigned int col) const
00077 {
00078 return v[row * columns() + col];
00079 }
00080
00081 unsigned int Table::rows() const
00082 {
00083 return (v.size() + c - 1) / c;
00084 }
00085
00086 unsigned int Table::width(unsigned int col) const
00087 {
00088 unsigned int w = 0;
00089 for (unsigned int i=col; i<v.size(); i+=c)
00090 {
00091
00092
00093
00094
00095 unsigned int x(strlen(v[i]));
00096 w = max(w, x);
00097 }
00098 return w;
00099 }
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00122
00123 void printN(std::ostream& o, unsigned int n, char c = ' ')
00124 {
00125 while (n--)
00126 o << c;
00127 }
00128
00129 void printAlign(std::ostream& o, const char* s, unsigned int w, Table::Align a)
00130 {
00131 unsigned int l,r;
00132 unsigned int n = strlen(s);
00133
00134 switch (a) {
00135 case Table::left:
00136 l = 0;
00137 r = w - n;
00138 break;
00139 case Table::right:
00140 l = w - n;
00141 r = 0;
00142 break;
00143 case Table::center:
00144 l = (w - n) / 2;
00145 r = w - n - l;
00146 break;
00147 default:
00148 l = 0;
00149 r = 0;
00150 break;
00151 }
00152
00153 printN(o, l);
00154 o << s;
00155 printN(o, r);
00156 }
00157
00158 void Table::print(std::ostream& o) const
00159 {
00160 char hborder;
00161 char vborder;
00162 char colsep;
00163
00164 if (format) {
00165 hborder = '-';
00166 vborder = '|';
00167 colsep = ' ';
00168 } else {
00169 hborder = '\0';
00170 vborder = '\0';
00171 colsep = ',';
00172 }
00173
00174 unsigned int row, col;
00175
00176
00177 unsigned int tw = (vborder ? 2 : 0);
00178 std::vector<unsigned int> w(columns());
00179 for (col = 0; col<columns(); col++)
00180 {
00181 tw += w[col] = width(col);
00182 if (col != 0)
00183 tw++;
00184 }
00185
00186 if (format) {
00187
00188 o << title() << ":";
00189 o << endl;
00190
00191
00192 printN(o, tw, hborder);
00193 o << endl;
00194 }
00195
00196
00197 for (row=0; row<rows(); row++)
00198 {
00199
00200
00201
00202 if (format)
00203 o << vborder;
00204
00205 for(col=0; col<columns(); col++)
00206 {
00207
00208 if (col != 0)
00209 o << colsep;
00210
00211 if (format)
00212 printAlign(o, item(row,col), w[col], align(col));
00213 else
00214 o << item(row,col);
00215 }
00216
00217 if (format)
00218 o << vborder;
00219
00220 o << endl;
00221 }
00222
00223 if (format) {
00224
00225 printN(o, tw, hborder);
00226 o << endl;
00227 }
00228 }
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312