StreamTokenizer.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. //
  2. // StreamTokenizer.h
  3. //
  4. // $Id: //poco/1.4/Foundation/include/Poco/StreamTokenizer.h#1 $
  5. //
  6. // Library: Foundation
  7. // Package: Streams
  8. // Module: StreamTokenizer
  9. //
  10. // Definition of the StreamTokenizer class.
  11. //
  12. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  13. // and Contributors.
  14. //
  15. // SPDX-License-Identifier: BSL-1.0
  16. //
  17. #ifndef Foundation_StreamTokenizer_INCLUDED
  18. #define Foundation_StreamTokenizer_INCLUDED
  19. #include "Poco/Foundation.h"
  20. #include "Poco/Token.h"
  21. #include <istream>
  22. #include <vector>
  23. namespace Poco {
  24. class Foundation_API StreamTokenizer
  25. /// A stream tokenizer splits an input stream
  26. /// into a sequence of tokens of different kinds.
  27. /// Various token kinds can be registered with
  28. /// the tokenizer.
  29. {
  30. public:
  31. StreamTokenizer();
  32. /// Creates a StreamTokenizer with no attached stream.
  33. StreamTokenizer(std::istream& istr);
  34. /// Creates a StreamTokenizer with no attached stream.
  35. virtual ~StreamTokenizer();
  36. /// Destroys the StreamTokenizer and deletes all
  37. /// registered tokens.
  38. void attachToStream(std::istream& istr);
  39. /// Attaches the tokenizer to an input stream.
  40. void addToken(Token* pToken);
  41. /// Adds a token class to the tokenizer. The
  42. /// tokenizer takes ownership of the token and
  43. /// deletes it when no longer needed. Comment
  44. /// and whitespace tokens will be marked as
  45. /// ignorable, which means that next() will not
  46. /// return them.
  47. void addToken(Token* pToken, bool ignore);
  48. /// Adds a token class to the tokenizer. The
  49. /// tokenizer takes ownership of the token and
  50. /// deletes it when no longer needed.
  51. /// If ignore is true, the token will be marked
  52. /// as ignorable, which means that next() will
  53. /// not return it.
  54. const Token* next();
  55. /// Extracts the next token from the input stream.
  56. /// Returns a pointer to an EOFToken if there are
  57. /// no more characters to read.
  58. /// Returns a pointer to an InvalidToken if an
  59. /// invalid character is encountered.
  60. /// If a token is marked as ignorable, it will not
  61. /// be returned, and the next token will be
  62. /// examined.
  63. /// Never returns a NULL pointer.
  64. /// You must not delete the token returned by next().
  65. private:
  66. struct TokenInfo
  67. {
  68. Token* pToken;
  69. bool ignore;
  70. };
  71. typedef std::vector<TokenInfo> TokenVec;
  72. TokenVec _tokens;
  73. std::istream* _pIstr;
  74. InvalidToken _invalidToken;
  75. EOFToken _eofToken;
  76. };
  77. } // namespace Poco
  78. #endif // Foundation_StreamTokenizer_INCLUDED