OPALX (Object Oriented Parallel Accelerator Library for Exascal) master (dc2a29eed580)
OPALX
Loading...
Searching...
No Matches
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 <algorithm>
21#include <cctype>
22
23SDDS::SDDSParser::SDDSParser() : sddsFileName_m("") {}
24
25SDDS::SDDSParser::SDDSParser(const std::string& input) : sddsFileName_m(input) {}
26
27void SDDS::SDDSParser::setInput(const std::string& input) { sddsFileName_m = input; }
28
30 sddsData_m.clear();
31 paramNameToID_m.clear();
32 columnNameToID_m.clear();
33
34 std::string contents = readFile();
35
36 // Use simple parser instead of boost::spirit
37 parser::SimpleParser parser(contents);
38 try {
39 sddsData_m = parser.parse();
40 } catch (const std::exception& e) {
42 "SDDSParser::run", std::string("could not parse SDDS file: ") + e.what());
43 }
44
45 // Parse parameter values
46 size_t pos = 0;
47 for (auto& param : sddsData_m.sddsParameters_m) {
48 if (!param.parse(contents, pos)) {
49 throw SDDSParserException("SDDSParser::run", "could not parse parameter value");
50 }
51 // Skip comma or whitespace
52 while (pos < contents.length()
53 && (std::isspace(static_cast<unsigned char>(contents[pos]))
54 || contents[pos] == ',')) {
55 pos++;
56 }
57 }
58
59 // Parse column values (rows)
60 while (pos < contents.length()) {
61 bool found_value = false;
62 for (auto& col : sddsData_m.sddsColumns_m) {
63 if (col.parse(contents, pos)) {
64 found_value = true;
65 // Skip comma or whitespace
66 while (pos < contents.length()
67 && (std::isspace(static_cast<unsigned char>(contents[pos]))
68 || contents[pos] == ',')) {
69 pos++;
70 }
71 }
72 }
73 if (!found_value) {
74 break;
75 }
76 }
77
78 unsigned int param_order = 0;
79 for (const SDDS::parameter& param : sddsData_m.sddsParameters_m) {
80 std::string name = *param.name_m;
81 fixCaseSensitivity(name);
82 paramNameToID_m.insert(std::make_pair(name, param_order));
83 ++param_order;
84 }
85
86 unsigned int col_order = 0;
87 for (const SDDS::column& col : sddsData_m.sddsColumns_m) {
88 std::string name = *col.name_m;
89 fixCaseSensitivity(name);
90 columnNameToID_m.insert(std::make_pair(name, col_order));
91 ++col_order;
92 }
93
94 return sddsData_m;
95}
96
98 std::ifstream in(sddsFileName_m.c_str());
99
100 if (in) {
101 std::string contents;
102 in.seekg(0, std::ios::end);
103 contents.resize(in.tellg());
104 in.seekg(0, std::ios::beg);
105
106 in.read(&contents[0], contents.size());
107
108 in.close();
109
110 return contents;
111 }
112
114 "SDDSParser::readSDDSFile", "could not open file '" + sddsFileName_m + "'");
115
116 return std::string("");
117}
118
120 int idx = getColumnIndex(columnName);
121
122 return sddsData_m.sddsColumns_m[idx].values_m;
123}
124
125int SDDS::SDDSParser::getColumnIndex(std::string col_name) const {
126 fixCaseSensitivity(col_name);
127 auto it = columnNameToID_m.find(col_name);
128 if (it != columnNameToID_m.end()) {
129 return it->second;
130 }
131
133 "SDDSParser::getColumnIndex", "could not find column '" + col_name + "'");
134}
135
136// XXX use either all upper, or all lower case chars
137void SDDS::SDDSParser::fixCaseSensitivity(std::string& for_string) {
138 std::transform(for_string.begin(), for_string.end(), for_string.begin(), [](unsigned char c) {
139 return std::tolower(c);
140 });
141}
std::string readFile()
ast::columnData_t getColumnData(const std::string &columnName)
static void fixCaseSensitivity(std::string &for_string)
void setInput(const std::string &input)
int getColumnIndex(std::string col_name) const
std::vector< variant_t > columnData_t
Definition ast.hpp:36
void clear()
Definition file.hpp:45