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

swmgr.cpp

00001 /******************************************************************************
00002  *  swmgr.cpp   - implementaion of class SWMgr used to interact with an install
00003  *                              base of sword modules.
00004  *
00005  * $Id: swmgr_8cpp-source.html,v 1.3 2002/06/20 20:23:10 mgruner Exp $
00006  *
00007  * Copyright 1998 CrossWire Bible Society (http://www.crosswire.org)
00008  *      CrossWire Bible Society
00009  *      P. O. Box 2528
00010  *      Tempe, AZ  85280-2528
00011  *
00012  * This program is free software; you can redistribute it and/or modify it
00013  * under the terms of the GNU General Public License as published by the
00014  * Free Software Foundation version 2.
00015  *
00016  * This program is distributed in the hope that it will be useful, but
00017  * WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019  * General Public License for more details.
00020  *
00021  */
00022 
00023 #include <stdio.h>
00024 #include <stdlib.h>
00025 #include <fcntl.h>
00026 
00027 #ifndef __GNUC__
00028 #include <io.h>
00029 #else
00030 #include <unistd.h>
00031 #include <unixstr.h>
00032 #endif
00033 #include <sys/stat.h>
00034 #ifndef _MSC_VER
00035 #include <iostream.h>
00036 #endif
00037 #include <dirent.h>
00038 
00039 #include <swmgr.h>
00040 #include <rawtext.h>
00041 #include <rawgenbook.h>
00042 #include <rawcom.h>
00043 #include <hrefcom.h>
00044 #include <rawld.h>
00045 #include <rawld4.h>
00046 #include <utilfuns.h>
00047 #include <gbfplain.h>
00048 #include <thmlplain.h>
00049 #include <gbfstrongs.h>
00050 #include <gbffootnotes.h>
00051 #include <gbfheadings.h>
00052 #include <gbfmorph.h>
00053 #include <thmlstrongs.h>
00054 #include <thmlfootnotes.h>
00055 #include <thmlheadings.h>
00056 #include <thmlmorph.h>
00057 #include <thmllemma.h>
00058 #include <thmlscripref.h>
00059 #include <cipherfil.h>
00060 #include <rawfiles.h>
00061 #include <ztext.h>
00062 #include <zld.h>
00063 #include <zcom.h>
00064 #include <lzsscomprs.h>
00065 #include <utf8greekaccents.h>
00066 #include <utf8cantillation.h>
00067 #include <utf8hebrewpoints.h>
00068 #include <greeklexattribs.h>
00069 #include <swfiltermgr.h>
00070 
00071 
00072 
00073 #ifdef ICU
00074 #include <utf8transliterator.h>
00075 #endif
00076 
00077 #ifndef EXCLUDEZLIB
00078 #include <zipcomprs.h>
00079 #endif
00080 
00081 bool SWMgr::debug = false;
00082 
00083 #ifdef GLOBCONFPATH
00084 const char *SWMgr::globalConfPath = GLOBCONFPATH;
00085 #else
00086 const char *SWMgr::globalConfPath = "/etc/sword.conf:/usr/local/etc/sword.conf";
00087 #endif
00088 
00089 void SWMgr::init() {
00090         SWFilter *tmpFilter = 0;
00091         configPath  = 0;
00092         prefixPath  = 0;
00093         configType  = 0;
00094         myconfig    = 0;
00095         mysysconfig = 0;
00096         homeConfig = 0;
00097 
00098 
00099         cipherFilters.clear();
00100         optionFilters.clear();
00101         cleanupFilters.clear();
00102 
00103         tmpFilter = new GBFStrongs();
00104         optionFilters.insert(FilterMap::value_type("GBFStrongs", tmpFilter));
00105         cleanupFilters.push_back(tmpFilter);
00106 
00107         tmpFilter = new GBFFootnotes();
00108         optionFilters.insert(FilterMap::value_type("GBFFootnotes", tmpFilter));
00109         cleanupFilters.push_back(tmpFilter);
00110 
00111         tmpFilter = new GBFMorph();
00112         optionFilters.insert(FilterMap::value_type("GBFMorph", tmpFilter));
00113         cleanupFilters.push_back(tmpFilter);
00114 
00115         tmpFilter = new GBFHeadings();
00116         optionFilters.insert(FilterMap::value_type("GBFHeadings", tmpFilter));
00117         cleanupFilters.push_back(tmpFilter);
00118 
00119         tmpFilter = new ThMLStrongs();
00120         optionFilters.insert(FilterMap::value_type("ThMLStrongs", tmpFilter));
00121         cleanupFilters.push_back(tmpFilter);
00122 
00123         tmpFilter = new ThMLFootnotes();
00124         optionFilters.insert(FilterMap::value_type("ThMLFootnotes", tmpFilter));
00125         cleanupFilters.push_back(tmpFilter);
00126 
00127         tmpFilter = new ThMLMorph();
00128         optionFilters.insert(FilterMap::value_type("ThMLMorph", tmpFilter));
00129         cleanupFilters.push_back(tmpFilter);
00130 
00131         tmpFilter = new ThMLHeadings();
00132         optionFilters.insert(FilterMap::value_type("ThMLHeadings", tmpFilter));
00133         cleanupFilters.push_back(tmpFilter);
00134 
00135         tmpFilter = new ThMLLemma();
00136         optionFilters.insert(FilterMap::value_type("ThMLLemma", tmpFilter));
00137         cleanupFilters.push_back(tmpFilter);
00138 
00139         tmpFilter = new ThMLScripref();
00140         optionFilters.insert(FilterMap::value_type("ThMLScripref", tmpFilter));
00141         cleanupFilters.push_back(tmpFilter);
00142 
00143         tmpFilter = new UTF8GreekAccents();
00144         optionFilters.insert(FilterMap::value_type("UTF8GreekAccents", tmpFilter));
00145         cleanupFilters.push_back(tmpFilter);
00146 
00147         tmpFilter = new UTF8HebrewPoints();
00148         optionFilters.insert(FilterMap::value_type("UTF8HebrewPoints", tmpFilter));
00149         cleanupFilters.push_back(tmpFilter);
00150 
00151         tmpFilter = new UTF8Cantillation();
00152         optionFilters.insert(FilterMap::value_type("UTF8Cantillation", tmpFilter));
00153         cleanupFilters.push_back(tmpFilter);
00154 
00155         tmpFilter = new GreekLexAttribs();
00156         optionFilters.insert(FilterMap::value_type("GreekLexAttribs", tmpFilter));
00157         cleanupFilters.push_back(tmpFilter);
00158 
00159 /* UTF8Transliterator needs to be handled differently because it should always available as an option, for all modules
00160 #ifdef ICU
00161         tmpFilter = new UTF8Transliterator();
00162         optionFilters.insert(FilterMap::value_type("UTF8Transliterator", tmpFilter));
00163         cleanupFilters.push_back(tmpFilter);
00164 #endif
00165 */
00166         gbfplain = new GBFPlain();
00167         cleanupFilters.push_back(gbfplain);
00168 
00169         thmlplain = new ThMLPlain();
00170         cleanupFilters.push_back(thmlplain);
00171 }
00172 
00173 
00174 SWMgr::SWMgr(SWFilterMgr *filterMgr) {
00175         commonInit(0, 0, true, filterMgr);
00176 }
00177 
00178 
00179 SWMgr::SWMgr(SWConfig *iconfig, SWConfig *isysconfig, bool autoload, SWFilterMgr *filterMgr) {
00180         commonInit(iconfig, isysconfig, autoload, filterMgr);
00181 }
00182 
00183 
00184 void SWMgr::commonInit(SWConfig * iconfig, SWConfig * isysconfig, bool autoload, SWFilterMgr *filterMgr) {
00185         this->filterMgr = filterMgr;
00186         if (filterMgr)
00187                 filterMgr->setParentMgr(this);
00188 
00189         init();
00190         
00191         if (iconfig) {
00192                 config   = iconfig;
00193                 myconfig = 0;
00194         }
00195         else config = 0;
00196         if (isysconfig) {
00197                 sysconfig   = isysconfig;
00198                 mysysconfig = 0;
00199         }
00200         else sysconfig = 0;
00201 
00202         if (autoload)
00203                 Load();
00204 }
00205 
00206 
00207 SWMgr::SWMgr(const char *iConfigPath, bool autoload, SWFilterMgr *filterMgr) {
00208 
00209         string path;
00210         
00211         this->filterMgr = filterMgr;
00212         if (filterMgr)
00213                 filterMgr->setParentMgr(this);
00214 
00215         init();
00216         
00217         path = iConfigPath;
00218         if ((iConfigPath[strlen(iConfigPath)-1] != '\\') && (iConfigPath[strlen(iConfigPath)-1] != '/'))
00219                 path += "/";
00220         if (FileMgr::existsFile(path.c_str(), "mods.conf")) {
00221                 stdstr(&prefixPath, path.c_str());
00222                 path += "mods.conf";
00223                 stdstr(&configPath, path.c_str());
00224         }
00225         else {
00226                 if (FileMgr::existsDir(path.c_str(), "mods.d")) {
00227                         stdstr(&prefixPath, path.c_str());
00228                         path += "mods.d";
00229                         stdstr(&configPath, path.c_str());
00230                         configType = 1;
00231                 }
00232         }
00233 
00234         config = 0;
00235         sysconfig = 0;
00236 
00237         if (autoload && configPath)
00238                 Load();
00239 }
00240 
00241 
00242 SWMgr::~SWMgr() {
00243 
00244         DeleteMods();
00245 
00246         for (FilterList::iterator it = cleanupFilters.begin(); it != cleanupFilters.end(); it++)
00247                 delete (*it);
00248                         
00249         if (homeConfig)
00250                 delete homeConfig;
00251 
00252         if (myconfig)
00253                 delete myconfig;
00254 
00255         if (prefixPath)
00256                 delete [] prefixPath;
00257 
00258         if (configPath)
00259                 delete [] configPath;
00260 
00261         if (filterMgr)
00262                 delete filterMgr;
00263 }
00264 
00265 
00266 void SWMgr::findConfig(char *configType, char **prefixPath, char **configPath) {
00267         string path;
00268         ConfigEntMap::iterator entry;
00269 
00270         char *envsworddir = getenv ("SWORD_PATH");
00271         char *envhomedir  = getenv ("HOME");
00272 
00273         *configType = 0;
00274 
00275 #ifndef _MSC_VER
00276         // check working directory
00277 if (debug)
00278         cerr << "Checking working directory for mods.conf...";
00279 #endif
00280 
00281         if (FileMgr::existsFile(".", "mods.conf")) {
00282 
00283 #ifndef _MSC_VER
00284 if (debug)
00285         cerr << "found\n";
00286 #endif
00287 
00288                 stdstr(prefixPath, "./");
00289                 stdstr(configPath, "./mods.conf");
00290                 return;
00291         }
00292 
00293 #ifndef _MSC_VER
00294 if (debug)
00295         cerr << "\nChecking working directory for mods.d...";
00296 #endif
00297 
00298         if (FileMgr::existsDir(".", "mods.d")) {
00299 
00300 #ifndef _MSC_VER
00301 if (debug)
00302         cerr << "found\n";
00303 #endif
00304 
00305                 stdstr(prefixPath, "./");
00306                 stdstr(configPath, "./mods.d");
00307                 *configType = 1;
00308                 return;
00309         }
00310 
00311 
00312         // check environment variable SWORD_PATH
00313 #ifndef _MSC_VER
00314 if (debug)
00315         cerr << "\nChecking SWORD_PATH...";
00316 #endif
00317 
00318         if (envsworddir != NULL) {
00319 
00320 #ifndef _MSC_VER
00321 if (debug)
00322         cerr << "found (" << envsworddir << ")\n";
00323 #endif
00324 
00325                 path = envsworddir;
00326                 if ((envsworddir[strlen(envsworddir)-1] != '\\') && (envsworddir[strlen(envsworddir)-1] != '/'))
00327                         path += "/";
00328 
00329 #ifndef _MSC_VER
00330 if (debug)
00331         cerr << "\nChecking $SWORD_PATH for mods.conf...";
00332 #endif
00333 
00334                 if (FileMgr::existsFile(path.c_str(), "mods.conf")) {
00335 
00336 #ifndef _MSC_VER
00337 if (debug)
00338         cerr << "found\n";
00339 #endif
00340 
00341                         stdstr(prefixPath, path.c_str());
00342                         path += "mods.conf";
00343                         stdstr(configPath, path.c_str());
00344                         return;
00345                 }
00346 
00347 #ifndef _MSC_VER
00348 if (debug)
00349         cerr << "\nChecking $SWORD_PATH for mods.d...";
00350 #endif
00351 
00352                 if (FileMgr::existsDir(path.c_str(), "mods.d")) {
00353 
00354 #ifndef _MSC_VER
00355 if (debug)
00356         cerr << "found\n";
00357 #endif
00358 
00359                         stdstr(prefixPath, path.c_str());
00360                         path += "mods.d";
00361                         stdstr(configPath, path.c_str());
00362                         *configType = 1;
00363                         return;
00364                 }
00365         }
00366 
00367 
00368         // check for systemwide globalConfPath
00369 
00370 #ifndef _MSC_VER
00371 if (debug)
00372         cerr << "\nParsing " << globalConfPath << "...";
00373 #endif
00374 
00375         char *globPaths = 0;
00376         char *gfp;
00377         stdstr(&globPaths, globalConfPath);
00378         for (gfp = strtok(globPaths, ":"); gfp; gfp = strtok(0, ":")) {
00379 
00380         #ifndef _MSC_VER
00381 if (debug)
00382         cerr << "\nChecking for " << gfp << "...";
00383 #endif
00384 
00385                 if (FileMgr::existsFile(gfp))
00386                         break;
00387         }
00388 
00389         if (gfp) {
00390 
00391 #ifndef _MSC_VER
00392 if (debug)
00393         cerr << "found\n";
00394 #endif
00395 
00396                 SWConfig etcconf(gfp);
00397                 if ((entry = etcconf.Sections["Install"].find("DataPath")) != etcconf.Sections["Install"].end()) {
00398                         path = (*entry).second;
00399                         if (((*entry).second.c_str()[strlen((*entry).second.c_str())-1] != '\\') && ((*entry).second.c_str()[strlen((*entry).second.c_str())-1] != '/'))
00400                                 path += "/";
00401 
00402 #ifndef _MSC_VER
00403 if (debug)
00404         cerr << "DataPath in " << gfp << " is set to: " << path;
00405 #endif
00406 
00407 #ifndef _MSC_VER
00408 if (debug)
00409         cerr << "\nChecking for mods.conf in DataPath ";
00410 #endif
00411                         if (FileMgr::existsFile(path.c_str(), "mods.conf")) {
00412 
00413 #ifndef _MSC_VER
00414 if (debug)
00415         cerr << "found\n";
00416 #endif
00417 
00418                                 stdstr(prefixPath, path.c_str());
00419                                 path += "mods.conf";
00420                                 stdstr(configPath, path.c_str());
00421                                 delete [] globPaths;
00422                                 return;
00423                         }
00424 
00425 #ifndef _MSC_VER
00426 if (debug)
00427         cerr << "\nChecking for mods.d in DataPath ";
00428 #endif
00429 
00430                         if (FileMgr::existsDir(path.c_str(), "mods.d")) {
00431 
00432 #ifndef _MSC_VER
00433 if (debug)
00434         cerr << "found\n";
00435 #endif
00436 
00437                                 stdstr(prefixPath, path.c_str());
00438                                 path += "mods.d";
00439                                 stdstr(configPath, path.c_str());
00440                                 *configType = 1;
00441                                 delete [] globPaths;
00442                                 return;
00443                         }
00444                 }
00445         }
00446 
00447         delete [] globPaths;
00448 
00449         // check ~/.sword/
00450 
00451 #ifndef _MSC_VER
00452 if (debug)
00453         cerr << "\nChecking home directory for ~/.sword/mods.conf" << path;
00454 #endif
00455 
00456         if (envhomedir != NULL) {
00457                 path = envhomedir;
00458                 if ((envhomedir[strlen(envhomedir)-1] != '\\') && (envhomedir[strlen(envhomedir)-1] != '/'))
00459                         path += "/";
00460                 path += ".sword/";
00461                 if (FileMgr::existsFile(path.c_str(), "mods.conf")) {
00462 
00463 #ifndef _MSC_VER
00464 if (debug)
00465         cerr << " found\n";
00466 #endif
00467 
00468                         stdstr(prefixPath, path.c_str());
00469                         path += "mods.conf";
00470                         stdstr(configPath, path.c_str());
00471                         return;
00472                 }
00473 
00474 #ifndef _MSC_VER
00475 if (debug)
00476         cerr << "\nChecking home directory for ~/.sword/mods.d" << path;
00477 #endif
00478 
00479                 if (FileMgr::existsDir(path.c_str(), "mods.d")) {
00480 
00481 #ifndef _MSC_VER
00482 if (debug)
00483         cerr << "found\n";
00484 #endif
00485 
00486                         stdstr(prefixPath, path.c_str());
00487                         path += "mods.d";
00488                         stdstr(configPath, path.c_str());
00489                         *configType = 2;
00490                         return;
00491                 }
00492         }
00493 }
00494 
00495 
00496 void SWMgr::loadConfigDir(const char *ipath)
00497 {
00498    DIR *dir;
00499    struct dirent *ent;
00500    string newmodfile;
00501  
00502         if ((dir = opendir(ipath))) {
00503                 rewinddir(dir);
00504                 while ((ent = readdir(dir))) {
00505                         if ((strcmp(ent->d_name, ".")) && (strcmp(ent->d_name, ".."))) {
00506                                 newmodfile = ipath;
00507                                 if ((ipath[strlen(ipath)-1] != '\\') && (ipath[strlen(ipath)-1] != '/'))
00508                                         newmodfile += "/";
00509                                 newmodfile += ent->d_name;
00510                                 if (config) {
00511                                         SWConfig tmpConfig(newmodfile.c_str());
00512                                         *config += tmpConfig;
00513                                 }
00514                                 else    config = myconfig = new SWConfig(newmodfile.c_str());
00515                         }
00516                 }
00517                 closedir(dir);
00518                 if (!config) {  // if no .conf file exist yet, create a default
00519                         newmodfile = ipath;
00520                         if ((ipath[strlen(ipath)-1] != '\\') && (ipath[strlen(ipath)-1] != '/'))
00521                                 newmodfile += "/";
00522                         newmodfile += "globals.conf";
00523                         config = myconfig = new SWConfig(newmodfile.c_str());
00524                 }
00525         }
00526 }
00527 
00528 
00529 /***********************************************************************
00530  * SWMgr::Load - loads actual modules
00531  *
00532  * RET: status - 0 = ok; -1 no config found; 1 = no modules installed
00533  *
00534  */
00535 
00536 signed char SWMgr::Load() {
00537         signed char ret = 0;
00538 
00539         if (!config) {  // If we weren't passed a config object at construction, find a config file
00540                 if (!configPath)        // If we weren't passed a config path at construction...
00541                         findConfig(&configType, &prefixPath, &configPath);
00542                 if (configPath) {
00543                         if (configType)
00544                                 loadConfigDir(configPath);
00545                         else    config = myconfig = new SWConfig(configPath);
00546                 }
00547         }
00548 
00549         if (config) {
00550                 SectionMap::iterator Sectloop, Sectend;
00551                 ConfigEntMap::iterator Entryloop, Entryend;
00552 
00553                 DeleteMods();
00554 
00555                 for (Sectloop = config->Sections.lower_bound("Globals"), Sectend = config->Sections.upper_bound("Globals"); Sectloop != Sectend; Sectloop++) {          // scan thru all 'Globals' sections
00556                         for (Entryloop = (*Sectloop).second.lower_bound("AutoInstall"), Entryend = (*Sectloop).second.upper_bound("AutoInstall"); Entryloop != Entryend; Entryloop++)   // scan thru all AutoInstall entries
00557                                 InstallScan((*Entryloop).second.c_str());               // Scan AutoInstall entry directory for new modules and install
00558                 }               
00559                 if (configType) {       // force reload on config object because we may have installed new modules
00560                         delete myconfig;
00561                         config = myconfig = 0;
00562                         loadConfigDir(configPath);
00563                 }
00564                 else    config->Load();
00565 
00566                 CreateMods();
00567 
00568 //      augment config with ~/.sword/mods.d if it exists ---------------------
00569                         char *envhomedir  = getenv ("HOME");
00570                         if (envhomedir != NULL && configType != 2) { // 2 = user only
00571                                 string path = envhomedir;
00572                                 if ((envhomedir[strlen(envhomedir)-1] != '\\') && (envhomedir[strlen(envhomedir)-1] != '/'))
00573                                         path += "/";
00574                                 path += ".sword/";
00575                                 if (FileMgr::existsDir(path.c_str(), "mods.d")) {
00576                                         char *savePrefixPath = 0;
00577                                         char *saveConfigPath = 0;
00578                                         SWConfig *saveConfig = 0;
00579                                         stdstr(&savePrefixPath, prefixPath);
00580                                         stdstr(&prefixPath, path.c_str());
00581                                         path += "mods.d";
00582                                         stdstr(&saveConfigPath, configPath);
00583                                         stdstr(&configPath, path.c_str());
00584                                         saveConfig = config;
00585                                         config = myconfig = 0;
00586                                         loadConfigDir(configPath);
00587 
00588                                         CreateMods();
00589 
00590                                         stdstr(&prefixPath, savePrefixPath);
00591                                         delete []savePrefixPath;
00592                                         stdstr(&configPath, saveConfigPath);
00593                                         delete []saveConfigPath;
00594                                         (*saveConfig) += *config;
00595                                         homeConfig = myconfig;
00596                                         config = myconfig = saveConfig;
00597                                 }
00598                         }
00599 // -------------------------------------------------------------------------
00600                 if ( !Modules.size() ) // config exists, but no modules
00601                         ret = 1;
00602 
00603         }
00604         else {
00605                 SWLog::systemlog->LogError("SWMgr: Can't find 'mods.conf' or 'mods.d'.  Try setting:\n\tSWORD_PATH=<directory containing mods.conf>\n\tOr see the README file for a full description of setup options (%s)", (configPath) ? configPath : "<configPath is null>");
00606                 ret = -1;
00607         }
00608 
00609         return ret;
00610 }
00611 
00612 SWModule *SWMgr::CreateMod(string name, string driver, ConfigEntMap &section)
00613 {
00614         string description, datapath, misc1;
00615         ConfigEntMap::iterator entry;
00616         SWModule *newmod = 0;
00617         string lang, sourceformat, encoding;
00618         signed char direction, enc, markup;
00619 
00620         description  = ((entry = section.find("Description")) != section.end()) ? (*entry).second : (string)"";
00621         lang  = ((entry = section.find("Lang")) != section.end()) ? (*entry).second : (string)"en";
00622         sourceformat = ((entry = section.find("SourceType"))  != section.end()) ? (*entry).second : (string)"";
00623         encoding = ((entry = section.find("Encoding"))  != section.end()) ? (*entry).second : (string)"";
00624         datapath = prefixPath;
00625         if ((prefixPath[strlen(prefixPath)-1] != '\\') && (prefixPath[strlen(prefixPath)-1] != '/'))
00626                 datapath += "/";
00627         misc1 += ((entry = section.find("DataPath")) != section.end()) ? (*entry).second : (string)"";
00628         char *buf = new char [ strlen(misc1.c_str()) + 1 ];
00629         char *buf2 = buf;
00630         strcpy(buf, misc1.c_str());
00631 //      for (; ((*buf2) && ((*buf2 == '.') || (*buf2 == '/') || (*buf2 == '\\'))); buf2++);
00632         for (; ((*buf2) && ((*buf2 == '/') || (*buf2 == '\\'))); buf2++);
00633         if (*buf2)
00634                 datapath += buf2;
00635         delete [] buf;
00636 
00637         section["AbsoluteDataPath"] = datapath;
00638 
00639         if (!stricmp(sourceformat.c_str(), "GBF"))
00640                 markup = FMT_GBF;
00641         else if (!stricmp(sourceformat.c_str(), "ThML"))
00642                 markup = FMT_THML;
00643         else if (!stricmp(sourceformat.c_str(), "OSIS"))
00644                 markup = FMT_OSIS;
00645         else
00646                 markup = FMT_PLAIN;
00647 
00648         if (!stricmp(encoding.c_str(), "SCSU"))
00649                 enc = ENC_SCSU;
00650         else if (!stricmp(encoding.c_str(), "UTF-8")) {
00651                 enc = ENC_UTF8;
00652         }
00653         else enc = ENC_LATIN1;
00654 
00655         if ((entry = section.find("Direction")) == section.end()) {
00656                 direction = DIRECTION_LTR;
00657         }
00658         else if (!stricmp((*entry).second.c_str(), "rtol")) {
00659                 direction = DIRECTION_RTL;
00660         }
00661         else if (!stricmp((*entry).second.c_str(), "bidi")) {
00662                 direction = DIRECTION_BIDI;
00663         }
00664         else {
00665                 direction = DIRECTION_LTR;
00666         }
00667 
00668         if ((!stricmp(driver.c_str(), "zText")) || (!stricmp(driver.c_str(), "zCom"))) {
00669                 SWCompress *compress = 0;
00670                 int blockType = CHAPTERBLOCKS;
00671                 misc1 = ((entry = section.find("BlockType")) != section.end()) ? (*entry).second : (string)"CHAPTER";
00672                 if (!stricmp(misc1.c_str(), "VERSE"))
00673                         blockType = VERSEBLOCKS;
00674                 else if (!stricmp(misc1.c_str(), "CHAPTER"))
00675                         blockType = CHAPTERBLOCKS;
00676                 else if (!stricmp(misc1.c_str(), "BOOK"))
00677                         blockType = BOOKBLOCKS;
00678 
00679                 misc1 = ((entry = section.find("CompressType")) != section.end()) ? (*entry).second : (string)"LZSS";
00680 #ifndef EXCLUDEZLIB
00681                 if (!stricmp(misc1.c_str(), "ZIP"))
00682                         compress = new ZipCompress();
00683                 else
00684 #endif
00685                 if (!stricmp(misc1.c_str(), "LZSS"))
00686                         compress = new LZSSCompress();
00687 
00688                 if (compress) {
00689                         if (!stricmp(driver.c_str(), "zText"))
00690                                 newmod = new zText(datapath.c_str(), name.c_str(), description.c_str(), blockType, compress, 0, enc, direction, markup, lang.c_str());
00691                         else    newmod = new zCom(datapath.c_str(), name.c_str(), description.c_str(), blockType, compress, 0, enc, direction, markup, lang.c_str());
00692                 }
00693         }
00694 
00695         if (!stricmp(driver.c_str(), "RawText")) {
00696                 newmod = new RawText(datapath.c_str(), name.c_str(), description.c_str(), 0, enc, direction, markup, lang.c_str());
00697         }
00698 
00699         // backward support old drivers
00700         if (!stricmp(driver.c_str(), "RawGBF")) {
00701                 newmod = new RawText(datapath.c_str(), name.c_str(), description.c_str(), 0, enc, direction, markup, lang.c_str());
00702         }
00703 
00704         if (!stricmp(driver.c_str(), "RawCom")) {
00705                 newmod = new RawCom(datapath.c_str(), name.c_str(), description.c_str(), 0, enc, direction, markup, lang.c_str());
00706         }
00707 
00708         if (!stricmp(driver.c_str(), "RawFiles")) {
00709                 newmod = new RawFiles(datapath.c_str(), name.c_str(), description.c_str(), 0, enc, direction, markup, lang.c_str());
00710         }
00711 
00712         if (!stricmp(driver.c_str(), "HREFCom")) {
00713                 misc1 = ((entry = section.find("Prefix")) != section.end()) ? (*entry).second : (string)"";
00714                 newmod = new HREFCom(datapath.c_str(), misc1.c_str(), name.c_str(), description.c_str());
00715         }
00716 
00717         if (!stricmp(driver.c_str(), "RawLD"))
00718                 newmod = new RawLD(datapath.c_str(), name.c_str(), description.c_str(), 0, enc, direction, markup, lang.c_str());
00719 
00720         if (!stricmp(driver.c_str(), "RawLD4"))
00721                 newmod = new RawLD4(datapath.c_str(), name.c_str(), description.c_str(), 0, enc, direction, markup, lang.c_str());
00722 
00723         if (!stricmp(driver.c_str(), "zLD")) {
00724                 SWCompress *compress = 0;
00725                 int blockCount;
00726                 misc1 = ((entry = section.find("BlockCount")) != section.end()) ? (*entry).second : (string)"200";
00727                 blockCount = atoi(misc1.c_str());
00728                 blockCount = (blockCount) ? blockCount : 200;
00729 
00730                 misc1 = ((entry = section.find("CompressType")) != section.end()) ? (*entry).second : (string)"LZSS";
00731 #ifndef EXCLUDEZLIB
00732                 if (!stricmp(misc1.c_str(), "ZIP"))
00733                         compress = new ZipCompress();
00734                 else
00735 #endif
00736                 if (!stricmp(misc1.c_str(), "LZSS"))
00737                         compress = new LZSSCompress();
00738 
00739                 if (compress) {
00740                         newmod = new zLD(datapath.c_str(), name.c_str(), description.c_str(), blockCount, compress, 0, enc, direction, markup, lang.c_str());
00741                 }
00742         }
00743 
00744         if (!stricmp(driver.c_str(), "RawGenBook")) {
00745                 newmod = new RawGenBook(datapath.c_str(), name.c_str(), description.c_str(), 0, enc, direction, markup, lang.c_str());
00746         }
00747     // if a specific module type is set in the config, use this
00748     if ((entry = section.find("Type")) != section.end())
00749         newmod->Type(entry->second.c_str());
00750 
00751      newmod->setConfig(&section);
00752         return newmod;
00753 }
00754 
00755 
00756 void SWMgr::AddGlobalOptions(SWModule *module, ConfigEntMap &section, ConfigEntMap::iterator start, ConfigEntMap::iterator end) {
00757         for (;start != end; start++) {
00758                 FilterMap::iterator it;
00759                 it = optionFilters.find((*start).second);
00760                 if (it != optionFilters.end()) {
00761                         module->AddOptionFilter((*it).second);  // add filter to module and option as a valid option
00762                         OptionsList::iterator loop;
00763                         for (loop = options.begin(); loop != options.end(); loop++) {
00764                                 if (!strcmp((*loop).c_str(), (*it).second->getOptionName()))
00765                                         break;
00766                         }
00767                         if (loop == options.end())      // if we have not yet included the option
00768                                 options.push_back((*it).second->getOptionName());
00769                 }
00770         }
00771         if (filterMgr)
00772                 filterMgr->AddGlobalOptions(module, section, start, end);
00773 }
00774 
00775 
00776 void SWMgr::AddLocalOptions(SWModule *module, ConfigEntMap &section, ConfigEntMap::iterator start, ConfigEntMap::iterator end)
00777 {
00778         for (;start != end; start++) {
00779                 FilterMap::iterator it;
00780                 it = optionFilters.find((*start).second);
00781                 if (it != optionFilters.end()) {
00782                         module->AddOptionFilter((*it).second);  // add filter to module
00783                 }
00784         }
00785 
00786         if (filterMgr)
00787                 filterMgr->AddLocalOptions(module, section, start, end);
00788 }
00789 
00790 
00791 void SWMgr::AddRawFilters(SWModule *module, ConfigEntMap &section) {
00792         string sourceformat, cipherKey;
00793         ConfigEntMap::iterator entry;
00794 
00795         cipherKey = ((entry = section.find("CipherKey")) != section.end()) ? (*entry).second : (string)"";
00796         if (!cipherKey.empty()) {
00797                 SWFilter *cipherFilter = new CipherFilter(cipherKey.c_str());
00798                 cipherFilters.insert(FilterMap::value_type(module->Name(), cipherFilter));
00799                 cleanupFilters.push_back(cipherFilter);
00800                 module->AddRawFilter(cipherFilter);
00801         }
00802 
00803         if (filterMgr)
00804                 filterMgr->AddRawFilters(module, section);
00805 }
00806 
00807 
00808 void SWMgr::AddEncodingFilters(SWModule *module, ConfigEntMap &section) {
00809 
00810         if (filterMgr)
00811                 filterMgr->AddEncodingFilters(module, section);
00812 }
00813 
00814 
00815 void SWMgr::AddRenderFilters(SWModule *module, ConfigEntMap &section) {
00816         string sourceformat;
00817         ConfigEntMap::iterator entry;
00818 
00819         sourceformat = ((entry = section.find("SourceType")) != section.end()) ? (*entry).second : (string)"";
00820 
00821         // Temporary: To support old module types
00822         // TODO: Remove at 1.6.0 release?
00823         if (sourceformat.empty()) {
00824                 sourceformat = ((entry = section.find("ModDrv")) != section.end()) ? (*entry).second : (string)"";
00825                 if (!stricmp(sourceformat.c_str(), "RawGBF"))
00826                         sourceformat = "GBF";
00827                 else sourceformat = "";
00828         }
00829 
00830 // process module       - eg. follows
00831 //      if (!stricmp(sourceformat.c_str(), "GBF")) {
00832 //              module->AddRenderFilter(gbftortf);
00833 //      }
00834 
00835         if (filterMgr)
00836                 filterMgr->AddRenderFilters(module, section);
00837 
00838 }
00839 
00840 
00841 void SWMgr::AddStripFilters(SWModule *module, ConfigEntMap &section)
00842 {
00843         string sourceformat;
00844         ConfigEntMap::iterator entry;
00845 
00846         sourceformat = ((entry = section.find("SourceType")) != section.end()) ? (*entry).second : (string)"";
00847         // Temporary: To support old module types
00848         if (sourceformat.empty()) {
00849                 sourceformat = ((entry = section.find("ModDrv")) != section.end()) ? (*entry).second : (string)"";
00850                 if (!stricmp(sourceformat.c_str(), "RawGBF"))
00851                         sourceformat = "GBF";
00852                 else sourceformat = "";
00853         }
00854         
00855         if (!stricmp(sourceformat.c_str(), "GBF")) {
00856                 module->AddStripFilter(gbfplain);
00857         }
00858         else if (!stricmp(sourceformat.c_str(), "ThML")) {
00859                 module->AddStripFilter(thmlplain);
00860         }
00861 
00862         if (filterMgr)
00863                 filterMgr->AddStripFilters(module, section);
00864 
00865 }
00866 
00867 
00868 void SWMgr::CreateMods() {
00869         SectionMap::iterator it;
00870         ConfigEntMap::iterator start;
00871         ConfigEntMap::iterator end;
00872         ConfigEntMap::iterator entry;
00873         SWModule *newmod;
00874         string driver, misc1;
00875         for (it = config->Sections.begin(); it != config->Sections.end(); it++) {
00876                 ConfigEntMap &section = (*it).second;
00877                 newmod = 0;
00878                 
00879                 driver = ((entry = section.find("ModDrv")) != section.end()) ? (*entry).second : (string)"";
00880                 if (!driver.empty()) {
00881                         newmod = CreateMod((*it).first, driver, section);
00882                         if (newmod) {
00883                                 start = (*it).second.lower_bound("GlobalOptionFilter");
00884                                 end   = (*it).second.upper_bound("GlobalOptionFilter");
00885                                 AddGlobalOptions(newmod, section, start, end);
00886 
00887                                 start = (*it).second.lower_bound("LocalOptionFilter");
00888                                 end   = (*it).second.upper_bound("LocalOptionFilter");
00889                                 AddLocalOptions(newmod, section, start, end);
00890 
00891                                 AddRawFilters(newmod, section);
00892                                 AddStripFilters(newmod, section);
00893                                 AddRenderFilters(newmod, section);
00894                                 AddEncodingFilters(newmod, section);
00895                                 
00896                                 Modules.insert(ModMap::value_type(newmod->Name(), newmod));
00897                         }
00898                 }
00899         }
00900 }
00901 
00902 
00903 void SWMgr::DeleteMods() {
00904 
00905         ModMap::iterator it;
00906 
00907         for (it = Modules.begin(); it != Modules.end(); it++)
00908                 delete (*it).second;
00909 
00910         Modules.clear();
00911 }
00912 
00913 
00914 void SWMgr::InstallScan(const char *dirname)
00915 {
00916    DIR *dir;
00917    struct dirent *ent;
00918    int conffd = 0;
00919    string newmodfile;
00920    string targetName;
00921  
00922         if (!access(dirname, 04)) {
00923           if ((dir = opendir(dirname))) {
00924                rewinddir(dir);
00925                while ((ent = readdir(dir))) {
00926                     if ((strcmp(ent->d_name, ".")) && (strcmp(ent->d_name, ".."))) {
00927                          newmodfile = dirname;
00928                          if ((dirname[strlen(dirname)-1] != '\\') && (dirname[strlen(dirname)-1] != '/'))
00929                               newmodfile += "/";
00930                          newmodfile += ent->d_name;
00931                          if (configType) {
00932                               if (config > 0)
00933                                    close(conffd);
00934                               targetName = configPath;
00935                               if ((configPath[strlen(configPath)-1] != '\\') && (configPath[strlen(configPath)-1] != '/'))
00936                                    targetName += "/";
00937                               targetName += ent->d_name;
00938                               conffd = open(targetName.c_str(), O_WRONLY|O_CREAT, S_IREAD|S_IWRITE);
00939                          }
00940                          else {
00941                               if (conffd < 1) {
00942                                    conffd = open(config->filename.c_str(), O_WRONLY|O_APPEND);
00943                                    if (conffd > 0)
00944                                         lseek(conffd, 0L, SEEK_END);
00945                               }
00946                          }
00947                          AddModToConfig(conffd, newmodfile.c_str());
00948                          unlink(newmodfile.c_str());
00949                     }
00950                }
00951                if (conffd > 0)
00952                     close(conffd);
00953                closedir(dir);
00954           }
00955      }
00956 }
00957 
00958 
00959 char SWMgr::AddModToConfig(int conffd, const char *fname)
00960 {
00961         int modfd;
00962         char ch;
00963 
00964         SWLog::systemlog->LogTimedInformation("Found new module [%s]. Installing...", fname);
00965         modfd = open(fname, O_RDONLY);
00966         ch = '\n';
00967         write(conffd, &ch, 1);
00968         while (read(modfd, &ch, 1) == 1)
00969                 write(conffd, &ch, 1);
00970         ch = '\n';
00971         write(conffd, &ch, 1);
00972         close(modfd);
00973         return 0;
00974 }
00975 
00976 
00977 void SWMgr::setGlobalOption(const char *option, const char *value)
00978 {
00979         for (FilterMap::iterator it = optionFilters.begin(); it != optionFilters.end(); it++) {
00980                 if ((*it).second->getOptionName()) {
00981                         if (!stricmp(option, (*it).second->getOptionName()))
00982                                 (*it).second->setOptionValue(value);
00983                 }
00984         }
00985 }
00986 
00987 
00988 const char *SWMgr::getGlobalOption(const char *option)
00989 {
00990         for (FilterMap::iterator it = optionFilters.begin(); it != optionFilters.end(); it++) {
00991                 if ((*it).second->getOptionName()) {
00992                         if (!stricmp(option, (*it).second->getOptionName()))
00993                                 return (*it).second->getOptionValue();
00994                 }
00995         }
00996         return 0;
00997 }
00998 
00999 
01000 const char *SWMgr::getGlobalOptionTip(const char *option)
01001 {
01002         for (FilterMap::iterator it = optionFilters.begin(); it != optionFilters.end(); it++) {
01003                 if ((*it).second->getOptionName()) {
01004                         if (!stricmp(option, (*it).second->getOptionName()))
01005                                 return (*it).second->getOptionTip();
01006                 }
01007         }
01008         return 0;
01009 }
01010 
01011 
01012 OptionsList SWMgr::getGlobalOptions()
01013 {
01014         return options;
01015 }
01016 
01017 
01018 OptionsList SWMgr::getGlobalOptionValues(const char *option)
01019 {
01020         OptionsList options;
01021         for (FilterMap::iterator it = optionFilters.begin(); it != optionFilters.end(); it++) {
01022                 if ((*it).second->getOptionName()) {
01023                         if (!stricmp(option, (*it).second->getOptionName())) {
01024                                 options = (*it).second->getOptionValues();
01025                                 break;  // just find the first one.  All option filters with the same option name should expect the same values
01026                         }
01027                 }
01028         }
01029         return options;
01030 }
01031 
01032 
01033 signed char SWMgr::setCipherKey(const char *modName, const char *key) {
01034         FilterMap::iterator it;
01035         ModMap::iterator it2;
01036 
01037         // check for filter that already exists
01038         it = cipherFilters.find(modName);
01039         if (it != cipherFilters.end()) {
01040                 ((CipherFilter *)(*it).second)->getCipher()->setCipherKey(key);
01041                 return 0;
01042         }
01043         // check if module exists
01044         else {
01045                 it2 = Modules.find(modName);
01046                 if (it2 != Modules.end()) {
01047                         SWFilter *cipherFilter = new CipherFilter(key);
01048                         cipherFilters.insert(FilterMap::value_type(modName, cipherFilter));
01049                         cleanupFilters.push_back(cipherFilter);
01050                         (*it2).second->AddRawFilter(cipherFilter);
01051                         return 0;
01052                 }
01053         }
01054         return -1;
01055 }

Generated on Thu Jun 20 22:13:00 2002 for The Sword Project by doxygen1.2.15