#include #include #include #include #include #include #include #include "JLang/JEquationParameters.hh" #include "JLang/JEquation.hh" #include "JLang/JToken.hh" #include "JSupport/JMeta.hh" #include "Jeep/JProperties.hh" #include "Jeep/JComment.hh" #include "Jeep/JParser.hh" #include "Jeep/JMessage.hh" /** * \file * * General purpose program to edit or create properties file. * \author mdejong */ int main(int argc, char **argv) { using namespace std; using namespace JPP; typedef JToken<';'> token_type; string inputFile; string outputFile; JEquationParameters parameters("=", ";\n", "", "#"); vector modifier; set rm; int width; bool squash; int debug; try { JProperties properties; properties["sep"] = parameters.getSeparator(); properties["eol"] = parameters.getEndOfLine(); properties["div"] = parameters.getDivision(); properties["skip"] = parameters.getSkipLine(); properties["left"] = parameters.getLeftBracket(); properties["right"] = parameters.getRightBracket(); properties["ws"] = parameters.getWhiteSpace(); properties["cc"] = parameters.getComment(); JParser<> zap("General purpose program to edit or create properties file."); zap['f'] = make_field(inputFile) = ""; zap['o'] = make_field(outputFile) = ""; zap['@'] = make_field(properties) = JPARSER::initialised(); zap['M'] = make_field(modifier, "modifier; syntax \" = [" << token_type::SEPARATOR << "..]\"") = JPARSER::initialised(); zap['r'] = make_field(rm, "remove key") = JPARSER::initialised(); zap['w'] = make_field(width, "format width of key") = 12; zap['q'] = make_field(squash, "squash meta data"); zap['d'] = make_field(debug, "debug level") = 2; zap(argc, argv); } catch(const exception &error) { FATAL(error.what() << endl); } JComment comment; map data; const JEquationFacet facet(parameters); // input if (inputFile != "") { ifstream in(inputFile.c_str()); in >> comment; in.imbue(locale(in.getloc(), facet.clone())); for (JEquation equation; in >> equation; ) { data[equation.getKey()] = equation.getValue(); } in.close(); } if (squash) { comment.clear(); } comment.add(JMeta(argc, argv)); // edit or create if (!modifier.empty()) { istringstream is; is.imbue(locale(is.getloc(), facet.clone())); for (const token_type& token : modifier) { is.clear(); is.str(token); JEquation equation; if (is >> equation && facet.isSeparator(equation.getSeparator())) data[equation.getKey()] = equation.getValue(); else ERROR("Invalid modifier: " << token << endl); } } if (!rm.empty()) { for (const string& key : rm) { map::iterator i = data.find(key); if (i != data.end()) { data.erase(i); } } } // output filebuf buffer; if (outputFile != "") { buffer.open(outputFile.c_str(), ios::out); } ostream os(buffer.is_open() ? &buffer : cout.rdbuf()); os << comment; os.imbue(locale(cout.getloc(), facet.clone())); for (const auto& item : data) { os << setw(width) << left << item.first << right << facet.getDefaultWhiteSpace() << facet.getSeparator() << facet.getDefaultWhiteSpace() << item.second << facet.getEndOfLine(); } buffer.close(); }