Jpp  17.0.0
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 <set>
7 #include <map>
8 
10 #include "JLang/JEquation.hh"
11 #include "JLang/JToken.hh"
12 #include "JSupport/JMeta.hh"
13 
14 #include "Jeep/JComment.hh"
15 #include "Jeep/JParser.hh"
16 #include "Jeep/JMessage.hh"
17 
18 /**
19  * \file
20  *
21  * General purpose program to edit or create properties file.
22  * \author mdejong
23  */
24 int main(int argc, char **argv)
25 {
26  using namespace std;
27  using namespace JPP;
28 
29  typedef JToken<';'> token_type;
30 
31  string inputFile;
32  string outputFile;
33  vector<token_type> modifier;
35  int width;
36  int debug;
37 
38  try {
39 
40  JParser<> zap("General purpose program to edit or create properties file.");
41 
42  zap['f'] = make_field(inputFile) = "";
43  zap['o'] = make_field(outputFile) = "";
44  zap['@'] = make_field(modifier, "modifier; syntax \"<key> = <value>[" << token_type::SEPARATOR << "..]\"") = JPARSER::initialised();
45  zap['r'] = make_field(rm, "remove key") = JPARSER::initialised();
46  zap['w'] = make_field(width, "format width of key") = 12;
47  zap['d'] = make_field(debug) = 0;
48 
49  zap(argc, argv);
50  }
51  catch(const exception &error) {
52  FATAL(error.what() << endl);
53  }
54 
55 
56  JComment comment;
58 
59  const JEquationFacet facet(JEquationParameters("=", ";\n", "", "#"));
60 
61  // input
62 
63  if (inputFile != "") {
64 
65  ifstream in(inputFile.c_str());
66 
67  in >> comment;
68 
69  in.imbue(locale(in.getloc(), facet.clone()));
70 
71  for (JEquation equation; in >> equation; ) {
72  data[equation.getKey()] = equation.getValue();
73  }
74 
75  in.close();
76  }
77 
78  comment.add(JMeta(argc, argv));
79 
80  // edit or create
81 
82  if (!modifier.empty()) {
83 
84  istringstream is;
85 
86  is.imbue(locale(is.getloc(), facet.clone()));
87 
88  for (const token_type& token : modifier) {
89 
90  is.clear();
91  is.str(token);
92 
93  JEquation equation;
94 
95  if (is >> equation && facet.isSeparator(equation.getSeparator()))
96  data[equation.getKey()] = equation.getValue();
97  else
98  ERROR("Invalid modifier: " << token << endl);
99  }
100  }
101 
102  if (!rm.empty()) {
103 
104  for (const string& key : rm) {
105 
106  map<string, string>::iterator i = data.find(key);
107 
108  if (i != data.end()) {
109  data.erase(i);
110  }
111  }
112  }
113 
114  // output
115 
116  filebuf buffer;
117 
118  if (outputFile != "") {
119  buffer.open(outputFile.c_str(), ios::out);
120  }
121 
122  ostream os(buffer.is_open() ? &buffer : cout.rdbuf());
123 
124  os << comment;
125 
126  os.imbue(locale(cout.getloc(), facet.clone()));
127 
128  for (const auto& item : data) {
129  os << setw(width) << left << item.first << right << facet.getDefaultWhiteSpace()
130  << facet.getSeparator() << facet.getDefaultWhiteSpace()
131  << item.second << facet.getEndOfLine();
132  }
133 
134  buffer.close();
135 }
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:66
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 JAcoustics sh $DETECTOR_ID source JAcousticsToolkit sh CHECK_EXIT_CODE typeset A EMITTERS get_tripods $WORKDIR tripod txt EMITTERS get_transmitters $WORKDIR transmitter txt EMITTERS for EMITTER in
Definition: JCanberra.sh:46