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 #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
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
00044 FILE *f = fopen(fname, "r");
00045
00046
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
00053 int numtokens = 0;
00054 string *words = 0;
00055 int linenum = 0;
00056 while (read_meta_line(f, words, numtokens)) {
00057
00058 ++linenum;
00059
00060
00061 if (words != 0 && numtokens > 0) {
00062 Lines.insert(value_type(linenum, element_t(numtokens, words)));
00063 }
00064 }
00065
00066
00067 fclose(f);
00068
00069 }
00070 }
00071
00072
00074
00075 DiscMeta::~DiscMeta() {
00076
00077 for (iterator a = begin(); a != end(); ++a) {
00078 if ((*a).second.second != 0)
00079 delete [] ((*a).second.second);
00080 }
00081 }
00082
00083
00085
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
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
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
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
00152
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
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
00171 buffer = bufferstore;
00172 while (*buffer == ' ' || *buffer == '\t')
00173 buffer++;
00174 if (*buffer == '#' || *buffer == '\n' || *buffer == '\0')
00175 return true;
00176
00177
00178 numtokens = DiscConfig::dc_tokenize_string(buffer, " =\t", tokens);
00179 return true;
00180 }
00181
00182
00184
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
00203
00204
00205