1 /*****************************************************************************\
4 * Author : Chris Koeritz *
8 * Provides command line ini editing capabilities. These include both *
9 * reading of ini files and writing new entries to them. *
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 \*****************************************************************************/
20 #include <application/hoople_main.h>
21 #include <configuration/ini_configurator.h>
22 #include <filesystem/filename.h>
23 #include <loggers/console_logger.h>
24 #include <structures/static_memory_gremlin.h>
25 #include <structures/string_table.h>
26 #include <textual/list_parsing.h>
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;
39 #define LOG(to_print) program_wide_logger::get().log(to_print, ALWAYS_PRINT)
41 class ini_editor : public application_shell
45 : application_shell(),
46 _app_name(filename(application::_global_argv[0]).basename()) {}
47 DEFINE_CLASS_NAME("ini_editor");
48 virtual int execute();
49 int print_instructions();
55 int ini_editor::print_instructions()
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\
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\
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\
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\
82 int ini_editor::execute()
86 if (application::_global_argc < 6) return print_instructions();
88 astring operation = application::_global_argv[1];
90 if ( (operation[0] == 'w') || (operation[0] == 'W') ) read_op = false;
91 astring ini_file = application::_global_argv[2];
92 astring section = application::_global_argv[3];
93 astring entry = application::_global_argv[4];
94 astring value = application::_global_argv[5];
95 ini_configurator ini(ini_file, ini_configurator::RETURN_ONLY);
97 // read the entry from the ini file.
99 if (entry.equal_to("whole_section")) {
100 // they want the whole section back at them.
102 bool worked = ini.get_section(section, found);
103 if (!worked) program_wide_logger::get().log(value, ALWAYS_PRINT); // default.
105 // generate answer as csv.
107 list_parsing::create_csv_line(found, temp);
108 program_wide_logger::get().log(temp, ALWAYS_PRINT); // real value read.
111 bool worked = ini.get(section, entry, found);
112 /// program_wide_logger::get().eol(log_base::NO_ENDING);
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.
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.
125 HOOPLE_MAIN(ini_editor, )