F:/KPlato/koffice/libs/kross/console/main.cpp

Aller à la documentation de ce fichier.
00001 /***************************************************************************
00002  * main.cpp
00003  * This file is part of the KDE project
00004  * copyright (C)2006 by Sebastian Sauer (mail@dipe.org)
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Library General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2 of the License, or (at your option) any later version.
00010  * This program 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  * Library General Public License for more details.
00014  * You should have received a copy of the GNU Library General Public License
00015  * along with this program; see the file COPYING.  If not, write to
00016  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018  ***************************************************************************/
00019 
00020 // for std namespace
00021 #include <string>
00022 #include <iostream>
00023 
00024 // Qt
00025 #include <QString>
00026 #include <QFile>
00027 
00028 // KDE
00029 #include <kinstance.h>
00030 #include <kapplication.h>
00031 #include <kcmdlineargs.h>
00032 #include <kaboutdata.h>
00033 #include <ksharedptr.h>
00034 #include <kurl.h>
00035 
00036 // Kross
00037 #include "../core/manager.h"
00038 #include "../core/action.h"
00039 #include "../core/interpreter.h"
00040 
00041 #define ERROR_OK 0
00042 #define ERROR_HELP -1
00043 #define ERROR_NOSUCHFILE -2
00044 #define ERROR_OPENFAILED -3
00045 #define ERROR_NOINTERPRETER -4
00046 #define ERROR_EXCEPTION -6
00047 
00048 KApplication* app = 0;
00049 
00050 int runScriptFile(const QString& scriptfile)
00051 {
00052     // Read the scriptfile
00053     QFile f(QFile::encodeName(scriptfile));
00054     if(! f.exists()) {
00055         std::cerr << "No such scriptfile: " << scriptfile.toLatin1().data() << std::endl;
00056         return ERROR_NOSUCHFILE;
00057     }
00058     if(! f.open(QIODevice::ReadOnly)) {
00059         std::cerr << "Failed to open scriptfile: " << scriptfile.toLatin1().data() << std::endl;
00060         return ERROR_OPENFAILED;
00061     }
00062     QString scriptcode = f.readAll();
00063     f.close();
00064 
00065     // Determinate the matching interpreter
00066     Kross::InterpreterInfo* interpreterinfo = Kross::Manager::self().interpreterInfo( Kross::Manager::self().interpreternameForFile(scriptfile) );
00067     if(! interpreterinfo) {
00068         std::cerr << "No interpreter for file: " << scriptfile.toLatin1().data() << std::endl;
00069         return ERROR_NOINTERPRETER;
00070     }
00071 
00072     // First we need a Action and fill it.
00073     Kross::Action* action = new Kross::Action( scriptfile );
00074     action->setInterpreter( interpreterinfo->interpreterName() );
00075     action->setCode(scriptcode);
00076 
00077     // Now execute the Action.
00078     action->trigger();
00079 
00080     if(action->hadError()) {
00081         // We had an exception.
00082         std::cerr << QString("%2\n%1").arg(action->errorTrace()).arg(action->errorMessage()).toLatin1().data() << std::endl;
00083         delete action;
00084         return ERROR_EXCEPTION;
00085     }
00086 
00087     delete action;
00088     return ERROR_OK;
00089 }
00090 
00091 int main(int argc, char **argv)
00092 {
00093     int result = ERROR_OK;
00094 
00095     KAboutData about("kross",
00096                      "kross",
00097                      "0.1",
00098                      "KDE application to run Kross scripts.",
00099                      KAboutData::License_LGPL,
00100                      "(C) 2006 Sebastian Sauer",
00101                      "Run Kross scripts.",
00102                      "http://kross.dipe.org",
00103                      "kross@dipe.org");
00104     about.addAuthor("Sebastian Sauer", "Author", "mail@dipe.org");
00105 
00106     // Initialize command line args
00107     KCmdLineArgs::init(argc, argv, &about);
00108     // Tell which options are supported and parse them.
00109     static KCmdLineOptions options[] = {
00110         { "+file", "Scriptfile", 0 },
00111         KCmdLineLastOption
00112     };
00113     KCmdLineArgs::addCmdLineOptions(options);
00114     KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
00115 
00116     // If no options are defined.
00117     if(args->count() < 1) {
00118         std::cout << "Syntax: " << KCmdLineArgs::appName() << " scriptfile1 [scriptfile2] [scriptfile3] ..." << std::endl;
00119         return ERROR_HELP;
00120     }
00121 
00122     // Create KApplication instance.
00123     app = new KApplication( /* GUIenabled */ true );
00124 
00125     // Each argument is a scriptfile to open
00126     for(int i = 0; i < args->count(); i++) {
00127         result = runScriptFile(QFile::decodeName(args->arg(i)));
00128         if(result != ERROR_OK)
00129             break;
00130     }
00131 
00132     // Free the KApplication instance and exit the program.
00133     delete app;
00134     return result;
00135 }

Généré le Wed Nov 22 23:41:07 2006 pour KPlato par  doxygen 1.5.1-p1