Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

sfw_loader.C

00001 /* $Id: sfw_loader.C,v 1.2 2000/11/03 22:03:28 rpav Exp $
00002  *
00003  * SFW - Studio FrameWork
00004  * Copyright (C) 2000  Ryan Pavlik
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  * 
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00019  *
00020  */
00021 
00022 /*
00023  * Loader program for SFW applications
00024  *
00025  */ 
00026 
00027 #include <iostream>
00028 #include <fstream>
00029 #include <cstdlib>
00030 #include <cstdio>
00031 #include <string>
00032 #include <SFW/Application.H>
00033 #include <SFW/GC.H>
00034 #include <SFW/Log.H>
00035 #include <SFW/PluginManager.H>
00036 #include <SFW/Plugin.H>
00037 #include <SFW/Var.H>
00038 
00039 using namespace SFW;
00040 
00041 void initPlugins(string filename) {
00042     /* This is very simple, just line-by-line paths to plugins */
00043     ifstream ifile(filename.c_str());
00044 
00045     if(!ifile) {
00046     Log(L_FATAL, "sfw_loader: No such file or directory: %s", filename.c_str());
00047     exit(1);
00048     }
00049     
00050     string       pluginPath;
00051     list<string> plugins; 
00052     
00053     while(ifile >> pluginPath, !ifile.eof()) {
00054     /* Fix weird paths */
00055     string::size_type i;
00056     
00057     while((i = pluginPath.find("//")) != string::npos)
00058         pluginPath.erase(i,1);
00059 
00060     plugins.push_back(pluginPath);
00061     }
00062 
00063     list<string>::iterator i = plugins.begin();
00064 
00065     while(i != plugins.end()) {
00066     Plugin *p   = new Plugin(*i);
00067         Plugin *tmp = PM.get(p);
00068 
00069         if(tmp) {
00070             delete p;
00071             p = tmp;
00072         }
00073     
00074     Log(L_DBG0, "Loading '%s' type: %s", p->getIDString().c_str(),
00075         p->getType().c_str());
00076     p->load();
00077 
00078         if(!tmp)
00079             PM.add(p);
00080     
00081     i++;
00082     }
00083 
00084     ifile.close();
00085 }
00086 
00087 namespace SFW {
00088     /* [start log: #sawmill, 4/17/2000]
00089      * <oGMo> if you specify a friend function, i wonder if it has to exist ;)
00090      * <rahul> C++'s OO is sick
00091      * <oGMo> wow i guess it works
00092      * <rahul> you can have a friend that doesn't exist?
00093      * <oGMo> rahul: guess so
00094      * <rahul> cool... imaginary friends...
00095      * [end log]
00096      */
00097     bool parseArgs(Application *_app, int _argc, char *_argv[],
00098            ArgMap &_argm, ArgList &_argl) {
00099     int opt_idx = 0;
00100     int c;
00101 
00102     if(!_app->argSpecLong && !_app->argSpecShort)
00103         return true;
00104 
00105     for(;;) {
00106         c = getopt_long(_argc, _argv, _app->argSpecShort,
00107                 _app->argSpecLong, &opt_idx);
00108 
00109         if(c == -1)
00110         break;
00111 
00112         if(c == '?') {
00113         _app->usage();
00114         return false;
00115         }
00116 
00117         string optstr;
00118 
00119         if(optarg)
00120         optstr = optarg;
00121 
00122         /* If we have a long option */
00123         if(!c)
00124         _argm[_app->argSpecLong[opt_idx].name] = optstr;
00125         else {
00126         char buf[] = { c, 0 };
00127         _argm[buf] = optstr;
00128         }
00129     }
00130 
00131     /* Remember, the first argument is actuall the .app file,
00132      * so skip it */
00133     for(optind++; optind < _argc; optind++)
00134         _argl.push_back(_argv[optind]);
00135 
00136     return true;
00137     }
00138 }
00139 
00140 void usage() {
00141     cerr << "usage: sfw_loader specfile.app [arg1 .. argn]" << endl;
00142 }
00143 
00144 int main(int _argc, char *_argv[]) {
00145     string  specfile;
00146     int     retcode;
00147     
00148     PluginSet           *ps     = 0;
00149     PluginSet::iterator  i;
00150     int                  pCount = 0;
00151     Plugin              *app_p  = 0;
00152     Application         *app    = 0;
00153     ArgMap               argm;
00154     ArgList              argl;
00155 
00156     /* strto* and ato* suck. */
00157     char *dbgStr = getenv("SFW_DEBUG_LEVEL");
00158     int   dbgLev;
00159 
00160     if(dbgStr) {
00161     if(sscanf(dbgStr, "%d", &dbgLev) != 1)
00162         dbgLev = L_DBG0;
00163     } else
00164     dbgLev = L_DBG0;
00165     
00166     Log.setLevel((LOG_LEVEL)dbgLev);
00167 
00168     char *pluginPath = getenv("SFW_PLUGIN_PATH");
00169     if(pluginPath)
00170     PM.addPath(string(pluginPath));
00171         
00172     if(_argc < 2) {
00173     usage();
00174     goto error;
00175     } else {
00176     specfile = _argv[1];
00177     }
00178 
00179     initPlugins(specfile);
00180     
00181     ps = PM.getSet("SFW::Application");
00182     i  = ps->begin();
00183 
00184     while(i != ps->end()) {
00185     Plugin *p = *i;
00186 
00187     if(p->isLoaded()) {
00188         pCount++;
00189         app_p = p;
00190     }
00191 
00192     i++;
00193     }
00194 
00195     if(!pCount) {
00196     Log(L_FATAL, "sfw_loader: No plugin of type SFW::Application found in %s",
00197         _argv[1]);
00198     goto error;
00199     } else if (pCount > 1) {
00200     Log(L_FATAL, "sfw_loader: Multiple SFW::Application plugins found in %s",
00201         _argv[1]);
00202     goto error;
00203     }
00204 
00205     app = (Application*)app_p->makeObject();
00206 
00207     GC.done(ps); ps = 0;
00208 
00209     if(!app) {
00210     Log(L_FATAL, "sfw_loader: Application plugin lied about its type, or\n"
00211         "is broken and not producing objects.");
00212     goto error;
00213     }
00214 
00215     strcpy(_argv[0], app->getType().c_str());
00216     if(!parseArgs(app, _argc, _argv, argm, argl))
00217     goto error;
00218     
00219     retcode = app->main(argm, argl);
00220 
00221     GC.finalize();
00222     return retcode;
00223 
00224  error:
00225     if(ps)
00226     GC.done(ps);
00227 
00228     GC.finalize();
00229     return EXIT_FAILURE;
00230 }

Generated at Tue Jan 2 15:38:34 2001 for SFW by doxygen1.2.4 written by Dimitri van Heesch, © 1997-2000