00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119 #include "StDbBuffer.h"
00120 #include <stdlib.h>
00121 #include <string.h>
00122 #include "stdb_streams.h"
00123
00124 #ifndef __STDB_STANDALONE__
00125 #include "StMessMgr.h"
00126 #else
00127 #define LOG_DEBUG cout
00128 #define LOG_INFO cout
00129 #define LOG_WARN cout
00130 #define LOG_ERROR cerr
00131 #define LOG_FATAL cerr
00132 #define LOG_QA cout
00133 #define endm "\n"
00134 #endif
00135
00136 #ifdef HPUX
00137 #define freeze(i) str()
00138 #endif
00139
00141
00142 void StDbBuffer::Print(){
00143 if (mCol) {
00144 int i;
00145 for (i=0; i<=mLast;i++) {
00146 if (mCol[i].type==_char) {
00147 char* tVal;int len;
00148 ReadArray(tVal,len,mCol[i].name);
00149 cout << mCol[i].name <<"=" << "#BIN (" << len << ") bytes" <<endl;
00150 }else {
00151 char** tVal; int len;
00152 ReadArray(tVal,len,mCol[i].name);
00153 int j;
00154 for (j=0;j<len;j++)
00155 {cout << mCol[i].name <<"["<< j << "]=" << tVal[j] <<endl;}
00156 };
00157 };
00158 };
00159 };
00160
00162 void StDbBuffer::zeroColumn(int istart, int iend) {
00163 for(int i=istart;i<iend+1;i++){
00164 mCol[i].name = 0;
00165 mCol[i].val = 0;
00166 }
00167 }
00168
00169
00171 void StDbBuffer::Raz(){
00172
00173 int i;
00174 for (i=0;i<=mLast;i++) {
00175 if (mCol[i].val) {
00176 if (mCol[i].type>=_ascii){
00177 char ** tTextVal= (char**)mCol[i].val;
00178 int j;
00179 for(j=0;j<(int)mCol[i].length;j++) {
00180
00181 if (tTextVal[j]) delete [] tTextVal[j];
00182 };
00183 delete [] tTextVal;
00184 } else {
00185 delete [] mCol[i].val ;
00186 };
00187 };
00188 if (mCol[i].name) delete [] mCol[i].name;
00189 mCol[i].val=0;
00190 mCol[i].name=0;
00191 mCol[i].type=_char;
00192 mCol[i].length=0;
00193 };
00194 mCur=0;
00195 mLast=-1;
00196 }
00197
00199 char **StDbBuffer::WhatsIn(){
00200 char **tIn;
00201 tIn=new char*[mLast+2];
00202 int i;
00203 for (i=0;i<=mLast;i++){
00204 tIn[i]=mCol[i].name;
00205 };
00206 tIn[mLast+1]=0;
00207 return tIn;
00208 }
00209
00211 bool StDbBuffer::Find_Col (const char *aName){
00212
00213
00214 if (mLast==-1) return false;
00215
00216 for (int tCount=0;tCount<mLast+1;tCount++){
00217 mCur++;
00218 if (mCur>mLast) mCur=0;
00219
00220 if (!strcmp(mCol[mCur].name,aName)) return true;
00221 };
00222
00223 return false;
00224 }
00225
00226
00228 void StDbBuffer::AddField(const char *aName, const myctype aTpe,const void* aVal,const int aLen) {
00229
00230
00231
00232
00233 if (mLast+1>mMax) {
00234 column *tCol=new column[mMax*2+1];
00235 memcpy(tCol,mCol,(mMax+1)*sizeof(column));
00236 if (mCol) delete [] mCol;
00237 mCol=tCol;
00238 zeroColumn(mMax+1,2*mMax);
00239 mMax=mMax*2;
00240 }
00241 mLast++;
00242 mCur=mLast;
00243 if(mCol[mCur].name) delete [] mCol[mCur].name;
00244 mCol[mCur].name=new char[strlen(aName)+1];
00245 strcpy(mCol[mCur].name,aName);
00246 mCol[mCur].val=0;
00247 ChangeField(aTpe,aVal,aLen);
00248 };
00249
00251 void StDbBuffer::ChangeField(const myctype aTpe,const void* aVal,const int aLen) {
00252 if (mCol[mCur].val){
00253 if(mCol[mCur].type>=_ascii){
00254 char ** tTextVal= (char**)mCol[mCur].val;
00255 int j;
00256 for(j=0;j<(int)mCol[mCur].length;j++) {
00257 if (tTextVal[j]) delete [] tTextVal[j];
00258 };
00259 delete [] tTextVal ; mCol[mCur].val=0;
00260 }
00261 }
00262 mCol[mCur].type=aTpe;
00263 mCol[mCur].length=aLen;
00264 if (aTpe<_ascii) {
00265 mCol[mCur].val=new char[aLen*mycsize[aTpe]];
00266 MemSwapCpy(mCol[mCur].val,(char*)aVal,aLen*mycsize[aTpe],mycswapl[aTpe],Auto);
00267 } else {
00268 mCol[mCur].val=(char*) new char*[aLen];
00269 char** tVal=(char**)aVal;
00270 char** tStoreVal=(char**)mCol[mCur].val;
00271 int i;
00272 for (i=0;i<aLen;i++) {
00273 if (tVal[i]==0) {
00274 (tStoreVal)[i]=0;
00275 } else {
00276 tStoreVal[i]=new char[strlen(tVal[i])+1];
00277 strcpy(tStoreVal[i],tVal[i]);
00278 };
00279 };
00280 };
00281
00282
00283 };
00284
00286 void StDbBuffer::MemSwapCpy(char* where,char* from,int len,int swaplen,BuffMode mode) {
00287 if (mode==Auto) mode=mMode;
00288 if (swaplen<=1||mode==Storage) {
00289 memcpy(where,from,len);
00290 } else {
00291 if (len%swaplen!=0) {
00292 LOG_ERROR << "memswapcy: len not in agreement with swapping - binary truncate " << endm;
00293 };
00294 int tNbCpy=len/swaplen;
00295 int i;
00296 for (i=0;i<tNbCpy;i++){
00297 where+=swaplen;
00298 int j;
00299 for (j=0;j<swaplen;j++){
00300 where--;
00301 *where=*from;
00302 from++;
00303 }
00304 where+=swaplen;
00305 };
00306 };
00307 };
00308
00309
00311 void StDbBuffer::StrConv(char* aVal,char &s){s=aVal[0];};
00312 void StDbBuffer::StrConv(char* aVal,unsigned char &s){s=(unsigned char)atoi(aVal);};
00313 void StDbBuffer::StrConv(char* aVal,short &s){s=(short)atoi(aVal);};
00314 void StDbBuffer::StrConv(char* aVal,unsigned short &s){s=(unsigned short) atoi(aVal);};
00315 void StDbBuffer::StrConv(char* aVal,int &s){s=atoi(aVal);};
00316 void StDbBuffer::StrConv(char* aVal,unsigned int &s){s=strtoul(aVal,0,10);};
00317 void StDbBuffer::StrConv(char* aVal,long &s){s=atoi(aVal);};
00318 void StDbBuffer::StrConv(char* aVal,unsigned long &s){s=strtoul(aVal,0,10);};
00319 #ifndef __osf__
00320 void StDbBuffer::StrConv(char* aVal,long long &s){s=atoll(aVal);};
00321 #else
00322 void StDbBuffer::StrConv(char* aVal,long long &s){s=atol(aVal);};
00323 #endif
00324 void StDbBuffer::StrConv(char* aVal,float &s){s=(float) atof(aVal);};
00325 void StDbBuffer::StrConv(char* aVal,double &s){s=atof(aVal);};
00326 void StDbBuffer::StrConv(char* aVal,char* &s){s=new char[strlen(aVal)+1];strcpy(s,aVal);};
00327
00328 #define castcase(typelist,casttype,tpe) case typelist: {casttype *tVal=(casttype*)aVal;casttype tValSwap;MemSwapCpy((char*)&tValSwap,(char*)tVal,mycsize[typelist],mycswapl[typelist],Client);*s=(tpe)tValSwap;};break;
00329
00330 #define genwritemem(tpe) \
00331 bool StDbBuffer::WriteMem( tpe *s,void* aVal, myctype type)\
00332 {bool tRetVal=true;\
00333 switch (type) {\
00334 castcase(_char,char,tpe);\
00335 castcase(_uchar,unsigned char,tpe);\
00336 castcase(_short,short,tpe);\
00337 castcase(_ushort,unsigned short,tpe);\
00338 castcase(_int,int,tpe);\
00339 castcase(_uint,unsigned int,tpe);\
00340 castcase(_long,long,tpe);\
00341 castcase(_ulong,unsigned long,tpe);\
00342 castcase(_longlong,long long,tpe);\
00343 castcase(_float,float,tpe);\
00344 castcase(_double,double,tpe);\
00345 case _string: {char** tVal=(char**)aVal;StrConv(*tVal,*s);};break;\
00346 default: cout <<"wrong type" << endl;tRetVal=false;\
00347 };\
00348 return tRetVal;\
00349 }
00350 genwritemem(char);
00351 genwritemem(unsigned char);
00352 genwritemem(short);
00353 genwritemem(unsigned short);
00354 genwritemem(int);
00355 genwritemem(unsigned int);
00356 genwritemem(long);
00357 genwritemem(unsigned long);
00358 genwritemem(long long);
00359 genwritemem(float);
00360 genwritemem(double);
00361
00362 #define castcasest(typelist,casttype) case typelist: {casttype tVal; MemSwapCpy((char*)&tVal,(char*)aVal,mycsize[typelist],mycswapl[typelist],Client);StString sStream;sStream.precision(10); sStream << tVal; string s2=sStream.str();char *tStr=new char[s2.length()+1];strcpy(tStr,s2.c_str());s[0]=tStr; };break
00363
00364 bool StDbBuffer::WriteMem( char **s,void* aVal, myctype type) {
00365 bool tRetVal=true;
00366
00367 switch (type) {
00368 castcasest(_char,char);
00369 castcasest(_uchar,unsigned char);
00370 castcasest(_short,short);
00371 castcasest(_ushort,unsigned short);
00372 castcasest(_int,int);
00373 castcasest(_uint,unsigned int);
00374 castcasest(_long,long);
00375 castcasest(_ulong,unsigned long);
00376 castcasest(_longlong,long long);
00377 castcasest(_float,float);
00378 castcasest(_double,double);
00379 case _string: {char** tVal=(char**)aVal;
00380 *s=new char[strlen(*(char**)aVal)+1];
00381 strcpy(*s,*tVal);
00382 }
00383 break;
00384 default: cout <<"wrong type" << endl;tRetVal=false;\
00385 };\
00386 return tRetVal;\
00387 }
00388
00389 #define Rscal(tpe) \
00390 bool StDbBuffer::ReadScalar(tpe s,const char *aName) \
00391 {bool tRetVal=false; \
00392 if(Find_Col(aName) && WriteMem(&s,mCol[mCur].val,mCol[mCur].type)) \
00393 tRetVal=true;\
00394 return tRetVal;}
00395
00396
00397
00398
00399
00400
00401
00402 Rscal(char&);
00403 Rscal(unsigned char&);
00404 Rscal( short& );
00405 Rscal(unsigned short& );
00406 Rscal(int&);
00407 Rscal(unsigned int&);
00408 Rscal(long&);
00409 Rscal(unsigned long& );
00410 Rscal(long long& );
00411 Rscal(float& );
00412 Rscal(double&);
00413 Rscal(char*&);
00414
00415
00416 #define Wscal(tpe,tpelist,len) \
00417 bool StDbBuffer::WriteScalar(const tpe s,const char *aName) \
00418 { if (Find_Col(aName))\
00419 {ChangeField(tpelist,(void*)&s,len);}\
00420 else\
00421 {AddField(aName,tpelist,(void*)&s,len);};\
00422 return true;\
00423 }
00424
00425 Wscal(char,_char,1);
00426 Wscal(unsigned char,_uchar,1);
00427 Wscal(short,_short ,1);
00428 Wscal(unsigned short,_ushort,1);
00429 Wscal(int ,_int ,1);
00430 Wscal(unsigned int ,_uint ,1);
00431 Wscal(long ,_long ,1);
00432 Wscal(unsigned long ,_ulong,1);
00433 Wscal(long long ,_longlong,1);
00434 Wscal(float ,_float,1);
00435 Wscal(double,_double,1);
00436
00437
00439 bool StDbBuffer::WriteScalar(const char* s,const char *aName)
00440 {
00441 if(!s) return false;
00442
00443
00444 char* aVal=new char[strlen(s)+1];
00445 strcpy(aVal,s);
00446 char** tVal=&aVal;
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457 if (Find_Col(aName))
00458 { ChangeField(_string,(void*)tVal,1); delete [] aVal;}
00459 else
00460 { AddField(aName,_string,(void*)tVal,1); delete [] aVal;};
00461 return true;
00462 }
00463
00465 #define Rarray(tpe,tpelist) \
00466 bool StDbBuffer::ReadArray(tpe* &s, int &len,const char *aName)\
00467 { bool tRetVal=false; \
00468 bool newCheck=false;\
00469 if (Find_Col(aName)) \
00470 {int i;\
00471 if (mCol[mCur].type==_char ) {\
00472 len=mCol[mCur].length/sizeof(tpe);\
00473 s=new tpe[len];\
00474 newCheck=true;\
00475 MemSwapCpy((char*)s,(char*)mCol[mCur].val,len*sizeof(tpe),mycswapl[tpelist],Auto);\
00476 tRetVal=true;\
00477 } else {\
00478 len=mCol[mCur].length;\
00479 s=new tpe[len];\
00480 newCheck=true;\
00481 for (i=0;i<len;i++)\
00482 { if (!(WriteMem(&s[i],(void*)(((char*)mCol[mCur].val)+i*mycsize[mCol[mCur].type]),mCol[mCur].type))) break;}\
00483 if (i==(int)mCol[mCur].length) tRetVal=true;}}\
00484 return tRetVal;\
00485 }
00486
00487
00488
00489 Rarray(unsigned char,_uchar);
00490 Rarray(short,_short );
00491 Rarray(unsigned short,_ushort);
00492 Rarray(int,_int );
00493 Rarray(unsigned int,_uint );
00494 Rarray(long,_long );
00495 Rarray(unsigned long,_ulong );
00496 Rarray(long long,_longlong );
00497 Rarray(float,_float );
00498 Rarray(double,_double);
00499 Rarray(char*,_string);
00500
00501
00502 bool StDbBuffer::ReadArray(char* &s, int &len,const char *aName)
00503 { bool tRetVal=false;
00504 if (Find_Col(aName)) {
00505 len=mCol[mCur].length*mycsize[mCol[mCur].type];
00506 s=new char[len];
00507 MemSwapCpy((char*)s,(char*)mCol[mCur].val,len,mycswapl[mCol[mCur].type],Auto);
00508 tRetVal=true;
00509 } else {
00510 s=0;
00511
00512 };
00513 return tRetVal;\
00514 };
00515
00516 #define Warray(tpe,tpelist) \
00517 bool StDbBuffer::WriteArray(tpe* s,int len,const char *aName) \
00518 { if (Find_Col(aName))\
00519 {ChangeField(tpelist,(void*)s,len);}\
00520 else\
00521 {AddField(aName,tpelist,(void*)s,len);};\
00522 return true;\
00523 }
00524
00525 Warray(char,_char);
00526 Warray(unsigned char,_uchar);
00527 Warray(short,_short );
00528 Warray(unsigned short,_ushort);
00529 Warray(int,_int );
00530 Warray(unsigned int,_uint );
00531 Warray(long,_long );
00532 Warray(unsigned long,_ulong );
00533 Warray(long long,_longlong );
00534 Warray(float,_float );
00535 Warray(double,_double);
00536 Warray(char*,_string);
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549