cmanip.cc

Go to the documentation of this file.
00001 /*
00002 Copyright (C) 2000-2001  The Exult Team
00003 
00004 This program is free software; you can redistribute it and/or
00005 modify it under the terms of the GNU General Public License
00006 as published by the Free Software Foundation; either version 2
00007 of the License, or (at your option) any later version.
00008 
00009 This program is distributed in the hope that it will be useful,
00010 but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 GNU General Public License for more details.
00013 
00014 You should have received a copy of the GNU General Public License
00015 along with this program; if not, write to the Free Software
00016 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00017 */
00018 
00019 #ifdef HAVE_CONFIG_H
00020 #  include <config.h>
00021 #endif
00022 
00023 
00024 #ifndef ALPHA_LINUX_CXX
00025 #  include <cstdio>
00026 #endif
00027 #include "Configuration.h"
00028 #include <iostream>
00029 #include <string>
00030 #include "exult_constants.h"
00031 #include <vector>
00032 #include <cassert>
00033 
00034 const std::string c_empty_string;
00035 
00036 using std::cout;
00037 using std::cerr;
00038 using std::endl;
00039 using std::vector;
00040 using std::string;
00041 using std::pair;
00042 
00043 enum DoOps { DoAdd, DoRem, DoGet };
00044 
00045 typedef pair<DoOps, vector<string> > DoListPair;
00046 
00047 typedef vector<DoListPair> DoList;
00048 
00049 DoList      dolist;
00050 Configuration *config= new Configuration();
00051 string      config_file_name;
00052 bool      verbose=false; // dump verbose output to cerr
00053 
00054 /* #include <cstd_usage_function> */
00055 void usage(unsigned int i)
00056 {
00057   cout << "cmanip - simple commandline, conf/ file manipulator" << endl
00058   #ifdef HAVE_CONFIG_H
00059        << "    compiled with " << PACKAGE << " " << VERSION << endl
00060   #endif
00061        << endl
00062        << "usage:" << endl
00063        << "\tcmanip <conffile> [option [parameters]] ..." << endl
00064        << endl
00065        << "options:" << endl
00066        << "\t[-a | add | create | mk | new | modify] <key> <value>" << endl
00067        << "\t\t\t- adds the <value> to the <key> in the <conffile>" << endl
00068        << "\t[-r | rem | remove | rm | del] <key>" << endl
00069        << "\t\t\t- removes the <key> from the <conffile>" << endl
00070        << "\t[-x | get | extract | rd | see | read] <key>" << endl
00071        << "\t\t\t- extracts the value set for <key> from the <conffile> or \"unknown\"" << endl
00072        << "\t[-v | verbose]\t- print verbose output to stderr" << endl
00073        << endl
00074        << "examples:" << endl
00075        << "\tcmanip exult.cfg add config/foo stuff rem config/bar" << endl
00076        << "\tcmanip exult.cfg new config/foo \"more stuff\" add config/bar useless" << endl
00077        << "\tcmanip exult.cfg -v -r config/foo del config/bar" << endl
00078        ;
00079   exit(i);
00080 }
00081 
00082 /* Turn the various command line parameters into operations that can be handled by process_ops */
00083 void read_params(const int argc, char *argv[])
00084 {
00085   config_file_name = argv[1];
00086   
00087   // "I'd appreciate your input"...
00088   for(unsigned int i=2; i<argc; i++)
00089   {
00090     string s(argv[i]);
00091     
00092     /* Adds the value (argv[i+2]) to the key (argv[i+1]) in the conf file. */
00093     if((s=="add") || (s=="create") || (s=="mk") || (s=="new") || (s=="-a") || (s=="modify"))
00094     {
00095       if(i+2>=argc)
00096       {
00097         cout << "error: insufficient parameters supplied for '" << s << "'" << endl;
00098         usage(1);
00099       }
00100       
00101       DoListPair dlp;
00102       dlp.first = DoAdd;
00103       dlp.second.push_back(argv[i+1]);
00104       dlp.second.push_back(argv[i+2]);
00105       dolist.push_back(dlp);
00106       i+=2;
00107     }
00108     /* Removes the key (argv[i+1]) from the conf file.
00109       FIXME: Currently only sets it to 'null', since there is no 'remove' ability
00110       in Configuration. */
00111     else if((s=="rem") || (s=="remove") || (s=="rm") || (s=="mk") || (s=="del") || (s=="-r"))
00112     {
00113       if(i+1>=argc)
00114       {
00115         cout << "error: insufficient parameters supplied for '" << s << "'" << endl;
00116         usage(1);
00117       }
00118       
00119       DoListPair dlp;
00120       dlp.first = DoRem;
00121       dlp.second.push_back(argv[i+1]);
00122       dolist.push_back(dlp);
00123       i++;
00124     }
00125     /* Added by Artaxerxes. Extracts the values for a given path. Do not change anything. Just read.*/
00126     else if((s=="extract") || (s=="read") || (s=="rd") || (s=="see") || (s=="get") || (s=="-x"))
00127     {
00128       if(i+1>=argc)
00129       {
00130         cout << "error: insufficient parameters supplied for '" << s << "'" << endl;
00131         usage(1);
00132       }
00133       
00134       DoListPair dlp;
00135       dlp.first = DoGet;
00136       dlp.second.push_back(argv[i+1]);
00137       dolist.push_back(dlp);
00138       i++;
00139     }
00140     /* Just turns on 'verbose' mode. Nothing of particular intrest. */
00141     else if((s=="-v") || (s=="verbose"))
00142       verbose=true;
00143     else
00144     {
00145       cout << "error: unknown parameter \"" << s << "\"" << endl;
00146     }
00147   }
00148 }
00149 
00150 /* Walk over the operations list (dolist) and execute each operation in the order
00151   it was listed on the command line */
00152 void process_ops()
00153 {
00154   for(DoList::iterator i=dolist.begin(); i!=dolist.end(); i++)
00155     {
00156     if(i->first==DoAdd)
00157     {
00158       assert(i->second.size()==2);
00159       if(verbose)
00160       {
00161         string s;
00162         assert(config!=0);
00163         config->value(i->second[0].c_str(),s,"---nil---");
00164         cerr << "Original value of " << i->second[0] << " was " << s << endl;
00165       
00166       }
00167       
00168       assert(config!=0);
00169       config->set(i->second[0].c_str(), i->second[1].c_str(), false);
00170       
00171       if(verbose)
00172         cerr << "Added " << i->second[1] << " to " << i->second[0] << endl;
00173     }
00174     if(i->first==DoRem)
00175     {
00176       assert(i->second.size()==1);
00177       if(verbose)
00178       {
00179         string s;
00180         assert(config!=0);
00181         config->value(i->second[0].c_str(),s,"---nil---");
00182         cerr << "Original value was " << i->second[0] << " was " << s << endl;
00183       
00184       }
00185       
00186       assert(config!=0);
00187       config->set(i->second[0].c_str(), "", false);
00188       
00189       if(verbose)
00190         cerr << "Removed " << i->second[0] << endl;
00191     }
00192     if(i->first==DoGet)
00193     {
00194       assert(i->second.size()==1);
00195       if(verbose)
00196       {
00197               string s;
00198         assert(config!=0);
00199         config->value(i->second[0].c_str(),s,"unknown");
00200         cerr << "Return value for " << i->second[0] << " is " << s << endl;
00201       
00202       }
00203       
00204       assert(config!=0);
00205       //config->set(i->second[0].c_str(), "", false);
00206       string s;
00207       config->value(i->second[0].c_str(),s,"unknown");
00208       cout << s << endl;
00209       
00210     }
00211     }
00212 }
00213 
00214 int main(int argc, char *argv[])
00215 {
00216   if(argc<3)
00217     usage(0);
00218   
00219   read_params(argc, argv);
00220   
00221   if(verbose)
00222   {
00223     cerr << "Operations:" << endl;
00224     for(DoList::iterator i=dolist.begin(); i!=dolist.end(); i++)
00225     {
00226       cerr << '\t' << ((i->first==DoAdd) ? "add" : ((i->first==DoRem) ? "rem" : ((i->first==DoGet) ? "get" : "unknown"))) << '\t';
00227       for(vector<string>::iterator j=i->second.begin(); j!=i->second.end(); j++)
00228         cerr << *j << '\t';
00229       cerr << endl;
00230     }
00231   }
00232   
00233   assert(config!=0);
00234   config->read_config_file(config_file_name.c_str());
00235   
00236   process_ops();
00237   
00238   assert(config!=0);
00239   config->write_back();
00240   
00241   return 0;
00242 }

Generated on Mon Jul 9 14:42:50 2007 for ExultEngine by  doxygen 1.5.1