COOPY » Guide  version 0.6.5
/home/paulfitz/cvs/coopy_scm/coopy/src/libcoopy_core/include/coopy/CsvSheet.h
Go to the documentation of this file.
00001 #ifndef SSFOSSIL_CSVSHEET
00002 #define SSFOSSIL_CSVSHEET
00003 
00004 #include <coopy/TypedSheet.h>
00005 
00006 #include <vector>
00007 #include <string>
00008 
00009 namespace coopy {
00010   namespace store {
00011     class CsvSheet;
00012     class CsvSheetSchema;
00013   }
00014 }
00015 
00016 class coopy::store::CsvSheet : public EscapedTypedSheet<std::string> {
00017 private:
00018   std::vector<pairCellType> rec;
00019   int th, tw;
00020   bool valid;
00021   SheetStyle style;
00022   bool strict;
00023   Poly<SheetSchema> pSchema;
00024   std::string sheetName;
00025 public:
00026   using EscapedTypedSheet<std::string>::resize;
00027   using EscapedTypedSheet<std::string>::insertRow;
00028 
00029   CsvSheet() {
00030     tw = th = 0;
00031     valid = true;
00032     strict = true;
00033   }
00034 
00035   virtual SheetSchema *getSchema() const {
00036     return pSchema.getContent();
00037   }
00038 
00039   void setSchema(Poly<SheetSchema> pSchema) {
00040     this->pSchema = pSchema;
00041   }
00042 
00043   const SheetStyle& getStyle() {
00044     return style;
00045   }
00046 
00047   void setStyle(const SheetStyle& style) {
00048     this->style = style;
00049   }
00050 
00051   bool setStrict(bool strict) {
00052     this->strict = strict;
00053     return strict;
00054   }
00055 
00056   bool isStrict() {
00057     return strict;
00058   }
00059 
00060   // Deprecated, should migrate to deleteRow
00061   bool removeRow(int index) {
00062     s.arr.erase(s.arr.begin()+index);
00063     if (s.arr.size()<(size_t)s.h) {
00064       th = s.h = s.arr.size();
00065       return true;
00066     }
00067     return false;
00068   }
00069 
00070   /*
00071   // Deprecated
00072   bool insertRow(int index, int width = -1) {
00073     std::vector<pairCellType> rec;
00074     if (width == -1) { width = s.w; }
00075     for (int i=0; i<width; i++) {
00076       rec.push_back(pairCellType("",true));
00077     }
00078     if (index == -1 || index>=height()) {
00079       s.arr.push_back(rec);
00080     } else {
00081       s.arr.insert(s.arr.begin()+index,rec);
00082     }
00083     th = s.h = s.arr.size();
00084     tw = s.w = rec.size();
00085     return true;
00086   }
00087   */
00088 
00089   void addField(const char *s, bool escaped);
00090 
00091   void addField(const coopy::store::SheetCell& c) {
00092     addField(c.text.c_str(),c.escaped);
00093   }
00094 
00095   void addField(const char *s, int len, bool escaped) {
00096     std::string str(s,len);
00097     str += "\0";
00098     addField(str.c_str(),escaped);
00099   }
00100 
00101   void addRecord();
00102 
00103   void addRow(CsvSheet& alt, int row) {
00104     for (int i=0; i<alt.width(); i++) {
00105       const pairCellType& p = alt.pcell(i,row);
00106       addField(p.first.c_str(),p.second);
00107     }
00108     addRecord();
00109   }
00110 
00111   void clear() {
00112     tw = th = 0;
00113     s.w = s.h = 0;
00114     s.arr.clear();
00115     rec.clear();
00116     valid = true;
00117   }
00118 
00119   bool isValid() const {
00120     return valid;
00121   }
00122 
00123   virtual std::string cellString(int x, int y) const {
00124     if (valid) { return cell(x,y); }
00125     if (s.arr[y].size()>x) {
00126       return cell(x,y);
00127     }
00128     return "";
00129   }
00130 
00131   virtual std::string cellString(int x, int y, bool& escaped) const {
00132     if (valid) {
00133       const pairCellType& c = pcell(x,y);
00134       escaped = c.second;
00135       return c.first;
00136     }
00137     if (s.arr[y].size()>x) {
00138       const pairCellType& c = pcell(x,y);
00139       escaped = c.second;
00140       return c.first;
00141     }
00142     // for CSV-friendliness, return "" instead of NULL
00143     // for cells that did not get appended.  This "feature"
00144     // is only used by the patch generator.
00145     escaped = false;
00146     return ""; 
00147   }
00148 
00149   virtual bool cellString(int x, int y, const std::string& str) {
00150     cell(x,y) = str;
00151     return true;
00152   }
00153 
00154   virtual bool cellString(int x, int y, const std::string& str, 
00155                           bool escaped) {
00156     pairCellType& p = pcell(x,y);
00157     p.first = str;
00158     p.second = escaped;
00159     return true;
00160   }
00161 
00162   const CsvSheet& copy(const CsvSheet& alt) {
00163     s.arr = alt.s.arr;
00164     s.h = alt.s.h;
00165     s.w = alt.s.w;
00166     return *this;
00167   }
00168 
00169   const CsvSheet& copy(const DataSheet& alt) {
00170     clear();
00171     for (int i=0; i<alt.height(); i++) {
00172       for (int j=0; j<alt.width(); j++) {
00173         addField(alt.cellSummary(j,i));
00174       }
00175       addRecord();
00176     }
00177     return *this;
00178   }
00179 
00180   virtual bool canResize() { return true; }
00181 
00182   virtual bool resize(int w, int h) {
00183     clear();
00184     SheetCell cell;
00185     for (int i=0; i<h; i++) {
00186       for (int j=0; j<w; j++) {
00187         addField(cell);
00188       }
00189       addRecord();
00190     }
00191     s.w = w;
00192     s.h = h;
00193     return true;
00194   }
00195 
00196   bool setWidth(int w) {
00197     s.w = w;
00198     return true;
00199   }
00200 
00201   virtual std::string getDescription() const {
00202     return "csv";
00203   }
00204   
00205   virtual bool hasSheetName() const {
00206     return sheetName!="";
00207   }
00208   
00209   void setSheetName(const char *name) {
00210     sheetName = name;
00211   }
00212 };
00213 
00214 
00215 
00216 /*
00217 class coopy::store::CsvSheetSchema : public SheetSchema {
00218 public:
00219   CsvSheet *sheet;
00220   std::string name;
00221   int row;
00222 
00223   CsvSheetSchema(CsvSheet *sheet, const std::string& name, int row) :
00224     sheet(sheet), name(name), row(row)
00225   {
00226     row = 0;
00227   }
00228 
00229   virtual int headerHeight() const {
00230     return row;
00231   }
00232 
00233   virtual std::string getSheetName() const {
00234     return name;
00235   }
00236 
00237   virtual ColumnInfo getColumnInfo(int x) const {
00238     if (row==-1) return ColumnInfo();
00239     return ColumnInfo(sheet.cellString(x,row),ColumnType());
00240   }
00241 
00242   virtual int getColumnCount() const {
00243     return sheet->width();
00244   }
00245 
00246   virtual bool providesPrimaryKeys() const {
00247     return false;
00248   }
00249 
00250   virtual bool isGuess() const {
00251     return false;
00252   }
00253 };
00254 */
00255 
00256 #endif
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines