You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /* FatLib Library
  2. * Copyright (C) 2013 by William Greiman
  3. *
  4. * This file is part of the FatLib Library
  5. *
  6. * This Library is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This Library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with the FatLib Library. If not, see
  18. * <http://www.gnu.org/licenses/>.
  19. */
  20. #ifndef bufstream_h
  21. #define bufstream_h
  22. /**
  23. * \file
  24. * \brief \ref ibufstream and \ref obufstream classes
  25. */
  26. #include <string.h>
  27. #include "iostream.h"
  28. //==============================================================================
  29. /**
  30. * \class ibufstream
  31. * \brief parse a char string
  32. */
  33. class ibufstream : public istream {
  34. public:
  35. /** Constructor */
  36. ibufstream() : m_buf(0), m_len(0) {}
  37. /** Constructor
  38. * \param[in] str pointer to string to be parsed
  39. * Warning: The string will not be copied so must stay in scope.
  40. */
  41. explicit ibufstream(const char* str) {
  42. init(str);
  43. }
  44. /** Initialize an ibufstream
  45. * \param[in] str pointer to string to be parsed
  46. * Warning: The string will not be copied so must stay in scope.
  47. */
  48. void init(const char* str) {
  49. m_buf = str;
  50. m_len = strlen(m_buf);
  51. m_pos = 0;
  52. clear();
  53. }
  54. protected:
  55. /// @cond SHOW_PROTECTED
  56. int16_t getch() {
  57. if (m_pos < m_len) {
  58. return m_buf[m_pos++];
  59. }
  60. setstate(eofbit);
  61. return -1;
  62. }
  63. void getpos(FatPos_t *pos) {
  64. pos->position = m_pos;
  65. }
  66. bool seekoff(off_type off, seekdir way) {
  67. return false;
  68. }
  69. bool seekpos(pos_type pos) {
  70. if (pos < m_len) {
  71. m_pos = pos;
  72. return true;
  73. }
  74. return false;
  75. }
  76. void setpos(FatPos_t *pos) {
  77. m_pos = pos->position;
  78. }
  79. pos_type tellpos() {
  80. return m_pos;
  81. }
  82. /// @endcond
  83. private:
  84. const char* m_buf;
  85. size_t m_len;
  86. size_t m_pos;
  87. };
  88. //==============================================================================
  89. /**
  90. * \class obufstream
  91. * \brief format a char string
  92. */
  93. class obufstream : public ostream {
  94. public:
  95. /** constructor */
  96. obufstream() : m_in(0) {}
  97. /** Constructor
  98. * \param[in] buf buffer for formatted string
  99. * \param[in] size buffer size
  100. */
  101. obufstream(char *buf, size_t size) {
  102. init(buf, size);
  103. }
  104. /** Initialize an obufstream
  105. * \param[in] buf buffer for formatted string
  106. * \param[in] size buffer size
  107. */
  108. void init(char *buf, size_t size) {
  109. m_buf = buf;
  110. buf[0] = '\0';
  111. m_size = size;
  112. m_in = 0;
  113. }
  114. /** \return a pointer to the buffer */
  115. char* buf() {
  116. return m_buf;
  117. }
  118. /** \return the length of the formatted string */
  119. size_t length() {
  120. return m_in;
  121. }
  122. protected:
  123. /// @cond SHOW_PROTECTED
  124. void putch(char c) {
  125. if (m_in >= (m_size - 1)) {
  126. setstate(badbit);
  127. return;
  128. }
  129. m_buf[m_in++] = c;
  130. m_buf[m_in] = '\0';
  131. }
  132. void putstr(const char *str) {
  133. while (*str) {
  134. putch(*str++);
  135. }
  136. }
  137. bool seekoff(off_type off, seekdir way) {
  138. return false;
  139. }
  140. bool seekpos(pos_type pos) {
  141. if (pos > m_in) {
  142. return false;
  143. }
  144. m_in = pos;
  145. m_buf[m_in] = '\0';
  146. return true;
  147. }
  148. bool sync() {
  149. return true;
  150. }
  151. pos_type tellpos() {
  152. return m_in;
  153. }
  154. /// @endcond
  155. private:
  156. char *m_buf;
  157. size_t m_size;
  158. size_t m_in;
  159. };
  160. #endif // bufstream_h