HexBinaryDecoder.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. //
  2. // HexBinaryDecoder.cpp
  3. //
  4. // $Id: //poco/1.4/Foundation/src/HexBinaryDecoder.cpp#2 $
  5. //
  6. // Library: Foundation
  7. // Package: Streams
  8. // Module: HexBinary
  9. //
  10. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #include "Poco/HexBinaryDecoder.h"
  16. #include "Poco/Exception.h"
  17. namespace Poco {
  18. HexBinaryDecoderBuf::HexBinaryDecoderBuf(std::istream& istr):
  19. _buf(*istr.rdbuf())
  20. {
  21. }
  22. HexBinaryDecoderBuf::~HexBinaryDecoderBuf()
  23. {
  24. }
  25. int HexBinaryDecoderBuf::readFromDevice()
  26. {
  27. int c;
  28. int n;
  29. if ((n = readOne()) == -1) return -1;
  30. if (n >= '0' && n <= '9')
  31. c = n - '0';
  32. else if (n >= 'A' && n <= 'F')
  33. c = n - 'A' + 10;
  34. else if (n >= 'a' && n <= 'f')
  35. c = n - 'a' + 10;
  36. else throw DataFormatException();
  37. c <<= 4;
  38. if ((n = readOne()) == -1) throw DataFormatException();
  39. if (n >= '0' && n <= '9')
  40. c |= n - '0';
  41. else if (n >= 'A' && n <= 'F')
  42. c |= n - 'A' + 10;
  43. else if (n >= 'a' && n <= 'f')
  44. c |= n - 'a' + 10;
  45. else throw DataFormatException();
  46. return c;
  47. }
  48. int HexBinaryDecoderBuf::readOne()
  49. {
  50. int ch = _buf.sbumpc();
  51. while (ch == ' ' || ch == '\r' || ch == '\t' || ch == '\n')
  52. ch = _buf.sbumpc();
  53. return ch;
  54. }
  55. HexBinaryDecoderIOS::HexBinaryDecoderIOS(std::istream& istr): _buf(istr)
  56. {
  57. poco_ios_init(&_buf);
  58. }
  59. HexBinaryDecoderIOS::~HexBinaryDecoderIOS()
  60. {
  61. }
  62. HexBinaryDecoderBuf* HexBinaryDecoderIOS::rdbuf()
  63. {
  64. return &_buf;
  65. }
  66. HexBinaryDecoder::HexBinaryDecoder(std::istream& istr): HexBinaryDecoderIOS(istr), std::istream(&_buf)
  67. {
  68. }
  69. HexBinaryDecoder::~HexBinaryDecoder()
  70. {
  71. }
  72. } // namespace Poco