votca 2024.2-dev
Loading...
Searching...
No Matches
csg_property.cc
Go to the documentation of this file.
1/*
2 * Copyright 2009-2021 The VOTCA Development Team (http://www.votca.org)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17
18// Standard includes
19#include <iostream>
20
21// Third party includes
22#include <boost/program_options.hpp>
23
24// VOTCA includes
27
28// Local VOTCA includes
29#include "votca/csg/version.h"
30
31using namespace std;
32using namespace votca::csg;
33using namespace votca::tools;
34
35void help_text() {
36 votca::csg::HelpTextHeader("csg_property");
37 cout << "Helper program called by inverse scripts to parse xml file.\n\n";
38}
39
40int main(int argc, char **argv) {
41 string filter, file, path, print;
42 bool short_output = false;
43 bool with_path = false;
44
45 // lets read in some program options
46 namespace po = boost::program_options;
47
48 // Declare the supported options.
49 po::options_description desc("Allowed options");
50 desc.add_options()("help", "produce this help message")(
51 "path", po::value<string>(&path)->default_value(""),
52 "path to part of the xml file to print")(
53 "filter", po::value<string>(&filter)->default_value(""),
54 "list option values that match given criteria")(
55 "print", po::value<string>(&print)->default_value("."),
56 "specifies which children or root to print")(
57 "file", po::value<string>(&file), "xml file to parse")(
58 "short", "short version of output")("with-path",
59 "include path of node in output");
60
61 // now read in the command line
62 po::variables_map vm;
63 try {
64 po::store(po::parse_command_line(argc, argv, desc), vm);
65 po::notify(vm);
66 } catch (po::error &err) {
67 cout << "error parsing command line: " << err.what() << endl;
68 return -1;
69 }
70 // does the user want help?
71 if (vm.count("help")) {
72 help_text();
73 cout << desc << endl;
74 return 0;
75 }
76 // file specified
77 if (!vm.count("file")) {
78 cout << "please specify file\n";
79 cout << desc << endl;
80 return -1;
81 }
82 if (vm.count("short")) {
83 short_output = true;
84 }
85 if (vm.count("with-path")) {
86 with_path = true;
87 }
88
89 try {
90 Property p;
91 p.LoadFromXML(file);
92
93 for (Property *prop : p.Select(path)) {
94 if (filter != "") {
95 Tokenizer tokenizer(filter, "=");
97 tok = tokenizer.begin();
98 if (tok == tokenizer.end()) {
99 throw std::invalid_argument("error, specified invalid filter");
100 }
101
102 string field = *tok;
103 ++tok;
104 if (tok == tokenizer.end()) {
105 throw std::invalid_argument("error, specified invalid filter");
106 }
107
108 string value = *tok;
109 if (!wildcmp(value, prop->get(field).value())) {
110 continue;
111 }
112 }
113
114 std::vector<Property *> printvalues;
115
116 // Select returns empty on "." thus we have to handle that case explicitly
117 if (print == ".") {
118 printvalues.push_back(&(prop->get(print)));
119 } else {
120 printvalues = prop->Select(print);
121 }
122
123 for (Property *p2 : printvalues) {
124
125 if (!short_output && with_path) {
126 cout << p2->path() << ".";
127 }
128 if (!short_output) {
129 cout << p2->name() << " = ";
130 }
131 cout << p2->value();
132 cout << endl;
133 }
134 }
135 } catch (std::exception &error) {
136 cerr << "Warning from parsing xml file '" << file << "':\n"
137 << error.what() << endl;
138 return -1;
139 }
140 return 0;
141}
class to manage program options with xml serialization functionality
Definition property.h:55
std::vector< Property * > Select(const std::string &filter)
select property based on a filter
Definition property.cc:185
void LoadFromXML(std::string filename)
Definition property.cc:238
break string into words
Definition tokenizer.h:72
iterator end()
end iterator
Definition tokenizer.h:102
iterator begin()
iterator to first element
Definition tokenizer.h:97
boost::tokenizer< boost::char_separator< char > >::iterator iterator
Definition tokenizer.h:74
int main(int argc, char **argv)
void help_text()
STL namespace.
void HelpTextHeader(const std::string &tool_name)
Definition version.cc:42
int wildcmp(const char *wild, const char *string)
Definition tokenizer.cc:28