COOPY » Guide
version 0.6.5
|
00001 #ifndef COOPY_CSVTEXTBOOK 00002 #define COOPY_CSVTEXTBOOK 00003 00004 #include <coopy/TextBook.h> 00005 #include <coopy/TextBookFactory.h> 00006 #include <coopy/CsvSheet.h> 00007 #include <coopy/CsvFile.h> 00008 00009 #include <vector> 00010 #include <map> 00011 00012 namespace coopy { 00013 namespace store { 00014 class CsvTextBook; 00015 class CsvTextBookFactory; 00016 } 00017 } 00018 00024 class coopy::store::CsvTextBook : public TextBook, public CsvSheetReader { 00025 public: 00026 CsvTextBook(bool compact) : compact(compact) { 00027 named = true; 00028 } 00029 00030 std::vector<PolySheet> sheets; 00031 std::vector<std::string> names; 00032 std::map<std::string,int> name2index; 00033 00034 virtual std::vector<std::string> getNames() { 00035 return names; 00036 } 00037 00038 virtual PolySheet readSheet(const std::string& name) { 00039 if (name2index.find(name)!=name2index.end()) { 00040 return sheets[name2index[name]]; 00041 } 00042 return PolySheet(); 00043 } 00044 00045 bool clear() { 00046 sheets.clear(); 00047 names.clear(); 00048 name2index.clear(); 00049 return true; 00050 } 00051 00052 bool readCsvs(const char *fname); 00053 00054 bool readCsvsData(const char *data, int len); 00055 00056 std::string writeCsvsData(); 00057 00058 bool writeCsvs(const char *fname) { 00059 return write(fname,this,compact); 00060 } 00061 00062 static bool write(const char *fname, TextBook *book, bool compact, 00063 std::string *output = NULL); 00064 00065 // virtual bool open(const Property& config); 00066 00067 bool addSheet(const SheetSchema& schema); 00068 00069 virtual CsvSheet *nextSheet(const char *name, bool named); 00070 00071 virtual bool namedSheets() const { 00072 return named; 00073 } 00074 00075 private: 00076 bool compact; 00077 bool named; 00078 }; 00079 00080 00086 class coopy::store::CsvTextBookFactory : public TextBookFactory { 00087 private: 00088 bool compact; 00089 public: 00090 CsvTextBookFactory(bool compact) : compact(compact) { 00091 } 00092 00093 virtual std::string getName() { 00094 return compact?"csvs":"book"; 00095 } 00096 00097 virtual TextBook *open(AttachConfig& config, AttachReport& report) { 00098 if (config.shouldWrite) { 00099 if (config.prevBook!=NULL) { 00100 dbg_printf("writing csvs book file %s\n", config.options.get("file").asString().c_str()); 00101 //int r = CsvFile::write(config.prevBook->readSheetByIndex(0), 00102 //config.options); 00103 if (CsvTextBook::write(config.options.get("file").asString().c_str(), 00104 config.prevBook, 00105 compact)) { 00106 report.success = true; 00107 return config.prevBook; 00108 } 00109 } else { 00110 dbg_printf("CSVS write without content?\n"); 00111 } 00112 return NULL; 00113 } 00114 00115 CsvTextBook *book = new CsvTextBook(compact); 00116 if (book==NULL) return NULL; 00117 00118 if (config.shouldRead) { 00119 if (config.options.check("attach_read")||!config.options.check("should_attach")) { 00120 bool ok = true; 00121 if (config.options.check("attach_read")) { 00122 ok = TextBook::exists(config.fname.c_str()); 00123 } 00124 if (ok) { 00125 dbg_printf("reading csv file %s\n", config.options.get("file").asString().c_str()); 00126 bool r = book->readCsvs(config.fname.c_str()); 00127 if (!r) { 00128 delete book; 00129 book = NULL; 00130 } 00131 } 00132 } 00133 } 00134 00135 if (book!=NULL) { 00136 report.success = true; 00137 } 00138 00139 return book; 00140 } 00141 00142 static CsvTextBookFactory *makeFactory() { 00143 return new CsvTextBookFactory(false); 00144 } 00145 00146 static CsvTextBookFactory *makeCompactFactory() { 00147 return new CsvTextBookFactory(true); 00148 } 00149 }; 00150 00151 00152 #endif