00001 // -*- c++ -*- 00002 // 00003 // $Id: cfg.h 3356 2010-09-18 21:06:17Z rafi $ 00004 // 00005 // Copyright (C) 2009-2010 Rafael Ostertag 00006 // 00007 // This file is part of YAPET. 00008 // 00009 // YAPET is free software: you can redistribute it and/or modify it under the 00010 // terms of the GNU General Public License as published by the Free Software 00011 // Foundation, either version 3 of the License, or (at your option) any later 00012 // version. 00013 // 00014 // YAPET is distributed in the hope that it will be useful, but WITHOUT ANY 00015 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 00016 // FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 00017 // details. 00018 // 00019 // You should have received a copy of the GNU General Public License along with 00020 // YAPET. If not, see <http://www.gnu.org/licenses/>. 00021 // 00022 00023 #ifndef _CFG_H 00024 #define _CFG_H 00025 00026 #ifdef HAVE_CONFIG_H 00027 # include <config.h> 00028 #endif 00029 00030 #ifdef HAVE_STRING 00031 # include <string> 00032 #endif 00033 00034 #ifdef HAVE_SYS_TYPES_H 00035 # include <sys/types.h> 00036 #endif 00037 00038 #include "pwgen/rng.h" 00039 #include "consts.h" 00040 #include "cfgfile.h" 00041 00042 namespace YAPET { 00043 namespace CONFIG { 00044 00050 class Config { 00051 private: 00052 ConfigFile* cfgfile; 00053 00055 static const std::string def_petfile; 00057 static const int def_timeout; 00059 static const bool def_filesecurity; 00061 static const bool def_ignorerc; 00063 static const size_t def_pwlen; 00065 static const YAPET::PWGEN::RNGENGINE def_pwgen_rng; 00067 static const int def_character_pools; 00069 static const bool def_allow_lock_quit; 00071 static const unsigned int def_pw_input_timeout; 00072 00073 struct s_cl_struct { 00074 // For indicating whether or not it has been set on the command 00075 // line 00076 bool set_on_cl; 00077 inline s_cl_struct() : set_on_cl (false) {} 00078 inline s_cl_struct (const s_cl_struct& r) { 00079 set_on_cl = r.set_on_cl; 00080 } 00081 inline const s_cl_struct& operator= (const s_cl_struct& r) { 00082 if (&r == this) 00083 return *this; 00084 00085 set_on_cl = r.set_on_cl; 00086 return *this; 00087 } 00088 }; 00090 struct s_cl_petfile : public s_cl_struct { 00091 std::string name; 00092 inline s_cl_petfile() : s_cl_struct(), name ("") {} 00093 inline s_cl_petfile (const s_cl_petfile& r) : s_cl_struct (r) { 00094 name = r.name ; 00095 } 00096 inline const s_cl_petfile& operator= (const s_cl_petfile& r) { 00097 if (&r == this) 00098 return *this; 00099 00100 s_cl_struct::operator= (r); 00101 name = r.name; 00102 return *this; 00103 } 00104 }; 00106 struct s_cl_timeout : public s_cl_struct { 00107 unsigned int amount; 00108 inline s_cl_timeout() : s_cl_struct(), amount (0) {} 00109 inline s_cl_timeout (const s_cl_timeout& r) : s_cl_struct (r) { 00110 amount = r.amount ; 00111 } 00112 inline const s_cl_timeout& operator= (const s_cl_timeout& r) { 00113 if (&r == this) 00114 return *this; 00115 00116 s_cl_struct::operator= (r); 00117 amount = r.amount; 00118 return *this; 00119 } 00120 }; 00122 struct s_cl_filesecurity : public s_cl_struct { 00123 bool check; 00124 inline s_cl_filesecurity() : s_cl_struct(), check (false) {} 00125 inline s_cl_filesecurity (const s_cl_filesecurity& r) : s_cl_struct (r) { 00126 check = r.check ; 00127 } 00128 inline const s_cl_filesecurity& operator= (const s_cl_filesecurity& r) { 00129 if (&r == this) 00130 return *this; 00131 00132 s_cl_struct::operator= (r); 00133 check = r.check; 00134 return *this; 00135 } 00136 }; 00138 struct s_cl_ignorerc : public s_cl_struct { 00139 bool ignore; 00140 inline s_cl_ignorerc() : s_cl_struct(), ignore (false) {} 00141 inline s_cl_ignorerc (const s_cl_ignorerc& r) : s_cl_struct (r) { 00142 ignore = r.ignore ; 00143 } 00144 inline const s_cl_ignorerc& operator= (const s_cl_ignorerc& r) { 00145 if (&r == this) 00146 return *this; 00147 00148 s_cl_struct::operator= (r); 00149 ignore = r.ignore; 00150 return *this; 00151 } 00152 }; 00153 s_cl_petfile cl_petfile; 00154 s_cl_timeout cl_timeout; 00155 s_cl_filesecurity cl_filesecurity; 00156 s_cl_ignorerc cl_ignorerc; 00157 00159 std::string cleanupPath (const std::string& s) const; 00160 00161 public: 00162 static std::string getDefPetfile(); 00163 static unsigned int getDefTimeout(); 00164 static bool getDefFilesecurity(); 00165 static bool getDefIgnorerc(); 00166 static YAPET::PWGEN::RNGENGINE getDefPWGenRNG(); 00167 static size_t getDefPWLength(); 00168 static int getDefCharPools(); 00169 static bool getDefAllowLockQuit(); 00170 static unsigned int getDefPwInputTimeout(); 00171 static bool getDefCPoolLetters(); 00172 static bool getDefCPoolDigits(); 00173 static bool getDefCPoolPunct(); 00174 static bool getDefCPoolSpecial(); 00175 static bool getDefCPoolOther(); 00176 00177 Config(); 00178 Config (const Config& c); 00179 ~Config(); 00180 00181 void loadConfigFile (std::string filename = ""); 00182 00191 inline void setPetFile (std::string s) { 00192 cl_petfile.set_on_cl = true; 00193 cl_petfile.name = s; 00194 00195 if ( cl_petfile.name.find (YAPET::CONSTS::Consts::getDefaultSuffix(), 00196 cl_petfile.name.length() - 00197 YAPET::CONSTS::Consts::getDefaultSuffix().length() ) 00198 == std::string::npos ) 00199 cl_petfile.name += YAPET::CONSTS::Consts::getDefaultSuffix(); 00200 00201 cl_petfile.name = cleanupPath (cl_petfile.name); 00202 } 00203 inline void setTimeout (int i) { 00204 cl_timeout.set_on_cl = true; 00205 cl_timeout.amount = i; 00206 } 00207 inline void setFilesecurity (bool b) { 00208 cl_filesecurity.set_on_cl = true; 00209 cl_filesecurity.check = b; 00210 } 00211 inline void setIgnorerc (bool b) { 00212 cl_ignorerc.set_on_cl = true; 00213 cl_ignorerc.ignore = b; 00214 } 00215 00217 std::string getPetFile() const; 00218 int getTimeout() const; 00219 bool getFilesecurity() const; 00220 YAPET::PWGEN::RNGENGINE getPWGenRNG() const; 00221 size_t getPWGenPWLen() const; 00222 int getCharPools() const; 00223 bool getAllowLockQuit() const; 00224 unsigned int getPwInputTimeout() const; 00225 00226 const Config& operator= (const Config& c); 00227 }; 00228 00229 } 00230 00231 } 00232 00233 #endif // _CFG_H
1.7.1