main.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002   file : $URL: https://frepple.svn.sourceforge.net/svnroot/frepple/trunk/src/main.cpp $
00003   version : $LastChangedRevision: 599 $  $LastChangedBy: jdetaeye $
00004   date : $LastChangedDate: 2007-12-08 15:02:52 +0100 (Sat, 08 Dec 2007) $
00005  ***************************************************************************/
00006 
00007 /***************************************************************************
00008  *                                                                         *
00009  * Copyright (C) 2007 by Johan De Taeye                                    *
00010  *                                                                         *
00011  * This library is free software; you can redistribute it and/or modify it *
00012  * under the terms of the GNU Lesser General Public License as published   *
00013  * by the Free Software Foundation; either version 2.1 of the License, or  *
00014  * (at your option) any later version.                                     *
00015  *                                                                         *
00016  * This library is distributed in the hope that it will be useful,         *
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of          *
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser *
00019  * General Public License for more details.                                *
00020  *                                                                         *
00021  * You should have received a copy of the GNU Lesser General Public        *
00022  * License along with this library; if not, write to the Free Software     *
00023  * Foundation Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA *
00024  *                                                                         *
00025  ***************************************************************************/
00026 
00027 
00028 #include "freppleinterface.h"
00029 #include <iostream>
00030 #include <sstream>
00031 using namespace std;
00032 
00033 
00034 void usage()
00035 {
00036   cout << "\nFrepple v" << FreppleVersion() << "command line application\n"
00037     "\nUsage:\n"
00038     "  frepple [options] [files | directories]\n"
00039     "\nThis program reads xml input data, and executes the modeling and\n"
00040     "planning commands included in them.\n"
00041     "The xml input can be provided in the following ways:\n"
00042     "  - Passing one or more XML files and/or directories as arguments.\n"
00043     "    When a directory is specified, the application will process\n"
00044     "    all files with the extension '.xml'.\n"
00045     "  - When passing no file or directory arguments, input will be read\n"
00046     "    from the standard input. XML data can be piped to the application.\n"
00047     "\nOptions:\n"
00048     "  -validate -v  Validate the xml input for correctness.\n"
00049     "  -check -c     Only validate the input, without executing the content.\n"
00050     "  -? -h -help   Show these instructions.\n"
00051     "\nEnvironment: The variable FREPPLE_HOME is required to point to a\n"
00052     "               directory. The application will use the files init.xml,\n"
00053     "               frepple.xsd and module libraries from this directory.\n"
00054     "\nReturn codes: 0 when succesfull, non-zero in case of errors\n"
00055     "\nMore information on this program: http://frepple.sourceforge.net\n\n"
00056     << endl;
00057 }
00058 
00059 
00060 int main (int argc, char *argv[])
00061 {
00062 
00063   // Storing the chosen options...
00064   bool validate = false;
00065   bool validate_only = false;
00066   bool input = false;
00067 
00068   try
00069   {
00070     // Analyze the command line arguments.
00071     for (int i = 1; i < argc; ++i)
00072     {
00073       if (argv[i][0] == '-')
00074       {
00075         // An option on the command line
00076         if (!strcmp(argv[i],"-validate") || !strcmp(argv[i],"-v"))
00077           validate = true;
00078         else if (!strcmp(argv[i],"-check") || !strcmp(argv[i],"-c"))
00079           validate_only = true;
00080         else
00081         {
00082           if (strcmp(argv[i],"-?")
00083               && strcmp(argv[i],"-h")
00084               && strcmp(argv[i],"-help"))
00085             cout << "\nError: Option '" << argv[i]
00086             << "' not recognized." << endl;
00087           usage();
00088           return EXIT_FAILURE;
00089         }
00090       }
00091       else
00092       {
00093         // A file or directory name on the command line
00094         if (!input)
00095         {
00096           // Initialize the library if this wasn't done before
00097           FreppleInitialize(NULL);
00098           input = true;
00099         }
00100         FreppleReadXMLFile(argv[i], validate, validate_only);
00101       }
00102     }
00103 
00104     // When no filenames are specified, we read the standard input
00105     if (!input)
00106     {
00107       FreppleInitialize(NULL);
00108       FreppleReadXMLFile(NULL, validate, validate_only);
00109     }
00110   }
00111   catch (exception& e)
00112   {
00113     ostringstream ch;
00114     ch << "Error: " << e.what();
00115     FreppleLog(ch.str());
00116     return EXIT_FAILURE;
00117   }
00118   catch (...)
00119   {
00120     FreppleLog("Error: Unknown exception type");
00121     return EXIT_FAILURE;
00122   }
00123   return EXIT_SUCCESS;
00124 }

Documentation generated by  doxygen