feisty meow concerns codebase  2.140
ini_edit.cpp
Go to the documentation of this file.
1 /*****************************************************************************\
2 * *
3 * Name : ini_edit *
4 * Author : Chris Koeritz *
5 * *
6 * Purpose: *
7 * *
8 * Provides command line ini editing capabilities. These include both *
9 * reading of ini files and writing new entries to them. *
10 * *
11 *******************************************************************************
12 * Copyright (c) 2006-$now By Author. This program is free software; you can *
13 * redistribute it and/or modify it under the terms of the GNU General Public *
14 * License as published by the Free Software Foundation; either version 2 of *
15 * the License or (at your option) any later version. This is online at: *
16 * http://www.fsf.org/copyleft/gpl.html *
17 * Please send any updates to: fred@gruntose.com *
18 \*****************************************************************************/
19 
22 #include <filesystem/filename.h>
23 #include <loggers/console_logger.h>
26 #include <textual/list_parsing.h>
27 
28 #include <stdlib.h>
29 
30 using namespace application;
31 using namespace basis;
32 using namespace configuration;
33 using namespace filesystem;
34 using namespace loggers;
35 using namespace structures;
36 using namespace textual;
37 
38 #undef LOG
39 #define LOG(to_print) program_wide_logger::get().log(to_print, ALWAYS_PRINT)
40 
41 class ini_editor : public application_shell
42 {
43 public:
44  ini_editor()
46  _app_name(filename(application::_global_argv[0]).basename()) {}
47  DEFINE_CLASS_NAME("ini_editor");
48  virtual int execute();
49  int print_instructions();
50 
51 private:
52  astring _app_name;
53 };
54 
56 {
57  LOG(a_sprintf("\
58 %s: This program needs five parameters to process an ini file.\n\
59 There are two major operations, read and write. The type of operation\n\
60 should be the first parameter. The other parameters are similar for both\n\
61 operations, except for the last parameter. These are as follows:\n\
62 Reading:\n\
63 \tread inifile section entry defaultvalue\n\
64  This reads the \"inifile\" specified and looks for the \"section\" and\n\
65 \"entry\" name in the file. It will either return (via standard output)\n\
66 the value found there or it will return the \"defaultvalue\". No error\n\
67 will be raised if the entry is missing, but the default signals that no\n\
68 value was defined.\n\
69  Additionally, if the entry name is the special value \"whole_section\",\n\
70 then the entire section will be read and returned as a CSV list. If the\n\
71 section is empty, then the default string is returned instead.\n\
72 Writing:\n\
73 \twrite inifile section entry newvalue\n\
74  This writes a new item with contents \"newvalue\" into the \"inifile\"\n\
75 in the \"section\" at the \"entry\" specified. This should always succeed\n\
76 unless the ini file is not writable (in which case an error should be\n\
77 returned). Nothing is send to standard output for a write operation.\n\
78 ", _app_name.s()));
79  return 23;
80 }
81 
82 int ini_editor::execute()
83 {
85 
87 
88  astring operation = application::_global_argv[1];
89  bool read_op = true;
90  if ( (operation[0] == 'w') || (operation[0] == 'W') ) read_op = false;
91  astring ini_file = application::_global_argv[2];
95  ini_configurator ini(ini_file, ini_configurator::RETURN_ONLY);
96  if (read_op) {
97  // read the entry from the ini file.
98  astring found;
99  if (entry.equal_to("whole_section")) {
100  // they want the whole section back at them.
101  string_table found;
102  bool worked = ini.get_section(section, found);
103  if (!worked) program_wide_logger::get().log(value, ALWAYS_PRINT); // default.
104  else {
105  // generate answer as csv.
106  astring temp;
107  list_parsing::create_csv_line(found, temp);
108  program_wide_logger::get().log(temp, ALWAYS_PRINT); // real value read.
109  }
110  } else {
111  bool worked = ini.get(section, entry, found);
113  if (!worked) program_wide_logger::get().log(value, ALWAYS_PRINT); // default.
114  else program_wide_logger::get().log(found, ALWAYS_PRINT); // real value read.
115  }
116  } else {
117  // write the entry to the ini file.
118  bool worked = ini.put(section, entry, value);
119  if (!worked) exit(28); // failure to write the entry.
120  }
121 
122  return 0;
123 }
124 
125 HOOPLE_MAIN(ini_editor, )
126 
int print_instructions(bool good, const astring &program_name)
Definition: checker.cpp:45
The application_shell is a base object for console programs.
a_sprintf is a specialization of astring that provides printf style support.
Definition: astring.h:440
Provides a dynamically resizable ASCII character string.
Definition: astring.h:35
bool equal_to(const char *that) const
returns true if "that" is equal to this.
Definition: astring.cpp:159
Supports a configurator-based interface on text initialization files.
Provides operations commonly needed on file names.
Definition: filename.h:64
Provides a symbol_table that holds strings as the content.
Definition: string_table.h:32
#define SETUP_CONSOLE_LOGGER
< a macro that retasks the program-wide logger as a console_logger.
#define DEFINE_CLASS_NAME(objname)
Defines the name of a class by providing a couple standard methods.
Definition: enhance_cpp.h:45
Provides macros that implement the 'main' program of an application.
#define HOOPLE_MAIN(obj_name, obj_args)
options that should work for most unix and linux apps.
Definition: hoople_main.h:61
#define LOG(to_print)
Definition: ini_edit.cpp:39
Implements an application lock to ensure only one is running at once.
char ** _global_argv
The guards collection helps in testing preconditions and reporting errors.
Definition: array.h:30
A platform independent way to obtain the timestamp of a file.
Definition: byte_filer.cpp:37
A logger that sends to the console screen using the standard output device.
A dynamic container class that holds any kind of object via pointers.
Definition: amorph.h:55