Token.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. //
  2. // Token.cpp
  3. //
  4. // $Id: //poco/1.4/Foundation/src/Token.cpp#1 $
  5. //
  6. // Library: Foundation
  7. // Package: Streams
  8. // Module: StringTokenizer
  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/Token.h"
  16. #include "Poco/NumberParser.h"
  17. #include "Poco/Ascii.h"
  18. namespace Poco {
  19. Token::Token()
  20. {
  21. }
  22. Token::~Token()
  23. {
  24. }
  25. bool Token::start(char c, std::istream& istr)
  26. {
  27. _value = c;
  28. return false;
  29. }
  30. void Token::finish(std::istream& istr)
  31. {
  32. }
  33. Token::Class Token::tokenClass() const
  34. {
  35. return INVALID_TOKEN;
  36. }
  37. std::string Token::asString() const
  38. {
  39. return _value;
  40. }
  41. #if defined(POCO_HAVE_INT64)
  42. Int64 Token::asInteger64() const
  43. {
  44. return NumberParser::parse64(_value);
  45. }
  46. UInt64 Token::asUnsignedInteger64() const
  47. {
  48. return NumberParser::parseUnsigned64(_value);
  49. }
  50. #endif
  51. int Token::asInteger() const
  52. {
  53. return NumberParser::parse(_value);
  54. }
  55. unsigned Token::asUnsignedInteger() const
  56. {
  57. return NumberParser::parseUnsigned(_value);
  58. }
  59. double Token::asFloat() const
  60. {
  61. return NumberParser::parseFloat(_value);
  62. }
  63. char Token::asChar() const
  64. {
  65. return _value.empty() ? 0 : _value[0];
  66. }
  67. InvalidToken::InvalidToken()
  68. {
  69. }
  70. InvalidToken::~InvalidToken()
  71. {
  72. }
  73. Token::Class InvalidToken::tokenClass() const
  74. {
  75. return INVALID_TOKEN;
  76. }
  77. EOFToken::EOFToken()
  78. {
  79. }
  80. EOFToken::~EOFToken()
  81. {
  82. }
  83. Token::Class EOFToken::tokenClass() const
  84. {
  85. return EOF_TOKEN;
  86. }
  87. WhitespaceToken::WhitespaceToken()
  88. {
  89. }
  90. WhitespaceToken::~WhitespaceToken()
  91. {
  92. }
  93. Token::Class WhitespaceToken::tokenClass() const
  94. {
  95. return WHITESPACE_TOKEN;
  96. }
  97. bool WhitespaceToken::start(char c, std::istream& istr)
  98. {
  99. if (Ascii::isSpace(c))
  100. {
  101. _value = c;
  102. return true;
  103. }
  104. return false;
  105. }
  106. void WhitespaceToken::finish(std::istream& istr)
  107. {
  108. int c = istr.peek();
  109. while (Ascii::isSpace(c))
  110. {
  111. istr.get();
  112. _value += (char) c;
  113. c = istr.peek();
  114. }
  115. }
  116. } // namespace Poco