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: 1505 $ $LastChangedBy: jdetaeye $ 00004 date : $LastChangedDate: 2011-08-26 18:55:08 +0200 (Fri, 26 Aug 2011) $ 00005 ***************************************************************************/ 00006 00007 /*************************************************************************** 00008 * * 00009 * Copyright (C) 2007-2011 by Johan De Taeye, frePPLe bvba * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 * 00024 * USA * 00025 * * 00026 ***************************************************************************/ 00027 00028 #include "freppleinterface.h" 00029 #include <iostream> 00030 #include <sstream> 00031 #include <cstring> 00032 #include <cstdlib> 00033 using namespace std; 00034 00035 00036 void usage() 00037 { 00038 cout << "\nfrePPLe v" << FreppleVersion() << " command line application\n" 00039 "\nUsage:\n" 00040 " frepple [options] [files | directories]\n" 00041 "\nThis program reads XML input data, and executes the modeling and\n" 00042 "planning commands included in them.\n" 00043 "The XML input can be provided in the following ways:\n" 00044 " - Passing one or more XML files and/or directories as arguments.\n" 00045 " When a directory is specified, the application will process\n" 00046 " all files with the extension '.xml'.\n" 00047 " - When passing no file or directory arguments, input will be read\n" 00048 " from the standard input. XML data can be piped to the application.\n" 00049 "\nOptions:\n" 00050 " -validate -v Validate the XML input for correctness.\n" 00051 " -check -c Only validate the input, without executing the content.\n" 00052 " -? -h -help Show these instructions.\n" 00053 "\nEnvironment: The variable FREPPLE_HOME optionally points to a\n" 00054 " directory where the initialization files init.xml, init.py,\n" 00055 " frepple.xsd and module libraries will be searched.\n" 00056 "\nReturn codes: 0 when successful, non-zero in case of errors\n" 00057 "\nMore information on this program: http://www.frepple.com\n\n" 00058 << endl; 00059 } 00060 00061 00062 int main (int argc, char *argv[]) 00063 { 00064 00065 // Storing the chosen options... 00066 bool validate = false; 00067 bool validate_only = false; 00068 bool input = false; 00069 00070 try 00071 { 00072 // Analyze the command line arguments. 00073 for (int i = 1; i < argc; ++i) 00074 { 00075 if (argv[i][0] == '-') 00076 { 00077 // An option on the command line 00078 if (!strcmp(argv[i],"-validate") || !strcmp(argv[i],"-v")) 00079 validate = true; 00080 else if (!strcmp(argv[i],"-check") || !strcmp(argv[i],"-c")) 00081 validate_only = true; 00082 else 00083 { 00084 if (strcmp(argv[i],"-?") 00085 && strcmp(argv[i],"-h") 00086 && strcmp(argv[i],"-help")) 00087 cout << "\nError: Option '" << argv[i] 00088 << "' not recognized." << endl; 00089 usage(); 00090 return EXIT_FAILURE; 00091 } 00092 } 00093 else 00094 { 00095 // A file or directory name on the command line 00096 if (!input) 00097 { 00098 // Initialize the library if this wasn't done before 00099 FreppleInitialize(); 00100 input = true; 00101 } 00102 if (strlen(argv[i])>=3 && !strcmp(argv[i]+strlen(argv[i])-3,".py")) 00103 // Execute as Python file 00104 FreppleReadPythonFile(argv[i]); 00105 else 00106 // Execute as XML file 00107 FreppleReadXMLFile(argv[i], validate, validate_only); 00108 } 00109 } 00110 00111 // When no filenames are specified, we read the standard input 00112 if (!input) 00113 { 00114 FreppleInitialize(); 00115 FreppleReadXMLFile(NULL, validate, validate_only); 00116 } 00117 } 00118 catch (exception& e) 00119 { 00120 ostringstream ch; 00121 ch << "Error: " << e.what(); 00122 FreppleLog(ch.str()); 00123 return EXIT_FAILURE; 00124 } 00125 catch (...) 00126 { 00127 FreppleLog("Error: Unknown exception type"); 00128 return EXIT_FAILURE; 00129 } 00130 return EXIT_SUCCESS; 00131 }