ysfx_reader.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. #include "ysfx_reader.hpp"
  18. namespace ysfx {
  19. //------------------------------------------------------------------------------
  20. bool text_reader::read_next_line(std::string &line)
  21. {
  22. line.clear();
  23. char next = read_next_char();
  24. if (next == '\0')
  25. return false;
  26. while (next != '\0' && next != '\r' && next != '\n') {
  27. line.push_back(next);
  28. next = read_next_char();
  29. }
  30. if (next == '\r') {
  31. next = peek_next_char();
  32. if (next == '\n')
  33. read_next_char();
  34. }
  35. return true;
  36. }
  37. //------------------------------------------------------------------------------
  38. char string_text_reader::read_next_char()
  39. {
  40. const char *ptr = m_char_ptr;
  41. if (!ptr || *ptr == '\0')
  42. return '\0';
  43. char next = *ptr;
  44. m_char_ptr = ptr + 1;
  45. return next;
  46. }
  47. char string_text_reader::peek_next_char()
  48. {
  49. const char *ptr = m_char_ptr;
  50. if (!ptr)
  51. return '\0';
  52. return *ptr;
  53. }
  54. void string_text_reader::rewind()
  55. {
  56. m_char_ptr = m_char_start;
  57. }
  58. //------------------------------------------------------------------------------
  59. char stdio_text_reader::read_next_char()
  60. {
  61. FILE *stream = m_stream;
  62. if (!stream)
  63. return '\0';
  64. int next = fgetc(stream);
  65. if (next == EOF)
  66. return '\0';
  67. return (unsigned char)next;
  68. }
  69. char stdio_text_reader::peek_next_char()
  70. {
  71. FILE *stream = m_stream;
  72. if (!stream)
  73. return '\0';
  74. int next = fgetc(stream);
  75. if (next == EOF)
  76. return '\0';
  77. ungetc(next, stream);
  78. return (unsigned char)next;
  79. }
  80. void stdio_text_reader::rewind()
  81. {
  82. fseek(m_stream, 0, SEEK_SET);
  83. }
  84. } // namespace ysfx