Main Page | Class Hierarchy | Compound List | File List | Compound Members | File Members

table.cc

Go to the documentation of this file.
00001 /*--------------------------------------------------------------------
00002  *
00003  * (C) Copyright Koninklijke Philips Electronics NV 2006. 
00004  * All rights reserved. This software is licensed under the terms of
00005  * version 2.1 of the GNU Lesser General Public License as published 
00006  * by the Free Software Foundation. For licensing and warranty
00007  * information, see the file COPYING in the main directory.
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 // windows  cannot handle
00092 //  w = max(w, (unsigned int)strlen(v[i]));
00093 // it seems that determining the right type for the template 
00094 // function max is the problem !?
00095           unsigned int x(strlen(v[i])); 
00096     w = max(w, x);
00097         }
00098   return w;
00099 }
00100 
00101 /*
00102 unsigned int Table::width() const 
00103 {
00104   unsigned int w = strlen(vborder) + strlen(vborder);
00105   for (unsigned int i=0; i<c; i++)
00106   {
00107     w += width(i);
00108     if (i != 0)
00109       w += strlen(colsep);
00110   }
00111   return w;
00112 }
00113 
00114 unsigned int Table::height() const 
00115 {
00116   return rows() + 2;
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   // find the width of each column and the total width
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     // print title
00188     o << title() << ":";
00189     o << endl;
00190 
00191     // print top border
00192     printN(o, tw, hborder);
00193     o << endl;
00194   }
00195 
00196   // print table
00197   for (row=0; row<rows(); row++)
00198   {
00199     // print row separator
00200     // if (row != 0) o << rowsep;
00201 
00202     if (format)
00203       o << vborder;
00204     
00205     for(col=0; col<columns(); col++)
00206     {
00207       // print column separator
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     // print bottom border
00225     printN(o, tw, hborder);
00226     o << endl;
00227   }
00228 }
00229 
00230 /*
00231 void Html::print(ostream& o, const Table& t) const
00232 {
00233   unsigned int row,col;
00234 
00235   o << "<html>\n";
00236   o << "<head>\n";
00237   o << "<title>" << t.title() << "</title>\n";
00238   o << "</head>\n";
00239   o << "<body bgcolor=\"white\">\n";
00240   o << "<h1>" << t.title() << "</h1>\n";
00241   o << "<table border=\"0\" cellpadding=\"2\" cellspacing=\"4\">\n";
00242 
00243   // print table
00244   for (row=0; row<t.rows(); row++)
00245   {
00246     o << "<tr bgcolor=\"red\">";
00247     
00248     for(col=0; col<t.columns(); col++)
00249     {
00250       o << "<td";
00251       switch (t.align(col)) {
00252       case left:   o << " align=\"left\"";   break;
00253       case right:  o << " align=\"right\"";  break;
00254       case center: o << " align=\"center\""; break;
00255       default: break;
00256       }
00257       o << ">";
00258   
00259       o << t.item(row, col);
00260   
00261       o << "</td>";
00262     }
00263 
00264     o << "</tr>\n";
00265 
00266   }
00267 
00268   o << "</table>\n";
00269   o << "</body>\n";
00270   o << "</html>\n";
00271 }
00272 
00273 
00274 void Latex::print(ostream& o, const Table& t) const
00275 {
00276   unsigned int row,col;
00277 
00278   o << "\\documentclass[12pt]{article}\n";
00279   o << "\\begin{document}\n";
00280   o << "\\begin{table}[hbtp]\n";
00281   o << "\\begin{center}\n";
00282   o << "\\begin{tabular}{";
00283   for (col=0; col<t.columns(); col++) 
00284   {
00285     switch (t.align(col)) {
00286     case left:   o << "l";   break;
00287     case right:  o << "r";  break;
00288     case center: o << "c"; break;
00289     default: break;
00290     }
00291   }
00292   o << "}\n";
00293 
00294   // print table
00295   for (row=0; row<t.rows(); row++)
00296   {
00297     for(col=0; col<t.columns(); col++)
00298     {
00299       if (col != 0)
00300         o << "&";
00301       o << t.item(row,col);
00302     }
00303     o << "\\\\\n";
00304   }
00305 
00306   o << "\\end{tabular}\n";
00307   o << "\\end{center}\n";
00308   o << "\\caption{" << t.title() << "}\n";
00309   o << "\\end{table}\n";
00310   o << "\\end{document}\n";
00311 }
00312 */

Generated on Wed Feb 15 14:52:39 2006 for yapi by doxygen 1.3.2