UniSet
1.4.0
|
00001 /* This file is part of the UniSet project 00002 * Copyright (c) 2009 Free Software Foundation, Inc. 00003 * Copyright (c) 2009 Ivan Donchevskiy 00004 * 00005 * This program is free software; you can redistribute it and/or modify 00006 * it under the terms of the GNU General Public License as published by 00007 * the Free Software Foundation; either version 2 of the License, or 00008 * (at your option) any later version. 00009 * 00010 * This program is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 * GNU General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU General Public License 00016 * along with this program; if not, write to the Free Software 00017 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00018 */ 00019 // -------------------------------------------------------------------------- 00023 // -------------------------------------------------------------------------- 00024 00025 #ifndef Storages_H_ 00026 #define Storages_H_ 00027 00028 #include <stdio.h> 00029 #include <stdlib.h> 00030 #include <string.h> 00031 #include <string> 00032 00035 #define EMPTY_BLOCK -5 00036 #define EMPTY_ELEM -1 00037 00038 #define key_size 20 00039 00041 struct StorageAttr 00042 { 00043 int k_size, inf_size,size,block_number; 00044 int lim, seekpos; 00045 } __attribute__((__packed__)); 00046 00048 struct CycleStorageAttr 00049 { 00050 int size, inf_size, seekpos; 00051 } __attribute__((__packed__)); 00052 00054 struct TableStorageElem 00055 { 00056 char status; 00057 char key[key_size]; 00058 } __attribute__((__packed__)); 00059 00061 struct TableBlockStorageElem 00062 { 00063 int count; 00064 } __attribute__((__packed__)); 00065 00067 struct CycleStorageElem 00068 { 00069 char status; 00070 } __attribute__((__packed__)); 00071 00072 class TableStorage 00073 { 00074 FILE *file; 00075 int size,seekpos, inf_size; 00076 int head; 00077 public: 00078 TableStorage(const char* name, int inf_sz, int sz, int seek); 00079 ~TableStorage(); 00080 int addRow(char* key, char* val); 00081 int delRow(char* key); 00082 char* findKeyValue(char* key, char* val); 00083 }; 00084 00085 class TableBlockStorage 00086 { 00087 public: 00089 TableBlockStorage(); 00090 00093 TableBlockStorage(const char* name, int byte_sz, int key_sz, int inf_sz, int inf_count, int block_num, int block_lim, int seek, bool create=false); 00094 00095 ~TableBlockStorage(); 00096 00110 bool open(const char* name, int byte_sz, int inf_sz, int key_sz, int inf_count, int block_num, int block_lim, int seek); 00111 bool create(const char* name, int byte_sz, int inf_sz, int key_sz, int inf_count, int block_num, int block_lim, int seek); 00112 00114 bool addRow(void* key, void* val); 00115 00117 bool delRow(void* key); 00118 00120 void* findKeyValue(void* key, void* val); 00121 00123 int getCurBlock(void); 00124 00125 inline int getByteSize() { return (size*full_size + sizeof(StorageAttr)); } 00126 00127 bool checkAttr( int key_sz, int inf_sz, int sz, int block_num, int block_lim, int seek ); 00128 00129 protected: 00130 FILE *file; 00131 int inf_size; 00132 private: 00133 int max,cur_block; 00134 TableBlockStorageElem* mem; 00135 int k_size, lim,seekpos; 00136 int size,block_size,block_number,full_size; 00137 void filewrite(int seek, bool needflush=true); 00138 bool copyToNextBlock(); 00139 bool keyCompare(int i, void* key); 00140 void* keyPointer(int num); 00141 void* valPointer(int num); 00142 TableBlockStorageElem* elemPointer(int num); 00143 }; 00144 00145 class CycleStorage 00146 { 00147 public: 00148 00149 class CycleStorageIterator 00150 { 00151 public: 00152 typedef CycleStorageIterator Self; 00153 CycleStorageIterator(): 00154 str(NULL), cs(NULL), current(0) {} 00155 CycleStorageIterator(CycleStorage* cstor) 00156 { 00157 cs = cstor; 00158 current = 0; 00159 } 00160 CycleStorageIterator(CycleStorage* cstor, int num) 00161 { 00162 cs = cstor; 00163 if( num<0 || num>=cs->getSize() ) current = 0; 00164 current = num; 00165 } 00166 00167 void* operator *() const 00168 { 00169 return str; 00170 } 00171 00172 Self& operator++(); 00173 Self operator++(int); 00174 00175 Self& operator--(); 00176 Self operator--(int); 00177 00178 inline bool operator==(const Self& other) const 00179 { 00180 if( str==NULL || other.str==NULL ) 00181 { 00182 if( str==NULL && other.str==NULL ) 00183 return true; 00184 else 00185 return false; 00186 } 00187 if( memcmp(str, other.str, cs->getInfSize())==0 ) 00188 return true; 00189 return false; 00190 } 00191 00192 inline bool operator!=(const Self& other) const 00193 { 00194 if( str==NULL || other.str==NULL ) 00195 { 00196 if( str==NULL && other.str==NULL ) 00197 return false; 00198 else 00199 return true; 00200 } 00201 if( memcmp(str, other.str, cs->getInfSize())==0 ) 00202 return false; 00203 return true; 00204 } 00205 private: 00206 void* str; 00207 CycleStorage* cs; 00208 int current; 00209 }; 00210 00211 typedef CycleStorageIterator iterator; 00212 00214 CycleStorage(); 00215 00218 CycleStorage(const char* name, int byte_sz, int inf_sz, int inf_count, int seek,bool create=false); 00219 00220 ~CycleStorage(); 00221 00231 bool open(const char* name, int byte_sz, int inf_sz, int inf_count, int seek); 00232 bool create(const char* name, int byte_sz, int inf_sz, int inf_count, int seek); 00233 bool isOpen(){ return (file!=NULL); } 00234 00235 00237 bool addRow(void* str); 00238 00240 bool delRow(int row); 00241 00243 bool delAllRows(void); 00244 00246 void* readRow(int num, void* str); 00247 00249 int getIter(void); 00250 00252 bool setSize(int count); 00253 00254 inline int getByteSize() { return (size*full_size + sizeof(CycleStorageAttr)); } 00255 inline int getSize(){ return size; } 00256 inline int getInfSize(){ return inf_size; } 00257 inline int getFullSize(){ return full_size; } 00258 00259 bool checkAttr(int inf_sz, int inf_count, int seek); 00260 00261 inline int getHead(){ return head; } 00262 inline int getTail(){ return tail; } 00263 00264 iterator begin() 00265 { 00266 return iterator(this); 00267 } 00268 00269 iterator end() 00270 { 00271 return iterator(this,this->getTail()); 00272 } 00273 00274 protected: 00275 FILE *file; 00276 int inf_size; 00277 int head,tail; 00278 private: 00279 int size,seekpos, iter; 00280 int full_size; 00281 void filewrite(CycleStorageElem* jrn,int seek, bool needflush=true); 00282 void* valPointer(void* pnt); 00283 bool findHead(); 00284 }; 00285 00286 #endif