Jpp  18.0.0-rc.3
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Functions
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/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

int main ( int  argc,
char **  argv 
)

Definition at line 24 of file JEditProperties.cc.

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:1514
Empty structure for specification of parser element that is initialised (i.e. does not require input)...
Definition: JParser.hh:83
string outputFile
is
Definition: JDAQCHSM.chsm:167
then rm
Definition: sftpput.zsh:30
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1989
#define ERROR(A)
Definition: JMessage.hh:66
#define FATAL(A)
Definition: JMessage.hh:67
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
int debug
debug level