122 #include "StDbBuffer.h"
125 #include "stdb_streams.h"
127 #ifndef __STDB_STANDALONE__
128 #include "StMessMgr.h"
130 #define LOG_DEBUG cout
131 #define LOG_INFO cout
132 #define LOG_WARN cout
133 #define LOG_ERROR cerr
134 #define LOG_FATAL cerr
140 #define freeze(i) str()
145 void StDbBuffer::Print(){
148 for (i=0; i<=mLast;i++) {
149 if (mCol[i].type==_char) {
151 ReadArray(tVal,len,mCol[i].name);
152 cout << mCol[i].name <<
"=" <<
"#BIN (" << len <<
") bytes" <<endl;
154 char** tVal;
int len;
155 ReadArray(tVal,len,mCol[i].name);
158 {cout << mCol[i].name <<
"["<< j <<
"]=" << tVal[j] <<endl;}
165 void StDbBuffer::zeroColumn(
int istart,
int iend) {
166 for(
int i=istart;i<iend+1;i++){
174 void StDbBuffer::Raz(){
177 for (i=0;i<=mLast;i++) {
179 if (mCol[i].type>=_ascii){
180 char ** tTextVal= (
char**)mCol[i].val;
182 for(j=0;j<(int)mCol[i].length;j++) {
184 if (tTextVal[j])
delete [] tTextVal[j];
188 delete [] mCol[i].val ;
191 if (mCol[i].name)
delete [] mCol[i].name;
202 char **StDbBuffer::WhatsIn(){
204 tIn=
new char*[mLast+2];
206 for (i=0;i<=mLast;i++){
214 bool StDbBuffer::Find_Col (
const char *aName){
217 if (mLast==-1)
return false;
219 for (
int tCount=0;tCount<mLast+1;tCount++){
221 if (mCur>mLast) mCur=0;
223 if (!strcmp(mCol[mCur].name,aName))
return true;
231 void StDbBuffer::AddField(
const char *aName,
const myctype aTpe,
const void* aVal,
const int aLen) {
238 memcpy(tCol,mCol,(mMax+1)*
sizeof(
column));
239 if (mCol)
delete [] mCol;
241 zeroColumn(mMax+1,2*mMax);
246 if(mCol[mCur].name)
delete [] mCol[mCur].name;
247 mCol[mCur].name=
new char[strlen(aName)+1];
248 strcpy(mCol[mCur].name,aName);
250 ChangeField(aTpe,aVal,aLen);
254 void StDbBuffer::ChangeField(
const myctype aTpe,
const void* aVal,
const int aLen) {
256 if(mCol[mCur].type>=_ascii){
257 char ** tTextVal= (
char**)mCol[mCur].val;
259 for(j=0;j<(int)mCol[mCur].length;j++) {
260 if (tTextVal[j])
delete [] tTextVal[j];
262 delete [] tTextVal ; mCol[mCur].val=0;
265 mCol[mCur].type=aTpe;
266 mCol[mCur].length=aLen;
268 mCol[mCur].val=
new char[aLen*mycsize[aTpe]];
269 MemSwapCpy(mCol[mCur].val,(
char*)aVal,aLen*mycsize[aTpe],mycswapl[aTpe],Auto);
271 mCol[mCur].val=(
char*)
new char*[aLen];
272 char** tVal=(
char**)aVal;
273 char** tStoreVal=(
char**)mCol[mCur].val;
275 for (i=0;i<aLen;i++) {
279 tStoreVal[i]=
new char[strlen(tVal[i])+1];
280 strcpy(tStoreVal[i],tVal[i]);
289 void StDbBuffer::MemSwapCpy(
char* where,
char* from,
int len,
int swaplen,BuffMode mode) {
290 if (mode==Auto) mode=mMode;
291 if (swaplen<=1||mode==Storage) {
292 memcpy(where,from,len);
294 if (len%swaplen!=0) {
295 LOG_ERROR <<
"memswapcy: len not in agreement with swapping - binary truncate " << endm;
297 int tNbCpy=len/swaplen;
299 for (i=0;i<tNbCpy;i++){
302 for (j=0;j<swaplen;j++){
314 void StDbBuffer::StrConv(
char* aVal,
char &s){s=aVal[0];};
315 void StDbBuffer::StrConv(
char* aVal,
unsigned char &s){s=(
unsigned char)atoi(aVal);};
316 void StDbBuffer::StrConv(
char* aVal,
short &s){s=(short)atoi(aVal);};
317 void StDbBuffer::StrConv(
char* aVal,
unsigned short &s){s=(
unsigned short) atoi(aVal);};
318 void StDbBuffer::StrConv(
char* aVal,
int &s){s=atoi(aVal);};
319 void StDbBuffer::StrConv(
char* aVal,
unsigned int &s){s=strtoul(aVal,0,10);};
320 void StDbBuffer::StrConv(
char* aVal,
long &s){s=atoi(aVal);};
321 void StDbBuffer::StrConv(
char* aVal,
unsigned long &s){s=strtoul(aVal,0,10);};
323 void StDbBuffer::StrConv(
char* aVal,
long long &s){s=atoll(aVal);};
325 void StDbBuffer::StrConv(
char* aVal,
long long &s){s=atol(aVal);};
327 void StDbBuffer::StrConv(
char* aVal,
float &s){s=(float) atof(aVal);};
328 void StDbBuffer::StrConv(
char* aVal,
double &s){s=atof(aVal);};
329 void StDbBuffer::StrConv(
char* aVal,
char* &s){s=
new char[strlen(aVal)+1];strcpy(s,aVal);};
331 #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;
333 #define genwritemem(tpe) \
334 bool StDbBuffer::WriteMem( tpe *s,void* aVal, myctype type)\
337 castcase(_char,char,tpe);\
338 castcase(_uchar,unsigned char,tpe);\
339 castcase(_short,short,tpe);\
340 castcase(_ushort,unsigned short,tpe);\
341 castcase(_int,int,tpe);\
342 castcase(_uint,unsigned int,tpe);\
343 castcase(_long,long,tpe);\
344 castcase(_ulong,unsigned long,tpe);\
345 castcase(_longlong,long long,tpe);\
346 castcase(_float,float,tpe);\
347 castcase(_double,double,tpe);\
348 case _string: {char** tVal=(char**)aVal;StrConv(*tVal,*s);};break;\
349 default: cout <<"wrong type" << endl;tRetVal=false;\
354 genwritemem(
unsigned char);
356 genwritemem(
unsigned short);
358 genwritemem(
unsigned int);
360 genwritemem(
unsigned long);
361 genwritemem(
long long);
365 #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
367 bool StDbBuffer::WriteMem(
char **s,
void* aVal, myctype type) {
371 castcasest(_char,
char);
372 castcasest(_uchar,
unsigned char);
373 castcasest(_short,
short);
374 castcasest(_ushort,
unsigned short);
375 castcasest(_int,
int);
376 castcasest(_uint,
unsigned int);
377 castcasest(_long,
long);
378 castcasest(_ulong,
unsigned long);
379 castcasest(_longlong,
long long);
380 castcasest(_float,
float);
381 castcasest(_double,
double);
382 case _string: {
char** tVal=(
char**)aVal;
383 *s=
new char[strlen(*(
char**)aVal)+1];
387 default: cout <<
"wrong type" << endl;tRetVal=
false;\
393 bool StDbBuffer::ReadScalar(tpe s,const char *aName) \
394 {bool tRetVal=false; \
395 if(Find_Col(aName) && WriteMem(&s,mCol[mCur].val,mCol[mCur].type)) \
406 Rscal(
unsigned char&);
408 Rscal(
unsigned short& );
410 Rscal(
unsigned int&);
412 Rscal(
unsigned long& );
419 #define Wscal(tpe,tpelist,len) \
420 bool StDbBuffer::WriteScalar(const tpe s,const char *aName) \
421 { if (Find_Col(aName))\
422 {ChangeField(tpelist,(void*)&s,len);}\
424 {AddField(aName,tpelist,(void*)&s,len);};\
429 Wscal(
unsigned char,_uchar,1);
430 Wscal(
short,_short ,1);
431 Wscal(
unsigned short,_ushort,1);
433 Wscal(
unsigned int ,_uint ,1);
434 Wscal(
long ,_long ,1);
435 Wscal(
unsigned long ,_ulong,1);
436 Wscal(
long long ,_longlong,1);
437 Wscal(
float ,_float,1);
438 Wscal(
double,_double,1);
442 bool StDbBuffer::WriteScalar(
const char* s,
const char *aName)
447 char* aVal=
new char[strlen(s)+1];
461 { ChangeField(_string,(
void*)tVal,1);
delete [] aVal;}
463 { AddField(aName,_string,(
void*)tVal,1);
delete [] aVal;};
468 #define Rarray(tpe,tpelist) \
469 bool StDbBuffer::ReadArray(tpe* &s, int &len,const char *aName)\
470 { bool tRetVal=false; \
471 bool newCheck=false;\
472 if (Find_Col(aName)) \
474 if (mCol[mCur].type==_char ) {\
475 len=mCol[mCur].length/sizeof(tpe);\
478 MemSwapCpy((char*)s,(char*)mCol[mCur].val,len*sizeof(tpe),mycswapl[tpelist],Auto);\
481 len=mCol[mCur].length;\
485 { if (!(WriteMem(&s[i],(void*)(((char*)mCol[mCur].val)+i*mycsize[mCol[mCur].type]),mCol[mCur].type))) break;}\
486 if (i==(int)mCol[mCur].length) tRetVal=true;}}\
492 Rarray(
unsigned char,_uchar);
493 Rarray(
short,_short );
494 Rarray(
unsigned short,_ushort);
496 Rarray(
unsigned int,_uint );
498 Rarray(
unsigned long,_ulong );
499 Rarray(
long long,_longlong );
500 Rarray(
float,_float );
501 Rarray(
double,_double);
502 Rarray(
char*,_string);
505 bool StDbBuffer::ReadArray(
char* &s,
int &len,
const char *aName)
506 {
bool tRetVal=
false;
507 if (Find_Col(aName)) {
508 len=mCol[mCur].length*mycsize[mCol[mCur].type];
510 MemSwapCpy((
char*)s,(
char*)mCol[mCur].val,len,mycswapl[mCol[mCur].type],Auto);
519 #define Warray(tpe,tpelist) \
520 bool StDbBuffer::WriteArray(tpe* s,int len,const char *aName) \
521 { if (Find_Col(aName))\
522 {ChangeField(tpelist,(void*)s,len);}\
524 {AddField(aName,tpelist,(void*)s,len);};\
529 Warray(
unsigned char,_uchar);
530 Warray(
short,_short );
531 Warray(
unsigned short,_ushort);
533 Warray(
unsigned int,_uint );
535 Warray(
unsigned long,_ulong );
536 Warray(
long long,_longlong );
537 Warray(
float,_float );
538 Warray(
double,_double);
539 Warray(
char*,_string);