00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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
00160
00161
00162
00163
00164
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
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
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
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
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) {
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
00531
00532
00533
00534
00535
00536 signed char SWMgr::Load() {
00537 signed char ret = 0;
00538
00539 if (!config) {
00540 if (!configPath)
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++) {
00556 for (Entryloop = (*Sectloop).second.lower_bound("AutoInstall"), Entryend = (*Sectloop).second.upper_bound("AutoInstall"); Entryloop != Entryend; Entryloop++)
00557 InstallScan((*Entryloop).second.c_str());
00558 }
00559 if (configType) {
00560 delete myconfig;
00561 config = myconfig = 0;
00562 loadConfigDir(configPath);
00563 }
00564 else config->Load();
00565
00566 CreateMods();
00567
00568
00569 char *envhomedir = getenv ("HOME");
00570 if (envhomedir != NULL && configType != 2) {
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() )
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 §ion)
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
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 else enc = ENC_LATIN1;
00653
00654 if ((entry = section.find("Direction")) == section.end()) {
00655 direction = DIRECTION_LTR;
00656 }
00657 else if (!stricmp((*entry).second.c_str(), "rtol")) {
00658 direction = DIRECTION_RTL;
00659 }
00660 else if (!stricmp((*entry).second.c_str(), "bidi")) {
00661 direction = DIRECTION_BIDI;
00662 }
00663 else {
00664 direction = DIRECTION_LTR;
00665 }
00666
00667 if ((!stricmp(driver.c_str(), "zText")) || (!stricmp(driver.c_str(), "zCom"))) {
00668 SWCompress *compress = 0;
00669 int blockType = CHAPTERBLOCKS;
00670 misc1 = ((entry = section.find("BlockType")) != section.end()) ? (*entry).second : (string)"CHAPTER";
00671 if (!stricmp(misc1.c_str(), "VERSE"))
00672 blockType = VERSEBLOCKS;
00673 else if (!stricmp(misc1.c_str(), "CHAPTER"))
00674 blockType = CHAPTERBLOCKS;
00675 else if (!stricmp(misc1.c_str(), "BOOK"))
00676 blockType = BOOKBLOCKS;
00677
00678 misc1 = ((entry = section.find("CompressType")) != section.end()) ? (*entry).second : (string)"LZSS";
00679 #ifndef EXCLUDEZLIB
00680 if (!stricmp(misc1.c_str(), "ZIP"))
00681 compress = new ZipCompress();
00682 else
00683 #endif
00684 if (!stricmp(misc1.c_str(), "LZSS"))
00685 compress = new LZSSCompress();
00686
00687 if (compress) {
00688 if (!stricmp(driver.c_str(), "zText"))
00689 newmod = new zText(datapath.c_str(), name.c_str(), description.c_str(), blockType, compress, 0, enc, direction, markup, lang.c_str());
00690 else newmod = new zCom(datapath.c_str(), name.c_str(), description.c_str(), blockType, compress, 0, enc, direction, markup, lang.c_str());
00691 }
00692 }
00693
00694 if (!stricmp(driver.c_str(), "RawText")) {
00695 newmod = new RawText(datapath.c_str(), name.c_str(), description.c_str(), 0, enc, direction, markup, lang.c_str());
00696 }
00697
00698
00699 if (!stricmp(driver.c_str(), "RawGBF")) {
00700 newmod = new RawText(datapath.c_str(), name.c_str(), description.c_str(), 0, enc, direction, markup, lang.c_str());
00701 }
00702
00703 if (!stricmp(driver.c_str(), "RawCom")) {
00704 newmod = new RawCom(datapath.c_str(), name.c_str(), description.c_str(), 0, enc, direction, markup, lang.c_str());
00705 }
00706
00707 if (!stricmp(driver.c_str(), "RawFiles")) {
00708 newmod = new RawFiles(datapath.c_str(), name.c_str(), description.c_str(), 0, enc, direction, markup, lang.c_str());
00709 }
00710
00711 if (!stricmp(driver.c_str(), "HREFCom")) {
00712 misc1 = ((entry = section.find("Prefix")) != section.end()) ? (*entry).second : (string)"";
00713 newmod = new HREFCom(datapath.c_str(), misc1.c_str(), name.c_str(), description.c_str());
00714 }
00715
00716 if (!stricmp(driver.c_str(), "RawLD"))
00717 newmod = new RawLD(datapath.c_str(), name.c_str(), description.c_str(), 0, enc, direction, markup, lang.c_str());
00718
00719 if (!stricmp(driver.c_str(), "RawLD4"))
00720 newmod = new RawLD4(datapath.c_str(), name.c_str(), description.c_str(), 0, enc, direction, markup, lang.c_str());
00721
00722 if (!stricmp(driver.c_str(), "zLD")) {
00723 SWCompress *compress = 0;
00724 int blockCount;
00725 misc1 = ((entry = section.find("BlockCount")) != section.end()) ? (*entry).second : (string)"200";
00726 blockCount = atoi(misc1.c_str());
00727 blockCount = (blockCount) ? blockCount : 200;
00728
00729 misc1 = ((entry = section.find("CompressType")) != section.end()) ? (*entry).second : (string)"LZSS";
00730 #ifndef EXCLUDEZLIB
00731 if (!stricmp(misc1.c_str(), "ZIP"))
00732 compress = new ZipCompress();
00733 else
00734 #endif
00735 if (!stricmp(misc1.c_str(), "LZSS"))
00736 compress = new LZSSCompress();
00737
00738 if (compress) {
00739 newmod = new zLD(datapath.c_str(), name.c_str(), description.c_str(), blockCount, compress, 0, enc, direction, markup, lang.c_str());
00740 }
00741 }
00742
00743 if (!stricmp(driver.c_str(), "RawGenBook")) {
00744 newmod = new RawGenBook(datapath.c_str(), name.c_str(), description.c_str(), 0, enc, direction, markup, lang.c_str());
00745 }
00746
00747 if ((entry = section.find("Type")) != section.end())
00748 newmod->Type(entry->second.c_str());
00749
00750 newmod->setConfig(§ion);
00751 return newmod;
00752 }
00753
00754
00755 void SWMgr::AddGlobalOptions(SWModule *module, ConfigEntMap §ion, ConfigEntMap::iterator start, ConfigEntMap::iterator end) {
00756 for (;start != end; start++) {
00757 FilterMap::iterator it;
00758 it = optionFilters.find((*start).second);
00759 if (it != optionFilters.end()) {
00760 module->AddOptionFilter((*it).second);
00761 OptionsList::iterator loop;
00762 for (loop = options.begin(); loop != options.end(); loop++) {
00763 if (!strcmp((*loop).c_str(), (*it).second->getOptionName()))
00764 break;
00765 }
00766 if (loop == options.end())
00767 options.push_back((*it).second->getOptionName());
00768 }
00769 }
00770 if (filterMgr)
00771 filterMgr->AddGlobalOptions(module, section, start, end);
00772 }
00773
00774
00775 void SWMgr::AddLocalOptions(SWModule *module, ConfigEntMap §ion, ConfigEntMap::iterator start, ConfigEntMap::iterator end)
00776 {
00777 for (;start != end; start++) {
00778 FilterMap::iterator it;
00779 it = optionFilters.find((*start).second);
00780 if (it != optionFilters.end()) {
00781 module->AddOptionFilter((*it).second);
00782 }
00783 }
00784
00785 if (filterMgr)
00786 filterMgr->AddLocalOptions(module, section, start, end);
00787 }
00788
00789
00790 void SWMgr::AddRawFilters(SWModule *module, ConfigEntMap §ion) {
00791 string sourceformat, cipherKey;
00792 ConfigEntMap::iterator entry;
00793
00794 cipherKey = ((entry = section.find("CipherKey")) != section.end()) ? (*entry).second : (string)"";
00795 if (!cipherKey.empty()) {
00796 SWFilter *cipherFilter = new CipherFilter(cipherKey.c_str());
00797 cipherFilters.insert(FilterMap::value_type(module->Name(), cipherFilter));
00798 cleanupFilters.push_back(cipherFilter);
00799 module->AddRawFilter(cipherFilter);
00800 }
00801
00802 if (filterMgr)
00803 filterMgr->AddRawFilters(module, section);
00804 }
00805
00806
00807 void SWMgr::AddEncodingFilters(SWModule *module, ConfigEntMap §ion) {
00808
00809 if (filterMgr)
00810 filterMgr->AddEncodingFilters(module, section);
00811 }
00812
00813
00814 void SWMgr::AddRenderFilters(SWModule *module, ConfigEntMap §ion) {
00815 string sourceformat;
00816 ConfigEntMap::iterator entry;
00817
00818 sourceformat = ((entry = section.find("SourceType")) != section.end()) ? (*entry).second : (string)"";
00819
00820
00821
00822 if (sourceformat.empty()) {
00823 sourceformat = ((entry = section.find("ModDrv")) != section.end()) ? (*entry).second : (string)"";
00824 if (!stricmp(sourceformat.c_str(), "RawGBF"))
00825 sourceformat = "GBF";
00826 else sourceformat = "";
00827 }
00828
00829
00830
00831
00832
00833
00834 if (filterMgr)
00835 filterMgr->AddRenderFilters(module, section);
00836
00837 }
00838
00839
00840 void SWMgr::AddStripFilters(SWModule *module, ConfigEntMap §ion)
00841 {
00842 string sourceformat;
00843 ConfigEntMap::iterator entry;
00844
00845 sourceformat = ((entry = section.find("SourceType")) != section.end()) ? (*entry).second : (string)"";
00846
00847 if (sourceformat.empty()) {
00848 sourceformat = ((entry = section.find("ModDrv")) != section.end()) ? (*entry).second : (string)"";
00849 if (!stricmp(sourceformat.c_str(), "RawGBF"))
00850 sourceformat = "GBF";
00851 else sourceformat = "";
00852 }
00853
00854 if (!stricmp(sourceformat.c_str(), "GBF")) {
00855 module->AddStripFilter(gbfplain);
00856 }
00857 else if (!stricmp(sourceformat.c_str(), "ThML")) {
00858 module->AddStripFilter(thmlplain);
00859 }
00860
00861 if (filterMgr)
00862 filterMgr->AddStripFilters(module, section);
00863
00864 }
00865
00866
00867 void SWMgr::CreateMods() {
00868 SectionMap::iterator it;
00869 ConfigEntMap::iterator start;
00870 ConfigEntMap::iterator end;
00871 ConfigEntMap::iterator entry;
00872 SWModule *newmod;
00873 string driver, misc1;
00874 for (it = config->Sections.begin(); it != config->Sections.end(); it++) {
00875 ConfigEntMap §ion = (*it).second;
00876 newmod = 0;
00877
00878 driver = ((entry = section.find("ModDrv")) != section.end()) ? (*entry).second : (string)"";
00879 if (!driver.empty()) {
00880 newmod = CreateMod((*it).first, driver, section);
00881 if (newmod) {
00882 start = (*it).second.lower_bound("GlobalOptionFilter");
00883 end = (*it).second.upper_bound("GlobalOptionFilter");
00884 AddGlobalOptions(newmod, section, start, end);
00885
00886 start = (*it).second.lower_bound("LocalOptionFilter");
00887 end = (*it).second.upper_bound("LocalOptionFilter");
00888 AddLocalOptions(newmod, section, start, end);
00889
00890 AddRawFilters(newmod, section);
00891 AddStripFilters(newmod, section);
00892 AddRenderFilters(newmod, section);
00893 AddEncodingFilters(newmod, section);
00894
00895 Modules.insert(ModMap::value_type(newmod->Name(), newmod));
00896 }
00897 }
00898 }
00899 }
00900
00901
00902 void SWMgr::DeleteMods() {
00903
00904 ModMap::iterator it;
00905
00906 for (it = Modules.begin(); it != Modules.end(); it++)
00907 delete (*it).second;
00908
00909 Modules.clear();
00910 }
00911
00912
00913 void SWMgr::InstallScan(const char *dirname)
00914 {
00915 DIR *dir;
00916 struct dirent *ent;
00917 int conffd = 0;
00918 string newmodfile;
00919 string targetName;
00920
00921 if (!access(dirname, 04)) {
00922 if ((dir = opendir(dirname))) {
00923 rewinddir(dir);
00924 while ((ent = readdir(dir))) {
00925 if ((strcmp(ent->d_name, ".")) && (strcmp(ent->d_name, ".."))) {
00926 newmodfile = dirname;
00927 if ((dirname[strlen(dirname)-1] != '\\') && (dirname[strlen(dirname)-1] != '/'))
00928 newmodfile += "/";
00929 newmodfile += ent->d_name;
00930 if (configType) {
00931 if (config > 0)
00932 close(conffd);
00933 targetName = configPath;
00934 if ((configPath[strlen(configPath)-1] != '\\') && (configPath[strlen(configPath)-1] != '/'))
00935 targetName += "/";
00936 targetName += ent->d_name;
00937 conffd = open(targetName.c_str(), O_WRONLY|O_CREAT, S_IREAD|S_IWRITE);
00938 }
00939 else {
00940 if (conffd < 1) {
00941 conffd = open(config->filename.c_str(), O_WRONLY|O_APPEND);
00942 if (conffd > 0)
00943 lseek(conffd, 0L, SEEK_END);
00944 }
00945 }
00946 AddModToConfig(conffd, newmodfile.c_str());
00947 unlink(newmodfile.c_str());
00948 }
00949 }
00950 if (conffd > 0)
00951 close(conffd);
00952 closedir(dir);
00953 }
00954 }
00955 }
00956
00957
00958 char SWMgr::AddModToConfig(int conffd, const char *fname)
00959 {
00960 int modfd;
00961 char ch;
00962
00963 SWLog::systemlog->LogTimedInformation("Found new module [%s]. Installing...", fname);
00964 modfd = open(fname, O_RDONLY);
00965 ch = '\n';
00966 write(conffd, &ch, 1);
00967 while (read(modfd, &ch, 1) == 1)
00968 write(conffd, &ch, 1);
00969 ch = '\n';
00970 write(conffd, &ch, 1);
00971 close(modfd);
00972 return 0;
00973 }
00974
00975
00976 void SWMgr::setGlobalOption(const char *option, const char *value)
00977 {
00978 for (FilterMap::iterator it = optionFilters.begin(); it != optionFilters.end(); it++) {
00979 if ((*it).second->getOptionName()) {
00980 if (!stricmp(option, (*it).second->getOptionName()))
00981 (*it).second->setOptionValue(value);
00982 }
00983 }
00984 }
00985
00986
00987 const char *SWMgr::getGlobalOption(const char *option)
00988 {
00989 for (FilterMap::iterator it = optionFilters.begin(); it != optionFilters.end(); it++) {
00990 if ((*it).second->getOptionName()) {
00991 if (!stricmp(option, (*it).second->getOptionName()))
00992 return (*it).second->getOptionValue();
00993 }
00994 }
00995 return 0;
00996 }
00997
00998
00999 const char *SWMgr::getGlobalOptionTip(const char *option)
01000 {
01001 for (FilterMap::iterator it = optionFilters.begin(); it != optionFilters.end(); it++) {
01002 if ((*it).second->getOptionName()) {
01003 if (!stricmp(option, (*it).second->getOptionName()))
01004 return (*it).second->getOptionTip();
01005 }
01006 }
01007 return 0;
01008 }
01009
01010
01011 OptionsList SWMgr::getGlobalOptions()
01012 {
01013 return options;
01014 }
01015
01016
01017 OptionsList SWMgr::getGlobalOptionValues(const char *option)
01018 {
01019 OptionsList options;
01020 for (FilterMap::iterator it = optionFilters.begin(); it != optionFilters.end(); it++) {
01021 if ((*it).second->getOptionName()) {
01022 if (!stricmp(option, (*it).second->getOptionName())) {
01023 options = (*it).second->getOptionValues();
01024 break;
01025 }
01026 }
01027 }
01028 return options;
01029 }
01030
01031
01032 signed char SWMgr::setCipherKey(const char *modName, const char *key) {
01033 FilterMap::iterator it;
01034 ModMap::iterator it2;
01035
01036
01037 it = cipherFilters.find(modName);
01038 if (it != cipherFilters.end()) {
01039 ((CipherFilter *)(*it).second)->getCipher()->setCipherKey(key);
01040 return 0;
01041 }
01042
01043 else {
01044 it2 = Modules.find(modName);
01045 if (it2 != Modules.end()) {
01046 SWFilter *cipherFilter = new CipherFilter(key);
01047 cipherFilters.insert(FilterMap::value_type(modName, cipherFilter));
01048 cleanupFilters.push_back(cipherFilter);
01049 (*it2).second->AddRawFilter(cipherFilter);
01050 return 0;
01051 }
01052 }
01053 return -1;
01054 }