00001 // -*- c++ -*- 00002 // 00003 // $Id: charpool.h 3343 2010-09-17 18:36:31Z java $ 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 _CHARPOOL_H 00024 #define _CHARPOOL_H 00025 00026 #ifdef HAVE_CONFIG_H 00027 # include "config.h" 00028 #endif 00029 00030 #ifdef HAVE_ASSERT_H 00031 # include <assert.h> 00032 #endif 00033 00034 #ifdef HAVE_STDEXCEPT 00035 # include <stdexcept> 00036 #endif 00037 00038 #ifdef HAVE_STRING_H 00039 # include <string.h> 00040 #endif 00041 00042 #include "../../intl.h" 00043 00044 namespace YAPET { 00045 namespace PWGEN { 00051 enum SUBPOOLS { 00055 LETTERS = (1 << 0), 00059 DIGITS = (1 << 1), 00063 PUNCT = (1 << 2), 00067 SPECIAL = (1 << 3), 00071 OTHER = (1 << 4), 00072 ALL = LETTERS | DIGITS | PUNCT | SPECIAL | OTHER, 00073 // Used only by fromPool() 00074 NOPOOL = 0 00075 }; 00081 class CharacterPool { 00082 private: 00083 static const char letters[]; 00084 static const char digits[]; 00085 static const char punct[]; 00086 static const char special[]; 00087 static const char other[]; 00088 00098 char* pool; 00099 00100 size_t pool_length; 00101 00105 int pools_allocated; 00106 00110 int pools_reads; 00111 00112 void init (int p) throw (std::runtime_error); 00113 00114 protected: 00115 static const char* get_letters(); 00116 static const char* get_digits(); 00117 static const char* get_punct(); 00118 static const char* get_special(); 00119 static const char* get_other(); 00120 00121 inline size_t pool_letters_len() const { 00122 return strlen (letters); 00123 } 00124 inline size_t pool_digits_len() const { 00125 return strlen (digits); 00126 } 00127 inline size_t pool_punct_len() const { 00128 return strlen (punct); 00129 } 00130 inline size_t pool_special_len() const { 00131 return strlen (special); 00132 } 00133 inline size_t pool_other_len() const { 00134 return strlen (other); 00135 } 00136 inline size_t pool_len(SUBPOOLS p) const { 00137 switch (p) { 00138 case LETTERS: 00139 return pool_letters_len(); 00140 case DIGITS: 00141 return pool_digits_len(); 00142 case PUNCT: 00143 return pool_punct_len(); 00144 case SPECIAL: 00145 return pool_special_len(); 00146 case OTHER: 00147 return pool_other_len(); 00148 case ALL: 00149 return pool_letters_len() + 00150 pool_digits_len() + 00151 pool_punct_len() + 00152 pool_special_len() + 00153 pool_other_len(); 00154 default: 00155 assert(0); 00156 return -1; 00157 } 00158 } 00159 00160 inline const char* getPool (size_t* len) const throw (std::runtime_error) { 00161 #ifndef NDEBUG 00162 assert (len != NULL); 00163 #else 00164 00165 if (len == NULL) 00166 throw std::runtime_error (_ ("NULL pointer passed.") ); 00167 00168 #endif 00169 *len = pool_length; 00170 return pool; 00171 } 00172 inline const char* getPool() const throw() { 00173 return pool; 00174 } 00175 00176 public: 00177 explicit CharacterPool (int p) throw (std::runtime_error); 00178 explicit CharacterPool (SUBPOOLS p) throw (std::runtime_error); 00179 virtual ~CharacterPool() throw(); 00180 CharacterPool (const CharacterPool& cp) throw (std::runtime_error); 00181 00185 int numPoolsAllocated() const; 00186 00187 inline int getAllocatedPools() const { 00188 return pools_allocated; 00189 } 00190 inline bool isPoolAllocated (SUBPOOLS p) const { 00191 return (pools_allocated & p) ? true : false; 00192 } 00193 inline bool hadPoolReads (SUBPOOLS p) const { 00194 return (pools_reads & p) ? true : false; 00195 } 00203 inline int getPoolsWithRead() const { 00204 return pools_reads; 00205 } 00212 inline void resetPoolsWithRead() { 00213 pools_reads = 0; 00214 } 00220 int numPoolsNotRead () const; 00221 00223 size_t getPoolPos(SUBPOOLS p, size_t* start) const; 00224 00226 SUBPOOLS fromPool(char c) const; 00227 00228 inline size_t getPoolLength() const throw() { 00229 return pool_length; 00230 } 00231 00233 char operator[] (size_t pos) throw (std::logic_error); 00234 00235 inline size_t operator() () const throw() { 00236 return getPoolLength(); 00237 } 00238 00239 const CharacterPool& operator= (const CharacterPool& cp) throw (std::runtime_error); 00240 #ifdef DEBUG 00241 int print_pools_allocated() const; 00242 #endif 00243 }; 00244 00245 } 00246 } 00247 00248 #endif // _CHARPOOL_H
1.7.1