ini_edit.cpp

Go to the documentation of this file.
00001 /*****************************************************************************\
00002 *                                                                             *
00003 *  Name   : ini_edit                                                          *
00004 *  Author : Chris Koeritz                                                     *
00005 *                                                                             *
00006 *  Purpose:                                                                   *
00007 *                                                                             *
00008 *    Provides command line ini editing capabilities.  These include both      *
00009 *  reading of ini files and writing new entries to them.                      *
00010 *                                                                             *
00011 *******************************************************************************
00012 * Copyright (c) 2006-$now By Author.  This program is free software; you can  *
00013 * redistribute it and/or modify it under the terms of the GNU General Public  *
00014 * License as published by the Free Software Foundation; either version 2 of   *
00015 * the License or (at your option) any later version.  This is online at:      *
00016 *     http://www.fsf.org/copyleft/gpl.html                                    *
00017 * Please send any updates to: fred@gruntose.com                               *
00018 \*****************************************************************************/
00019 
00020 #include <data_struct/string_table.h>
00021 #include <opsystem/application_shell.h>
00022 #include <loggers/console_logger.h>
00023 #include <opsystem/filename.h>
00024 #include <opsystem/ini_config.h>
00025 #include <data_struct/static_memory_gremlin.h>
00026 #include <textual/list_parsing.h>
00027 
00028 #include <stdlib.h>
00029 
00030 #undef LOG
00031 #define LOG(to_print) program_wide_logger().log(to_print)
00032 
00033 class ini_editor : public application_shell
00034 {
00035 public:
00036   ini_editor()
00037       : application_shell(static_class_name()),
00038         _app_name(filename(__argv[0]).basename()) {}
00039   IMPLEMENT_CLASS_NAME("ini_editor");
00040   virtual int execute();
00041   int print_instructions();
00042 
00043 private:
00044   istring _app_name;
00045 };
00046 
00047 int ini_editor::print_instructions()
00048 {
00049   LOG(isprintf("\
00050 %s: This program needs five parameters to process an ini file.\n\
00051 There are two major operations, read and write.  The type of operation\n\
00052 should be the first parameter.  The other parameters are similar for both\n\
00053 operations, except for the last parameter.  These are as follows:\n\
00054 Reading:\n\
00055 \tread inifile section entry defaultvalue\n\
00056   This reads the \"inifile\" specified and looks for the \"section\" and\n\
00057 \"entry\" name in the file.  It will either return (via standard output)\n\
00058 the value found there or it will return the \"defaultvalue\".  No error\n\
00059 will be raised if the entry is missing, but the default signals that no\n\
00060 value was defined.\n\
00061   Additionally, if the entry name is the special value \"whole_section\",\n\
00062 then the entire section will be read and returned as a CSV list.  If the\n\
00063 section is empty, then the default string is returned instead.\n\
00064 Writing:\n\
00065 \twrite inifile section entry newvalue\n\
00066   This writes a new item with contents \"newvalue\" into the \"inifile\"\n\
00067 in the \"section\" at the \"entry\" specified.  This should always succeed\n\
00068 unless the ini file is not writable (in which case an error should be\n\
00069 returned).  Nothing is send to standard output for a write operation.\n\
00070 ", _app_name.s()));
00071   return 23;
00072 }
00073 
00074 int ini_editor::execute()
00075 {
00076   SET_DEFAULT_CONSOLE_LOGGER;
00077 
00078   if (__argc < 6) return print_instructions();
00079 
00080   istring operation = __argv[1];
00081   bool read_op = true;
00082   if ( (operation[0] == 'w') || (operation[0] == 'W') ) read_op = false;
00083   istring ini_file = __argv[2];
00084   istring section = __argv[3];
00085   istring entry = __argv[4];
00086   istring value = __argv[5];
00087   ini_configurator ini(ini_file, ini_configurator::RETURN_ONLY);
00088   if (read_op) {
00089     // read the entry from the ini file.
00090     istring found;
00091     if (entry == "whole_section") {
00092       // they want the whole section back at them.
00093       string_table found;
00094       bool worked = ini.get_section(section, found);
00095       if (!worked) program_wide_logger().log(value);  // default.
00096       else {
00097         // generate answer as csv.
00098         istring temp;
00099         list_parsing::create_csv_line(found, temp);
00100         program_wide_logger().log(temp);  // real value read.
00101       }
00102     } else {
00103       bool worked = ini.get(section, entry, found);
00104       program_wide_logger().eol(log_base::NO_ENDING);
00105       if (!worked) program_wide_logger().log(value);  // default.
00106       else program_wide_logger().log(found);  // real value read.
00107     }
00108   } else {
00109     // write the entry to the ini file.
00110     bool worked = ini.put(section, entry, value);
00111     if (!worked) exit(28);  // failure to write the entry.
00112   }
00113 
00114   return 0;
00115 }
00116 
00117 HOOPLE_MAIN(ini_editor, )
00118 
00119 #ifdef __BUILD_STATIC_APPLICATION__
00120   // static dependencies found by buildor_gen_deps.sh:
00121   #include <basis/array.cpp>
00122   #include <basis/byte_array.cpp>
00123   #include <basis/callstack_tracker.cpp>
00124   #include <basis/chaos.cpp>
00125   #include <basis/convert_utf.cpp>
00126   #include <basis/definitions.cpp>
00127   #include <basis/earth_time.cpp>
00128   #include <basis/guards.cpp>
00129   #include <basis/istring.cpp>
00130   #include <basis/log_base.cpp>
00131   #include <basis/memory_checker.cpp>
00132   #include <basis/mutex.cpp>
00133   #include <basis/object_base.cpp>
00134   #include <basis/outcome.cpp>
00135   #include <basis/packable.cpp>
00136   #include <basis/portable.cpp>
00137   #include <basis/sequence.cpp>
00138   #include <basis/set.cpp>
00139   #include <basis/utility.cpp>
00140   #include <basis/version_record.cpp>
00141   #include <data_struct/amorph.cpp>
00142   #include <data_struct/bit_vector.cpp>
00143   #include <data_struct/byte_hasher.cpp>
00144   #include <data_struct/configurator.cpp>
00145   #include <data_struct/hash_table.cpp>
00146   #include <data_struct/pointer_hash.cpp>
00147   #include <data_struct/stack.cpp>
00148   #include <data_struct/static_memory_gremlin.cpp>
00149   #include <data_struct/string_hash.cpp>
00150   #include <data_struct/string_hasher.cpp>
00151   #include <data_struct/string_table.cpp>
00152   #include <data_struct/symbol_table.cpp>
00153   #include <data_struct/table_configurator.cpp>
00154   #include <loggers/console_logger.cpp>
00155   #include <loggers/file_logger.cpp>
00156   #include <loggers/locked_logger.cpp>
00157   #include <loggers/null_logger.cpp>
00158   #include <loggers/program_wide_logger.cpp>
00159   #include <opsystem/application_base.cpp>
00160   #include <opsystem/application_shell.cpp>
00161   #include <opsystem/byte_filer.cpp>
00162   #include <opsystem/command_line.cpp>
00163   #include <opsystem/critical_events.cpp>
00164   #include <opsystem/directory.cpp>
00165   #include <opsystem/filename.cpp>
00166   #include <opsystem/ini_config.cpp>
00167   #include <opsystem/ini_parser.cpp>
00168   #include <opsystem/path_configuration.cpp>
00169   #include <opsystem/rendezvous.cpp>
00170   #include <textual/byte_format.cpp>
00171   #include <textual/list_parsing.cpp>
00172   #include <textual/parser_bits.cpp>
00173   #include <textual/string_manipulation.cpp>
00174   #include <textual/tokenizer.cpp>
00175 #endif // __BUILD_STATIC_APPLICATION__
00176 

Generated on Fri Nov 28 04:28:51 2008 for HOOPLE Libraries by  doxygen 1.5.1