Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

168 lines
3.7KB

  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. #include "fstream.h"
  21. //==============================================================================
  22. /// @cond SHOW_PROTECTED
  23. int16_t FatStreamBase::getch() {
  24. uint8_t c;
  25. int8_t s = read(&c, 1);
  26. if (s != 1) {
  27. if (s < 0) {
  28. setstate(badbit);
  29. } else {
  30. setstate(eofbit);
  31. }
  32. return -1;
  33. }
  34. if (c != '\r' || (getmode() & ios::binary)) {
  35. return c;
  36. }
  37. s = read(&c, 1);
  38. if (s == 1 && c == '\n') {
  39. return c;
  40. }
  41. if (s == 1) {
  42. seekCur(-1);
  43. }
  44. return '\r';
  45. }
  46. //------------------------------------------------------------------------------
  47. void FatStreamBase::open(const char* path, ios::openmode mode) {
  48. uint8_t flags;
  49. switch (mode & (app | in | out | trunc)) {
  50. case app | in:
  51. case app | in | out:
  52. flags = O_RDWR | O_APPEND | O_CREAT;
  53. break;
  54. case app:
  55. case app | out:
  56. flags = O_WRITE | O_APPEND | O_CREAT;
  57. break;
  58. case in:
  59. flags = O_READ;
  60. break;
  61. case in | out:
  62. flags = O_RDWR;
  63. break;
  64. case in | out | trunc:
  65. flags = O_RDWR | O_TRUNC | O_CREAT;
  66. break;
  67. case out:
  68. case out | trunc:
  69. flags = O_WRITE | O_TRUNC | O_CREAT;
  70. break;
  71. default:
  72. goto fail;
  73. }
  74. if (mode & ios::ate) {
  75. flags |= O_AT_END;
  76. }
  77. if (!FatFile::open(path, flags)) {
  78. goto fail;
  79. }
  80. setmode(mode);
  81. clear();
  82. return;
  83. fail:
  84. FatFile::close();
  85. setstate(failbit);
  86. return;
  87. }
  88. //------------------------------------------------------------------------------
  89. void FatStreamBase::putch(char c) {
  90. if (c == '\n' && !(getmode() & ios::binary)) {
  91. write('\r');
  92. }
  93. write(c);
  94. if (getWriteError()) {
  95. setstate(badbit);
  96. }
  97. }
  98. //------------------------------------------------------------------------------
  99. void FatStreamBase::putstr(const char* str) {
  100. size_t n = 0;
  101. while (1) {
  102. char c = str[n];
  103. if (c == '\0' || (c == '\n' && !(getmode() & ios::binary))) {
  104. if (n > 0) {
  105. write(str, n);
  106. }
  107. if (c == '\0') {
  108. break;
  109. }
  110. write('\r');
  111. str += n;
  112. n = 0;
  113. }
  114. n++;
  115. }
  116. if (getWriteError()) {
  117. setstate(badbit);
  118. }
  119. }
  120. //------------------------------------------------------------------------------
  121. /** Internal do not use
  122. * \param[in] off
  123. * \param[in] way
  124. */
  125. bool FatStreamBase::seekoff(off_type off, seekdir way) {
  126. pos_type pos;
  127. switch (way) {
  128. case beg:
  129. pos = off;
  130. break;
  131. case cur:
  132. pos = curPosition() + off;
  133. break;
  134. case end:
  135. pos = fileSize() + off;
  136. break;
  137. default:
  138. return false;
  139. }
  140. return seekpos(pos);
  141. }
  142. //------------------------------------------------------------------------------
  143. /** Internal do not use
  144. * \param[in] pos
  145. */
  146. bool FatStreamBase::seekpos(pos_type pos) {
  147. return seekSet(pos);
  148. }
  149. //------------------------------------------------------------------------------
  150. int FatStreamBase::write(const void* buf, size_t n) {
  151. return FatFile::write(buf, n);
  152. }
  153. //------------------------------------------------------------------------------
  154. void FatStreamBase::write(char c) {
  155. write(&c, 1);
  156. }
  157. /// @endcond