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>
27
28#include <stdlib.h>
29
30using namespace application;
31using namespace basis;
32using namespace configuration;
33using namespace filesystem;
34using namespace loggers;
35using namespace structures;
36using namespace textual;
37
38#undef LOG
39#define LOG(to_print) program_wide_logger::get().log(to_print, ALWAYS_PRINT)
40
41class ini_editor : public application_shell
42{
43public:
44 ini_editor()
46 _app_name(filename(application::_global_argv[0]).basename()) {}
47 DEFINE_CLASS_NAME("ini_editor");
48 virtual int execute();
50
51private:
52 astring _app_name;
53};
54
55int ini_editor::print_instructions()
56{
57 LOG(a_sprintf("\
58%s: This program needs five parameters to process an ini file.\n\
59There are two major operations, read and write. The type of operation\n\
60should be the first parameter. The other parameters are similar for both\n\
61operations, except for the last parameter. These are as follows:\n\
62Reading:\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\
66the value found there or it will return the \"defaultvalue\". No error\n\
67will be raised if the entry is missing, but the default signals that no\n\
68value was defined.\n\
69 Additionally, if the entry name is the special value \"whole_section\",\n\
70then the entire section will be read and returned as a CSV list. If the\n\
71section is empty, then the default string is returned instead.\n\
72Writing:\n\
73\twrite inifile section entry newvalue\n\
74 This writes a new item with contents \"newvalue\" into the \"inifile\"\n\
75in the \"section\" at the \"entry\" specified. This should always succeed\n\
76unless the ini file is not writable (in which case an error should be\n\
77returned). Nothing is send to standard output for a write operation.\n\
78", _app_name.s()));
79 return 23;
80}
81
82int ini_editor::execute()
83{
85
87
89 bool read_op = true;
90 if ( (operation[0] == 'w') || (operation[0] == 'W') ) read_op = false;
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.
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
125HOOPLE_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.
virtual int execute()=0
< retrieves the command line from the /proc hierarchy on linux.
application_shell()
constructs an application_shell to serve as the root of the program.
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
virtual outcome log(const base_string &info, int filter)=0
writes the information in "info" to the logger using the "filter".
Supports a configurator-based interface on text initialization files.
Provides operations commonly needed on file names.
Definition filename.h:64
static loggers::standard_log_base & get()
Provided by the startup code within each application for logging.
Provides a symbol_table that holds strings as the content.
static void create_csv_line(const structures::string_array &to_csv, basis::astring &target)
#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:42
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.
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