src/Utility/DiscMeta.cpp

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 /***************************************************************************
00003  *
00004  * The IPPL Framework
00005  * 
00006  * This program was prepared by PSI. 
00007  * All rights in the program are reserved by PSI.
00008  * Neither PSI nor the author(s)
00009  * makes any warranty, express or implied, or assumes any liability or
00010  * responsibility for the use of this software
00011  *
00012  * Visit http://www.acl.lanl.gov/POOMS for more details
00013  *
00014  ***************************************************************************/
00015 
00016 // -*- C++ -*-
00017 /***************************************************************************
00018  *
00019  * The IPPL Framework
00020  * 
00021  *
00022  * Visit http://people.web.psi.ch/adelmann/ for more details
00023  *
00024  ***************************************************************************/
00025 
00026 // include files
00027 #include "Utility/DiscMeta.h"
00028 #include "Utility/DiscConfig.h"
00029 #include "Utility/PAssert.h"
00030 #include "Utility/IpplInfo.h"
00031 #include "Utility/Inform.h"
00032 #include "Profile/Profiler.h"
00033 
00035 // Constructor: read and parse the given meta file
00036 DiscMeta::DiscMeta(const char *fname) {
00037   TAU_TYPE_STRING(taustr, CT(*this) + " void (char *)" );
00038   TAU_PROFILE("DiscMeta::DiscMeta()", taustr, TAU_UTILITY | TAU_IO);
00039 
00040   if (fname != 0)
00041     MetaFile = fname;
00042 
00043   // open the input file
00044   FILE *f = fopen(fname, "r");
00045 
00046   // make sure it was OK ...
00047   if (f == 0) {
00048     ERRORMSG("DiscMeta: Could not open file '" << fname << "' on node ");
00049     ERRORMSG(Ippl::myNode() << "." << endl);
00050     Ippl::abort("Exiting due to DiscConfig error.");
00051   } else {
00052     // ... it was, parse it's contents, adding new valid lines to Lines list
00053     int numtokens = 0;
00054     string *words = 0;
00055     int linenum = 0;
00056     while (read_meta_line(f, words, numtokens)) {
00057       // indicate we've read another line
00058       ++linenum;
00059 
00060       // if the line actually contains some keywords and values, add it
00061       if (words != 0 && numtokens > 0) {
00062         Lines.insert(value_type(linenum, element_t(numtokens, words)));
00063       }
00064     }
00065 
00066     // close the file
00067     fclose(f);
00068 
00069   }
00070 }
00071 
00072 
00074 // Destructor
00075 DiscMeta::~DiscMeta() {
00076   // delete all the lists of strings
00077   for (iterator a = begin(); a != end(); ++a) {
00078     if ((*a).second.second != 0)
00079       delete [] ((*a).second.second);
00080   }
00081 }
00082 
00083 
00085 // return the line number of the Nth valid line (from 1 ... M)
00086 int DiscMeta::getLineNumber(unsigned int n) const {
00087   TAU_TYPE_STRING(taustr, CT(*this) +  " int (unsigned int)" );
00088   TAU_PROFILE("DiscMeta::getLineNumber()", taustr, TAU_UTILITY | TAU_IO);
00089   PAssert(n < size());
00090 
00091   int i=0;
00092   const_iterator iter = begin();
00093   while (i != n && iter != end()) {
00094     ++iter;
00095     ++i;
00096   }
00097 
00098   PAssert(iter != end());
00099   return (*iter).first;
00100 }
00101 
00102 
00104 // return the keyword of the Nth line
00105 const string &DiscMeta::getKeyword(unsigned int n) {
00106   TAU_TYPE_STRING(taustr, CT(*this) +  " string (unsigned int)" );
00107   TAU_PROFILE("DiscMeta::getKeyword()", taustr, TAU_UTILITY | TAU_IO);
00108 
00109   return getWords(n)[0];
00110 }
00111 
00112 
00114 // return the number of words in the value for the Nth line
00115 int DiscMeta::getNumWords(unsigned int n) const {
00116   TAU_TYPE_STRING(taustr, CT(*this) +  " int (unsigned int)" );
00117   TAU_PROFILE("DiscMeta::getNumWords()", taustr, TAU_UTILITY | TAU_IO);
00118   PAssert(n < size());
00119 
00120   int i=0;
00121   const_iterator iter = begin();
00122   while (i != n && iter != end()) {
00123     ++iter;
00124     ++i;
00125   }
00126 
00127   PAssert(iter != end());
00128   return (*iter).second.first;
00129 }
00130 
00131 
00133 // return the list of words in the Nth line
00134 string *DiscMeta::getWords(unsigned int n) {
00135   TAU_TYPE_STRING(taustr, CT(*this) +  " string * (unsigned int)" );
00136   TAU_PROFILE("DiscMeta::getWords()", taustr, TAU_UTILITY | TAU_IO);
00137 
00138   int i=0;
00139   iterator iter = begin();
00140   while (i != n && iter != end()) {
00141     ++iter;
00142     ++i;
00143   }
00144 
00145   PAssert(iter != end());
00146   return (*iter).second.second;
00147 }
00148 
00149 
00151 // read in a single line from the meta data file and parse it.
00152 // return success of operation.
00153 bool DiscMeta::read_meta_line(FILE *f, string *&tokens, int &numtokens) {
00154   TAU_TYPE_STRING(taustr, CT(*this) + " bool (FILE *, vector<string>)" );
00155   TAU_PROFILE("DiscMeta::read_meta_line()", taustr, TAU_UTILITY | TAU_IO);
00156 
00157   const int bufferSize = 1024*128;
00158   char bufferstore[bufferSize];
00159   char *buffer;
00160 
00161   // read next line
00162   tokens = 0;
00163   numtokens = 0;
00164   if (fgets(bufferstore, bufferSize, f) == 0)
00165     return false;
00166   unsigned int len = strlen(bufferstore);
00167   if (len > 0 && bufferstore[len-1] == '\n')
00168     bufferstore[len - 1] = '\0';
00169 
00170   // skip whitespace, and if a comment line, all the text
00171   buffer = bufferstore;
00172   while (*buffer == ' ' || *buffer == '\t')
00173     buffer++;
00174   if (*buffer == '#' || *buffer == '\n' || *buffer == '\0')
00175     return true;
00176 
00177   // not a comment or blank line ... tokenize it and return
00178   numtokens = DiscConfig::dc_tokenize_string(buffer, " =\t", tokens);
00179   return true;
00180 }
00181 
00182 
00184 // print out debugging information for this DiscMeta
00185 void DiscMeta::printDebug(Inform &msg) {
00186   TAU_TYPE_STRING(taustr, CT(*this) +  " void (Inform &)" );
00187   TAU_PROFILE("DiscMeta::printDebug()", taustr, TAU_UTILITY | TAU_IO);
00188 
00189   msg << "Meta file name = " << MetaFile << endl;
00190   msg << "Lines in file = " << size() << endl;
00191   for (int i=0; i < size(); ++i) {
00192     msg << "  Line " << i << ": '" << getKeyword(i) << "' =";
00193     for (int j=1; j < getNumWords(i); ++j) {
00194       msg << "  '" << getWords(i)[j] << "'";
00195     }
00196     msg << endl;
00197   }
00198 }
00199 
00200 
00201 /***************************************************************************
00202  * $RCSfile: DiscMeta.cpp,v $   $Author: adelmann $
00203  * $Revision: 1.1.1.1 $   $Date: 2003/01/23 07:40:33 $
00204  * IPPL_VERSION_ID: $Id: DiscMeta.cpp,v 1.1.1.1 2003/01/23 07:40:33 adelmann Exp $ 
00205  ***************************************************************************/

Generated on Mon Jan 16 13:23:58 2006 for IPPL by  doxygen 1.4.6