COOPY » Guide
version 0.6.5
|
00001 #ifndef COOPY_JSONBOOK 00002 #define COOPY_JSONBOOK 00003 00004 #include <coopy/FoldedSheet.h> 00005 #include <coopy/TextBook.h> 00006 #include <coopy/TextBookFactory.h> 00007 00008 namespace coopy { 00009 namespace store { 00015 namespace json { 00016 class JsonBook; 00017 class JsonBookFactory; 00018 } 00019 } 00020 } 00021 00022 class coopy::store::json::JsonBook : public TextBook { 00023 public: 00024 std::vector<PolySheet> sheets; 00025 std::vector<std::string> names; 00026 std::map<std::string,int> name2index; 00027 00028 virtual std::vector<std::string> getNames() { 00029 return names; 00030 } 00031 00032 virtual PolySheet readSheet(const std::string& name) { 00033 if (name2index.find(name)!=name2index.end()) { 00034 return sheets[name2index[name]]; 00035 } 00036 return PolySheet(); 00037 } 00038 00039 bool clear() { 00040 sheets.clear(); 00041 names.clear(); 00042 name2index.clear(); 00043 return true; 00044 } 00045 00046 bool read(const char *fname); 00047 00048 bool write(const char *fname) { 00049 Property p; 00050 return write(fname,this,p); 00051 } 00052 00053 static bool write(const char *fname, TextBook *book, const Property& options); 00054 00055 static std::string render(TextBook *book, const Property& options); 00056 00057 virtual bool open(const Property& config); 00058 00059 bool addSheet(const SheetSchema& schema); 00060 00061 virtual bool namedSheets() const { 00062 return true; 00063 } 00064 }; 00065 00066 00067 00068 class coopy::store::json::JsonBookFactory : public TextBookFactory { 00069 public: 00070 virtual std::string getName() { 00071 return "jsonbook"; 00072 } 00073 00074 virtual TextBook *open(AttachConfig& config, AttachReport& report) { 00075 //static int ct = 0; 00076 //printf("hallo! write %d read %d : '%s'\n", config.shouldWrite, config.shouldRead, config.options.get("file").asString().c_str()); 00077 //ct++; 00078 //COOPY_ASSERT(ct<5); 00079 if (config.shouldWrite) { 00080 if (config.prevBook!=NULL) { 00081 dbg_printf("writing book file %s\n", config.options.get("file").asString().c_str()); 00082 if (JsonBook::write(config.options.get("file").asString().c_str(), 00083 config.prevBook, 00084 config.options)) { 00085 report.success = true; 00086 return config.prevBook; 00087 } 00088 } 00089 return NULL; 00090 } 00091 00092 JsonBook *book = new JsonBook(); 00093 if (book==NULL) return NULL; 00094 00095 if (config.shouldRead) { 00096 if (!config.options.check("should_attach")) { 00097 dbg_printf("reading jsonbook file %s\n", config.options.get("file").asString().c_str()); 00098 bool r = book->read(config.fname.c_str()); 00099 if (!r) { 00100 delete book; 00101 book = NULL; 00102 } 00103 } 00104 } 00105 00106 if (book!=NULL) { 00107 report.success = true; 00108 } 00109 00110 return book; 00111 } 00112 00113 static JsonBookFactory *makeFactory() { 00114 return new JsonBookFactory(); 00115 } 00116 }; 00117 00118 00119 00120 00121 #endif 00122