Main Page Namespace List Class Hierarchy Compound List File List Namespace Members Compound Members File Members Related Pages

# DAGDBFile.h

Go to the documentation of this file.

    00001
    00040 #ifndef __DBFile_H__
    00041 #define __DBFile_H__
    00042
    00043 #include <fstream.h>
    00044 #include "String.h"
    00045 #include "SmartArray.h"
    00046 #include "DAG.h"
    00047
    00048 #define GROW_FACTOR     20
    00049
    00050 using namespace std;
    00051
    00052 class DAGDBFile : public fstream
    00053 {
    00054         struct DAGINFO
    00055         {
    00056                 int nType;
    00057                 streampos nOffset;
    00058
    00059                 friend istream& operator>>(istream& is, DAGINFO& x)
    00060                 { is.read((char*)&x, sizeof(x)); }
    00061
    00062                 friend ostream& operator<<(ostream& os, const DAGINFO& x)
    00063                 { os.write((char*)&x, sizeof(x)); }
    00064         };
    00065
    00066         class DBINFO
    00067         {
    00068                 int m_nErrorCheck;
    00069
    00070         public:
    00071                 int m_nMaxTSVDimension;
    00072                 streampos m_nTailOffset; // Leave it as the last field if possible.
    00073
    00074                 DBINFO() { Clear(); }
    00075
    00076                 void Clear()
    00077                 {
    00078                         m_nMaxTSVDimension = 0;
    00079                         m_nTailOffset = 0;
    00080                         m_nErrorCheck = -1;
    00081                 }
    00082
    00083                 bool IsValid()
    00084                 {
    00085                         return m_nErrorCheck == (m_nMaxTSVDimension + m_nTailOffset);
    00086                 }
    00087
    00088                 void MakeValid()
    00089                 {
    00090                         m_nErrorCheck = m_nMaxTSVDimension + m_nTailOffset;
    00091                 }
    00092         };
    00093
    00094         int m_nDBSize;
    00095         String m_strFileName;
    00096
    00097         SmartArray<DAGINFO> m_dagInfo;
    00098         DBINFO* m_pDBInfo;
    00099         bool* m_pbModified;
    00100
    00101 public:
    00102         DAGDBFile() : m_dagInfo(0, GROW_FACTOR)
    00103         {
    00104                 m_nDBSize = 0;
    00105                 m_pbModified = new bool(false);
    00106                 m_pDBInfo = new DBINFO;
    00107         }
    00108
    00109         ~DAGDBFile() { close(); delete m_pDBInfo; delete m_pbModified; }
    00110
    00111         int GetDAGCount() const { return m_dagInfo.GetSize(); }
    00112         String GetName() const { return m_strFileName; }
    00113         int GetClassID(const String& strClassName) const;
    00114         DAG* CreateDAGObj(int nClassID) const;
    00115
    00116         void MoveTo(std::streamoff off) { clear(); seekg(off, ios::beg);        }
    00117         void MoveToBeg() { MoveTo(0); }
    00118         void MoveToEnd() { clear(); seekg(0, ios::end); }
    00119
    00121         void open(const char* szName, ios_base::openmode nMode);
    00122
    00124         void close();
    00125
    00126         bool ReadTailInfo();
    00127         void WriteTailInfo();
    00128         DAGPtr ReadDAG(int nDagId, bool bOnlyDataForMatching = false);
    00129         DAGPtr ReadDAG(std::streamoff nDagOffset, const String& strClassName);
    00130         bool AddDAG(const DAG* pDag, bool bReadSaveTailInfo = false);
    00131         bool RebuildTailInfo();
    00132
    00133         bool IsDBModified() const { return *m_pbModified; }
    00134         bool SetDBModified(bool bNewVal) { return *m_pbModified = bNewVal; }
    00135
    00136         const DBINFO& DBInfo() const { return *m_pDBInfo; }
    00137         DBINFO& DBInfo() { return *m_pDBInfo; }
    00138
    00139         int GetMaxTSVDimension() const { return DBInfo().m_nMaxTSVDimension; }
    00140 };
    00141
    00142 #endif //__DBFile_H__