• Main Page
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

yapet/cfg.cc

Go to the documentation of this file.
00001 // $Id: cfg.cc 3356 2010-09-18 21:06:17Z rafi $
00002 //
00003 // Copyright (C) 2009-2010  Rafael Ostertag
00004 //
00005 // This file is part of YAPET.
00006 //
00007 // YAPET is free software: you can redistribute it and/or modify it under the
00008 // terms of the GNU General Public License as published by the Free Software
00009 // Foundation, either version 3 of the License, or (at your option) any later
00010 // version.
00011 //
00012 // YAPET is distributed in the hope that it will be useful, but WITHOUT ANY
00013 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00014 // FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
00015 // details.
00016 //
00017 // You should have received a copy of the GNU General Public License along with
00018 // YAPET.  If not, see <http://www.gnu.org/licenses/>.
00019 //
00020 
00021 #ifdef HAVE_CONFIG_H
00022 # include <config.h>
00023 #endif
00024 
00025 #ifdef CFGDEBUG
00026 # ifdef HAVE_IOSTREAM
00027 #  include <iostream>
00028 # endif
00029 #endif
00030 
00031 #include "cfg.h"
00032 // Used for the character pools
00033 #include "pwgen/pwgen.h"
00034 
00035 using namespace YAPET::CONFIG;
00036 
00038 const std::string Config::def_petfile ("");
00040 const int Config::def_timeout (600);
00042 const bool Config::def_filesecurity (true);
00044 const bool Config::def_ignorerc (false);
00045 const YAPET::PWGEN::RNGENGINE Config::def_pwgen_rng (YAPET::PWGEN::AUTO);
00046 const size_t Config::def_pwlen (10);
00047 const int Config::def_character_pools (YAPET::PWGEN::LETTERS |
00048                        YAPET::PWGEN::DIGITS |
00049                        YAPET::PWGEN::PUNCT |
00050                        YAPET::PWGEN::SPECIAL);
00051 const bool Config::def_allow_lock_quit (true);
00052 const unsigned int Config::def_pw_input_timeout (60); // in seconds
00053 std::string
00054 Config::getDefPetfile() {
00055     return def_petfile;
00056 }
00057 unsigned int
00058 Config::getDefTimeout() {
00059     return def_timeout;
00060 }
00061 bool
00062 Config::getDefFilesecurity() {
00063     return def_filesecurity;
00064 }
00065 bool
00066  Config::getDefIgnorerc() {
00067     return def_ignorerc;
00068 }
00069 YAPET::PWGEN::RNGENGINE
00070 Config::getDefPWGenRNG() {
00071     return def_pwgen_rng;
00072 }
00073 size_t
00074 Config::getDefPWLength() {
00075     return def_pwlen;
00076 }
00077 int
00078 Config::getDefCharPools() {
00079     return def_character_pools;
00080 }
00081 bool
00082 Config::getDefAllowLockQuit() {
00083     return def_allow_lock_quit;
00084 }
00085 unsigned int
00086 Config::getDefPwInputTimeout() {
00087     return def_pw_input_timeout;
00088 }
00089 bool
00090 Config::getDefCPoolLetters() {
00091    return def_character_pools & YAPET::PWGEN::LETTERS ? true : false;
00092 }
00093 bool
00094 Config::getDefCPoolDigits(){
00095     return def_character_pools & YAPET::PWGEN::DIGITS ? true : false;
00096 }
00097 bool
00098 Config::getDefCPoolPunct(){
00099     return def_character_pools & YAPET::PWGEN::PUNCT ? true : false;
00100 }
00101 bool 
00102 Config::getDefCPoolSpecial(){
00103     return def_character_pools & YAPET::PWGEN::SPECIAL ? true : false;
00104 }
00105 bool
00106 Config::getDefCPoolOther(){
00107    return def_character_pools & YAPET::PWGEN::OTHER ? true : false;
00108 }
00109 
00110 std::string
00111 Config::cleanupPath (const std::string& s) const {
00112 #ifdef CFGDEBUG
00113     std::cout << " === ";
00114     std::cout << "Config::cleanupPath(std::string)";
00115     std::cout << ":" << std::endl;
00116 #endif
00117 
00118     if (s.empty() ) {
00119 #ifdef CFGDEBUG
00120         std::cout << "\tgot empty string." << std::endl;
00121 #endif
00122         return s;
00123     }
00124 
00125     std::string work_copy (s);
00126 #ifdef CFGDEBUG
00127     std::cout << "\tBefore cleanup: " << s << std::endl;
00128 #endif
00129     std::string::size_type pos;
00130 
00131     while ( (pos = work_copy.find ("//", 0) ) != std::string::npos)
00132         work_copy.erase (pos, 1);
00133 
00134 #ifdef CFGDEBUG
00135     std::cout << "\tAfter cleanup: " << work_copy << std::endl;
00136 #endif
00137     return work_copy;
00138 }
00139 
00140 Config::Config() : cfgfile (NULL),
00141         cl_petfile(),
00142         cl_timeout(),
00143         cl_filesecurity(),
00144         cl_ignorerc() {
00145     // Empty
00146 }
00147 
00148 Config::Config (const Config& c) {
00149     if (c.cfgfile != NULL)
00150         cfgfile = new ConfigFile (* (c.cfgfile) );
00151     else
00152         cfgfile = NULL;
00153 
00154     cl_petfile = c.cl_petfile;
00155     cl_timeout = c.cl_timeout;
00156     cl_filesecurity = c.cl_filesecurity;
00157     cl_ignorerc = c.cl_ignorerc;
00158 }
00159 
00160 Config::~Config() {
00161     if (cfgfile != NULL)
00162         delete cfgfile;
00163 }
00164 
00168 void
00169 Config::loadConfigFile (std::string filename) {
00170 #ifdef CFGDEBUG
00171     std::cout << " === ";
00172     std::cout << "Config::loadConfigFile(std::string)";
00173     std::cout << ":" << std::endl;
00174 #endif
00175 
00176     if (cl_ignorerc.ignore) {
00177 #ifdef CFGDEBUG
00178         std::cout << "\tadvised to ignore rc file!" << std::endl;
00179 #endif
00180 
00181         if (cfgfile != NULL) {
00182             delete cfgfile;
00183             cfgfile = NULL;
00184         }
00185 
00186         return;
00187     }
00188 
00189     if (cfgfile != NULL)
00190         delete cfgfile;
00191 
00192     cfgfile = new ConfigFile (filename);
00193 
00194     if (!cfgfile->isOpenSuccess() ) {
00195         delete cfgfile;
00196         cfgfile = NULL;
00197 #ifdef CFGDEBUG
00198         std::cout << "\topen " << filename << " was not successful" << std::endl;
00199 #endif
00200     } else {
00201 #ifdef CFGDEBUG
00202         std::cout << "\topen " << cfgfile->getConfigFilePath() << " successful" << std::endl;
00203 
00204         if (cfgfile->getIgnoreRC() ) {
00205             std::cout << "\tRC file says to ignore itself!" << std::endl;
00206         }
00207 
00208 #endif
00209         cl_ignorerc.ignore = cfgfile->getIgnoreRC();
00210     }
00211 }
00212 
00213 std::string
00214 Config::getPetFile() const {
00215 #ifdef CFGDEBUG
00216     std::cout << " === ";
00217     std::cout << "Config::getPetFile() const";
00218     std::cout << ":" << std::endl;
00219 #endif
00220 
00221     if (cl_petfile.set_on_cl) {
00222 #ifdef CFGDEBUG
00223         std::cout << "\tvalue from cmd line: " << cl_petfile.name << std::endl;
00224 #endif
00225         return cl_petfile.name;
00226     }
00227 
00228     if (cl_ignorerc.ignore) {
00229 #ifdef CFGDEBUG
00230         std::cout << "\tvalue from rc file ignored. Taking default: " << Config::def_petfile << std::endl;
00231 #endif
00232         return Config::def_petfile;
00233     }
00234 
00235     if (cfgfile != NULL) {
00236 #ifdef CFGDEBUG
00237         std::cout << "\tvalue from cfgfile: " << cfgfile->getFileToLoad() << std::endl;
00238 #endif
00239         return std::string (cleanupPath (cfgfile->getFileToLoad() ) );
00240     }
00241 
00242 #ifdef CFGDEBUG
00243     else {
00244         std::cout << "\tcfgfile == NULL " << std::endl;
00245     }
00246 
00247 #endif
00248 #ifdef CFGDEBUG
00249     std::cout << "\tsimply returning default value: " << Config::def_petfile << std::endl;
00250 #endif
00251     return Config::def_petfile;
00252 }
00253 
00254 int
00255 Config::getTimeout() const {
00256 #ifdef CFGDEBUG
00257     std::cout << " === ";
00258     std::cout << "Config::getTimeout() const";
00259     std::cout << ":" << std::endl;
00260 #endif
00261 
00262     if (cl_timeout.set_on_cl) {
00263 #ifdef CFGDEBUG
00264         std::cout << "\tvalue from cmd line: " << cl_timeout.amount << std::endl;
00265 #endif
00266         return cl_timeout.amount;
00267     }
00268 
00269     if (cl_ignorerc.ignore) {
00270 #ifdef CFGDEBUG
00271         std::cout << "\tvalue from rc file ignored. Taking default: " << Config::def_timeout << std::endl;
00272 #endif
00273         return Config::def_timeout;
00274     }
00275 
00276     if (cfgfile != NULL) {
00277 #ifdef CFGDEBUG
00278         std::cout << "\tvalue from cfgfile: " << cfgfile->getLockTimeout() << std::endl;
00279 #endif
00280         return cfgfile->getLockTimeout();
00281     }
00282 
00283 #ifdef CFGDEBUG
00284     else {
00285         std::cout << "\tcfgfile == NULL " << std::endl;
00286     }
00287 
00288 #endif
00289 #ifdef CFGDEBUG
00290     std::cout << "\tsimply returning default value: " << Config::def_timeout << std::endl;
00291 #endif
00292     return Config::def_timeout;
00293 }
00294 
00295 bool
00296 Config::getFilesecurity() const {
00297 #ifdef CFGDEBUG
00298     std::cout << " === ";
00299     std::cout << "Config::getFilesecurity() const";
00300     std::cout << ":" << std::endl;
00301 #endif
00302 
00303     if (cl_filesecurity.set_on_cl) {
00304 #ifdef CFGDEBUG
00305         std::cout << "\tvalue from cmd line: " << cl_filesecurity.check << std::endl;
00306 #endif
00307         return cl_filesecurity.check;
00308     }
00309 
00310     if (cl_ignorerc.ignore) {
00311 #ifdef CFGDEBUG
00312         std::cout << "\tvalue from rc file ignored. Taking default: " << Config::def_filesecurity << std::endl;
00313 #endif
00314         return Config::def_filesecurity;
00315     }
00316 
00317     if (cfgfile != NULL) {
00318 #ifdef CFGDEBUG
00319         std::cout << "\tvalue from cfgfile: " << cfgfile->getUseFileSecurity() << std::endl;
00320 #endif
00321         return cfgfile->getUseFileSecurity();
00322     }
00323 
00324 #ifdef CFGDEBUG
00325     else {
00326         std::cout << "\tcfgfile == NULL " << std::endl;
00327     }
00328 
00329 #endif
00330 #ifdef CFGDEBUG
00331     std::cout << "\tsimply returning default value: " << Config::def_filesecurity << std::endl;
00332 #endif
00333     return Config::def_filesecurity;
00334 }
00335 
00336 // These functions are trivial, since at the time being it can only be set in the
00337 // configuration file.
00338 YAPET::PWGEN::RNGENGINE
00339 Config::getPWGenRNG() const {
00340     if (cfgfile != NULL) {
00341     return cfgfile->getPWGenRNG();
00342     } else {
00343     return def_pwgen_rng;
00344     }
00345 }
00346 
00347 size_t
00348 Config::getPWGenPWLen() const {
00349     if (cfgfile != NULL) {
00350     return cfgfile->getPWGenPWLen() > 0 ? cfgfile->getPWGenPWLen() :
00351     def_pwlen;
00352     } else {
00353     return def_pwlen;
00354     }
00355 }
00356 
00357 int
00358 Config::getCharPools() const {
00359     if (cfgfile != NULL) {
00360     int retval = 0;
00361     if (cfgfile->getPWGenLetters())
00362         retval |= YAPET::PWGEN::LETTERS;
00363     if (cfgfile->getPWGenDigits())
00364         retval |= YAPET::PWGEN::DIGITS;
00365     if (cfgfile->getPWGenPunct())
00366         retval |= YAPET::PWGEN::PUNCT;
00367     if (cfgfile->getPWGenSpecial())
00368         retval |= YAPET::PWGEN::SPECIAL;
00369     if (cfgfile->getPWGenOther())
00370         retval |= YAPET::PWGEN::OTHER;
00371     
00372     return retval != 0 ? retval : def_character_pools;
00373     } else {
00374     return def_character_pools;
00375     }
00376 }
00377 
00378 bool
00379 Config::getAllowLockQuit() const {
00380    if (cfgfile != NULL) {
00381     return cfgfile->getAllowLockQuit();
00382     } else {
00383     return def_allow_lock_quit;
00384     }
00385 }
00386 
00387 unsigned int
00388 Config::getPwInputTimeout() const {
00389     if (cfgfile != NULL) {
00390     return cfgfile->getPwInputTimeout() > 5 ? 
00391         cfgfile->getPwInputTimeout() : 5;
00392     } else {
00393     return def_pw_input_timeout;
00394     }
00395 }
00396 
00397 const Config&
00398 Config::operator= (const Config & c) {
00399     if (&c == this)
00400         return *this;
00401 
00402     if (c.cfgfile != NULL &&
00403             cfgfile != NULL ) {
00404         delete cfgfile;
00405         cfgfile = new ConfigFile (* (c.cfgfile) );
00406     }
00407 
00408     if (c.cfgfile == NULL &&
00409     cfgfile != NULL ) {
00410         delete cfgfile;
00411         cfgfile = NULL;
00412     } else {
00413         cfgfile = NULL;
00414     }
00415 
00416     cl_petfile = c.cl_petfile;
00417     cl_timeout = c.cl_timeout;
00418     cl_filesecurity = c.cl_filesecurity;
00419     cl_ignorerc = c.cl_ignorerc;
00420     return *this;
00421 }

Generated on Sun Sep 19 2010 15:37:13 for YAPET by  doxygen 1.7.1