ysfx_reader.hpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright 2021 Jean Pierre Cimalando
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. // SPDX-License-Identifier: Apache-2.0
  16. //
  17. #pragma once
  18. #include <string>
  19. #include <cstdio>
  20. #include <cstddef>
  21. namespace ysfx {
  22. class text_reader
  23. {
  24. public:
  25. virtual ~text_reader() = default;
  26. virtual char read_next_char() = 0;
  27. virtual char peek_next_char() = 0;
  28. virtual void rewind() = 0;
  29. bool read_next_line(std::string &line);
  30. };
  31. //------------------------------------------------------------------------------
  32. class string_text_reader : public text_reader
  33. {
  34. public:
  35. explicit string_text_reader(const char *text) : m_char_ptr(text), m_char_start(text) {}
  36. char read_next_char() override;
  37. char peek_next_char() override;
  38. void rewind() override;
  39. private:
  40. const char *m_char_ptr = nullptr;
  41. const char *m_char_start = nullptr;
  42. };
  43. //------------------------------------------------------------------------------
  44. class stdio_text_reader : public text_reader
  45. {
  46. public:
  47. explicit stdio_text_reader(FILE *stream) : m_stream(stream) {}
  48. char read_next_char() override;
  49. char peek_next_char() override;
  50. void rewind() override;
  51. private:
  52. FILE *m_stream = nullptr;
  53. };
  54. } // namespace ysfx