Jpp 20.0.0-195-g190c9e876
the software that should make you happy
Loading...
Searching...
No Matches
JEditProperties.cc File Reference

General purpose program to edit or create properties file. More...

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <vector>
#include <set>
#include <map>
#include "JLang/JEquationParameters.hh"
#include "JLang/JEquation.hh"
#include "JLang/JToken.hh"
#include "JSupport/JMeta.hh"
#include "Jeep/JProperties.hh"
#include "Jeep/JComment.hh"
#include "Jeep/JParser.hh"
#include "Jeep/JMessage.hh"

Go to the source code of this file.

Functions

int main (int argc, char **argv)
 

Detailed Description

General purpose program to edit or create properties file.

Author
mdejong

Definition in file JEditProperties.cc.

Function Documentation

◆ main()

int main ( int argc,
char ** argv )

Definition at line 67 of file JEditProperties.cc.

68{
69 using namespace std;
70 using namespace JPP;
71
72 typedef JToken<';'> token_type;
73
74 string inputFile;
75 string outputFile;
76 JEquationParameters parameters("=", "\n;", "", "#");
77 vector<token_type> modifier;
78 set<string> rm;
79 bool squash;
80 int debug;
81
82 try {
83
84 JProperties properties;
85
86 properties["sep"] = parameters.getSeparator();
87 properties["eol"] = parameters.getEndOfLine();
88 properties["div"] = parameters.getDivision();
89 properties["skip"] = parameters.getSkipLine();
90 properties["left"] = parameters.getLeftBracket();
91 properties["right"] = parameters.getRightBracket();
92 properties["ws"] = parameters.getWhiteSpace();
93 properties["cc"] = parameters.getComment();
94
95 JParser<> zap("General purpose program to edit or create properties file.");
96
97 zap['f'] = make_field(inputFile) = "";
98 zap['o'] = make_field(outputFile) = "";
99 zap['@'] = make_field(properties) = JPARSER::initialised();
100 zap['M'] = make_field(modifier, "modifier; syntax \"<key> = <value>[" << token_type::SEPARATOR << "..]\"") = JPARSER::initialised();
101 zap['r'] = make_field(rm, "remove key") = JPARSER::initialised();
102 zap['q'] = make_field(squash, "squash meta data");
103 zap['d'] = make_field(debug, "debug level") = 2;
104
105 zap(argc, argv);
106 }
107 catch(const exception &error) {
108 FATAL(error.what() << endl);
109 }
110
111
112 // modifiers
113
114 map_type zip;
115
116 if (!modifier.empty()) {
117
118 const JEquationFacet facet(JEquationParameters("=", string(1,token_type::SEPARATOR), "", ""));
119
120 istringstream is;
121
122 is.imbue(locale(is.getloc(), facet.clone()));
123
124 for (const token_type& token : modifier) {
125
126 is.clear();
127 is.str(token);
128
129 JEquation equation;
130
131 if (is >> equation && facet.isSeparator(equation.getSeparator()))
132 zip[equation.getKey()] = { equation.getValue(), 0 };
133 else
134 ERROR("Invalid modifier: " << token << endl);
135 }
136 }
137
138
139 // intermediate storage
140
141 const JEquationFacet facet(parameters);
142
143 ostringstream os;
144
145 os.imbue(locale(cout.getloc(), facet.clone()));
146
147
148 // input
149
150 ifstream in(inputFile.c_str());
151
152 JComment comment;
153
154 in >> comment;
155
156 if (squash) {
157 comment.clear();
158 }
159
160 comment.add(JMeta(argc, argv));
161
162 os << comment;
163
164 istringstream is;
165
166 is.imbue(locale(is.getloc(), facet.clone()));
167
168 for (string buffer; getline(in, buffer, facet.getDefaultEndOfLine()); ) {
169
170 is.clear();
171 is.str(buffer);
172
173 JEquation equation;
174
175 if (is >> equation && facet.isSeparator(equation.getSeparator())) {
176
177 if (rm.count(equation.getKey()) == 0) {
178 os << zip(equation);
179 }
180
181 } else {
182
183 os << buffer << endl;
184 }
185 }
186
187 in.close();
188
189
190 // add pending modifiers
191
192 for (const auto& i : zip) {
193 if (i.second.count == 0) {
194 os << JEquation(i.first, i.second.value);
195 }
196 }
197
198
199 // output
200
201 filebuf buffer;
202
203 if (outputFile != "") {
204 buffer.open(outputFile.c_str(), ios::out);
205 }
206
207 ostream out(buffer.is_open() ? &buffer : cout.rdbuf());
208
209 out << os.str();
210
211 buffer.close();
212}
string outputFile
#define ERROR(A)
Definition JMessage.hh:66
#define FATAL(A)
Definition JMessage.hh:67
int debug
debug level
Definition JSirene.cc:74
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition JParser.hh:2142
Utility class to parse parameter values.
Facet class to specify parsing of equations in currect locale (see class JLANG::JEquation).
Simple data structure to support I/O of equations (see class JLANG::JEquation).
const std::string & getSkipLine() const
Get skip line characters.
const std::string & getComment() const
Get comment string.
const std::string & getSeparator() const
Get separator characters.
char getLeftBracket() const
Get left bracket.
const string_type & getEndOfLine() const
Get end of line characters.
char getRightBracket() const
Get right bracket.
const std::string & getDivision() const
Get division characters.
const string_type & getWhiteSpace() const
Get white space characters.
General purpose equation class.
Definition JEquation.hh:47
const std::string & getKey() const
Get key.
Definition JEquation.hh:163
const std::string & getValue() const
Get value.
Definition JEquation.hh:185
const char getSeparator() const
Get separator.
Definition JEquation.hh:174
Wrapper class around string.
Definition JToken.hh:26
Utility class to parse command line options.
Definition JParser.hh:1698
std::istream & getline(std::istream &in, JString &object)
Read string from input stream until end of line.
Definition JString.hh:478
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary class for comment.
Definition JComment.hh:43
JComment & add(const std::string &comment)
Add comment.
Definition JComment.hh:100
Empty structure for specification of parser element that is initialised (i.e. does not require input)...
Definition JParser.hh:68
Auxiliary class for ROOT I/O of application specific meta data.
Definition JMeta.hh:72