# make_help.cpp

    00001 // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
    00002
    00003 //-----------------------------------------------------------------------------
    00004 // make_help.h
    00005 // (c) Maarten Keijzer, Marc Schoenauer and GeNeura Team, 2001
    00006 /*
    00007     This library is free software; you can redistribute it and/or
    00008     modify it under the terms of the GNU Lesser General Public
    00009     License as published by the Free Software Foundation; either
    00010     version 2 of the License, or (at your option) any later version.
    00011
    00012     This library is distributed in the hope that it will be useful,
    00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
    00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00015     Lesser General Public License for more details.
    00016
    00017     You should have received a copy of the GNU Lesser General Public
    00018     License along with this library; if not, write to the Free Software
    00019     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    00020
    00021     Contact: todos@geneura.ugr.es, http://geneura.ugr.es
    00022              Marc.Schoenauer@polytechnique.fr
    00023              mkeijzer@dhi.dk
    00024  */
    00025 //-----------------------------------------------------------------------------
    00026 #ifdef _MSC_VER
    00027 // to avoid long name warnings
    00028 #pragma warning(disable:4786)
    00029 #endif
    00030
    00031 #include <utils/eoParser.h>
    00032 #include <fstream>
    00033 #include <stdexcept>
    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       system(s.c_str());
    00094       return true;
    00095     }
    00096   //  else
    00097   if (_erase)                      // OK to erase
    00098     {
    00099       s = string("/bin/rm ")+ _dirName + "/*";
    00100       system(s.c_str());
    00101       return true;
    00102     }
    00103   //else
    00104   // WARNING: bug if dir exists and is empty; this says it is not!
    00105   // shoudl use scandir instead - no time now :-(((    MS Aug. 01
    00106   s = "Dir " + _dirName + " is not empty";
    00107   throw runtime_error(s);
    00108   return true;
    00109 }