StreamTokenizer.h 2.4 KB

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