00001
00008
00009
00010
00011
00012 #ifndef ECL_CONVERTERS_TO_BYTE_ARRAY_HPP_
00013 #define ECL_CONVERTERS_TO_BYTE_ARRAY_HPP_
00014
00015
00016
00017
00018
00019 #include <vector>
00020 #include <ecl/concepts/containers.hpp>
00021 #include <ecl/errors/compile_time_assert.hpp>
00022 #include <ecl/exceptions/standard_exception.hpp>
00023 #include <ecl/exceptions/macros.hpp>
00024 #include <ecl/type_traits/fundamental_types.hpp>
00025 #include <ecl/type_traits/numeric_limits.hpp>
00026 #include "converter.hpp"
00027 #include <iostream>
00028
00029
00030
00031
00032 namespace ecl
00033 {
00034
00038 namespace converters
00039 {
00040
00041
00042
00043
00044
00055 template<typename ByteArray, typename Integral>
00056 class IntegralToByteArray : public ConverterBase
00057 {
00058 public:
00078 const ByteArray& operator()(ByteArray &byte_array, const Integral &input) ecl_debug_throw_decl(ecl::StandardException)
00079 {
00080
00081
00082
00083 ecl_compile_time_concept_check(ecl::ContainerConcept<ByteArray>);
00084 ecl_compile_time_concept_check(ecl::ByteContainerConcept<ByteArray>);
00085 ecl_compile_time_assert(is_integral<Integral>::value);
00086
00087
00088
00089 if (byte_array.size() != ecl::numeric_limits<Integral>::bytes)
00090 {
00091 std::cout << "Failed size check" << std::endl;
00092 ecl_debug_throw(
00093 StandardException(LOC,ConversionError,"The specified byte array size does not match the byte size of the integral type."));
00094 error_handler = ConversionError;
00095 return byte_array;
00096 }
00097
00098
00099
00100
00101 for (unsigned int i = 0; i < ecl::numeric_limits<Integral>::bytes; ++i)
00102 {
00103 byte_array[i] = static_cast<typename ByteArray::value_type>(input >> 8 * i);
00104
00105
00106 }
00107 return byte_array;
00108 }
00112 virtual ~IntegralToByteArray() {}
00113 };
00114
00115 }
00120
00121
00122
00126 template<>
00127 class Converter<std::vector<char>, int> : public converters::IntegralToByteArray<std::vector<char>, int>
00128 {
00129 };
00130
00134 template<>
00135 class Converter<std::vector<unsigned char>, int> : public converters::IntegralToByteArray<std::vector<unsigned char>,
00136 int>
00137 {
00138 };
00139
00143 template<>
00144 class Converter<std::vector<signed char>, int> : public converters::IntegralToByteArray<std::vector<signed char>, int>
00145 {
00146 };
00147
00151 template<>
00152 class Converter<std::vector<char>, unsigned int> : public converters::IntegralToByteArray<std::vector<char>,
00153 unsigned int>
00154 {
00155 };
00156
00160 template<>
00161 class Converter<std::vector<unsigned char>, unsigned int> : public converters::IntegralToByteArray<
00162 std::vector<unsigned char>, unsigned int>
00163 {
00164 };
00165
00169 template<>
00170 class Converter<std::vector<signed char>, unsigned int> : public converters::IntegralToByteArray<
00171 std::vector<signed char>, unsigned int>
00172 {
00173 };
00174
00178 template<>
00179 class Converter<std::vector<char>, long> : public converters::IntegralToByteArray<std::vector<char>, long>
00180 {
00181 };
00182
00186 template<>
00187 class Converter<std::vector<unsigned char>, long> : public converters::IntegralToByteArray<std::vector<unsigned char>,
00188 long>
00189 {
00190 };
00191
00195 template<>
00196 class Converter<std::vector<signed char>, long> : public converters::IntegralToByteArray<std::vector<signed char>, long>
00197 {
00198 };
00199
00203 template<>
00204 class Converter<std::vector<char>, unsigned long> : public converters::IntegralToByteArray<std::vector<char>,
00205 unsigned long>
00206 {
00207 };
00208
00212 template<>
00213 class Converter<std::vector<unsigned char>, unsigned long> : public converters::IntegralToByteArray<
00214 std::vector<unsigned char>, unsigned long>
00215 {
00216 };
00217
00221 template<>
00222 class Converter<std::vector<signed char>, unsigned long> : public converters::IntegralToByteArray<
00223 std::vector<signed char>, unsigned long>
00224 {
00225 };
00226
00230 template<>
00231 class Converter<std::vector<char>, long long> : public converters::IntegralToByteArray<std::vector<char>, long long>
00232 {
00233 };
00234
00238 template<>
00239 class Converter<std::vector<unsigned char>, long long> : public converters::IntegralToByteArray<std::vector<unsigned char>,
00240 long long>
00241 {
00242 };
00243
00247 template<>
00248 class Converter<std::vector<signed char>, long long> : public converters::IntegralToByteArray<std::vector<signed char>, long long>
00249 {
00250 };
00251
00252
00253
00254
00262 template<>
00263 class Converter<std::vector<char>, char*> : public converters::ConverterBase
00264 {
00265 public:
00283 std::vector<char> operator ()(const char* input) ecl_debug_throw_decl(StandardException)
00284 {
00285
00286 std::vector<char> bytes;
00287 state = waiting_zero;
00288
00289 while (*input != '\0')
00290 {
00291 switch (state)
00292 {
00293 case (waiting_zero):
00294 {
00295 if (*input == '0')
00296 {
00297 state = waiting_x;
00298 }
00299 else if (*input == ' ')
00300 {
00301
00302 }
00303 else
00304 {
00305 ecl_debug_throw(StandardException(LOC,ConversionError));
00306 error_handler = ConversionError;
00307 bytes.clear();
00308 return bytes;
00309 }
00310 break;
00311 }
00312 case (waiting_x):
00313 {
00314 if (*input == 'x')
00315 {
00316 state = waiting_first_digit;
00317 }
00318 else
00319 {
00320 ecl_debug_throw(StandardException(LOC,ConversionError));
00321 error_handler = ConversionError;
00322 bytes.clear();
00323 return bytes;
00324 }
00325 break;
00326 }
00327 case (waiting_first_digit):
00328 {
00329 if ((*input >= '0') && (*input <= '9'))
00330 {
00331 byte = (*input - '0') << 4;
00332 state = waiting_second_digit;
00333 }
00334 else if ((*input >= 'a') && (*input <= 'f'))
00335 {
00336 byte = ((*input - 'a') + 0x0A) << 4;
00337 state = waiting_second_digit;
00338 }
00339 else if ((*input >= 'A') && (*input <= 'F'))
00340 {
00341 byte = ((*input - 'A') + 0x0A) << 4;
00342 state = waiting_second_digit;
00343 }
00344 else
00345 {
00346 ecl_debug_throw(StandardException(LOC,ConversionError));
00347 error_handler = ConversionError;
00348 bytes.clear();
00349 return bytes;
00350 }
00351 break;
00352 }
00353 case (waiting_second_digit):
00354 {
00355 if ((*input >= '0') && (*input <= '9'))
00356 {
00357 byte += *input - '0';
00358 bytes.push_back(byte);
00359 }
00360 else if ((*input >= 'a') && (*input <= 'f'))
00361 {
00362 byte += (*input - 'a') + 0x0A;
00363 bytes.push_back(byte);
00364 }
00365 else if ((*input >= 'A') && (*input <= 'F'))
00366 {
00367 byte += (*input - 'A') + 0x0A;
00368 bytes.push_back(byte);
00369 }
00370 else
00371 {
00372 ecl_debug_throw(StandardException(LOC,ConversionError));
00373 error_handler = ConversionError;
00374 bytes.clear();
00375 return bytes;
00376 }
00377 state = waiting_zero;
00378 break;
00379 }
00380 default:
00381 {
00382 state = waiting_zero;
00383 break;
00384 }
00385 }
00386 ++input;
00387 }
00388 return bytes;
00389 }
00390 ;
00391 virtual ~Converter()
00392 {
00393 }
00394
00395 private:
00396 unsigned int state;
00397 char byte;
00398 static const unsigned int waiting_zero = 0;
00399 static const unsigned int waiting_x = 1;
00400 static const unsigned int waiting_first_digit = 2;
00401 static const unsigned int waiting_second_digit = 3;
00402 };
00403
00404
00405
00406
00410 template<>
00411 class Converter<std::vector<char>, void> : public Converter<std::vector<char>, char*>, public Converter<
00412 std::vector<char>, int>,
00413 public Converter<std::vector<char>, unsigned int>, public Converter<
00414 std::vector<char>, long>,
00415 public Converter<std::vector<char>, unsigned long>
00416 {
00417 public:
00418 virtual ~Converter()
00419 {
00420 }
00421
00422 using Converter<std::vector<char>, char*>::operator();
00423 using Converter<std::vector<char>, int>::operator();
00424 using Converter<std::vector<char>, unsigned int>::operator();
00425 using Converter<std::vector<char>, long>::operator();
00426 using Converter<std::vector<char>, unsigned long>::operator();
00427 };
00428
00432 template<>
00433 class Converter<std::vector<unsigned char>, void> : public Converter<std::vector<unsigned char>, int>,
00434 public Converter<std::vector<unsigned char>, unsigned int>,
00435 public Converter<std::vector<unsigned char>, long>,
00436 public Converter<std::vector<unsigned char>, unsigned long>
00437 {
00438 public:
00439 virtual ~Converter()
00440 {
00441 }
00442
00443 using Converter<std::vector<unsigned char>, int>::operator();
00444 using Converter<std::vector<unsigned char>, unsigned int>::operator();
00445 using Converter<std::vector<unsigned char>, long>::operator();
00446 using Converter<std::vector<unsigned char>, unsigned long>::operator();
00447 };
00448
00452 template<>
00453 class Converter<std::vector<signed char>, void> : public Converter<std::vector<signed char>, int>,
00454 public Converter<std::vector<signed char>, unsigned int>,
00455 public Converter<std::vector<signed char>, long>,
00456 public Converter<std::vector<signed char>, unsigned long>
00457 {
00458 public:
00459 virtual ~Converter()
00460 {
00461 }
00462
00463 using Converter<std::vector<signed char>, int>::operator();
00464 using Converter<std::vector<signed char>, unsigned int>::operator();
00465 using Converter<std::vector<signed char>, long>::operator();
00466 using Converter<std::vector<signed char>, unsigned long>::operator();
00467 };
00468
00469 }
00470
00471 #endif