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
#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
00102
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){
00114
if(
tab->
curRow==-1)
tab->
curRow=0;
00115
if(
tab->
curRow==
tab->
numRows)
return retVal;
00116
for(k=0;k<
tab->
row[
tab->
curRow]->nelems;k++){
00117
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
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
00165
loca[i] =
new char[strlen(line2)+1];
00166 strcpy(
loca[i],line2);
00167
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
00188
while (!done) {
00189
if(strstr(
loca[j],
tab->
startKey)){
00190
tab->
istart = j;
00191 done = 1;
00192 }
00193 j++;
00194 }
00195
00196
00197 j=
tab->
istart+1;
00198 done = 0;
00199
while (!done) {
00200
00201
if(strstr(
loca[j],(
tab->
a).startKey)){
00202 (
tab->
a).istart = j;
00203 done = 1;
00204 }
00205 j++;
00206 }
00207
00208
00209
00210
elem* e0;
00211
elem *e =
new elem();
00212
00213 done = 0;
00214
int nelems = 0;
00215 j=(
tab->
a).istart+1;
00216
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
00236
00237 j=(
tab->
a).iend+1;
00238
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
00262
00263
00264
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
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
00279 }
else if(strstr(
loca[j],e->
endKey)){
00280 e0->
iend = j;
00281
00282 }
else if(strstr(
loca[j],row.
endKey)){
00283
tab->
row[nrows]->nelems = nelems;
00284 done = 1;
00285
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
00317
00318 len = strlen(
tab->
row[0]->startKey);
00319
00320
for(
int n=0; n<
tab->
numRows;n++){
00321
00322
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
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
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
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
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;
00388
00389
00390
00391
00392 p1=strstr(
loca[e->
istart],(e->
val).startKey);
00393
if(p1){
00394
00395
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
00405
00406
00407
00408
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];
00414 string fs2=fs.
str();
00415 e->
val.
data =
new char[fs2.length()+1];
00416
00417 strcpy(e->
val.
data,fs2.c_str());
00418
00419 }
00420
00421
00422
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
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
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