OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
DiscMeta.cpp
Go to the documentation of this file.
1 // -*- C++ -*-
2 /***************************************************************************
3  *
4  * The IPPL Framework
5  *
6  * This program was prepared by PSI.
7  * All rights in the program are reserved by PSI.
8  * Neither PSI nor the author(s)
9  * makes any warranty, express or implied, or assumes any liability or
10  * responsibility for the use of this software
11  *
12  * Visit www.amas.web.psi for more details
13  *
14  ***************************************************************************/
15 
16 // -*- C++ -*-
17 /***************************************************************************
18  *
19  * The IPPL Framework
20  *
21  *
22  * Visit http://people.web.psi.ch/adelmann/ for more details
23  *
24  ***************************************************************************/
25 
26 // include files
27 #include "Utility/DiscMeta.h"
28 #include "Utility/DiscConfig.h"
29 #include "Utility/PAssert.h"
30 #include "Utility/IpplInfo.h"
31 #include "Utility/Inform.h"
32 
33 
35 // Constructor: read and parse the given meta file
36 DiscMeta::DiscMeta(const char *fname) {
37 
38 
39 
40  if (fname != 0)
41  MetaFile = fname;
42 
43  // open the input file
44  FILE *f = fopen(fname, "r");
45 
46  // make sure it was OK ...
47  if (f == 0) {
48  ERRORMSG("DiscMeta: Could not open file '" << fname << "' on node ");
49  ERRORMSG(Ippl::myNode() << "." << endl);
50  Ippl::abort("Exiting due to DiscConfig error.");
51  } else {
52  // ... it was, parse it's contents, adding new valid lines to Lines list
53  int numtokens = 0;
54  std::string *words = 0;
55  int linenum = 0;
56  while (read_meta_line(f, words, numtokens)) {
57  // indicate we've read another line
58  ++linenum;
59 
60  // if the line actually contains some keywords and values, add it
61  if (words != 0 && numtokens > 0) {
62  Lines.insert(value_type(linenum, element_t(numtokens, words)));
63  }
64  }
65 
66  // close the file
67  fclose(f);
68 
69  }
70 }
71 
72 
74 // Destructor
76  // delete all the lists of strings
77  for (iterator a = begin(); a != end(); ++a) {
78  if ((*a).second.second != 0)
79  delete [] ((*a).second.second);
80  }
81 }
82 
83 
85 // return the line number of the Nth valid line (from 1 ... M)
86 int DiscMeta::getLineNumber(unsigned int n) const {
87 
88 
89  PAssert_LT(n, size());
90 
91  unsigned int i=0;
92  const_iterator iter = begin();
93  while (i != n && iter != end()) {
94  ++iter;
95  ++i;
96  }
97 
98  PAssert(iter != end());
99  return (*iter).first;
100 }
101 
102 
104 // return the keyword of the Nth line
105 const std::string &DiscMeta::getKeyword(unsigned int n) {
106 
107 
108 
109  return getWords(n)[0];
110 }
111 
112 
114 // return the number of words in the value for the Nth line
115 int DiscMeta::getNumWords(unsigned int n) const {
116 
117 
118  PAssert_LT(n, size());
119 
120  unsigned int i=0;
121  const_iterator iter = begin();
122  while (i != n && iter != end()) {
123  ++iter;
124  ++i;
125  }
126 
127  PAssert(iter != end());
128  return (*iter).second.first;
129 }
130 
131 
133 // return the list of words in the Nth line
134 std::string *DiscMeta::getWords(unsigned int n) {
135 
136 
137 
138  unsigned int i=0;
139  iterator iter = begin();
140  while (i != n && iter != end()) {
141  ++iter;
142  ++i;
143  }
144 
145  PAssert(iter != end());
146  return (*iter).second.second;
147 }
148 
149 
151 // read in a single line from the meta data file and parse it.
152 // return success of operation.
153 bool DiscMeta::read_meta_line(FILE *f, std::string *&tokens, int &numtokens) {
154 
155 
156 
157  const int bufferSize = 1024*128;
158  char bufferstore[bufferSize];
159  char *buffer;
160 
161  // read next line
162  tokens = 0;
163  numtokens = 0;
164  if (fgets(bufferstore, bufferSize, f) == 0)
165  return false;
166  unsigned int len = strlen(bufferstore);
167  if (len > 0 && bufferstore[len-1] == '\n')
168  bufferstore[len - 1] = '\0';
169 
170  // skip whitespace, and if a comment line, all the text
171  buffer = bufferstore;
172  while (*buffer == ' ' || *buffer == '\t')
173  buffer++;
174  if (*buffer == '#' || *buffer == '\n' || *buffer == '\0')
175  return true;
176 
177  // not a comment or blank line ... tokenize it and return
178  numtokens = DiscConfig::dc_tokenize_string(buffer, " =\t", tokens);
179  return true;
180 }
181 
182 
184 // print out debugging information for this DiscMeta
186 
187 
188 
189  msg << "Meta file name = " << MetaFile << endl;
190  msg << "Lines in file = " << size() << endl;
191  for (unsigned int i=0; i < size(); ++i) {
192  msg << " Line " << i << ": '" << getKeyword(i) << "' =";
193  for (int j=1; j < getNumWords(i); ++j) {
194  msg << " '" << getWords(i)[j] << "'";
195  }
196  msg << endl;
197  }
198 }
199 
200 
201 /***************************************************************************
202  * $RCSfile: DiscMeta.cpp,v $ $Author: adelmann $
203  * $Revision: 1.1.1.1 $ $Date: 2003/01/23 07:40:33 $
204  * IPPL_VERSION_ID: $Id: DiscMeta.cpp,v 1.1.1.1 2003/01/23 07:40:33 adelmann Exp $
205  ***************************************************************************/
std::pair< iterator, bool > insert(const value_type &x)
Definition: vmap.hpp:73
static void abort(const char *=0, int exitcode=(-1))
Definition: IpplInfo.cpp:696
unsigned int size() const
Definition: DiscMeta.h:61
#define ERRORMSG(msg)
Definition: IpplInfo.h:399
std::string * getWords(unsigned int)
Definition: DiscMeta.cpp:134
static int dc_tokenize_string(const char *s, const char *tok, std::string *&)
Definition: DiscConfig.cpp:52
std::pair< int, std::string * > element_t
Definition: DiscMeta.h:39
void printDebug(Inform &)
Definition: DiscMeta.cpp:185
static int myNode()
Definition: IpplInfo.cpp:794
iterator end()
Definition: DiscMeta.h:77
container_t Lines
Definition: DiscMeta.h:89
#define PAssert_LT(a, b)
Definition: PAssert.h:121
~DiscMeta()
Definition: DiscMeta.cpp:75
iterator begin()
Definition: DiscMeta.h:76
container_t::value_type value_type
Definition: DiscMeta.h:43
DiscMeta(const char *fname)
Definition: DiscMeta.cpp:36
std::string MetaFile
Definition: DiscMeta.h:86
int getNumWords(unsigned int) const
Definition: DiscMeta.cpp:115
bool read_meta_line(FILE *, std::string *&, int &)
Definition: DiscMeta.cpp:153
int getLineNumber(unsigned int) const
Definition: DiscMeta.cpp:86
container_t::const_iterator const_iterator
Definition: DiscMeta.h:42
const std::string & getKeyword(unsigned int)
Definition: DiscMeta.cpp:105
container_t::iterator iterator
Definition: DiscMeta.h:41
#define PAssert(c)
Definition: PAssert.h:117
Definition: Inform.h:41
Inform & endl(Inform &inf)
Definition: Inform.cpp:42