00001 #ifndef __STORE_COMMON_H
00002 #define __STORE_COMMON_H
00003
00004 #include "GPassword.h"
00005
00006 class StorageItem;
00007 class StorageObj;
00008 class StorageKit;
00009
00010 enum NodeRelation
00011 {
00012 NodeChild,
00013 NodeNext,
00014 NodePrev,
00015 };
00016
00017 #define GSUBFILE_NOBUFFERING 0
00018
00019 class GSubFilePtr;
00020 class GSubFile : public GFile
00021 {
00022 GSemaphore *Lck;
00023 GArray<GSubFilePtr*> Ptrs;
00024
00025 #if GSUBFILE_NOBUFFERING
00026 bool Buffer;
00027 uint8 *Buf;
00028 int Block;
00029 int Shift;
00030 int64 Cur;
00031 int64 Pos;
00032 bool Dirty;
00033
00034 bool SetBlock(int64 b);
00035 #endif
00036
00037 public:
00038 typedef GAutoPtr<GSemaphore::Auto> SubLock;
00039
00040 GSubFile(GSemaphore *lock, bool Buffering = true);
00041 ~GSubFile();
00042
00043 GSubFilePtr *Create(const char *file, int line);
00044 void Detach(GSubFilePtr *Ptr);
00045 SubLock Lock(char *file, int line);
00046
00047 #if GSUBFILE_NOBUFFERING
00048 int Open(char *Str = 0, int Int = 0);
00049 int Read(void *Buffer, int Size, int Flags = 0);
00050 int Write(const void *Buffer, int Size, int Flags = 0);
00051 int64 GetPos();
00052 int64 SetPos(int64 pos);
00053 #endif
00054 };
00055
00056 class GSubFilePtr : public GFile
00057 {
00058 friend class GSubFile;
00059 GSubFile *File;
00060
00061
00062 bool Sub;
00063
00064 int64 Start;
00065 int64 Len;
00066 int64 Pos;
00067
00068
00069 char *SrcFile;
00070 int SrcLine;
00071
00072
00073 bool OurStatus;
00074
00075
00076 int64 ActualPos;
00077 bool ActualStatus;
00078 bool ActualSwap;
00079
00080 bool SaveState();
00081 bool RestoreState();
00082
00083 public:
00084 GSubFilePtr(GSubFile *Parent, const char *file, int line);
00085 ~GSubFilePtr();
00086
00088 bool SetSub
00089 (
00091 int64 start,
00093 int64 len
00094 );
00095
00097 bool GetSub(int64 &start, int64 &len);
00098
00100 void ClearSub();
00101
00102
00103 int Open(char *Str = 0, int Int = 0);
00104 bool IsOpen();
00105 int Close();
00106 int64 GetSize();
00107 int64 SetSize(int64 Size);
00108 int64 GetPos();
00109 int64 SetPos(int64 pos);
00110 int64 Seek(int64 To, int Whence);
00111 GStreamI *Clone();
00112 bool Eof();
00113 bool GetStatus();
00114 void SetStatus(bool s);
00115 int Read(void *Buffer, int Size, int Flags = 0);
00116 int Write(const void *Buffer, int Size, int Flags = 0);
00117 int ReadStr(char *Buf, int Size);
00118 int WriteStr(char *Buf, int Size);
00119 };
00120
00121 class StorageItem
00122 {
00123 public:
00124 StorageObj *Object;
00125
00126 virtual ~StorageItem() {}
00127
00128
00129 virtual int GetType() = 0;
00130 virtual void SetType(int i) = 0;
00131 virtual int GetTotalSize() = 0;
00132 virtual int GetObjectSize() = 0;
00133 virtual StorageKit *GetTree() = 0;
00134
00135
00136 virtual StorageItem *GetNext() = 0;
00137 virtual StorageItem *GetPrev() = 0;
00138 virtual StorageItem *GetParent() = 0;
00139 virtual StorageItem *GetChild() = 0;
00140 virtual StorageItem *CreateNext(StorageObj *Obj) = 0;
00141 virtual StorageItem *CreateChild(StorageObj *Obj) = 0;
00142 virtual StorageItem *CreateSub(StorageObj *Obj) = 0;
00143 virtual bool DeleteChild(StorageItem *Obj) = 0;
00144 virtual bool DeleteAllChildren() = 0;
00145
00146
00147 virtual bool Save() = 0;
00148 virtual GFile *GotoObject(const char *file, int line) = 0;
00149 virtual bool EndOfObj(GFile &f) = 0;
00150 };
00151
00152 class StorageObj
00153 {
00154 friend class StorageKit;
00155
00156 bool StoreDirty;
00157
00158 public:
00159 StorageItem *Store;
00160
00161 StorageObj()
00162 {
00163 StoreDirty = false;
00164 Store = 0;
00165 }
00166
00167 virtual ~StorageObj()
00168 {
00169 if (Store)
00170 {
00171 Store->Object = 0;
00172 DeleteObj(Store);
00173 }
00174 }
00175
00176 bool GetDirty() { return StoreDirty; }
00177
00178 virtual int Type() = 0;
00179 virtual int Sizeof() = 0;
00180 virtual bool Serialize(GFile &f, bool Write) = 0;
00181 virtual bool SetDirty(bool d = true) { StoreDirty = d; return true; }
00182 };
00183
00184 class StorageValidator
00185 {
00186 public:
00187 virtual bool CompactValidate(GView *Parent, StorageItem *Item) = 0;
00188 virtual void CompactDone(StorageItem *Item) = 0;
00189 };
00190
00191 class StorageKit
00192 {
00193 protected:
00194 void StorageObj_SetDirty(StorageObj *Obj, bool d)
00195 {
00196 Obj->StoreDirty = d;
00197 }
00198
00199 public:
00200 virtual ~StorageKit() {}
00201
00202
00203 virtual int GetStatus() = 0;
00204 virtual bool GetReadOnly() = 0;
00205 virtual int GetVersion() = 0;
00206 virtual uint64 GetFileSize() = 0;
00207 virtual bool GetPassword(GPassword *p) = 0;
00208 virtual bool SetPassword(GPassword *p) = 0;
00209 virtual GSemaphore *GetLock() = 0;
00210 virtual char *GetFileName() = 0;
00211
00212
00213 virtual StorageItem *GetRoot() = 0;
00214 virtual StorageItem *CreateRoot(StorageObj *Obj) = 0;
00215 virtual bool DeleteItem(StorageItem *Item) = 0;
00216 virtual bool SeparateItem(StorageItem *Item) = 0;
00217 virtual bool AttachItem(StorageItem *Item, StorageItem *To, NodeRelation Relationship = NodeChild) = 0;
00218
00219
00220 virtual bool Compact(Progress *p, bool Interactive, StorageValidator *Validator = 0) = 0;
00221 };
00222
00223 #endif