Main Page | Class Hierarchy | Class List | File List | Class Members | File Members

StDbXmlReader.cc

Go to the documentation of this file.
00001 /*************************************************************************** 00002 * 00003 * $Id: StDbXmlReader.cc,v 1.12 2004/01/15 00:02:25 fisyak Exp $ 00004 * 00005 * Author: R. Jeff Porter 00006 *************************************************************************** 00007 * 00008 * Description: implement typeAcceptor for READING XML files of DB-tables 00009 * 00010 *************************************************************************** 00011 * 00012 * $Log: StDbXmlReader.cc,v $ 00013 * Revision 1.12 2004/01/15 00:02:25 fisyak 00014 * Replace ostringstream => StString, add option for alpha 00015 * 00016 * Revision 1.11 2003/09/16 22:44:18 porter 00017 * got rid of all ostrstream objects; replaced with StString+string. 00018 * modified rules.make and added file stdb_streams.h for standalone compilation 00019 * 00020 * Revision 1.10 2003/09/02 17:57:50 perev 00021 * gcc 3.2 updates + WarnOff 00022 * 00023 * Revision 1.9 2001/10/24 04:05:20 porter 00024 * added long long type to I/O and got rid of obsolete dataIndex table 00025 * 00026 * Revision 1.8 2001/01/22 18:38:02 porter 00027 * Update of code needed in next year running. This update has little 00028 * effect on the interface (only 1 method has been changed in the interface). 00029 * Code also preserves backwards compatibility so that old versions of 00030 * StDbLib can read new table structures. 00031 * -Important features: 00032 * a. more efficient low-level table structure (see StDbSql.cc) 00033 * b. more flexible indexing for new systems (see StDbElememtIndex.cc) 00034 * c. environment variable override KEYS for each database 00035 * d. StMessage support & clock-time logging diagnostics 00036 * -Cosmetic features 00037 * e. hid stl behind interfaces (see new *Impl.* files) to again allow rootcint access 00038 * f. removed codes that have been obsolete for awhile (e.g. db factories) 00039 * & renamed some classes for clarity (e.g. tableQuery became StDataBaseI 00040 * and mysqlAccessor became StDbSql) 00041 * 00042 * Revision 1.7 2000/01/10 20:37:55 porter 00043 * expanded functionality based on planned additions or feedback from Online work. 00044 * update includes: 00045 * 1. basis for real transaction model with roll-back 00046 * 2. limited SQL access via the manager for run-log & tagDb 00047 * 3. balance obtained between enumerated & string access to databases 00048 * 4. 3-levels of diagnostic output: Quiet, Normal, Verbose 00049 * 5. restructured Node model for better XML support 00050 * 00051 * Revision 1.6 1999/12/07 21:25:25 porter 00052 * some fixes for linux warnings 00053 * 00054 * Revision 1.5 1999/12/03 17:03:22 porter 00055 * added multi-row support for the Xml reader & writer 00056 * 00057 * Revision 1.4 1999/09/30 02:06:11 porter 00058 * add StDbTime to better handle timestamps, modify SQL content (mysqlAccessor) 00059 * allow multiple rows (StDbTable), & Added the comment sections at top of 00060 * each header and src file 00061 * 00062 **************************************************************************/ 00063 #include "StDbXmlReader.h" 00064 #include "stdb_streams.h" 00065 #include <string.h> 00066 00067 #include "dbStruct.hh" 00068 00069 template<class T> 00070 static void passAux(elem* e, T*& i, int& len) 00071 { 00072 len = e->size.isize; 00073 i = new T[len]; 00074 memset(i,0,sizeof(T)*len); 00075 const char* p1 = e->val.data; 00076 const char* p2=p1; 00077 int iloop = 0; 00078 for(;1;p2++) { 00079 if (p2[0] && p2[0]!=',') continue; 00080 i[iloop++] = (T)atof(p1); 00081 p1=p2+1; 00082 if (iloop==len || p2[0]==0) break; 00083 } 00084 } 00085 00086 //---------------------------------------------------- 00087 StDbXmlReader::StDbXmlReader(){ tab = new dbTable(); } 00088 00089 //---------------------------------------------------- 00090 00091 StDbXmlReader::~StDbXmlReader() { delete tab; } 00092 00093 //---------------------------------------------------- 00094 elem* 00095 StDbXmlReader::findElement(char* name){ 00096 00097 elem* e = 0; 00098 elem* retVal = 0; 00099 00100 int k; 00101 // check accessor 1st 00102 // here it's a bit scary : if accessor element has same name as data-element 00103 if(tab->curRow==-1){ 00104 for(k=0;k<tab->a.nelems;k++){ 00105 e=tab->a.e[k]; 00106 if(strcmp(name,e->name)==0){ 00107 retVal=e; 00108 break; 00109 } 00110 } 00111 } 00112 00113 if(!retVal){ // now check rows 00114 if(tab->curRow==-1)tab->curRow=0; // init row 00115 if(tab->curRow==tab->numRows)return retVal; 00116 for(k=0;k<tab->row[tab->curRow]->nelems;k++){ 00117 // cout <<k<<" elements = " << tab->nelems << endl; 00118 e=tab->row[tab->curRow]->e[k]; 00119 if(strcmp(name,e->name)==0){ 00120 retVal=e; 00121 break; 00122 } 00123 } 00124 if(k==tab->row[tab->curRow]->nelems-1)tab->curRow++; 00125 } 00126 00127 return retVal; 00128 } 00129 00130 //---------------------------------------------------- 00131 00132 //---------------------------------------------------- 00133 00134 void 00135 StDbXmlReader::readTable(ifstream &is){ 00136 00137 int done = 0; 00138 int i = 0; 00139 int j,icount,k; 00140 char tmp[2]; 00141 char line[256]; 00142 char line2[256]; 00143 00144 while(!done){ 00145 if(is.eof()){ 00146 done = 1; 00147 } else { 00148 // cout << "line . " << i << endl; 00149 is.getline(line,255); 00150 k = strlen(line); 00151 j = 0; 00152 line2[j]='\0'; 00153 char* ptr=&line2[0]; 00154 for(icount=0;icount<k;icount++){ 00155 if(!(line[icount]==' ')){ 00156 *tmp=line[icount]; 00157 tmp[1]='\0'; 00158 j++; 00159 strncpy(ptr,tmp,1); 00160 ptr++; 00161 } 00162 } 00163 line2[j]='\0'; 00164 // cout << line << endl; 00165 loca[i] = new char[strlen(line2)+1]; 00166 strcpy(loca[i],line2); 00167 // cout << "line i = "<< loca[i] << endl; 00168 i++; 00169 } 00170 } 00171 00172 maxlines = i-1; 00173 00174 buildDbTable(); 00175 00176 } 00177 00178 //----------------------------------------------------------- 00179 00180 00181 void 00182 StDbXmlReader::buildDbTable(){ 00183 00184 int done = 0; 00185 int j=0; 00186 00187 // find table start 00188 while (!done) { 00189 if(strstr(loca[j],tab->startKey)){ 00190 tab->istart = j; 00191 done = 1; 00192 } 00193 j++; 00194 } 00195 00196 // find accessor start 00197 j=tab->istart+1; 00198 done = 0; 00199 while (!done) { 00200 // cout << loca[j] << endl; 00201 if(strstr(loca[j],(tab->a).startKey)){ 00202 (tab->a).istart = j; 00203 done = 1; 00204 } 00205 j++; 00206 } 00207 00208 // if(j>=maxlines)cout << "1 j to big " << j << " max= " << maxlines << endl; 00209 00210 elem* e0; 00211 elem *e = new elem(); // key holder 00212 // (tab->a).e.push_back(e) 00213 done = 0; 00214 int nelems = 0; 00215 j=(tab->a).istart+1; 00216 // if(j>=maxlines)cout << "2 j to big " << j << " max= " << maxlines << endl; 00217 while (!done) { 00218 if(strstr(loca[j],e->startKey)){ 00219 e0 = new elem(); 00220 e0->istart = j; 00221 (tab->a).e.push_back(e0); 00222 nelems++; 00223 if(strstr(loca[j],e->endKey))e0->iend = j; 00224 } else if(strstr(loca[j],e->endKey)){ 00225 e0->iend = j; 00226 } else if(strstr(loca[j],(tab->a).endKey)){ 00227 if(strstr(loca[j],e->endKey))e0->iend = j; 00228 (tab->a).iend = j; 00229 (tab->a).nelems = nelems; 00230 done = 1; 00231 } 00232 j++; 00233 } 00234 00235 // end of accessor 00236 // find all rows 00237 j=(tab->a).iend+1; 00238 // if(j>=maxlines)cout << "3 j to big " << j << " max= " << maxlines << endl; 00239 dbRow row; 00240 dbRow* row0; 00241 00242 done = 0; 00243 int numRows = 0; 00244 while (!done) { 00245 if(strstr(loca[j],row.startKey)){ 00246 row0 = new dbRow(); 00247 row0->istart = j; 00248 if(strstr(loca[j],row.endKey))row0->iend = j; 00249 tab->row.push_back(row0); 00250 row0->rowNumber = numRows; 00251 numRows++; 00252 } else if(strstr(loca[j],tab->endKey)){ 00253 tab->iend = j; 00254 tab->numRows = numRows; 00255 tab->curRow = -1; 00256 done = 1; 00257 } 00258 j++; 00259 } 00260 00261 // cout << "Numrows = " << numRows<< endl; 00262 // cout << "Current Row = " << tab->curRow << endl; 00263 // 00264 // Loop over rows 00265 // 00266 for(int nrows=0; nrows<tab->numRows; nrows++){ 00267 done = 0; 00268 nelems = 0; 00269 j=tab->row[nrows]->istart; 00270 while (!done) { 00271 // cout << loca[j] << endl; 00272 if(strstr(loca[j],e->startKey)){ 00273 e0 = new elem(); 00274 e0->istart = j; 00275 tab->row[nrows]->e.push_back(e0); 00276 nelems++; 00277 if(strstr(loca[j],e->endKey))e0->iend = j; 00278 // cout<<"estart"<< loca[j] <<endl; 00279 } else if(strstr(loca[j],e->endKey)){ 00280 e0->iend = j; 00281 // cout<<"eend"<< loca[j] <<endl; 00282 } else if(strstr(loca[j],row.endKey)){ 00283 tab->row[nrows]->nelems = nelems; 00284 done = 1; 00285 // cout<<"rowend"<< loca[j] <<endl; 00286 } 00287 j++; 00288 } 00289 } 00290 00291 buildStruct(); 00292 00293 } 00294 00295 //----------------------------------------------------------- 00296 00297 void 00298 StDbXmlReader::buildStruct(){ 00299 00300 char * p1 = strstr(loca[tab->istart],tab->startKey); 00301 int len = strlen(tab->startKey); 00302 int k; 00303 for(k=0;k<len+1;k++)p1++; 00304 00305 char* tmpName = new char[strlen(p1)+1]; 00306 strcpy(tmpName,p1); 00307 char* id; 00308 00309 if((id=strstr(tmpName,"<")))*id='\0'; 00310 tab->name=new char[strlen(tmpName)+1]; 00311 strcpy(tab->name,tmpName); 00312 delete [] tmpName; 00313 00314 accessor * a = &(tab->a); 00315 fillElements(a); 00316 // a = (accessor*)(tab); 00317 00318 len = strlen(tab->row[0]->startKey); 00319 00320 for(int n=0; n<tab->numRows;n++){ 00321 00322 // 1st get rowID (elementID) of this row 00323 p1 = strstr(loca[tab->row[n]->istart],tab->row[n]->startKey); 00324 for(k=0;k<len+1;k++)p1++; 00325 tmpName = new char[strlen(p1)+1]; 00326 strcpy(tmpName,p1); 00327 if((id=strstr(tmpName,"<")))*id='\0'; 00328 tab->row[n]->rowID = atoi(tmpName); 00329 delete [] tmpName; 00330 00331 // now get row values 00332 00333 a = (accessor*)(tab->row[n]); 00334 fillElements(a); 00335 00336 } 00337 00338 } 00339 00340 //----------------------------------------------------------- 00341 00342 void 00343 StDbXmlReader::fillElements(accessor* a){ 00344 00345 elem* e; 00346 int len,j; 00347 char *p1,*p2,*hlen; 00348 int ilist; 00349 int iline; 00350 00351 for(int k=0;k<a->nelems;k++){ 00352 00353 e=a->e[k]; 00354 00355 // get Type 00356 00357 p1=strstr(loca[e->istart],e->startKey) + strlen(e->startKey); 00358 00359 p2=strstr(loca[e->istart],">"); 00360 len = strlen(p1) - strlen(p2); 00361 e->type = new char[len]; 00362 memcpy(e->type,p1,len); 00363 e->type[len]='\0'; 00364 00365 // get Name 00366 p2++; 00367 while(p2[0]==' ')p2++; 00368 p1 = strstr(p2,"<"); 00369 len = strlen(p2) - strlen(p1); 00370 e->name = new char[len]; 00371 memcpy(e->name,p2,len); 00372 e->name[len]='\0'; 00373 00374 // get size 00375 00376 p1=strstr(loca[e->istart],e->size.startKey); 00377 00378 if(p1){ 00379 p1+=strlen(e->size.startKey); 00380 p2=strstr(loca[e->istart],e->size.endKey); 00381 len = strlen(p1) - strlen(p2)+1; 00382 hlen = new char[len]; 00383 memcpy(hlen,p1,len); 00384 hlen[len-1]='\0'; 00385 e->size.isize = atoi(hlen); 00386 } 00387 if(e->size.isize==0)e->size.isize=1; // <length> not required for len=1 00388 00389 00390 // get values : if array it is on separate lines 00391 00392 p1=strstr(loca[e->istart],(e->val).startKey); 00393 if(p1){ 00394 // <element Type> ... <value> data </value> 00395 // </element Type> 00396 p1+=strlen((e->val).startKey); 00397 while(p1[0]==' ')p1++; 00398 p2=strstr(loca[e->istart],(e->val).endKey); 00399 len = strlen(p1) - strlen(p2); 00400 (e->val).data = new char[len]; 00401 memcpy((e->val).data,p1,len); 00402 (e->val).data[len]='\0'; 00403 } else { 00404 // <element Type> ... 00405 // <value> 00406 // data 00407 // data 00408 // </value> </element Type> 00409 ilist =e->iend - e->istart; 00410 iline =0; 00411 for(j=2;j<ilist;j++)iline += strlen(loca[e->istart+j]); 00412 StString fs; 00413 for(j=2;j<ilist;j++) fs<<loca[e->istart+j]; //<<endl; 00414 string fs2=fs.str(); 00415 e->val.data = new char[fs2.length()+1]; 00416 //cout << "TEST:: " << fullLine << " len = " << strlen(fullLine) << endl; 00417 strcpy(e->val.data,fs2.c_str()); 00418 00419 } 00420 00421 // cout << "Test:: " << endl; 00422 // cout << e->name << " "<<e->type << " " << e->val.data << endl; 00423 } 00424 00425 } 00426 00427 00428 //---------------------------------------------------- 00429 00430 void 00431 StDbXmlReader::pass(char* name, char*& i, int& len){ 00432 elem* e = findElement(name); if(!e){ cerr<<name<<" not found"<<endl; return;} 00433 if(strcmp(e->type,"String")==0){ 00434 i=new char[strlen((e->val).data)+1]; 00435 strcpy(i,(e->val).data); 00436 } 00437 } 00438 00439 //---------------------------------------------------------- 00440 00441 void 00442 StDbXmlReader::pass(char* name, unsigned char& i, int& len){ 00443 elem* e = findElement(name); if(!e){ cerr<<name<<" not found"<<endl; return;} 00444 if(strcmp(e->type,"Char")==0)i=atoi((e->val).data); 00445 } 00446 00448 00449 void 00450 StDbXmlReader::pass(char* name, unsigned char*& i, int& len) 00451 { 00452 elem* e = findElement(name); if(!e){ cerr<<name<<" not found"<<endl; return;} 00453 if(!strstr(e->type,"UChar")) return; 00454 passAux(e,i,len); 00455 } 00456 00457 00458 //---------------------------------------------------- 00459 00460 00461 //---------------------------------------------------- 00462 00463 void 00464 StDbXmlReader::pass(char* name, short& i, int& len){ 00465 elem* e = findElement(name); if(!e){ cerr<<name<<" not found"<<endl; return;} 00466 if(strcmp(e->type,"Short")==0)i=atoi((e->val).data); 00467 } 00468 00469 00470 //---------------------------------------------------- 00471 00472 void 00473 StDbXmlReader::pass(char* name, short*& i, int& len){ 00474 00475 elem* e = findElement(name); if(!e){ cerr<<name<<" not found"<<endl; return;} 00476 if(!strstr(e->type,"Short"))return; 00477 passAux(e,i,len); 00478 } 00479 00480 00481 //---------------------------------------------------- 00482 00483 void 00484 StDbXmlReader::pass(char* name, unsigned short& i, int& len){ 00485 elem* e = findElement(name); if(!e){ cerr<<name<<" not found"<<endl; return;} 00486 if(strcmp(e->type,"UShort")==0)i=atoi((e->val).data); 00487 } 00488 00489 00490 //---------------------------------------------------- 00491 00492 void 00493 StDbXmlReader::pass(char* name, unsigned short*& i, int& len){ 00494 00495 elem* e = findElement(name); if(!e){ cerr<<name<<" not found"<<endl; return;} 00496 if(!strstr(e->type,"UShort"))return; 00497 passAux(e,i,len); 00498 } 00499 00500 void 00501 StDbXmlReader::pass(char* name, int& i, int& len){ 00502 // cout << "In Int " << endl; 00503 00504 elem* e = findElement(name); if(!e){ cerr<<name<<" not found"<<endl; return;} 00505 if(strcmp(e->type,"Int")==0)i=atoi((e->val).data); 00506 } 00507 00508 //---------------------------------------------------- 00509 00510 void 00511 StDbXmlReader::pass(char* name, int*& i, int& len){ 00512 00513 // cout << "In IntArray " << endl; 00514 elem* e = findElement(name); if(!e){ cerr<<name<<" not found"<<endl; return;} 00515 if(!strstr(e->type,"Int")) return; 00516 passAux(e,i,len); 00517 } 00518 void 00519 StDbXmlReader::pass(char* name,unsigned int& i, int& len){ 00520 elem* e = findElement(name); if(!e){ cerr<<name<<" not found"<<endl; return;} 00521 if(strcmp(e->type,"UInt")==0)i=atoi((e->val).data); 00522 } 00523 00524 //---------------------------------------------------- 00525 00526 void 00527 StDbXmlReader::pass(char* name,unsigned int*& i, int& len){ 00528 00529 elem* e = findElement(name); if(!e){ cerr<<name<<" not found"<<endl; return;} 00530 if(!strstr(e->type,"UInt"))return; 00531 passAux(e,i,len); 00532 } 00533 00534 //---------------------------------------------------- 00535 00536 void 00537 StDbXmlReader::pass(char* name, long& i, int& len){ 00538 elem* e = findElement(name); if(!e){ cerr<<name<<" not found"<<endl; return;} 00539 if(strcmp(e->type,"Long")==0)i=atol((e->val).data); 00540 } 00541 00542 00543 //---------------------------------------------------- 00544 00545 void 00546 StDbXmlReader::pass(char* name, long*& i, int& len){ 00547 00548 elem* e = findElement(name); if(!e){ cerr<<name<<" not found"<<endl; return;} 00549 if(!strstr(e->type,"Long")) return; 00550 passAux(e,i,len); 00551 } 00552 00553 //---------------------------------------------------- 00554 00555 void 00556 StDbXmlReader::pass(char* name, unsigned long& i, int& len){ 00557 elem* e = findElement(name); if(!e){ cerr<<name<<" not found"<<endl; return;} 00558 if(strcmp(e->type,"ULong")==0)i=atol((e->val).data); 00559 } 00560 00561 //---------------------------------------------------- 00562 00563 void 00564 StDbXmlReader::pass(char* name, long long& i, int& len){ 00565 elem* e = findElement(name); if(!e){ cerr<<name<<" not found"<<endl; return;} 00566 #ifndef __osf__ 00567 if(strcmp(e->type,"LongLong")==0)i=atoll((e->val).data); 00568 #else 00569 if(strcmp(e->type,"LongLong")==0)i=atol((e->val).data); 00570 #endif 00571 } 00572 void 00573 StDbXmlReader::pass(char* name, long long*& i, int& len){ 00574 // 00575 cout<<"Not Yet Implemented"<<endl; 00576 } 00577 00578 00579 //---------------------------------------------------- 00580 00581 void 00582 StDbXmlReader::pass(char* name, unsigned long*& i, int& len){ 00583 00584 elem* e = findElement(name); if(!e){ cerr<<name<<" not found"<<endl; return;} 00585 if(!strstr(e->type,"ULong"))return; 00586 passAux(e,i,len); 00587 } 00588 00589 00590 //---------------------------------------------------- 00591 00592 void 00593 StDbXmlReader::pass(char* name, float*& i, int& len){ 00594 00595 elem* e = findElement(name); if(!e){ cerr<<name<<" not found"<<endl; return;} 00596 if(!strstr(e->type,"Float")) return; 00597 passAux(e,i,len); 00598 } 00599 //---------------------------------------------------- 00600 00601 void 00602 StDbXmlReader::pass(char* name, double*& i, int& len){ 00603 00604 elem* e = findElement(name); if(!e){ cerr<<name<<" not found"<<endl; return;} 00605 if(!strstr(e->type,"Double"))return; 00606 passAux(e,i,len); 00607 } 00608 //---------------------------------------------------- 00609 00610 void 00611 StDbXmlReader::pass(char* name, float& i, int& len){ 00612 elem* e = findElement(name); if(!e){ cerr<<name<<" not found"<<endl; return;} 00613 if(strcmp(e->type,"Float")==0)i=(float)atof((e->val).data); 00614 00615 } 00616 00617 00618 void 00619 StDbXmlReader::pass(char* name, double& i, int& len){ 00620 elem* e = findElement(name); if(!e){ cerr<<name<<" not found"<<endl; return;} 00621 if(strcmp(e->type,"Double")==0)i=atof((e->val).data); 00622 00623 } 00624 00625 00626 00627 00628 00629 00630 00631

Generated on Thu Aug 24 14:45:28 2006 for Doxygen by doxygen 1.3.7