Jpp  17.1.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  bool squash;
37  int debug;
38 
39  try {
40 
41  JParser<> zap("General purpose program to edit or create properties file.");
42 
43  zap['f'] = make_field(inputFile) = "";
44  zap['o'] = make_field(outputFile) = "";
45  zap['@'] = make_field(modifier, "modifier; syntax \"<key> = <value>[" << token_type::SEPARATOR << "..]\"") = JPARSER::initialised();
46  zap['r'] = make_field(rm, "remove key") = JPARSER::initialised();
47  zap['w'] = make_field(width, "format width of key") = 12;
48  zap['q'] = make_field(squash, "squash meta data");
49  zap['d'] = make_field(debug, "debug level") = 2;
50 
51  zap(argc, argv);
52  }
53  catch(const exception &error) {
54  FATAL(error.what() << endl);
55  }
56 
57 
58  JComment comment;
60 
61  const JEquationFacet facet(JEquationParameters("=", ";\n", "", "#"));
62 
63  // input
64 
65  if (inputFile != "") {
66 
67  ifstream in(inputFile.c_str());
68 
69  in >> comment;
70 
71  in.imbue(locale(in.getloc(), facet.clone()));
72 
73  for (JEquation equation; in >> equation; ) {
74  data[equation.getKey()] = equation.getValue();
75  }
76 
77  in.close();
78  }
79 
80  if (squash) {
81  comment.clear();
82  }
83 
84  comment.add(JMeta(argc, argv));
85 
86  // edit or create
87 
88  if (!modifier.empty()) {
89 
90  istringstream is;
91 
92  is.imbue(locale(is.getloc(), facet.clone()));
93 
94  for (const token_type& token : modifier) {
95 
96  is.clear();
97  is.str(token);
98 
99  JEquation equation;
100 
101  if (is >> equation && facet.isSeparator(equation.getSeparator()))
102  data[equation.getKey()] = equation.getValue();
103  else
104  ERROR("Invalid modifier: " << token << endl);
105  }
106  }
107 
108  if (!rm.empty()) {
109 
110  for (const string& key : rm) {
111 
112  map<string, string>::iterator i = data.find(key);
113 
114  if (i != data.end()) {
115  data.erase(i);
116  }
117  }
118  }
119 
120  // output
121 
122  filebuf buffer;
123 
124  if (outputFile != "") {
125  buffer.open(outputFile.c_str(), ios::out);
126  }
127 
128  ostream os(buffer.is_open() ? &buffer : cout.rdbuf());
129 
130  os << comment;
131 
132  os.imbue(locale(cout.getloc(), facet.clone()));
133 
134  for (const auto& item : data) {
135  os << setw(width) << left << item.first << right << facet.getDefaultWhiteSpace()
136  << facet.getSeparator() << facet.getDefaultWhiteSpace()
137  << item.second << facet.getEndOfLine();
138  }
139 
140  buffer.close();
141 }
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:67
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