STEER/TFndFileManager.cxx

00001 // @(#)fROOT/STEER:$Name:  $:$Id: TFndFileManager.cxx,v 1.3 2007/09/05 11:09:42 Diego_Faso Exp $
00002 // Author: Diego Faso <mailto:faso@to.infn.it>, 2005/06/24
00003 
00005 //                TFndFileManager                      //
00006 //                                                     //
00007 // Both ROOT and ASCII files can be handled by         //
00008 // this class:                                         //
00009 //           - file existence check                    //
00010 //           - ascii characters search and replace     //
00011 //           - etc...                                  //
00012 //                                                     //
00014 
00015 #include <Riostream.h>
00016 #include "TROOT.h"
00017 #include "TSystem.h"
00018 #include "TString.h"
00019 #include "TFile.h"
00020 #include "TObjArray.h"
00021 
00022 #include "TFndFileManager.h"
00023 
00024 ClassImp(TFndFileManager) 
00025 
00026 //__________________________________________________________
00027 TFndFileManager::TFndFileManager()
00028   :TNamed("",""),fdebug(),fReady(),fAsciiLoaded(),fRootLoaded(),fOriginalPath(),fPath(),fFileName(),fAsciiFile(),fRootFile() {
00029   
00030   fFileName = new TString("");
00031 }
00032 
00033 //_________________________________________________________
00034 TFndFileManager::TFndFileManager(const char *path, Bool_t debug)
00035   :TNamed("",""),fdebug(debug),fReady(),fAsciiLoaded(),fRootLoaded(),fOriginalPath(),fPath(),fFileName(),fAsciiFile(),fRootFile(){ 
00036   
00037   fFileName = new TString("");
00038   // remove $ from enviroment variable
00039   if (path && *path == '$')
00040     path = gSystem->Getenv(path + 1);
00041   fPath = new TString(path); 
00042   fOriginalPath = fPath->Data();
00043   if(fdebug) cout << "TFndFileManager debug (constr): path is: \"" << fPath->Data() << "\""<< endl;
00044 }
00045 
00046 //___________________________________
00047 TFndFileManager::~TFndFileManager()
00048 {
00049   //
00050 }
00051 
00052 //___________________________________
00053 //_________________________________________________________________________
00054 
00055 Int_t TFndFileManager::Ascii(Char_t *filename, Char_t *option){
00056   BuildFileName(filename);
00057   if(!CheckExistence())return 0;
00058   if(fdebug) cout << "TFndFileManager debug: accessing to the ascii file in \"" << option << "\" mode." << endl;
00059   fAsciiFile = fopen(fFileName->Data(),option);
00060   fAsciiLoaded = kTRUE;
00061   return 1;
00062 }
00063 //___________________________________
00064 Int_t TFndFileManager::Root(Char_t *filename, Char_t *option){
00065   BuildFileName(filename);
00066   if(!CheckExistence())return 0;
00067   if(fdebug) cout << "TFndFileManager debug: accessing to the root file in \"" << option << "\" mode." << endl;
00068   fRootFile = new TFile(fFileName->Data(),option);
00069   if(fdebug) gROOT->ProcessLine(".ls");
00070   fRootLoaded = kTRUE;
00071   return 1;
00072 }
00073 
00074 //___________________________________
00075 void TFndFileManager::AppendToPath(Char_t *path){
00076   if(fdebug) cout << "fPath 1: " << fPath->Data() << endl;
00077   *fPath+="/";
00078   if(fdebug) cout << "fPath 2: " << fPath->Data() << endl;
00079   *fPath+=path;
00080   if(fdebug) cout << "fPath 3: " << fPath->Data() << endl;
00081 }
00082 //___________________________________
00083 Char_t *TFndFileManager::GetFileName() {
00084   if(!fReady) cout << "filename not complete!" << endl;
00085   return (Char_t *)fFileName->Data();
00086 }
00087 
00088 //___________________________________
00089 void TFndFileManager::ReadAscii(){
00090   rewind(fAsciiFile);
00091   char fgets_out[200][200];
00092   cout << "fgets output: " << fgets(fgets_out[0],200,fAsciiFile) << endl;
00093   cout << "fgets output: " << fgets(fgets_out[1],200,fAsciiFile) << endl;
00094   cout << "fgets output: " << fgets(fgets_out[2],200,fAsciiFile) << endl;
00095   rewind(fAsciiFile);
00096 }
00097 
00098 //___________________________________
00099 void TFndFileManager::ReplaceChar(Char_t *ori_char,Char_t *new_char){
00100   if(!fAsciiLoaded){
00101     cout << "Ascii file not loaded!" << endl;
00102     return;  
00103   }
00104   if(strlen(ori_char)>1 || strlen(new_char)>1){
00105     cout << "One character only!!!" << endl;
00106     return;
00107   }
00108   Int_t count = 0;
00109   Int_t *ori_num = (Int_t *) ori_char;
00110   Int_t *new_num = (Int_t *) new_char;
00111   freopen(fFileName->Data(),"r+",fAsciiFile); 
00112 
00113   if(fdebug) cout << "changing \"" << ori_char << "\"( " << *ori_num << " ) to \"" << new_char << "\"( " << *new_num << ")." << endl;
00114   
00115   if(fdebug) cout << "resetting position." << endl;
00116   rewind(fAsciiFile);
00117   if(fdebug) cout << "current stream pos.: " << ftell(fAsciiFile) << endl;
00118   Int_t current = 0;
00119   while(1){
00120     current = fgetc(fAsciiFile);
00121     if(current==-1) break; // stop at end of file
00122     
00123     if(fdebug) cout << "fgetc output: " << (Char_t) current << " (" << current << ")" << endl; 
00124     if(current==*ori_num)
00125       {
00126         count++;
00127         if(fdebug) cout << "replacing " << ori_char << " at position: " << ftell(fAsciiFile) << endl;
00128         fseek(fAsciiFile,-1,SEEK_CUR); // move back the streaming
00129         fputs(new_char,fAsciiFile);     
00130       }
00131   }
00132   cout << "Replaced " << count << " occurences." << endl;
00133   freopen(fFileName->Data(),"r",fAsciiFile); 
00134   rewind(fAsciiFile);
00135 }
00136 
00137 //___________________________________
00138 const Int_t TFndFileManager::GetNumOfColumns(Char_t *delimiter){
00139   rewind(fAsciiFile);
00140 
00141   Int_t check = 0;
00142   Int_t count_col = 0;
00143   for(Int_t line=0;line<10;line++){ // check the first ten lines
00144     Char_t *cur_str = "";     // get the line from the file 
00145     cur_str = GetNextLine(); // get the line from the file 
00146     if(!cur_str) break;
00147     
00148     TString cur_char;
00149     Int_t i = 0;
00150     if(strlen(cur_str)==0) count_col = 0;
00151     else count_col = 1;
00152     while(i<(Int_t)strlen(cur_str)){
00153       cur_char = TString(*(cur_str+i));
00154       if(fdebug) cout << "cur_char: " << cur_char.Data() << endl;
00155       if(!strcmp(cur_char.Data(),delimiter)){
00156         if(fdebug) cout << "delimiter found at position " << i << endl;
00157         count_col++;
00158       }
00159       i++;
00160     }
00161     if(line==0) check = count_col;
00162     else{
00163       if(check!=count_col){
00164         cout << "Number of columns not constant! (first ten lines)" << endl;
00165         check = count_col;}
00166       else if(fdebug) cout << "Number of columns constant." << endl;
00167     }
00168   }
00169   return count_col;
00170 
00171 }
00172 
00173 //___________________________________
00174 Char_t *TFndFileManager::GetNextLine(){
00175   Char_t line_content[1][7000];
00176   Char_t *out = ""; 
00177   out = fgets(line_content[0],7000,fAsciiFile);
00178   if(!out){
00179     cout << "End of file reached." << endl;
00180     return 0;
00181   }
00182   out[strlen(out)-1]='\0';
00183   if(fdebug) cout << "Line content: " << out << endl; 
00184   return out; 
00185 }
00186 
00187 //___________________________________
00188 Char_t *TFndFileManager::GetLine(Int_t n){
00189   Char_t line_content[1][7000];
00190   Char_t *out = ""; 
00191   rewind(fAsciiFile);
00192 
00193   for(Int_t l=0;l<n;l++){
00194     out = fgets(line_content[0],7000,fAsciiFile);
00195     if(!out){
00196       cout << "Line out of file range!" << endl;
00197       return 0;
00198     }
00199   }
00200   return out; 
00201 }
00202 
00203 //___________________________________
00204 Int_t TFndFileManager::GetNextLine(Float_t *array,Int_t nvar,Char_t *delimiter){
00205   // retuns -1 if end of file
00206   Char_t *cur_str = ""; // get the line from the file 
00207   cur_str = GetNextLine(); // get the line from the file 
00208   if(!cur_str) return -1;
00209   //  Char_t cur_char[2];
00210   //  Char_t *cur_char = "";
00211   Int_t i = 0;
00212   Int_t var_ID = 0;
00213   TString cur_char;
00214   TString cur_word = TString("");
00215   while(i<(Int_t)strlen(cur_str)){
00216     cur_char = TString(*(cur_str+i));
00217     if(fdebug) cout << "cur_char: " << cur_char.Data() << endl;
00218     if(!strcmp(cur_char.Data(),delimiter)){
00219       if(fdebug) cout << "delimiter found at position " << i << endl;
00220       Float_t var = ::atof(cur_word.Data());
00221       if(fdebug) cout << "atof: " << var << endl;
00222       if(var_ID<nvar){
00223         array[var_ID] = var;
00224         var_ID++;
00225       }
00226       cur_word.Resize(0);
00227     }
00228     else{
00229       cur_word+=cur_char;
00230       if(fdebug) cout << "cur_word: " << cur_word.Data() << endl;
00231     }
00232     i++;
00233   }
00234   if(fdebug) cout << "end of line found." << endl;
00235   Float_t var = ::atof(cur_word.Data());
00236   if(fdebug) cout << "atof: " << var << endl;
00237   if(var_ID<nvar){
00238     array[var_ID] = var;
00239     var_ID++;
00240   }
00241   return 0;
00242 }
00243 
00244 //___________________________________
00245 void TFndFileManager::Close(){
00246   if(!fAsciiFile && !fRootFile){
00247     cout << "TFndFileManager message: No file to be closed." << endl;
00248     return;
00249   }
00250   if(fAsciiFile){
00251     cout << "Closing ASCII file" << endl;
00252     fclose(fAsciiFile);
00253   }
00254   if(fRootFile){
00255     cout << "Closing ROOT file" << endl;
00256     fRootFile->Close();
00257   }
00258 }
00259 
00260 
00261 //___________________________________
00262 //___________________________________
00263 void TFndFileManager::Print(Option_t *)const{
00264 
00265   cout << "   __________________________________________________" << endl;
00266   cout << "  |               TFndFileManager::Print()            |" << endl;
00267   cout << "  | debug is:=======> " << fdebug << endl;
00268   cout << "  | path is:========> " << fPath->Data() << endl;
00269   if(fFileName) cout << "  | filename is:====> " << fFileName->Data() << endl;
00270 }
00271 //___________________________________
00272 void TFndFileManager::HowTo(){
00273   cout << "   ________________________________________________________________" << endl;
00274   cout << "  |               TFndFileManager::HowTo()                          |" << endl;
00275   cout << "  |                                                                |" << endl;
00276   cout << "  |  This class is designed for the ACII and the ROOT files        |" << endl;
00277   cout << "  |  management.                                                   |" << endl;
00278   cout << "  |                                                                |" << endl;
00279   cout << "  |  Usage:                                                        |" << endl;
00280   cout << "  |   - Constructor:                                               |" << endl;
00281   cout << "  |     TFndFileManager(Char_t *path,Bool_t debug=kFALSE)           |" << endl;
00282   cout << "  |       It requires a \'path\'                                     |" << endl;
00283   cout << "  |       The path can be passed as:                               |" << endl;
00284   cout << "  |             a) enviroment variable (with \"$\")                  |" << endl;
00285   cout << "  |             a) standard path (for instance \"/data/test\")       |" << endl;
00286   cout << "  |       NOTE: don't use a \"mixed\" path ($DATA/test)!             |" << endl;
00287   cout << "  |       You can specify the debug flag value (default:false).    |" << endl;
00288   cout << "  |                                                                |" << endl;
00289   cout << "  |    - AppendToPath(Char_t *path)                                |" << endl;
00290   cout << "  |       Use this method if you want to add a sub-path to the     |" << endl;
00291   cout << "  |       path that you have set in the constructor.               |" << endl;
00292   cout << "  |                                                                |" << endl;
00293   cout << "  |       IMPORTANT NOTE:                                          |" << endl;
00294   cout << "  |          Remember not to use the \"/\" in the end of the path    |" << endl;
00295   cout << "  |                                                                |" << endl;  
00296   cout << "  |    - Ascii(Char_t *filename, Char_t *option=\"r\")               |" << endl;
00297   cout << "  |        to access to an ACII file;                              |" << endl;
00298   cout << "  |        available options are:                                  |" << endl;
00299   cout << "  |        r  => Open text file for reading.  The stream is        |" << endl;
00300   cout << "  |              positioned at the beginning of the file.          |" << endl;
00301   cout << "  |        r+ => Open for reading and writing.  The stream is      |" << endl;
00302   cout << "  |              positioned at the beginning of the file.          |" << endl;
00303   cout << "  |        w  => Truncate  file  to  zero length or create         |" << endl;
00304   cout << "  |              text file for writing.  The stream is             |" << endl;
00305   cout << "  |              positioned at the beginning of the file.          |" << endl;
00306   cout << "  |        w+ => Open for reading and writing. The file is created |" << endl;
00307   cout << "  |              if it does not exist, otherwise it is truncated.  |" << endl;
00308   cout << "  |              Stream  at the beginning of the file.             |" << endl;
00309   cout << "  |        a  => Open for appending (writing at end of file).      |" << endl;  
00310   cout << "  |              The file is created if it does not exist.         |" << endl;  
00311   cout << "  |              Stream  at the end of the file.                   |" << endl;  
00312   cout << "  |        a+ => Open for reading and appending                    |" << endl;  
00313   cout << "  |              (writing at end of file)                          |" << endl;  
00314   cout << "  |              The file is created if it does not exist.         |" << endl;  
00315   cout << "  |              Stream  at the end of the file.                   |" << endl;  
00316   cout << "  |                                                                |" << endl;
00317   cout << "  |    - Root(Char_t *filename, Char_t *option=\"OPEN\")             |" << endl;
00318   cout << "  |       to access to a root file in the standard root way        |" << endl;
00319   cout << "  |                                                                |" << endl;
00320   cout << "  |    - GetRootFile()                                             |" << endl;
00321   cout << "  |       returns a pointer to TFile                               |" << endl;
00322   cout << "  |       (if already opened with Root(...) method                 |" << endl;
00323   cout << "  |                                                                |" << endl;
00324   cout << "  |    - GetAsciiFile()                                            |" << endl;
00325   cout << "  |       returns a pointer to FILE                                |" << endl;
00326   cout << "  |       (if already opened with Ascii(...) method                 |" << endl;
00327   cout << "  |                                                                |" << endl;
00328 
00329   cout << "  |                                                                |" << endl;
00330   cout << "  |________________________________________________________________|" << endl;
00331 }
00332 //____________________
00333 //_____ private ______
00334 //____________________
00335 void TFndFileManager::BuildFileName(Char_t *filename){
00336   fFileName = new TString(fPath->Data());
00337   *fFileName+="/";
00338   *fFileName+=filename;
00339   SetName(filename);
00340   SetTitle(filename);
00341   fReady = kTRUE;
00342 }
00343 //___________________________________
00344 Bool_t TFndFileManager::CheckExistence() 
00345 { // kTRUE if the file exists
00346   if(fdebug) cout << "TFndFileManager debug: Checking existence." << endl;
00347   if(gSystem->AccessPathName(fFileName->Data())){
00348     cout << "file \"" << fFileName->Data() << "\" not found!" << endl; 
00349     return kFALSE;
00350   }
00351   else 
00352     {
00353       if(fdebug) cout << "TFndFileManager debug: file \"" << fFileName->Data() << "\" found." << endl;
00354       return kTRUE;
00355     }
00356 }

Generated on Tue Oct 16 15:40:48 2007 by  doxygen 1.5.2