Jpp  19.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/JProperties.hh"
15 #include "Jeep/JComment.hh"
16 #include "Jeep/JParser.hh"
17 #include "Jeep/JMessage.hh"
18 
19 /**
20  * \file
21  *
22  * General purpose program to edit or create properties file.
23  * \author mdejong
24  */
25 int main(int argc, char **argv)
26 {
27  using namespace std;
28  using namespace JPP;
29 
30  typedef JToken<';'> token_type;
31 
32  string inputFile;
33  string outputFile;
34  JEquationParameters parameters("=", ";\n", "", "#");
35  vector<token_type> modifier;
37  int width;
38  bool squash;
39  int debug;
40 
41  try {
42 
43  JProperties properties;
44 
45  properties["sep"] = parameters.getSeparator();
46  properties["eol"] = parameters.getEndOfLine();
47  properties["div"] = parameters.getDivision();
48  properties["skip"] = parameters.getSkipLine();
49  properties["left"] = parameters.getLeftBracket();
50  properties["right"] = parameters.getRightBracket();
51  properties["ws"] = parameters.getWhiteSpace();
52  properties["cc"] = parameters.getComment();
53 
54  JParser<> zap("General purpose program to edit or create properties file.");
55 
56  zap['f'] = make_field(inputFile) = "";
57  zap['o'] = make_field(outputFile) = "";
58  zap['@'] = make_field(properties) = JPARSER::initialised();
59  zap['M'] = make_field(modifier, "modifier; syntax \"<key> = <value>[" << token_type::SEPARATOR << "..]\"") = JPARSER::initialised();
60  zap['r'] = make_field(rm, "remove key") = JPARSER::initialised();
61  zap['w'] = make_field(width, "format width of key") = 12;
62  zap['q'] = make_field(squash, "squash meta data");
63  zap['d'] = make_field(debug, "debug level") = 2;
64 
65  zap(argc, argv);
66  }
67  catch(const exception &error) {
68  FATAL(error.what() << endl);
69  }
70 
71 
72  JComment comment;
74 
75  const JEquationFacet facet(parameters);
76 
77  // input
78 
79  if (inputFile != "") {
80 
81  ifstream in(inputFile.c_str());
82 
83  in >> comment;
84 
85  in.imbue(locale(in.getloc(), facet.clone()));
86 
87  for (JEquation equation; in >> equation; ) {
88  data[equation.getKey()] = equation.getValue();
89  }
90 
91  in.close();
92  }
93 
94  if (squash) {
95  comment.clear();
96  }
97 
98  comment.add(JMeta(argc, argv));
99 
100  // edit or create
101 
102  if (!modifier.empty()) {
103 
104  istringstream is;
105 
106  is.imbue(locale(is.getloc(), facet.clone()));
107 
108  for (const token_type& token : modifier) {
109 
110  is.clear();
111  is.str(token);
112 
113  JEquation equation;
114 
115  if (is >> equation && facet.isSeparator(equation.getSeparator()))
116  data[equation.getKey()] = equation.getValue();
117  else
118  ERROR("Invalid modifier: " << token << endl);
119  }
120  }
121 
122  if (!rm.empty()) {
123 
124  for (const string& key : rm) {
125 
127 
128  if (i != data.end()) {
129  data.erase(i);
130  }
131  }
132  }
133 
134  // output
135 
136  filebuf buffer;
137 
138  if (outputFile != "") {
139  buffer.open(outputFile.c_str(), ios::out);
140  }
141 
142  ostream os(buffer.is_open() ? &buffer : cout.rdbuf());
143 
144  os << comment;
145 
146  os.imbue(locale(cout.getloc(), facet.clone()));
147 
148  for (const auto& item : data) {
149  os << setw(width) << left << item.first << right << facet.getDefaultWhiteSpace()
150  << facet.getSeparator() << facet.getDefaultWhiteSpace()
151  << item.second << facet.getEndOfLine();
152  }
153 
154  buffer.close();
155 }
Utility class to parse command line options.
Definition: JParser.hh:1711
const std::string & getEndOfLine() const
Get end of line characters.
int main(int argc, char *argv[])
Definition: Main.cc:15
Utility class to parse parameter values.
Definition: JProperties.hh:497
const std::string & getSeparator() const
Get separator characters.
*fatal Wrong number of arguments esac JCookie sh typeset Z DETECTOR typeset Z SOURCE_RUN typeset Z TARGET_RUN set_variable PARAMETERS_FILE $WORKDIR parameters
Definition: diff-Tuna.sh:38
Simple data structure to support I/O of equations (see class JLANG::JEquation).
Empty structure for specification of parser element that is initialised (i.e. does not require input)...
Definition: JParser.hh:84
string outputFile
is
Definition: JDAQCHSM.chsm:167
Utility class to parse parameter values.
then rm
Definition: sftpput.zsh:30
char getRightBracket() const
Get right bracket.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:2158
char getLeftBracket() const
Get left bracket.
ROOT I/O of application specific meta data.
#define ERROR(A)
Definition: JMessage.hh:66
const std::string & getDivision() const
Get division characters.
const std::string & getComment() const
Get comment string.
General purpose messaging.
const std::string & getSkipLine() const
Get skip line characters.
#define FATAL(A)
Definition: JMessage.hh:67
then fatal The output file must have the wildcard in the e g root fi 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:48
Utility class to parse command line options.
const std::string & getWhiteSpace() const
Get white space characters.
int debug
debug level