1 #ifndef __estring_h__ 2 #define __estring_h__ 3 4 #include <string> 5 6 #include "asserts.h" 7 #include "types.h" 8 9 /* TODO: Make estring more OO 10 11 1) Make an estring_value base class 12 2) Make an estring_value class for each type that inherits from the base 13 class 14 3) Revamp estring to use smart pointers to allocate the correct version of 15 estring value based on the value being stored. 16 */ 17 18 class estring_value 19 { 20 public: 21 typedef union { 22 unsigned int ui; 23 int i; 24 unsigned short us; 25 short s; 26 unsigned long ul; 27 long l; 28 unsigned long long ull; 29 long long ll; 30 char const * char_ptr; 31 void * void_ptr; 32 float f; 33 double d; 34 } value_type; 35 36 estring_value(); 37 38 value_type value; 39 40 void clear(void); 41 estring_value& operator=(const estring_value& a_estring_value); 42 43 }; 44 45 /** An extended string class. 46 47 Estring is a derivative of std::string with extra functionality added in 48 order to fascilitate (a) the conversion to strings from other types, (b) 49 special formatting of those converted strings, and (c) the conversion back 50 from a string to some other type. 51 */ 52 class estring : public std::string 53 { 54 public: 55 /** The type from which estring inherits, std::string. */ 56 typedef std::string value_type; 57 58 /** The size type. */ 59 typedef value_type::size_type size_type; 60 61 /** Alignment values for formatted strings. */ 62 enum alignment { 63 /** Left-justified */ 64 left, 65 /** Right-justified */ 66 right, 67 /** Centered */ 68 center 69 }; 70 71 /** The type last assigned. */ 72 enum set_from_type { 73 /** std::string */ 74 type_string, 75 /** unsigned int */ 76 type_unsigned_int, 77 /** int */ 78 type_int, 79 /** unsigned short*/ 80 type_unsigned_short, 81 /** short */ 82 type_short, 83 /** unsigned long */ 84 type_unsigned_long, 85 /** long */ 86 type_long, 87 /** unsigned long long */ 88 type_unsigned_long_long, 89 /** long long */ 90 type_long_long, 91 /** float */ 92 type_float, 93 /** double */ 94 type_double, 95 /** char* */ 96 type_char_ptr, 97 /** void* */ 98 type_void_ptr, 99 /** unknown */ 100 type_unknown 101 }; 102 103 void init(void); 104 void clear(void); 105 void reset(void); 106 estring(); 107 108 // 109 // String Formatting 110 // 111 size_type width(const size_type a_l); 112 size_type width(void) const; 113 alignment align(const alignment a_alignment); 114 alignment align(void) const; 115 char left_fillchar(const char a_char); 116 char left_fillchar(void) const; 117 char right_fillchar(const char a_char); 118 char right_fillchar(void) const; 119 void fillchar(const char a_char); 120 value_type fmt_str(void); 121 value_type fmt_str( 122 const size_type a_width, 123 const alignment a_alignment, 124 const char a_left_fill, 125 const char a_right_fill 126 ); 127 128 // String formatting for float and double 129 size_type precision(size_type a_p); 130 size_type precision(void) const; 131 132 // Formatting for conversion from different bases 133 const unsigned int base(const unsigned int a_base); 134 const unsigned int base(void) const; 135 136 // estring 137 const set_from_type& get_from_type(void) const; 138 const estring_value& get_from_value(void) const; 139 estring(const estring& a_estr); 140 estring& assign(const estring& a_estr); 141 estring& operator=(const estring& a_estr); 142 estring& lower(void); 143 estring& upper(void); 144 145 // char 146 estring(const char a_char); 147 estring& assign(const char a_char); 148 estring& operator=(const char a_char); 149 150 // string 151 estring(const value_type& a_string); 152 estring& assign(const value_type& a_string); 153 estring& operator=(const value_type& a_string); 154 155 // unsigned int 156 estring(const unsigned int a_int); 157 estring& assign(const unsigned int a_int); 158 estring& operator=(const unsigned int a_int); 159 operator unsigned int() const; 160 161 // int 162 estring(const int a_int); 163 estring& assign(const int a_int); 164 estring& operator=(const int a_int); 165 operator int() const; 166 167 // unsigned short 168 estring(const unsigned short a_short); 169 estring& assign(const unsigned short a_short); 170 estring& operator=(const unsigned short a_short); 171 operator unsigned short() const; 172 173 // short 174 estring(const short a_short); 175 estring& assign(const short a_short); 176 estring& operator=(const short a_short); 177 operator short() const; 178 179 // unsigned long 180 estring(const unsigned long a_long); 181 estring& assign(const unsigned long a_long); 182 estring& operator=(const unsigned long a_long); 183 operator unsigned long() const; 184 185 // long 186 estring(const long a_long); 187 estring& assign(const long a_long); 188 estring& operator=(const long a_long); 189 operator long() const; 190 191 // unsigned long long 192 estring(const unsigned long long a_long); 193 estring& assign(const unsigned long long a_long); 194 estring& operator=(const unsigned long long a_long); 195 operator unsigned long long() const; 196 197 // long long 198 estring(const long long a_long); 199 estring& assign(const long long a_long); 200 estring& operator=(const long long a_long); 201 operator long long() const; 202 203 // char* 204 estring(char const * a_ptr); 205 estring& assign(char const * a_ptr); 206 estring& operator=(char const * a_ptr); 207 operator char const *() const; 208 209 // void* 210 estring(void* const a_ptr); 211 estring& assign(void* const a_ptr); 212 estring& operator=(void* const a_ptr); 213 operator void*() const; 214 215 // float 216 estring(const float a_float); 217 estring& assign(const float a_float); 218 estring& operator=(const float a_float); 219 estring(const float a_float, unsigned a_precision, 220 unsigned int a_base = 10); 221 estring& assign(const float a_float, unsigned a_precision, 222 unsigned int a_base = 10); 223 operator float() const; 224 225 // double 226 estring(const double a_double); 227 estring& assign(const double a_double); 228 estring& operator=(const double a_double); 229 estring(const double a_double, unsigned a_precision, 230 unsigned int a_base = 10); 231 estring& assign(const double a_double, unsigned a_precision, 232 unsigned int a_base = 10); 233 operator double() const; 234 235 private: 236 /** The current fractional number precision */ 237 size_type m_precision; 238 /** The current numerical base */ 239 unsigned int m_base; 240 /** The alphabet used for any base from 2 to 36 */ 241 static const char *m_alphabet; 242 /** The length of the alphabet */ 243 static const size_t m_alphabet_len; 244 /** The current formatting width */ 245 size_type m_width; 246 /** The current formatting alignment */ 247 alignment m_alignment; 248 /** The current left-hand fill character */ 249 char m_left_fillchar; 250 /** The current right-hand fill character */ 251 char m_right_fillchar; 252 /** The current value type */ 253 set_from_type m_type; 254 /** The current value */ 255 estring_value m_value; 256 257 template <class T> 258 void T_fraction_to_strings(const T& a_t, 259 value_type& a_ws, value_type& a_fs); 260 261 template <class T> 262 void T_integral_to_string(const T& a_t, value_type& a_str); 263 264 template <class T> 265 void T_string_to_integral(const value_type& a_str, T& a_t) const; 266 267 template <class T> 268 void T_string_to_signed_integral(const value_type& a_str, T& a_t) const; 269 270 template <class T> 271 void T_string_to_fractional(const value_type& a_str, T& a_t) const; 272 }; 273 274 #endif 275