選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

148 行
3.5KB

  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) return m_buf[m_pos++];
  58. setstate(eofbit);
  59. return -1;
  60. }
  61. void getpos(FatPos_t *pos) {
  62. pos->position = m_pos;
  63. }
  64. bool seekoff(off_type off, seekdir way) {return false;}
  65. bool seekpos(pos_type pos) {
  66. if (pos < m_len) {
  67. m_pos = pos;
  68. return true;
  69. }
  70. return false;
  71. }
  72. void setpos(FatPos_t *pos) {
  73. m_pos = pos->position;
  74. }
  75. pos_type tellpos() {
  76. return m_pos;
  77. }
  78. /// @endcond
  79. private:
  80. const char* m_buf;
  81. size_t m_len;
  82. size_t m_pos;
  83. };
  84. //==============================================================================
  85. /**
  86. * \class obufstream
  87. * \brief format a char string
  88. */
  89. class obufstream : public ostream {
  90. public:
  91. /** constructor */
  92. obufstream() : m_in(0) {}
  93. /** Constructor
  94. * \param[in] buf buffer for formatted string
  95. * \param[in] size buffer size
  96. */
  97. obufstream(char *buf, size_t size) {
  98. init(buf, size);
  99. }
  100. /** Initialize an obufstream
  101. * \param[in] buf buffer for formatted string
  102. * \param[in] size buffer size
  103. */
  104. void init(char *buf, size_t size) {
  105. m_buf = buf;
  106. buf[0] = '\0';
  107. m_size = size;
  108. m_in = 0;
  109. }
  110. /** \return a pointer to the buffer */
  111. char* buf() {return m_buf;}
  112. /** \return the length of the formatted string */
  113. size_t length() {return m_in;}
  114. protected:
  115. /// @cond SHOW_PROTECTED
  116. void putch(char c) {
  117. if (m_in >= (m_size - 1)) {
  118. setstate(badbit);
  119. return;
  120. }
  121. m_buf[m_in++] = c;
  122. m_buf[m_in]= '\0';
  123. }
  124. void putstr(const char *str) {
  125. while (*str) putch(*str++);
  126. }
  127. bool seekoff(off_type off, seekdir way) {return false;}
  128. bool seekpos(pos_type pos) {
  129. if (pos > m_in) return false;
  130. m_in = pos;
  131. m_buf[m_in] = '\0';
  132. return true;
  133. }
  134. bool sync() {return true;}
  135. pos_type tellpos() {
  136. return m_in;
  137. }
  138. /// @endcond
  139. private:
  140. char *m_buf;
  141. size_t m_size;
  142. size_t m_in;
  143. };
  144. #endif // bufstream_h