15 static const size_t MinLine = 4;
16 static const size_t MaxLine = 256;
18 inline int min(
int a,
int b) {
return (a<b) ? a : b; }
26 resetString(
char *buf,
int nelem,
int maxlen)
28 memset(buf,0x20,nelem*maxlen);
29 for(
int i=0; i<nelem; i++ ) memset(buf+i*maxlen,EEMCDbStringDelim,0x01);
34 packString(
char *buf,
int pos,
int maxlen,
char *str)
36 int len = min(maxlen,strlen(str));
37 memcpy(buf+pos*maxlen,str,len);
38 memset(buf+pos*maxlen+len,EEMCDbStringDelim,0x01);
44 unpackString(
char *buf,
int pos,
int maxlen,
char *str) {
45 memcpy(str,buf+pos*maxlen,maxlen);
46 char *dollar = (
char *)memchr(str,EEMCDbStringDelim,maxlen);
47 if( dollar == NULL ) *(str+maxlen) = 0;
else *dollar = 0x0;
55 indexFromString(
const char *str,
const int MaxIndex)
60 short sec=-1,inp=-1,box=-1;
65 fprintf(stderr,
"indexFromString(): invalid string\n");
69 while(str[index]!=0x00 && ! isdigit(str[index]) ) index++;
71 int ns=sscanf(str+index,
"%2hd%2s%2hd",&sec,sbox,&inp);
73 if(sec<=0 || kEEmcMaxSect < sec) {
74 fprintf(stderr,
"indexFromString(): sector %d out of bounds (%s)\n",sec,str);
78 if(sbox!=NULL && strlen(sbox)>=2) {
79 for(
int i=0; kEEmcBoxInp[i]!=
"" ; i++) {
80 if(strncmp(kEEmcBoxInp[i],sbox,2)==0) { box=i;
break; }
84 if(box<0 || kEEmcMaxBox<box) {
85 fprintf(stderr,
"indexFromString(): box %.2s is invalid (%s)\n",sbox,str);
90 if(inp<=0 || kEEmcMaxInput < inp ) {
91 fprintf(stderr,
"indexFromString(): input %d out of bounds (%s)\n",inp,str);
94 index = ((sec-1)*12+box)*12+inp;
96 index = (sec-1)*12+box ;
98 if(index<=0 || MaxIndex<index) {
99 fprintf(stderr,
"indexFromString(): index %d out of bounds (%s)\n",index,str);
111 EEmcDbIOBase::EEmcDbIOBase (
int nelem,
int size)
115 bytePtr =
new char[bytes];
116 indexArr =
new int[nElem];
118 for(
int i=0;i<nElem;i++) indexArr[i]=i;
121 EEmcDbIOBase::~EEmcDbIOBase() {
122 if(bytePtr )
delete [] bytePtr ;
123 if(indexArr)
delete [] indexArr;
128 EEmcDbIOBase::checkLine(
const char *line) {
129 if(strlen(line)<=MinLine)
return false;
130 if(line[0]==
'#' )
return false;
144 char tn [EEMCDbMaxName];
147 memset(bytePtr,0x00,bytes);
148 resetString(s->name,getSize(),EEMCDbMaxName);
149 memset(s->comment,0x20,
sizeof(s->comment));
152 while ( fgets(line,MaxLine,f) != NULL && i<getSize() ) {
153 if( ! checkLine(line) )
continue;
154 memset(tn,0x00,EEMCDbMaxName);
155 if( ! scan(line,tn,i) ) *tn=0x00;
156 packString(s->name,i,EEMCDbMaxName,tn);
159 s->name [
sizeof(s->name) -1]=0x00;
160 if(comment) strncpy(s->comment,comment,EEMCDbMaxComment);
170 for(
int i=0;i<getSize();i++) {
171 char tn[EEMCDbMaxName];
172 unpackString(s->name ,i,EEMCDbMaxName ,tn);
177 setComment(s->comment);
191 memset(bytePtr,0x00,bytes);
192 while(fgets(line,MaxLine,f) != NULL && i<getSize()) {
193 if( ! checkLine(line) )
continue;
195 if(! scan(line,i) )
break;
196 if(comment) strncpy(
data(i)->comment,comment,EEMCDbMaxComment-1);
208 for(i=0;i<getSize();i++) {
209 if(! print(line,i) )
break;
211 setComment(
data(i)->comment);
225 memset(bytePtr,0x00,bytes);
226 while(fgets(line,MaxLine,f) != NULL && i<getSize()) {
227 if( ! checkLine(line) )
continue;
229 if(! scan(line,i) )
break;
230 if(comment &&
data(i)->comment[0]==0) strncpy(
data(i)->comment,comment,EEMCDbMaxComment-1);
242 for(
int i=0;i<getSize();i++) indexArr[i]=i;
243 for(i=0;i<getSize();i++) {
244 memset(line,0x00,MaxLine);
245 int r = print(line,i);
249 setComment(
data(i)->comment);
265 memset(bytePtr,0x00,bytes);
267 while(fgets(line,MaxLine,f) != NULL ) {
268 int len=strlen(line);
270 strncpy(ptr,line,len);
274 if(comment) strncpy(
data()->comment,comment,EEMCDbMaxComment-1);
286 strncpy(line,ptr,MaxLine);
289 fprintf(f,
"%s",line);
294 setComment(
data()->comment);
308 int r=sscanf(line,
"%s %d %d ",name,
data()->crate+i ,
data()->channel+i);
309 if(r>=3)dprintf(
"%s %d %d\n",name,
data()->crate[i],
data()->channel[i]);
317 return (sprintf(line,
"%s %d %d\n",name,
data()->crate[i],
data()->channel[i])>=3);
326 int r=sscanf(line,
"%s %d %d %d %d %d",name,
327 data()->barcode+i,
data()->sn+i ,
data()->baseBarcode+i,
328 data()->baseSN+i ,
data()->baseAddress+i);
330 if(r>=6)dprintf(
"%s %d %d %d %d %d\n",name,
331 data()->barcode[i],
data()->sn[i] ,
data()->baseBarcode[i],
332 data()->baseSN[i] ,
data()->baseAddress[i]);
341 return (sprintf(line,
"%s %d %d %d %d %d \n",name,
342 data()->barcode[i],
data()->sn[i] ,
data()->baseBarcode[i],
343 data()->baseSN[i] ,
data()->baseAddress[i])>=6);
352 int r=sscanf(line,
"%s %d %d",name,
data()->barcode+i,
data()->hvBranch+i);
353 if(r>=3)dprintf(
"%s %d %d\n",name,
data()->barcode[i],
data()->hvBranch[i]);
361 return (sprintf(line,
"%s %d %d\n",name,
data()->barcode[i],
data()->hvBranch[i])>=3);
369 int r=sscanf(line,
"%s %f %f %f" , name,
data()->gain+i ,
data()->egain+i ,
data()->hv+i);
370 if(r>=3)dprintf(
"%s %.4f %.4f %.3f\n",name,
data()->gain[i],
data()->egain[i],
data()->hv[i]);
379 return (sprintf(line,
"%s %.4f %.4f %.3f\n",name,
data()->gain[i],
data()->egain[i],
data()->hv[i])>=3);
388 memset(
data()->tubeName,
' ',
sizeof(
data()->tubeName));
389 data()->tubeName[
sizeof(
data()->tubeName)-1]=0;
391 char txt [EEMCDbMaxName];
392 int r=sscanf(line,
"%s %s", name,txt);
393 packString(
data()->tubeName,i,EEMCDbMaxName,txt);
394 if(r>=2)dprintf(
"%s %s\n",name,txt);
403 char txt [EEMCDbMaxName];
404 unpackString(
data()->tubeName ,i,EEMCDbMaxName ,txt);
405 return (sprintf(line,
"%s %s\n",name,txt)>=2);
413 int r=sscanf(line,
"%s %f %f" ,name,
data()->ped+i,
data()->sig+i);
414 if(r>=3)dprintf(
"%s %f %f\n",name,
data()->ped[i],
data()->sig[i]);
422 return (sprintf(line,
"%s %.4f %.4f\n",name,
data()->ped[i],
data()->sig[i])>=3);
430 int r=sscanf(line,
"%s %hx %hx" ,name,
data()->stat+i ,
data()->fail+i);
431 if(r>=3)dprintf(
"%s 0x%hx 0x%hx\n",name,
data()->stat[i],
data()->fail[i]);
439 return (sprintf(line,
"%s 0x%04hx 0x%04hx\n",name,
data()->stat[i],
data()->fail[i])>=3);
447 int r=sscanf(line,
"%s %f %f " , name,
data()->gain+i ,
data()->egain+i );
448 if(r>=3)dprintf(
"%s %.4f %.4f\n",name,
data()->gain[i],
data()->egain[i]);
457 return (sprintf(line,
"%s %.4f %.4f\n",name,
data()->gain[i],
data()->egain[i])>=3);
466 memset(
data()->tubeName,
' ',
sizeof(
data()->tubeName));
467 data()->tubeName[
sizeof(
data()->tubeName)-1]=0;
469 char txt [EEMCDbMaxName];
470 int r=sscanf(line,
"%s %s", name,txt);
471 packString(
data()->tubeName,i,EEMCDbMaxName,txt);
472 if(r>=2)dprintf(
"%s %s\n",name,txt);
481 char txt [EEMCDbMaxName];
482 unpackString(
data()->tubeName ,i,EEMCDbMaxName ,txt);
483 return (sprintf(line,
"%s %s\n",name,txt)>=2);
495 int r=sscanf(line,
"%d %f %f %f",&(s->sn),&(s->speRes),&(s->nomHV),&(s->darkC));
496 if(
data(i)->sn<= 0 )
return 0;
497 data(i)->comment[0]=0x00;
498 if(r>=4) dprintf(
"%8d %6.3f %8.3f %8.3f\n",s->sn,s->speRes,s->nomHV,s->darkC);
507 if(
data(i)->sn<= 0 )
return 0;
508 return (sprintf(line,
"%d %.4f %.3f %.3f\n",s->sn,s->speRes,s->nomHV,s->darkC));
517 int r=sscanf(line,
"%d %d %f",&(s->sn),&(s->address),&(s->maxHV));
518 if(
data(i)->sn<= 0 )
return 0;
520 data(i)->comment[0]=0x00;
521 if(r>=3) dprintf(
"%8d %8d %8.3f\n",s->sn,s->address,s->maxHV);
530 if(
data(i)->sn<= 0 )
return 0;
531 return (sprintf(line,
"%d %d %.3f\n",s->sn,s->address,s->maxHV));
545 int r=sscanf(line,
"%s %d %d %f %d %f %s",
546 s->name,&(s->hvBranch),&(s->address),&(s->maxHV),&(s->dac),&(s->hv),s->comment);
547 if( (indexArr[i]=indexFromString(s->name,getSize())) < 0 ) {
548 fprintf(stderr,
"EEmcDbHVIO<eemcDbHVsys>::scan(): name %s is invalid\n",s->name);
552 if(r>=6) dprintf(
"%8s %4d %4d %8.1f %4d %8.1f \"%s\" \n",
553 s->name,s->hvBranch,s->address,s->maxHV,s->dac,s->hv,s->comment);
562 if( s->name[0] == 0x00 )
return 0;
563 int r = sprintf(line,
"%s %d %d %7.3f %d %7.3f %s\n",
564 s->name,s->hvBranch,s->address,s->maxHV,s->dac,s->hv, s->comment);
575 int r=sscanf(line,
"%s %f %s",
576 s->name,&(s->tempC),s->comment);
577 if( (indexArr[i]=indexFromString(s->name,getSize())) < 0 ) {
578 fprintf(stderr,
"EEmcDbHVIO<eemcDbHVtemp>::scan(): name %s is invalid\n",s->name);
582 if(r>=2) dprintf(
"%8s %8.1f \"%s\" \n",
583 s->name,s->tempC,s->comment);
592 if( s->name[0] == 0x00 )
return 0;
593 int r = sprintf(line,
"%s %7.3f %s\n",
594 s->name,s->tempC, s->comment);