00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #ifndef __STORE2_H
00012 #define __STORE2_H
00013
00014 #include "Gdc2.h"
00015 #include "GContainers.h"
00016 #include "Progress.h"
00017 #include "StoreCommon.h"
00018
00019 class WriterItem;
00020 class GSubFilePtr;
00021
00022 namespace Storage2
00023 {
00024 #define STORAGE2_ITEM_MAGIC 0x123400AB
00025 #define STORAGE2_MAGIC 0x123400FE
00026
00027 class StorageKitImpl;
00028 class StorageItemImpl;
00029 class StorageKitImplPrivate;
00030
00031 struct StorageItemHeader
00032 {
00033 uint32 Magic;
00034
00035
00036 uint32 Type;
00037 uint32 DataLoc;
00038 uint32 DataSize;
00039
00040
00041 uint32 ParentLoc;
00042 uint32 DirLoc;
00043 uint32 DirCount;
00044 uint32 DirAlloc;
00045
00046
00047 StorageItemHeader();
00048 bool Serialize(GFile &f, bool Write);
00049 void SwapBytes();
00050 };
00051
00052 class StorageItemImpl : public StorageItem
00053 {
00054 friend class StorageKitImpl;
00055 friend class StorageKitImplPrivate;
00056 friend class ::StorageObj;
00057
00058 friend class WriterItem;
00059
00060 private:
00061 uint32 StoreLoc;
00062 bool HeaderDirty;
00063 StorageItemHeader *Header;
00064 StorageItemHeader *Dir;
00065 StorageItemHeader *Temp;
00066
00067 StorageItemImpl *Next;
00068 StorageItemImpl *Prev;
00069 StorageItemImpl *Parent;
00070 StorageItemImpl *Child;
00071
00072 StorageKitImpl *Tree;
00073
00074 bool IncDirSize();
00075 bool DirChange();
00076
00077 bool SetDirty(bool Dirty);
00078 bool SerializeHeader(GFile &f, bool Write);
00079 bool SerializeObject(GSubFilePtr &f, bool Write);
00080
00082 void CleanAll();
00083
00084 public:
00085 StorageItemImpl(StorageItemHeader *header);
00086 ~StorageItemImpl();
00087
00088
00089 StorageItem *GetNext();
00090 StorageItem *GetPrev();
00091 StorageItem *GetParent();
00092 StorageItem *GetChild();
00093
00094 StorageItem *CreateNext(StorageObj *Obj);
00095 StorageItem *CreateChild(StorageObj *Obj);
00096 StorageItem *CreateSub(StorageObj *Obj);
00097 bool InsertSub(StorageItem *Obj, int At = -1);
00098 bool DeleteChild(StorageItem *Obj);
00099 bool DeleteAllChildren();
00100
00101
00102 int GetType();
00103 void SetType(int i);
00104 int GetTotalSize();
00105 int GetObjectSize();
00106 StorageKit *GetTree();
00107
00108
00109 GFile *GotoObject(const char *file, int line);
00110 bool EndOfObj(GFile &f);
00111 bool Save();
00112
00113
00114 int GetStoreLoc() { return StoreLoc; }
00115 void Dump(int d)
00116 {
00117 char s[256];
00118 memset(s, ' ', d << 1);
00119 s[d << 1] = 0;
00120 LgiTrace("%sItem=%p Obj=%p Type=%x\n",
00121 s, this, Object, Object?Object->Type():0);
00122 for (StorageItemImpl *c=Child; c; c=c->Next)
00123 {
00124 c->Dump(d + 1);
00125 }
00126 }
00127 };
00128
00129 struct StorageHeader {
00130
00131 int Magic;
00132 int Version;
00133 int Reserved1;
00134 int Reserved2;
00135 char Password[32];
00136 int Reserved3[4];
00137 };
00138
00139 class StorageKitImpl :
00140 public GBase,
00141 public StorageKit,
00142 public GSemaphore
00143 {
00144 friend class StorageItemImpl;
00145 friend class StorageKitImplPrivate;
00146
00147 protected:
00148 StorageKitImplPrivate *d;
00149
00150
00151 char *FileName;
00152 bool Status;
00153 int StoreLoc;
00154 int Version;
00155 bool ReadOnly;
00156 GPassword Password;
00157
00158 class GSubFile *File;
00159 class GSubFilePtr *CreateFilePtr(const char *file, int line);
00160
00161
00162 StorageItemHeader RootHeader;
00163 StorageItemImpl *Root;
00164
00165 bool _ValidLoc(uint64 Loc);
00166 bool _Serialize(GFile &f, bool Write);
00167
00168 public:
00169 StorageKitImpl(char *FileName);
00170 ~StorageKitImpl();
00171
00172 bool IsOk();
00173 int GetStatus() { return Status; }
00174 bool GetReadOnly() { return ReadOnly; }
00175 int GetVersion() { return Version; }
00176 uint64 GetFileSize();
00177 bool GetPassword(GPassword *p);
00178 bool SetPassword(GPassword *p);
00179 GSemaphore *GetLock();
00180 char *GetFileName() { return FileName; }
00181
00182 StorageItem *GetRoot();
00183 StorageItem *CreateRoot(StorageObj *Obj);
00184
00185 bool Compact(Progress *p, bool Interactive, StorageValidator *Validator = 0);
00186 bool DeleteItem(StorageItem *Item);
00187 bool SeparateItem(StorageItem *Item);
00188 bool AttachItem(StorageItem *Item, StorageItem *To, NodeRelation Relationship = NodeChild);
00189 };
00190
00191 }
00192
00193 #endif