00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
#ifndef KEYHANDLING_H
00025
#define KEYHANDLING_H
00026
00027
#include <sys/types.h>
00028
#include <stdio.h>
00029
#include <stdlib.h>
00030
00031
#include "mnstream.h"
00032
00033 typedef u_int64_t
u_int64;
00034 typedef u_int32_t u_int32;
00035
00036
template <
class T>
00037 class MasterKey {
00038
00039
public:
00040
00041
00042 MasterKey(
const u_int32 Buckets,
const u_int32 BucketSize,
const u_int32 KeyLength) {
00043 _Buckets = Buckets;
00044 _BucketSize = BucketSize;
00045 _KeyLength = KeyLength;
00046 _initialized =
false;
00047 _data = NULL;
00048 }
00049
00050
00051 ~MasterKey() {
00052
deinit();
00053 }
00054
00055
00056
void init();
00057
00058
00059 void deinit() {
00060 free(_data);
00061 _data = NULL;
00062 _initialized =
false;
00063 };
00064
00065
00066 T*
getKey(
const u_int32 Bucket,
const u_int32 Key) {
00067
if ( (Bucket > _Buckets) || (Key > _BucketSize) || (!_initialized) )
00068
return NULL;
00069
return _data + ( ( (Bucket * _BucketSize) + Key ) * _KeyLength ) *
sizeof(T);
00070 }
00071
00072
00073 T*
setKey(
const u_int32 Bucket,
const u_int32 Key,
const char*
const Source) {
00074
if ( (Bucket > _Buckets) || (Bucket < 0) || (Key > _BucketSize) || (Key < 0) || (!_initialized) || (Source == 0) )
00075
return NULL;
00076 T* Destination = _data + ( ( (Bucket * _BucketSize) + Key ) * _KeyLength ) *
sizeof(T);
00077
return (T*)memcpy( Destination, Source, _KeyLength *
sizeof(T) );
00078
00079 }
00080
00081
00082 u_int32 getKeyLength() {
return _KeyLength; };
00083
00084
00085 u_int32 getBucketSize() {
return _BucketSize; };
00086
00087
00088 u_int32 getBucketNumber() {
return _Buckets; };
00089
00090
00091
00092
00093
int readFile(FILE* file);
00094
00095
00096
00097
00098
int writeFile(FILE* file);
00099
00100
private:
00101
00102
bool _initialized;
00103
00104
00105
u_int32 _Buckets;
00106
00107
00108
u_int32 _BucketSize;
00109
00110
00111
u_int32 _KeyLength;
00112
00113
00114 T* _data;
00115
00116 };
00117
00118
00119
template <
class T>
00120 class UserKey {
00121
00122
public:
00123
00124
00125
UserKey(
const u_int32 Buckets,
const u_int32 KeyLength);
00126
00127
00128
~UserKey();
00129
00130
00131 void setID(u_int32 ID[]) {
for(
u_int32 i=0; i<_Buckets; i++) _ID[i] = ID[i]; };
00132
00133
00134
00135
00136
00137
void readMaster(
MasterKey<T>* Master);
00138
00139
00140
00141
00142
int readFile(FILE* file);
00143
00144
00145
00146
00147
int writeFile(FILE* file);
00148
00149
00150 T*
getKey(u_int32 Number) {
return _data[Number]; };
00151
00152
00153 u_int32 getID(u_int32 Number) {
return _ID[Number]; };
00154
00155
private:
00156
00157
bool _memReserved;
00158
00159
00160
u_int32 _Buckets;
00161
00162
00163
u_int32 _KeyLength;
00164
00165
00166 T** _data;
00167
00168
00169 T* _mem;
00170
00171
00172
u_int32* _ID;
00173
00174 };
00175
00176
00177
00178
template <
class T>
00179 void MasterKey<T>::init() {
00180
00181
if (_initialized)
deinit();
00182
00183 _data = (T*)malloc(_Buckets * _BucketSize * _KeyLength *
sizeof(T));
00184
00185
if (_data == NULL) {
00186 cerr << __FILE__ <<
":" << __LINE__ <<
" malloc() failed" << endl;
00187 exit(1);
00188 }
00189
00190 _initialized =
true;
00191
00192 }
00193
00194
00195
00196
00197
00198
template <
class T>
00199 int MasterKey<T>::readFile(FILE* file) {
00200
00201
if (!_initialized)
init();
00202
00203
u_int32 ElementCount = fread(_data,
sizeof(T)*_KeyLength, _Buckets*_BucketSize, file);
00204
00205
return ElementCount - _Buckets*_BucketSize;
00206
00207 }
00208
00209
00210
00211
00212
00213
template <
class T>
00214 int MasterKey<T>::writeFile(FILE* file) {
00215
00216
if (!_initialized)
return -1;
00217
00218
u_int32 ElementCount = fwrite(_data,
sizeof(T)*_KeyLength, _Buckets*_BucketSize, file);
00219
00220
return ElementCount - _Buckets*_BucketSize;
00221
00222 }
00223
00224
00225
00226
00227
00228
00229
00230
template <
class T>
00231 UserKey<T>::UserKey(
const u_int32 Buckets,
const u_int32 KeyLength) {
00232
00233 _Buckets = Buckets;
00234 _KeyLength = KeyLength;
00235
00236 _ID =
new u_int32[Buckets];
00237 _data =
new T*[Buckets];
00238
00239 _memReserved =
false;
00240
00241 }
00242
00243
00244
00245
template <
class T>
00246 UserKey<T>::~UserKey() {
00247
00248
if (_memReserved) free(_mem);
00249
00250
delete[] _ID;
00251
delete[] _data;
00252
00253 }
00254
00255
00256
00257
template <
class T>
00258 void UserKey<T>::readMaster(
MasterKey<T>* Master) {
00259
00260
for(
u_int32 counter=0; counter < _Buckets; counter++)
00261 _data[counter] = Master->
getKey(counter, _ID[counter]);
00262
00263 };
00264
00265
00266
00267
00268
00269
template <
class T>
00270 int UserKey<T>::readFile(FILE* file) {
00271
00272
u_int32 KeyBytes = _KeyLength *
sizeof(T);
00273
00274
if (!_memReserved) {
00275
00276 _mem = (T*)malloc(_Buckets * KeyBytes);
00277
00278
if (_mem == NULL) {
00279 cerr << __FILE__ <<
":" << __LINE__ <<
" malloc() failed" << endl;
00280 exit(1);
00281 }
00282
00283 }
00284
00285
u_int32 ElementCount = fread(_mem, KeyBytes, _Buckets, file);
00286
00287
for(
u_int32 counter=0; counter < _Buckets; counter++)
00288 _data[counter] = _mem + counter * KeyBytes;
00289
00290
return ElementCount - _Buckets;
00291
00292 };
00293
00294
00295
00296
00297
00298
template <
class T>
00299 int UserKey<T>::writeFile(FILE* file) {
00300
00301
u_int32 counter;
00302
00303
u_int32 KeyBytes = _KeyLength *
sizeof(T);
00304
00305
for(counter=0; counter < _Buckets; counter++) {
00306
00307
u_int32 n = fwrite(_data[counter], KeyBytes, 1, file);
00308
if (n != 1) { counter--;
break; }
00309
00310 }
00311
00312
return counter - _Buckets;
00313
00314 }
00315
00316
00317
#endif