StreamTokenizer.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. //
  2. // StreamTokenizer.cpp
  3. //
  4. // $Id: //poco/1.4/Foundation/src/StreamTokenizer.cpp#1 $
  5. //
  6. // Library: Foundation
  7. // Package: Streams
  8. // Module: StreamTokenizer
  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/StreamTokenizer.h"
  16. namespace Poco {
  17. StreamTokenizer::StreamTokenizer():
  18. _pIstr(0)
  19. {
  20. }
  21. StreamTokenizer::StreamTokenizer(std::istream& istr):
  22. _pIstr(&istr)
  23. {
  24. }
  25. StreamTokenizer::~StreamTokenizer()
  26. {
  27. for (TokenVec::iterator it = _tokens.begin(); it != _tokens.end(); ++it)
  28. {
  29. delete it->pToken;
  30. }
  31. }
  32. void StreamTokenizer::attachToStream(std::istream& istr)
  33. {
  34. _pIstr = &istr;
  35. }
  36. void StreamTokenizer::addToken(Token* pToken)
  37. {
  38. poco_check_ptr (pToken);
  39. TokenInfo ti;
  40. ti.pToken = pToken;
  41. ti.ignore = (pToken->tokenClass() == Token::COMMENT_TOKEN || pToken->tokenClass() == Token::WHITESPACE_TOKEN);
  42. _tokens.push_back(ti);
  43. }
  44. void StreamTokenizer::addToken(Token* pToken, bool ignore)
  45. {
  46. poco_check_ptr (pToken);
  47. TokenInfo ti;
  48. ti.pToken = pToken;
  49. ti.ignore = ignore;
  50. _tokens.push_back(ti);
  51. }
  52. const Token* StreamTokenizer::next()
  53. {
  54. poco_check_ptr (_pIstr);
  55. static const int eof = std::char_traits<char>::eof();
  56. int first = _pIstr->get();
  57. TokenVec::const_iterator it = _tokens.begin();
  58. while (first != eof && it != _tokens.end())
  59. {
  60. const TokenInfo& ti = *it;
  61. if (ti.pToken->start((char) first, *_pIstr))
  62. {
  63. ti.pToken->finish(*_pIstr);
  64. if (ti.ignore)
  65. {
  66. first = _pIstr->get();
  67. it = _tokens.begin();
  68. }
  69. else return ti.pToken;
  70. }
  71. else ++it;
  72. }
  73. if (first == eof)
  74. {
  75. return &_eofToken;
  76. }
  77. else
  78. {
  79. _invalidToken.start((char) first, *_pIstr);
  80. return &_invalidToken;
  81. }
  82. }
  83. } // namespace Poco