Jpp  16.0.2
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JEditProperties.cc
Go to the documentation of this file.
1 #include <iostream>
2 #include <fstream>
3 #include <iomanip>
4 #include <string>
5 #include <vector>
6 #include <map>
7 
9 #include "JLang/JEquation.hh"
10 #include "JLang/JToken.hh"
11 #include "JSupport/JMeta.hh"
12 
13 #include "Jeep/JComment.hh"
14 #include "Jeep/JParser.hh"
15 #include "Jeep/JMessage.hh"
16 
17 /**
18  * \file
19  *
20  * General purpose program to edit or create properties file.
21  * \author mdejong
22  */
23 int main(int argc, char **argv)
24 {
25  using namespace std;
26  using namespace JPP;
27 
28  typedef JToken<';'> token_type;
29 
30  string inputFile;
31  string outputFile;
32  vector<token_type> modifier;
33  int width;
34  int debug;
35 
36  try {
37 
38  JParser<> zap("General purpose program to edit or create properties file.");
39 
40  zap['f'] = make_field(inputFile) = "";
41  zap['o'] = make_field(outputFile) = "";
42  zap['@'] = make_field(modifier, "modifier; syntax \"<key> = <value>[" << token_type::SEPARATOR << "..]\"") = JPARSER::initialised();
43  zap['w'] = make_field(width, "format width of key") = 12;
44  zap['d'] = make_field(debug) = 0;
45 
46  zap(argc, argv);
47  }
48  catch(const exception &error) {
49  FATAL(error.what() << endl);
50  }
51 
52 
53  JComment comment;
55 
56  const JEquationFacet facet(JEquationParameters("=", ";\n", "", "#"));
57 
58  // input
59 
60  if (inputFile != "") {
61 
62  ifstream in(inputFile.c_str());
63 
64  in >> comment;
65 
66  in.imbue(locale(in.getloc(), facet.clone()));
67 
68  for (JEquation equation; in >> equation; ) {
69  data[equation.getKey()] = equation.getValue();
70  }
71 
72  in.close();
73  }
74 
75  comment.add(JMeta(argc, argv));
76 
77  // edit or create
78 
79  if (!modifier.empty()) {
80 
81  istringstream is;
82 
83  is.imbue(locale(is.getloc(), facet.clone()));
84 
85  for (const token_type& token : modifier) {
86 
87  is.clear();
88  is.str(token);
89 
90  JEquation equation;
91 
92  if (is >> equation && facet.isSeparator(equation.getSeparator()))
93  data[equation.getKey()] = equation.getValue();
94  else
95  ERROR("Invalid modifier: " << token << endl);
96  }
97  }
98 
99  // output
100 
101  filebuf buffer;
102 
103  if (outputFile != "") {
104  buffer.open(outputFile.c_str(), ios::out);
105  }
106 
107  ostream os(buffer.is_open() ? &buffer : cout.rdbuf());
108 
109  os << comment;
110 
111  os.imbue(locale(cout.getloc(), facet.clone()));
112 
113  for (const auto& item : data) {
114  os << setw(width) << left << item.first << right << facet.getDefaultWhiteSpace()
115  << facet.getSeparator() << facet.getDefaultWhiteSpace()
116  << item.second << facet.getEndOfLine();
117  }
118 
119  buffer.close();
120 }
Utility class to parse command line options.
Definition: JParser.hh:1500
int main(int argc, char *argv[])
Definition: Main.cc:15
Empty structure for specification of parser element that is initialised (i.e. does not require input)...
Definition: JParser.hh:66
string outputFile
is
Definition: JDAQCHSM.chsm:167
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1961
ROOT I/O of application specific meta data.
#define ERROR(A)
Definition: JMessage.hh:66
int debug
debug level
Definition: JSirene.cc:63
General purpose messaging.
#define FATAL(A)
Definition: JMessage.hh:67
Utility class to parse command line options.
then fatal Wrong number of arguments fi set_variable DETECTOR $argv[1] set_variable INPUT_FILE $argv[2] eval JPrintDetector a $DETECTOR O IDENTIFIER eval JPrintDetector a $DETECTOR O SUMMARY source JAcoustics sh $DETECTOR_ID CHECK_EXIT_CODE typeset A TRIPODS get_tripods $WORKDIR tripod txt TRIPODS for EMITTER in
Definition: JCanberra.sh:42