WordContext.cc

Go to the documentation of this file.
00001 //
00002 // WordContext.cc 
00003 //
00004 // WordContext: call Initialize for all classes that need to.
00005 //              This will enable the Instance() static member
00006 //              of each to return a properly allocated and configured 
00007 //              object.
00008 //
00009 // Part of the ht://Dig package   <http://www.htdig.org/>
00010 // Copyright (c) 1999, 2000, 2001 The ht://Dig Group
00011 // For copyright details, see the file COPYING in your distribution
00012 // or the GNU General Public License version 2 or later
00013 // <http://www.gnu.org/copyleft/gpl.html>
00014 //
00015 // $Id: WordContext_8cc-source.html,v 1.1 2008/06/08 10:12:59 sebdiaz Exp $
00016 //
00017 
00018 #ifdef HAVE_CONFIG_H
00019 #include "config.h"
00020 #endif /* HAVE_CONFIG_H */
00021 
00022 #include <stdlib.h>
00023 #include <sys/stat.h>
00024 #include <errno.h>
00025 
00026 #include "WordContext.h"
00027 #include "WordListOne.h"
00028 #include "WordMonitor.h"
00029 
00030 void WordContext::Initialize(const Configuration &config)
00031 {
00032   Finish();
00033   configuration = new Configuration(config);
00034   type = new WordType(*configuration);
00035   key_info = new WordKeyInfo(*configuration);
00036   record_info = new WordRecordInfo(*configuration);
00037   db_info = new WordDBInfo(*configuration);
00038   if(db_info->dbenv) db_info->dbenv->app_private = (void*)this;
00039   if(config.Boolean("wordlist_monitor"))
00040     monitor = new WordMonitor(*configuration);
00041 }
00042 
00043 int WordContext::Initialize(const ConfigDefaults* config_defaults /* = 0 */)
00044 {
00045   Configuration *config = new Configuration();
00046 
00047   if(config_defaults)
00048     config->Defaults(config_defaults);
00049 
00050   String filename = ConfigFile();
00051 
00052   if(!filename.empty())
00053     config->Read(filename);
00054 
00055   Initialize(*config);
00056 
00057   delete config;
00058 
00059   return OK;
00060 }
00061 
00062 int WordContext::ReInitialize()
00063 {
00064   if(type) delete type;
00065   type = 0;
00066   if(key_info) delete key_info;
00067   key_info = 0;
00068   if(record_info) delete record_info;
00069   record_info = 0;
00070   if(db_info) delete db_info;
00071   db_info = 0;
00072   if(monitor) delete monitor;
00073   monitor = 0;
00074 
00075   Configuration& config = *configuration;
00076 
00077   type = new WordType(config);
00078   key_info = new WordKeyInfo(config);
00079   record_info = new WordRecordInfo(config);
00080   db_info = new WordDBInfo(config);
00081   if(db_info->dbenv) db_info->dbenv->app_private = (void*)this;
00082   if(config.Boolean("wordlist_monitor")) {
00083     monitor = new WordMonitor(config);
00084     GetDBInfo().dbenv->mp_monitor = monitor;
00085   }
00086 
00087   return OK;
00088 }
00089 
00090 void WordContext::Finish()
00091 {
00092   if(type) delete type;
00093   type = 0;
00094   if(key_info) delete key_info;
00095   key_info = 0;
00096   if(record_info) delete record_info;
00097   record_info = 0;
00098   if(db_info) delete db_info;
00099   db_info = 0;
00100   if(monitor) delete monitor;
00101   monitor = 0;
00102   if(configuration) delete configuration;
00103   configuration = 0;
00104 }
00105 
00106 WordList* WordContext::List()
00107 {
00108 #if 0
00109   if(configuration->Boolean("wordlist_multi"))
00110     return new WordListMulti(this);
00111   else
00112 #endif
00113     return new WordListOne(this);
00114 }
00115 
00116 WordReference* WordContext::Word()
00117 {
00118   return new WordReference(this);
00119 }
00120 WordReference* WordContext::Word(const String& key0, const String& record0)
00121 {
00122   return new WordReference(this, key0, record0);
00123 }
00124 WordReference* WordContext::Word(const String& word)
00125 {
00126   return new WordReference(this, word);
00127 }
00128 
00129 WordRecord* WordContext::Record()
00130 {
00131   return new WordRecord(this);
00132 }
00133 
00134 WordKey* WordContext::Key()
00135 {
00136   return new WordKey(this);
00137 }
00138 WordKey* WordContext::Key(const String& word)
00139 {
00140   return new WordKey(this, word);
00141 }
00142 
00143 WordKey* WordContext::Key(const WordKey& other)
00144 {
00145   return new WordKey(other);
00146 }
00147 
00148 String WordContext::ConfigFile()
00149 {
00150   String filename;
00151   //
00152   // Check file pointed by MIFLUZ_CONFIG environment variable
00153   //
00154   if(getenv("MIFLUZ_CONFIG")) {
00155     filename << getenv("MIFLUZ_CONFIG");
00156     struct stat statbuf;
00157     if(stat((char*)filename, &statbuf) < 0) {
00158       if(errno != ENOENT) {
00159         fprintf(stderr, "WordContext::ConfigFile: MIFLUZ_CONFIG could not stat %s\n", (char*)filename);
00160         perror("");
00161         return NOTOK;
00162       }
00163       filename.trunc();
00164     }
00165   }
00166   //
00167   // Check for ~/.mifluz
00168   //
00169   if(filename.empty()) {
00170     const char* home = getenv("HOME");
00171     if(home) {
00172       filename << home << "/.mifluz";
00173       struct stat statbuf;
00174       if(stat((char*)filename, &statbuf) < 0) {
00175         if(errno != ENOENT) {
00176           fprintf(stderr, "WordContext::ConfigFile: could not stat %s ", (char*)filename);
00177           perror("");
00178           return NOTOK;
00179         }
00180         filename.trunc();
00181       }
00182     }
00183   }
00184   //
00185   // Check for system wide CONFIG_FILE
00186   //
00187   if(filename.empty()) {
00188     filename << CONFIG_FILE;
00189     struct stat statbuf;
00190     if(stat((char*)filename, &statbuf) < 0) {
00191       if(errno != ENOENT) {
00192         fprintf(stderr, "WordContext::ConfigFile: could not stat %s ", (char*)filename);
00193         perror("");
00194         return NOTOK;
00195       }
00196       filename.trunc();
00197     }
00198   }
00199 
00200   return filename;
00201 }

Generated on Sun Jun 8 10:56:39 2008 for GNUmifluz by  doxygen 1.5.5