EvolvingObjects
make_help.cpp
00001 //-----------------------------------------------------------------------------
00002 // make_help.h
00003 // (c) Maarten Keijzer, Marc Schoenauer and GeNeura Team, 2001
00004 /*
00005     This library is free software; you can redistribute it and/or
00006     modify it under the terms of the GNU Lesser General Public
00007     License as published by the Free Software Foundation; either
00008     version 2 of the License, or (at your option) any later version.
00009 
00010     This library is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013     Lesser General Public License for more details.
00014 
00015     You should have received a copy of the GNU Lesser General Public
00016     License along with this library; if not, write to the Free Software
00017     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 
00019     Contact: todos@geneura.ugr.es, http://geneura.ugr.es
00020              Marc.Schoenauer@polytechnique.fr
00021              mkeijzer@dhi.dk
00022  */
00023 //-----------------------------------------------------------------------------
00024 #ifdef _MSC_VER
00025 // to avoid long name warnings
00026 #pragma warning(disable:4786)
00027 #endif
00028 
00029 #include <cstdlib>
00030 #include <fstream>
00031 #include <stdexcept>
00032 
00033 #include <utils/eoParser.h>
00034 
00035 using namespace std;
00036 
00047 void make_help(eoParser & _parser)
00048 {
00049     // name of the "status" file where all actual parameter values will be saved
00050     string str_status = _parser.ProgramName() + ".status"; // default value
00051     eoValueParam<string>& statusParam = _parser.createParam(str_status, "status","Status file",'\0', "Persistence" );
00052 
00053     // dump status file BEFORE help, so the user gets a chance to use it:
00054     // it's probably the case where she/he needs it most!!!
00055     // Only help parameter will not be in status file - but who cares???
00056     if (statusParam.value() != "")
00057       {
00058         ofstream os(statusParam.value().c_str());
00059         os << _parser;          // and you can use that file as parameter file
00060       }
00061    // do the following AFTER ALL PARAMETERS HAVE BEEN PROCESSED
00062    // i.e. in case you need parameters somewhere else, postpone these
00063     if (_parser.userNeedsHelp())
00064       {
00065         _parser.printHelp(cout);
00066         cout << "You can use an edited copy of file " << statusParam.value()
00067              << " as parameter file" << endl;
00068         exit(1);
00069       }
00070 }
00071 
00079 bool testDirRes(std::string _dirName, bool _erase=true)
00080 {
00081   string s = "test -d " + _dirName;
00082   int res = system(s.c_str());
00083   // test for (unlikely) errors
00084   if ( (res==-1) || (res==127) )
00085     {
00086       s = "Problem executing test of dir " + _dirName;
00087       throw runtime_error(s);
00088     }
00089   // now make sure there is a dir without any file in it - or quit
00090   if (res)                    // no dir present
00091     {
00092       s = string("mkdir ")+ _dirName;
00093       int res = system(s.c_str());
00094       (void)res;
00095       return true;
00096     }
00097   //  else
00098   if (_erase)                      // OK to erase
00099     {
00100       s = string("/bin/rm ")+ _dirName + "/*";
00101       int res = system(s.c_str());
00102       (void)res;
00103       return true;
00104     }
00105   //else
00106   // WARNING: bug if dir exists and is empty; this says it is not!
00107   // shoudl use scandir instead - no time now :-(((    MS Aug. 01
00108   s = "Dir " + _dirName + " is not empty";
00109   throw runtime_error(s);
00110   return true;
00111 }
00112 
00113 
00114 // Local Variables:
00115 // coding: iso-8859-1
00116 // mode: C++
00117 // c-file-offsets: ((c . 0))
00118 // c-file-style: "Stroustrup"
00119 // fill-column: 80
00120 // End:
 All Classes Namespaces Files Functions Variables Typedefs Friends