include/EDoc/Persistence.h

Go to the documentation of this file.
00001 /*******************************************************************************
00002 
00003    Copyright (C) 2007 by Brendon Costa
00004 
00005    This library is free software; you can redistribute it and/or modify 
00006    it under the terms of the "LGPL Like" License defined in the file COPYING 
00007    that should have been distributed along with this source.
00008 
00009    This library is distributed in the hope that it will be useful, 
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of 
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
00012 
00013    You should have received a copy of the "LGPL Like" License 
00014    along with this library; see the file COPYING. if not, it can be 
00015    obtained from the EDoc++ website: 
00016    http://edoc.sourceforge.net/license.html
00017 
00018 *******************************************************************************/
00019 #ifndef EDOC_PERSISTENCE_H
00020 #define EDOC_PERSISTENCE_H
00021 
00022 #include "EDoc/PersistenceIFace.h"
00023 #include "EDoc/ManagedFile.h"
00024 
00025 #include <vector>
00026 #include <list>
00027 #include <string>
00028 
00029 namespace EDoc
00030 {
00031    class ArchiveIFace;
00032    
00033    //===========================================================================
00034    /** \brief Manages automatic switching between TextPersistence and
00035     * BinaryPersistence as necessary.
00036     *
00037     * EDocPersistence is a class that derives from persistence IFace that
00038     * constructs appropiate text/binary persistence layer and also manages the
00039     * lifetime of temporary files as necessary.
00040     *
00041     * When compiled with GPL lisensing enabled this will also extract a number
00042     * of temporary files from a BFD file object if there are multiple .edc files
00043     * embedded into it.
00044     */
00045    class Persistence : public PersistenceIFace
00046    {
00047    public:
00048 
00049       //------------------------------------------------------------------------
00050       /** \brief Type representing what format (Text/Binary) the .edc file is
00051        * encoded in.
00052        */
00053       enum EncodingType
00054       {
00055          UNKNOWN = 0,
00056          TEXT,
00057          BINARY
00058       };
00059       
00060       //------------------------------------------------------------------------
00061       /** \brief List of all currently open temporary files.
00062        *
00063        * This static list is used for claning up temporary files at program exit
00064        * regardless of exiting due to signal interruption or for normal exit.
00065        */
00066       static std::list<std::string> temp_files;
00067 
00068       //------------------------------------------------------------------------
00069       /** \brief Adds a new temporary file to the temp_files list.
00070        */
00071       static void AddTempFile(std::string filename);
00072 
00073       //------------------------------------------------------------------------
00074       /** \brief Adds a temporary file from the temp_files list after it has
00075        * been deleted.
00076        *
00077        * For atomicity you should only call this AFTER the temp file has been
00078        * unlinked.
00079        */
00080       static void RemoveTempFile(std::string filename);
00081 
00082 
00083 
00084 
00085 
00086       //------------------------------------------------------------------------
00087       /** \brief Adds new archive format that can be used for reading/writing.
00088        */
00089       static void AddArchiveFactory(ArchiveIFace* factory);
00090 
00091 
00092 
00093       //------------------------------------------------------------------------
00094       /** \brief Will extract a list of managed files from a given file if it 
00095        * is a known archive type.
00096        *
00097        * If it is not a known archive type then will return an empty list.
00098        */
00099       static std::list<ManagedFile> ReadExtract(ManagedFile filename);
00100 
00101       //------------------------------------------------------------------------
00102       /** \brief Opens the given file for reading from.
00103        *
00104        * If filename is an archive file, then this may extract more than one
00105        * .edc file from it. This will automatically determine if the file 
00106        * being read is an archive, and when opening the .edc files will
00107        * automatically determine if it is opening a binary or text edc file.
00108        */
00109       static std::list<Persistence> ReadOpen(std::string filename);
00110 
00111       //------------------------------------------------------------------------
00112       /** \brief Opens the given file for writing to.
00113        *
00114        * If filename references a file that already exists this will first check
00115        * to see  if it is a known archive type, in which case it will insert a
00116        * new edc file into that archive. Otherwise if the file exists but is not
00117        * a valid known archive type, then this will overwrite the existing file.
00118        *
00119        * \param filename The name of the file or archive to write to.
00120        *
00121        * \param format The .edc file format type to use for writing the edc
00122        * file. One of TEXT or BINARY.
00123        *
00124        * \param item_name If filename is an archive this can optionally be a
00125        * name to give the .edc file within that archive. By default EDoc will
00126        * use the name default.edc
00127        *
00128        * Note: Some archive formats will not ADD the edc file to the archive but
00129        * effectivly overwrite all existing .edc files in the archive with this
00130        * new .edc file. This si what the BFd implementation currently does.
00131        */
00132       static Persistence WriteOpen(std::string filename,
00133          EncodingType format = TEXT, std::string item_name="default.edc");
00134 
00135       //------------------------------------------------------------------------
00136       /** \brief Returns a representation of the name of the file being used.
00137        *
00138        * This is a string to will indicate the source AND work files if they
00139        * differ.
00140        */
00141       std::string GetFilenameRep() const;
00142 
00143       //------------------------------------------------------------------------
00144       /** \brief Destructor.
00145        *
00146        * Will delete any open temporary files.
00147        */
00148       virtual ~Persistence();
00149 
00150       //------------------------------------------------------------------------
00151       /** \brief See PersistenceIFace::Open()
00152        */
00153       virtual void Open()
00154       {return impl->Open();}
00155 
00156       //------------------------------------------------------------------------
00157       /** \brief See PersistenceIFace::Close()
00158        */
00159       virtual void Close()
00160       {return impl->Close();}
00161 
00162       //------------------------------------------------------------------------
00163       /** \brief See PersistenceIFace::ReadString()
00164        */
00165       virtual std::string ReadString(const char* key) 
00166       {return impl->ReadString(key);}
00167 
00168       //------------------------------------------------------------------------
00169       /** \brief See PersistenceIFace::ReadBoolean()
00170        */
00171       virtual bool ReadBoolean(const char* key)  
00172       {return impl->ReadBoolean(key);}
00173 
00174       //------------------------------------------------------------------------
00175       /** \brief See PersistenceIFace::ReadUInt8()
00176        */
00177       virtual uint8_t ReadUInt8(const char* key)  
00178       {return impl->ReadUInt8(key);}
00179 
00180       //------------------------------------------------------------------------
00181       /** \brief See PersistenceIFace::ReadInt32()
00182        */
00183       virtual int32_t ReadInt32(const char* key)  
00184       {return impl->ReadInt32(key);}
00185 
00186       //------------------------------------------------------------------------
00187       /** \brief See PersistenceIFace::ReadUInt32()
00188        */
00189       virtual uint32_t ReadUInt32(const char* key)  
00190       {return impl->ReadUInt32(key);}
00191 
00192       //------------------------------------------------------------------------
00193       /** \brief See PersistenceIFace::ReadRecordType()
00194        */
00195       virtual uint8_t ReadRecordType()  
00196       {return impl->ReadRecordType();}
00197 
00198       //------------------------------------------------------------------------
00199       /** \brief See PersistenceIFace::WriteString()
00200        */
00201       virtual void WriteString(const char* key, std::string value)  
00202       {return impl->WriteString(key, value);}
00203 
00204       //------------------------------------------------------------------------
00205       /** \brief See PersistenceIFace::WriteBoolean()
00206        */
00207       virtual void WriteBoolean(const char* key, bool value)  
00208       {return impl->WriteBoolean(key, value);}
00209 
00210       //------------------------------------------------------------------------
00211       /** \brief See PersistenceIFace::WriteUInt8()
00212        */
00213       virtual void WriteUInt8(const char* key, uint8_t value)  
00214       {return impl->WriteUInt8(key, value);}
00215 
00216       //------------------------------------------------------------------------
00217       /** \brief See PersistenceIFace::WriteInt32()
00218        */
00219       virtual void WriteInt32(const char* key, int32_t value)  
00220       {return impl->WriteInt32(key, value);}
00221 
00222       //------------------------------------------------------------------------
00223       /** \brief See PersistenceIFace::WriteUInt32()
00224        */
00225       virtual void WriteUInt32(const char* key, uint32_t value)  
00226       {return impl->WriteUInt32(key, value);}
00227 
00228       //------------------------------------------------------------------------
00229       /** \brief See PersistenceIFace::WriteUInt32Debug()
00230        */
00231       virtual void WriteUInt32Debug(const char* key, uint32_t value, 
00232          std::string debug_text)  
00233       {return impl->WriteUInt32Debug(key, value, debug_text);}
00234 
00235       //------------------------------------------------------------------------
00236       /** \brief See PersistenceIFace::WriteInt32Debug()
00237        */
00238       virtual void WriteInt32Debug(const char* key, int32_t value, 
00239          std::string debug_text) 
00240       {return impl->WriteInt32Debug(key, value, debug_text);}
00241 
00242       //------------------------------------------------------------------------
00243       /** \brief See PersistenceIFace::WriteRecordType()
00244        */
00245       virtual void WriteRecordType(uint8_t value)  
00246       {return impl->WriteRecordType(value);}
00247 
00248 
00249       //------------------------------------------------------------------------
00250       /** \brief Copy constructor.
00251        */
00252       Persistence(const Persistence& right);
00253 
00254       //------------------------------------------------------------------------
00255       /** \brief Assignment operator.
00256        */
00257       Persistence& operator=(const Persistence& right);
00258 
00259       //------------------------------------------------------------------------
00260 
00261    private:
00262 
00263       //------------------------------------------------------------------------
00264       /** \brief Private constructor.
00265        *
00266        * Users interface with the Open() and OpenSingle() calls which call this.
00267        */
00268       Persistence(bool read, 
00269          ManagedFile file_in,
00270          EncodingType format_in);
00271       
00272       //------------------------------------------------------------------------
00273       /** \brief Increases the reference count of impl.
00274        */
00275       void IncRef();
00276 
00277       //------------------------------------------------------------------------
00278       /** \brief Decreases the reference count of impl andwill delete it if
00279        * reference count reaches 0.
00280        */
00281       void DecRef();
00282       
00283       //------------------------------------------------------------------------
00284       /** \brief A reference to the managed file that is the source for this
00285        * persistence.
00286        *
00287        * This may represent a file inside an archive or a standard file. 
00288        */
00289       ManagedFile file;
00290 
00291       //------------------------------------------------------------------------
00292       /** \brief The format declared to use for encoding/decoding the file.
00293        *
00294        * This is one of text or binary.
00295        */
00296       EncodingType format;
00297       
00298       //------------------------------------------------------------------------
00299       /** \brief A pointer to the particular persistence layer to use.
00300        *
00301        * This is eithre an instance of TextPersistence or BinaryPersistence
00302        * depending on wether the .edc file is a text or binary format edc file.
00303        */
00304       PersistenceIFace* impl;
00305 
00306       //------------------------------------------------------------------------
00307       /** \brief A list of "factories" for all archive formats that can be
00308        * understood.
00309        */
00310       static std::list<ArchiveIFace*> archive_formats;
00311 
00312       //------------------------------------------------------------------------
00313 
00314    };
00315    //===========================================================================
00316 
00317 }
00318 
00319 #endif // EDOC_PERSISTENCE_H

Generated on Tue Jan 20 18:26:07 2009 for EDoc-0.2.1 by  doxygen 1.5.1