00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "organizer.h"
00016
00017 int AUXFile::close(int zip, bool bg)
00018 {
00019 LFile::Close(f);
00020
00021 if(type==SERIES) DUMP("AUXFile::close "<<extname);
00022 else DUMP("AUXFile::close "<<fname);
00023
00024 if(zip==1)
00025 {
00026 if(type==ENTRIES)
00027 if(strlen(fname)>0) LFile::GZipFile(fname,bg);
00028 }
00029 else if(zip==2)
00030 {
00031 if(type==ENTRIES)
00032 if(strlen(fname)>0) LFile::BZipFile(fname,bg);
00033 }
00034 return 0;
00035 }
00036
00037 int AUXFile::open(char *name,int format)
00038 {
00039 LFile::SubHomeDir(name,fname);
00040
00041 if(type==SERIES)
00042 {
00043 insertindex(extname,fname,count);
00044 INFO(HIM"FILEOPEN "NOR<<extname<<" "<<describe());
00045 }
00046 else INFO(HIM"FILEOPEN "NOR<<fname<<" "<<describe());
00047 if(f!=NULL)
00048 {
00049 ERROR("AUXFile open(): filehandler not NULL");
00050 return -1;
00051 }
00052 else
00053 {
00054 if(type==SERIES)
00055 {
00056 f=LFile::Open(extname,format);
00057 }
00058 else
00059 f=LFile::Open(fname,format);
00060 if(f==NULL)
00061 {
00062 if(type==BLOCK)
00063 {
00064 sprintf(zipname,"%s.bz2",name);
00065 LFile::SubHomeDir(zipname,fname);
00066
00067 f=LFile::Open(fname,format);
00068 if(f!=NULL) return 0;
00069
00070 sprintf(zipname,"%s.gz",name);
00071 LFile::SubHomeDir(zipname,fname);
00072
00073 f=LFile::Open(fname,format);
00074 if(f!=NULL) return 0;
00075 }
00076 ERROR("AUXFile open(): file open failure");
00077 return -1;
00078 }
00079 return 0;
00080 }
00081 }
00082
00083 int AUXFile::reopen()
00084 {
00085 LFile::Close(f);
00086 INFO(HIM"REOPEN "NOR<<extname<<" "<<describe());
00087 f=LFile::Open(extname,LFile::O_WriteNew);
00088 return (f==NULL)?-1:0;
00089 }
00090
00091 int AUXFile::write(void *p, int zip, bool bg)
00092 {
00093
00094
00095
00096
00097 if((type==BLOCK)||(type==SERIES))
00098 {
00099 int r;
00100 if(type==BLOCK) INFO(HIM"WRITEFILE "NOR<<fname);
00101 else INFO(HIM"WRITEFILE "NOR<<extname);
00102 if(f==NULL)
00103 {
00104 if((type==BLOCK)||((type==SERIES)&&(count==0)))
00105 {
00106 ERROR("AUXFile write(): file handler NULL");
00107 return -1;
00108 }
00109 else
00110 reopen();
00111 }
00112 r=writeblock(p); if(r!=0) return -1;
00113 LFile::Close(f);
00114
00115
00116 if(zip==1)
00117 {
00118 if(type==SERIES) LFile::GZipFile(extname,bg);
00119 else LFile::GZipFile(fname,bg);
00120 }
00121 else if(zip==2)
00122 {
00123 if(type==SERIES) LFile::BZipFile(extname,bg);
00124 else LFile::BZipFile(fname,bg);
00125 }
00126
00127
00128 if(type==SERIES)
00129 {
00130 count++;
00131 insertindex(extname,fname,count);
00132 }
00133 return r;
00134 }
00135 else
00136 {
00137 DUMP(HIM"(entry) WRITEFILE "NOR<<fname);
00138 if(f==NULL){
00139 ERROR("AUXFile write(): file handler NULL");
00140 return -1;
00141 }
00142 return writeentry(p);
00143 }
00144 }
00145
00146 int AUXFile::read(void *p)
00147 {
00148 INFO(HIM"READFILE "NOR<<fname);
00149 if(f==NULL){
00150 FATAL("AUXFile read(): file handler NULL");
00151 return -1;
00152 }
00153 if(type!=BLOCK)
00154 {
00155 ERROR("AUXFile read(): read file not BLOCK type");
00156 return -1;
00157 }
00158 else
00159 readblock(p);
00160 LFile::Close(f);
00161 return 0;
00162 }
00163
00164
00165 int AUXFile::lastoccur(char *n,char c)
00166 {
00167 int len,i; int ret;
00168 len=strlen(n);
00169 ret=-1;
00170 for(i=len-1;i>=0;i--)
00171 if(n[i]==c) {ret=i;break;}
00172 return ret;
00173 }
00174
00175
00176 void AUXFile::insertindex(char *ext,char *n,int c)
00177 {
00178 int i; char tmp[100];
00179 sprintf(tmp,"%04d",c);
00180 strcpy(ext,n);
00181 i=lastoccur(n,'.');
00182 if(i<0) strcat(ext,tmp);
00183 else
00184 {
00185 strcat(tmp,ext+i); ext[i]=0;
00186 strcat(ext,tmp);
00187 }
00188 }
00189
00190 int Organizer::exec(char *name)
00191 {
00192 INFO(eh<<name);
00193
00194 if(SCParser::exec(name)==0) return 0;
00195 bindcommand(name,"setoverwrite",setoverwrite());
00196 bindcommand(name,"setnolog",setnolog());
00197 bindcommand(name,"quit",quit());
00198 bindcommand(name,"sleep",getsleep());
00199 return -1;
00200 }
00201
00202 int Organizer::parse(FILE *file)
00203 {
00204 int ret;
00205 if (file==NULL)
00206 {
00207 ERROR("parse: file==NULL!");
00208 return -1;
00209 }
00210 SysParse(file);
00211
00212
00213 ret=SCParser::parse(file);
00214 printEndInfo();
00215 closefiles();
00216 return ret;
00217 }
00218
00219 int Organizer::parse_line(FILE *file)
00220 {
00221 int ret;
00222 if (file==NULL)
00223 {
00224 ERROR("parse: file==NULL!");
00225 return -1;
00226 }
00227 ret=SCParser::parse(file);
00228
00229
00230
00231
00232
00233
00234
00235 return ret;
00236 }
00237
00238 #if 0
00239 int Organizer::parse_(FILE *file)
00240 {
00241 LFile *f;
00242 f = LFile::Open("A.log",LFile::O_Read);
00243 LFile::Close(f);
00244 return 0;
00245 }
00246 #endif
00247
00248 int Organizer::assignvar(int offset)
00249 {
00250 int s;
00251 switch(vartype[curn])
00252 {
00253 case(INT): s=read_buffer("%d",int); break;
00254 case(LONG): s=read_buffer("%ld",long); break;
00255 case(DOUBLE): s=read_buffer("%lf",double); break;
00256
00257 case(STRING): s=1;strcpy((char *)varptr[curn],buffer);break;
00258 default: FATAL("unknown vartype ("<<vartype[curn]<<")");
00259 }
00260 if(s==0)WARNING("Expression syntax error: ("
00261 <<varname[curn]<<" = "<<buffer<<"), variable "
00262 <<varname[curn]<<" unchanged");
00263 switch(vartype[curn])
00264 {
00265 case(INT): INFO_buffer(ah,*,int); break;
00266 case(LONG): INFO_buffer(ah,*,long); break;
00267 case(DOUBLE): INFO_buffer(ah,*,double); break;
00268 case(STRING): INFO_buffer(ah,,char); break;
00269 default: WARNING("unknown vartype ("<<vartype[curn]<<")");
00270 }
00271
00272 if(vartype[curn]==STRING)
00273 if(strcmp(varname[curn],"dirname")==0)
00274 if(!diropened)
00275 {
00276 opendir();
00277 printSysInfo();
00278 diropened=true;
00279 }
00280
00281
00282
00283
00284
00285
00286
00287
00288 return (!s);
00289 }
00290
00291 void Organizer::quit()
00292 {
00293 INFO(HIC"QUIT"NOR);
00294 printEndInfo();
00295 #ifndef _USETK
00296 #ifdef _PARALLEL
00297 MPI_Finalize();
00298 #endif
00299 exit(0);
00300 #endif
00301 }
00302
00303 void Organizer::getsleep()
00304 {
00305 #ifndef _USETK
00306 if(sleepseconds>0)
00307 sleep((unsigned)sleepseconds);
00308 #endif
00309 }
00310 void Organizer::init()
00311 {
00312 SCParser::init();
00313 bindvar("dirname",dirname,STRING);
00314 bindvar("sleepseconds",&sleepseconds,INT);
00315 strcpy(logfilename,"A.log");
00316 }
00317 int Organizer::SysParse(FILE *file)
00318 {
00319 while(readnextstring(file)==0)
00320 {
00321 if(bufferis("setoverwrite")||bufferis("setnolog")
00322 ||bufferis("dirname"))
00323 {
00324 parse_buffer(file);
00325 if(strlen(dirname)>0)return 0;
00326 }
00327 else
00328 FATAL("Organizer expect \"dirname = \" entry!");
00329 }
00330 return -1;
00331 }
00332 char * Organizer::currentime()
00333 {
00334 static char s[100];
00335 time_t clock;
00336 clock=time(NULL);
00337 sprintf(s,"%s",ctime(&clock));
00338 s[strlen(s)-1]=0;
00339 return s;
00340 }
00341 void Organizer::printSysInfo()
00342 {
00343 if(!renew)INFO(HIM"RUN "NOR<<"dirname = "<<dirname);
00344 else INFO(HIM"RE"HIM"RUN "NOR<<"dirname = "<<dirname);
00345 INFO(HIM"Begin"HIG" Time = "NOR<<currentime());
00346 }
00347 void Organizer::printEndInfo()
00348 {
00349 struct rusage ru;
00350 int pid; char cmd[1000];
00351 INFO(HIM"End"HIG" Time = "NOR<<currentime());
00352 getrusage(RUSAGE_SELF,&ru);
00353
00354 INFO_Printf(HIG"CPU time spent:"NOR" %f s\n",
00355 ru.ru_utime.tv_sec+1e-6*ru.ru_utime.tv_usec);
00356
00357 #ifdef _USETCL
00358
00359 pid=getpid();
00360 sprintf(cmd,"rm -f /tmp/*.tmp%d.tcl\n",pid);
00361
00362 system(cmd);
00363 #endif
00364
00365 }
00366 void Organizer::closefiles(int zip, bool bg)
00367 {
00368 LFile::Close(olog);
00369 _IO.Restore();
00370 if(zip==1)
00371 LFile::GZipFile("A.log",bg);
00372 else
00373 LFile::BZipFile("A.log",bg);
00374 }
00375
00376 void SubHomeDir(const char *fname, char *extname)
00377 {
00378 char Home[400], *ph, *pt;
00379 ph=getenv("HOME");
00380 pt=strchr(fname, '~');
00381 if((pt==NULL)||(ph==NULL))
00382 {
00383 if(fname!=extname) strcpy(extname,fname);
00384
00385 return;
00386 }
00387 *pt=0;
00388 strcpy(Home,fname);
00389 strcpy(Home+strlen(Home),ph);
00390 strcpy(Home+strlen(Home),pt+1);
00391 strcpy(extname,Home);
00392 if(fname!=extname) *pt='~';
00393 INFO_Printf("replace %s by %s\n",fname,extname);
00394 }
00395
00396 int Organizer::opendir()
00397 {
00398 char extname[200];
00399 SubHomeDir(dirname, extname);
00400
00401
00402 DUMP("mkdir "<<dirname<<" overwrite="<<overwrite);
00403 if(mkdir(extname,S_IRWXU)!=0)
00404 {
00405 if(errno==EEXIST)
00406 {
00407 if(overwrite)
00408 {
00409 WARNING("directory "<<dirname<<" already exists");
00410 renew=true;
00411 }
00412 else
00413 FATAL("directory "<<dirname<<" already exists");
00414 }
00415 else
00416 {
00417 FATAL("directory open "<<dirname<<" failed");
00418 return -1;
00419 }
00420 }
00421 if(chdir(extname)!=0)
00422 {
00423 FATAL("cd "<<dirname<<" failed");
00424 return -1;
00425 }
00426 olog = LFile::Open(logfilename,LFile::O_WriteNew);
00427 if(!nolog)
00428 if (olog!=NULL) LSys::Redirect(olog);
00429 return 0;
00430 }
00431
00432
00433 #ifdef _TEST
00434
00435
00436 class TestOrg : public Organizer
00437 {
00438 public:
00439
00440 int intval;
00441 double x,y,z;
00442 char str[100];
00443 LFile *lfile;
00444
00445 TestOrg():intval(0),x(0),y(0),z(0){initvars();}
00446 virtual ~TestOrg(){};
00447 virtual void initparser()
00448 {
00449 bindvar("intval",&intval,INT);
00450 bindvar("x",&x,DOUBLE);
00451 bindvar("y",&y,DOUBLE);
00452 bindvar("z",&z,DOUBLE);
00453 bindvar("str",str,STRING);
00454 }
00455
00456 virtual int exec(char *name)
00457 {
00458 if(!diropened)
00459 if(strcmp(name,"setoverwrite")!=0)
00460 if(strcmp(name,"setnolog")!=0)
00461 ERROR("cannot run this command before dirname is set");
00462
00463 if(Organizer::exec(name)==0) return 0;
00464 bindcommand(name,"printall",printall());
00465 bindcommand(name,"openfile",openfile());
00466 bindcommand(name,"closefile",closefile());
00467
00468 return -1;
00469 }
00470
00471 virtual void initvars()
00472 {
00473 initparser();
00474 strcpy(str,"Hello, Org!");
00475 }
00476
00477 void printall()
00478 {
00479 INFO("intval = "<<intval<<"\nx = "<<x
00480 <<"\ny = "<<y<<"\nz = "<<z
00481 <<"\nstr = "<<str);
00482 }
00483
00484 void openfile()
00485 {
00486 lfile=LFile::Open(str,LFile::O_Read);
00487 }
00488
00489 void closefile()
00490 {
00491 LFile::Close(lfile);
00492 }
00493
00494 };
00495
00496 class TestOrg test;
00497
00498 int main(int argc, char *argv[])
00499 {
00500 test.initvars();
00501 test.parse(SCParser::getfilehandler(argc,argv));
00502 return 0;
00503 }
00504
00505 #endif