OPAL (Object Oriented Parallel Accelerator Library)  2024.1
OPAL
SDDSParser.cpp
Go to the documentation of this file.
1 //
2 // Class SDDSParser
3 // This class writes column entries of SDDS files.
4 //
5 // Copyright (c) 2015, Christof Metzger-Kraus, Helmholtz-Zentrum Berlin
6 // All rights reserved
7 //
8 // This file is part of OPAL.
9 //
10 // OPAL is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with OPAL. If not, see <https://www.gnu.org/licenses/>.
17 //
18 #include "SDDSParser.h"
19 
20 #include <boost/algorithm/string.hpp>
21 
23  sddsFileName_m("")
24 { }
25 
26 SDDS::SDDSParser::SDDSParser(const std::string &input):
27  sddsFileName_m(input)
28 { }
29 
30 void SDDS::SDDSParser::setInput(const std::string &input) {
31  sddsFileName_m = input;
32 }
33 
35  typedef std::string::const_iterator iterator_t;
36  typedef SDDS::parser::file_parser<iterator_t> file_parser_t;
38  typedef SDDS::error_handler<iterator_t> error_handler_t;
39 
40  sddsData_m.clear();
41  paramNameToID_m.clear();
42  columnNameToID_m.clear();
43 
44  skipper_t skipper;
45  std::string contents = readFile();
46  iterator_t contentsIter = contents.begin();
47  iterator_t contentsEnd = contents.end();
48  error_handler_t error_handler(contentsIter, contentsEnd);
49  file_parser_t parser(error_handler);
50 
51  bool success = phrase_parse(contentsIter, contentsEnd, parser, skipper, sddsData_m);
52  {
53  SDDS::parameterList::iterator piter = sddsData_m.sddsParameters_m.begin();
54  SDDS::parameterList::iterator pend = sddsData_m.sddsParameters_m.end();
55  for (; piter != pend && success; ++ piter) {
56  success = piter->parse(contentsIter, contentsEnd, skipper);
57  }
58  while (success && contentsIter != contentsEnd) {
59  SDDS::columnList::iterator citer = sddsData_m.sddsColumns_m.begin();
60  SDDS::columnList::iterator cend = sddsData_m.sddsColumns_m.end();
61  for (; citer != cend && success; ++ citer) {
62  success = citer->parse(contentsIter, contentsEnd, skipper);
63  }
64  }
65  }
66 
67  if (!success || contentsIter != contentsEnd)
68  {
69  throw SDDSParserException("SDDSParser::parseSDDSFile",
70  "could not parse SDDS file");
71  }
72 
73  unsigned int param_order = 0;
74  for (const SDDS::parameter &param: sddsData_m.sddsParameters_m) {
75  std::string name = *param.name_m;
76  fixCaseSensitivity(name);
77  paramNameToID_m.insert(std::make_pair(name,
78  param_order));
79  ++ param_order;
80  }
81 
82  unsigned int col_order = 0;
83  for (const SDDS::column &col: sddsData_m.sddsColumns_m) {
84  std::string name = *col.name_m;
85  fixCaseSensitivity(name);
86  columnNameToID_m.insert(std::make_pair(name,
87  col_order));
88  ++ col_order;
89  }
90 
91  return sddsData_m;
92 }
93 
95  std::ifstream in(sddsFileName_m.c_str());
96 
97  if (in) {
98  std::string contents;
99  in.seekg(0, std::ios::end);
100  contents.resize(in.tellg());
101  in.seekg(0, std::ios::beg);
102 
103  in.read(&contents[0], contents.size());
104 
105  in.close();
106 
107  return contents;
108  }
109 
110  throw SDDSParserException("SDDSParser::readSDDSFile",
111  "could not open file '" + sddsFileName_m + "'");
112 
113  return std::string("");
114 }
115 
117  int idx = getColumnIndex(columnName);
118 
119  return sddsData_m.sddsColumns_m[idx].values_m;
120 }
121 
122 
123 int SDDS::SDDSParser::getColumnIndex(std::string col_name) const {
124  fixCaseSensitivity(col_name);
125  auto it = columnNameToID_m.find(col_name);
126  if (it != columnNameToID_m.end()) {
127  return it->second;
128  }
129 
130  throw SDDSParserException("SDDSParser::getColumnIndex",
131  "could not find column '" + col_name + "'");
132 
133 }
134 
135 //XXX use either all upper, or all lower case chars
136 void SDDS::SDDSParser::fixCaseSensitivity(std::string &for_string) {
137 
138  boost::to_lower(for_string);
139 }
static void fixCaseSensitivity(std::string &for_string)
Definition: SDDSParser.cpp:136
int getColumnIndex(std::string col_name) const
Definition: SDDSParser.cpp:123
and that you know you can do these things To protect your we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights These restrictions translate to certain responsibilities for you if you distribute copies of the or if you modify it For if you distribute copies of such a whether gratis or for a you must give the recipients all the rights that you have You must make sure that receive or can get the source code And you must show them these terms so they know their rights We protect your rights with two distribute and or modify the software for each author s protection and we want to make certain that everyone understands that there is no warranty for this free software If the software is modified by someone else and passed we want its recipients to know that what they have is not the so that any problems introduced by others will not reflect on the original authors reputations any free program is threatened constantly by software patents We wish to avoid the danger that redistributors of a free program will individually obtain patent in effect making the program proprietary To prevent we have made it clear that any patent must be licensed for everyone s free use or not licensed at all The precise terms and conditions for distribution and modification follow GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR DISTRIBUTION AND MODIFICATION This License applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License The refers to any such program or and a work based on the Program means either the Program or any derivative work under copyright a work containing the Program or a portion of it
Definition: LICENSE:43
SDDS::parser::skipper< iterator_t > skipper_t
Definition: ast.cpp:21
std::string readFile()
Definition: SDDSParser.cpp:94
void setInput(const std::string &input)
Definition: SDDSParser.cpp:30
std::string::const_iterator iterator_t
Definition: array.cpp:19
std::string::iterator iterator
Definition: MSLang.h:15
FILECONTENTS * readFile(const char filename[])
Definition: read.cpp:40
std::vector< variant_t > columnData_t
Definition: ast.hpp:50
ast::columnData_t getColumnData(const std::string &columnName)
Definition: SDDSParser.cpp:116
const std::string name
boost::optional< std::string > name_m
Definition: column.hpp:47
end
Definition: multipole_t.tex:9
SDDS1 &description contents
Definition: test.stat:3
boost::optional< std::string > name_m
Definition: parameter.hpp:47