COOPY » Guide  version 0.6.5
/home/paulfitz/cvs/coopy_scm/coopy/src/libcoopy_full/include/coopy/PolyBook.h
Go to the documentation of this file.
00001 #ifndef COOPY_POLYBOOK
00002 #define COOPY_POLYBOOK
00003 
00004 #include <coopy/TextBook.h>
00005 #include <coopy/PolySheet.h>
00006 #include <coopy/Property.h>
00007 #include <coopy/FormatDesc.h>
00008 
00009 namespace coopy {
00010   namespace store {
00011     class PolyBook;
00012   }
00013 }
00014 
00021 class coopy::store::PolyBook : public TextBook {
00022 private:
00023   TextBook *book;
00024   coopy::store::Property options;
00025 public:
00026   PolyBook() {
00027     book = 0/*NULL*/;
00028   }
00029 
00030   virtual ~PolyBook() {
00031     clear();
00032   }
00033 
00034   void clear() {
00035     if (book!=0/*NULL*/) {
00036       int ref = book->removeReference();
00037       if (ref==0) {
00038         delete book;
00039       }
00040       book = 0/*NULL*/;
00041     }
00042   }
00043 
00044   void take(TextBook *book) {
00045     clear();
00046     this->book = book;
00047     if (book!=0) {
00048       book->addReference();
00049     }
00050   }
00051 
00052   TextBook *give() {
00053     if (!book) return 0/*NULL*/;
00054     book->removeReference();
00055     TextBook *result = book;
00056     book = 0/*NULL*/;
00057     return result;
00058   }
00059 
00060   PolyBook(const PolyBook& alt) {
00061     book = alt.book;
00062     if (book!=0) {
00063       book->addReference();
00064     }
00065   }
00066 
00067   const PolyBook& operator=(const PolyBook& alt) {
00068     clear();
00069     book = alt.book;
00070     if (book!=0) {
00071       book->addReference();
00072     }
00073     return *this;
00074   }
00075 
00076   virtual std::vector<std::string> getNames() {
00077     if (book) {
00078       return book->getNames();
00079     }
00080     return std::vector<std::string>();
00081   }
00082 
00083   virtual int getSheetCount() {
00084     return (int)getNames().size();
00085   }
00086 
00087   virtual PolySheet readSheet(const std::string& name) {
00088     if (book) {
00089       return book->readSheet(name);
00090     }
00091     return PolySheet();
00092   }
00093 
00094   bool isValid() const { return book!=NULL; }
00095 
00096   bool readForReference(const char *fname, PolyBook& base) {
00097     return read(fname,NULL,&base);
00098   }
00099 
00100   bool read(const char *fname, const char *ext = NULL, PolyBook *base = NULL) {
00101     coopy::store::Property p;
00102     p.put("file",fname);
00103     if (ext!=NULL) {
00104       if (std::string(ext)!="") {
00105         p.put("ext",ext);
00106       }
00107     }
00108     p.put("can_create",false);
00109     p.put("should_read",true);
00110     p.put("should_write",false);
00111     return attach(p,base);
00112   }
00113 
00114   bool readIfExists(const char *fname) {
00115     coopy::store::Property p;
00116     p.put("file",fname);
00117     p.put("can_create",false);
00118     p.put("only_if_exists",true);
00119     p.put("should_read",true);
00120     p.put("should_write",false);
00121     return attach(p);
00122   }
00123 
00124   bool readAndWillWrite(const char *in_name, const char *in_format,
00125                         const char *out_name, const char *out_format) {
00126     coopy::store::Property p;
00127     p.put("file",in_name);
00128     if (in_format!=NULL) {
00129       if (std::string(in_format)!="") {
00130         p.put("ext",in_format);
00131       }
00132     }
00133     p.put("can_create",false);
00134     p.put("should_read",true);
00135     p.put("should_write",false);
00136 
00137     Property& po = p.nest("output_info");
00138     po.put("file",out_name);
00139     if (out_format!=NULL) {
00140       if (std::string(out_format)!="") {
00141         po.put("ext",out_format);
00142       }
00143     }
00144     expand(po);
00145 
00146     return attach(p);
00147   }
00148 
00149 
00150   bool attach(const char *fname, const char *ext = NULL) {
00151     coopy::store::Property p;
00152     p.put("file",fname);
00153     if (ext!=NULL) {
00154       if (std::string(ext)!="") {
00155         p.put("ext",ext);
00156       }
00157     }
00158     p.put("can_create",true);
00159     p.put("should_read",false);
00160     p.put("should_attach",true);
00161     p.put("should_write",false);
00162     return attach(p);
00163   }
00164 
00165   bool attachReadWrite(const char *fname) {
00166     coopy::store::Property p;
00167     p.put("file",fname);
00168     p.put("can_create",true);
00169     p.put("should_read",true);
00170     p.put("should_attach",true);
00171     p.put("should_write",false);
00172     p.put("attach_read",true);
00173     return attach(p);
00174   }
00175 
00176   bool write(const char *fname, const char *ext = NULL) {
00177     coopy::store::Property p;
00178     p.put("file",fname);
00179     if (ext!=NULL) {
00180       if (std::string(ext)!="") {
00181         p.put("ext",ext);
00182       }
00183     }
00184     p.put("can_create",true);
00185     p.put("should_read",false);
00186     p.put("should_write",true);
00187     return attach(p);
00188   }
00189 
00190   bool attach(coopy::store::Property& config, PolyBook *base = NULL);
00191 
00192   static bool expand(coopy::store::Property& config);
00193 
00194   coopy::store::Property getType(const char *fname, const char *ext = NULL) {
00195     coopy::store::Property p;
00196     p.put("file",fname);
00197     if (ext!=NULL) {
00198       if (std::string(ext)!="") {
00199         p.put("ext",ext);
00200       }
00201     }
00202     expand(p);
00203     return p;
00204   }
00205 
00206   bool flush();
00207 
00208   bool inplace() const {
00209     if (book) {
00210       return book->inplace();
00211     }
00212     return false;
00213   }
00214 
00215   virtual bool canWrite() const { 
00216     if (book) {
00217       return book->inplace();
00218     }
00219     return false;
00220   }
00221 
00222 
00223   virtual bool copy(const TextBook& alt, const Property& options);
00224 
00225   virtual bool addSheet(const SheetSchema& schema) {
00226     if (book)
00227       return book->addSheet(schema);
00228     return false;
00229   }
00230 
00231   virtual bool namedSheets() const {
00232     if (book)
00233       return book->namedSheets();
00234     return true;
00235   }
00236 
00237   virtual PolySheet provideSheet(const SheetSchema& schema) {
00238     if (book)
00239       return book->provideSheet(schema);
00240     return PolySheet();
00241   }
00242 
00243   virtual std::string desc() const {
00244     if (book) {
00245       return book->desc();
00246     }
00247     return "PolyBook";
00248   }
00249 
00250   virtual TextBook& tail() {
00251     if (book) {
00252       return book->tail();
00253     }
00254     return *this;
00255   }
00256 
00257   static void showFormats();
00258 
00259   static std::vector<coopy::store::FormatDesc> getFormatList();
00260 
00261   static bool copyFile(const char *src, const char *dest);
00262 
00263   virtual bool setPool(Pool *pool) {
00264     if (book)
00265       return book->setPool(pool);
00266     return false;
00267   }
00268 
00269   virtual Pool *getPool() const {
00270     if (book)
00271       return book->getPool();
00272     return 0 /*NULL*/;
00273   }
00274 
00275   virtual bool writtenToFuture() const { 
00276     if (book)
00277       return book->writtenToFuture();
00278     return false;
00279   }
00280 
00281 };
00282 
00283 #endif
00284 
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines