OPAL (Object Oriented Parallel Accelerator Library)  2021.1.99
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 #include <cstring>
34 
36 // Constructor: read and parse the given meta file
37 DiscMeta::DiscMeta(const char *fname) {
38 
39 
40 
41  if (fname != 0)
42  MetaFile = fname;
43 
44  // open the input file
45  FILE *f = fopen(fname, "r");
46 
47  // make sure it was OK ...
48  if (f == 0) {
49  ERRORMSG("DiscMeta: Could not open file '" << fname << "' on node ");
50  ERRORMSG(Ippl::myNode() << "." << endl);
51  Ippl::abort("Exiting due to DiscConfig error.");
52  } else {
53  // ... it was, parse it's contents, adding new valid lines to Lines list
54  int numtokens = 0;
55  std::string *words = 0;
56  int linenum = 0;
57  while (read_meta_line(f, words, numtokens)) {
58  // indicate we've read another line
59  ++linenum;
60 
61  // if the line actually contains some keywords and values, add it
62  if (words != 0 && numtokens > 0) {
63  Lines.insert(value_type(linenum, element_t(numtokens, words)));
64  }
65  }
66 
67  // close the file
68  fclose(f);
69 
70  }
71 }
72 
73 
75 // Destructor
77  // delete all the lists of strings
78  for (iterator a = begin(); a != end(); ++a) {
79  if ((*a).second.second != 0)
80  delete [] ((*a).second.second);
81  }
82 }
83 
84 
86 // return the line number of the Nth valid line (from 1 ... M)
87 int DiscMeta::getLineNumber(unsigned int n) const {
88 
89 
90  PAssert_LT(n, size());
91 
92  unsigned int i=0;
93  const_iterator iter = begin();
94  while (i != n && iter != end()) {
95  ++iter;
96  ++i;
97  }
98 
99  PAssert(iter != end());
100  return (*iter).first;
101 }
102 
103 
105 // return the keyword of the Nth line
106 const std::string &DiscMeta::getKeyword(unsigned int n) {
107 
108 
109 
110  return getWords(n)[0];
111 }
112 
113 
115 // return the number of words in the value for the Nth line
116 int DiscMeta::getNumWords(unsigned int n) const {
117 
118 
119  PAssert_LT(n, size());
120 
121  unsigned int i=0;
122  const_iterator iter = begin();
123  while (i != n && iter != end()) {
124  ++iter;
125  ++i;
126  }
127 
128  PAssert(iter != end());
129  return (*iter).second.first;
130 }
131 
132 
134 // return the list of words in the Nth line
135 std::string *DiscMeta::getWords(unsigned int n) {
136 
137 
138 
139  unsigned int i=0;
140  iterator iter = begin();
141  while (i != n && iter != end()) {
142  ++iter;
143  ++i;
144  }
145 
146  PAssert(iter != end());
147  return (*iter).second.second;
148 }
149 
150 
152 // read in a single line from the meta data file and parse it.
153 // return success of operation.
154 bool DiscMeta::read_meta_line(FILE *f, std::string *&tokens, int &numtokens) {
155 
156 
157 
158  const int bufferSize = 1024*128;
159  char bufferstore[bufferSize];
160  char *buffer;
161 
162  // read next line
163  tokens = 0;
164  numtokens = 0;
165  if (fgets(bufferstore, bufferSize, f) == 0)
166  return false;
167  unsigned int len = strlen(bufferstore);
168  if (len > 0 && bufferstore[len-1] == '\n')
169  bufferstore[len - 1] = '\0';
170 
171  // skip whitespace, and if a comment line, all the text
172  buffer = bufferstore;
173  while (*buffer == ' ' || *buffer == '\t')
174  buffer++;
175  if (*buffer == '#' || *buffer == '\n' || *buffer == '\0')
176  return true;
177 
178  // not a comment or blank line ... tokenize it and return
179  numtokens = DiscConfig::dc_tokenize_string(buffer, " =\t", tokens);
180  return true;
181 }
182 
183 
185 // print out debugging information for this DiscMeta
187 
188 
189 
190  msg << "Meta file name = " << MetaFile << endl;
191  msg << "Lines in file = " << size() << endl;
192  for (unsigned int i=0; i < size(); ++i) {
193  msg << " Line " << i << ": '" << getKeyword(i) << "' =";
194  for (int j=1; j < getNumWords(i); ++j) {
195  msg << " '" << getWords(i)[j] << "'";
196  }
197  msg << endl;
198  }
199 }
200 
201 
202 /***************************************************************************
203  * $RCSfile: DiscMeta.cpp,v $ $Author: adelmann $
204  * $Revision: 1.1.1.1 $ $Date: 2003/01/23 07:40:33 $
205  * IPPL_VERSION_ID: $Id: DiscMeta.cpp,v 1.1.1.1 2003/01/23 07:40:33 adelmann Exp $
206  ***************************************************************************/
std::complex< double > a
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
#define ERRORMSG(msg)
Definition: IpplInfo.h:350
#define PAssert_LT(a, b)
Definition: PAssert.h:106
#define PAssert(c)
Definition: PAssert.h:102
static int dc_tokenize_string(const char *s, const char *tok, std::string *&)
Definition: DiscConfig.cpp:46
container_t::value_type value_type
Definition: DiscMeta.h:43
void printDebug(Inform &)
Definition: DiscMeta.cpp:186
unsigned int size() const
Definition: DiscMeta.h:61
iterator begin()
Definition: DiscMeta.h:76
std::string MetaFile
Definition: DiscMeta.h:86
int getNumWords(unsigned int) const
Definition: DiscMeta.cpp:116
container_t::const_iterator const_iterator
Definition: DiscMeta.h:42
~DiscMeta()
Definition: DiscMeta.cpp:76
iterator end()
Definition: DiscMeta.h:77
DiscMeta(const char *fname)
Definition: DiscMeta.cpp:37
container_t::iterator iterator
Definition: DiscMeta.h:41
int getLineNumber(unsigned int) const
Definition: DiscMeta.cpp:87
bool read_meta_line(FILE *, std::string *&, int &)
Definition: DiscMeta.cpp:154
container_t Lines
Definition: DiscMeta.h:89
const std::string & getKeyword(unsigned int)
Definition: DiscMeta.cpp:106
std::pair< int, std::string * > element_t
Definition: DiscMeta.h:39
std::string * getWords(unsigned int)
Definition: DiscMeta.cpp:135
Definition: Inform.h:42
static void abort(const char *=0)
Definition: IpplInfo.cpp:616
static int myNode()
Definition: IpplInfo.cpp:691
std::pair< iterator, bool > insert(const value_type &x)
Definition: vmap.hpp:73