fstream.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /**
  2. * Copyright (c) 20011-2017 Bill Greiman
  3. * This file is part of the SdFat library for SD memory cards.
  4. *
  5. * MIT License
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a
  8. * copy of this software and associated documentation files (the "Software"),
  9. * to deal in the Software without restriction, including without limitation
  10. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  11. * and/or sell copies of the Software, and to permit persons to whom the
  12. * Software is furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included
  15. * in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  18. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. */
  25. #include "fstream.h"
  26. //==============================================================================
  27. /// @cond SHOW_PROTECTED
  28. int16_t FatStreamBase::getch() {
  29. uint8_t c;
  30. int8_t s = read(&c, 1);
  31. if (s != 1) {
  32. if (s < 0) {
  33. setstate(badbit);
  34. } else {
  35. setstate(eofbit);
  36. }
  37. return -1;
  38. }
  39. if (c != '\r' || (getmode() & ios::binary)) {
  40. return c;
  41. }
  42. s = read(&c, 1);
  43. if (s == 1 && c == '\n') {
  44. return c;
  45. }
  46. if (s == 1) {
  47. seekCur(-1);
  48. }
  49. return '\r';
  50. }
  51. //------------------------------------------------------------------------------
  52. void FatStreamBase::open(const char* path, ios::openmode mode) {
  53. uint8_t flags;
  54. switch (mode & (app | in | out | trunc)) {
  55. case app | in:
  56. case app | in | out:
  57. flags = O_RDWR | O_APPEND | O_CREAT;
  58. break;
  59. case app:
  60. case app | out:
  61. flags = O_WRITE | O_APPEND | O_CREAT;
  62. break;
  63. case in:
  64. flags = O_READ;
  65. break;
  66. case in | out:
  67. flags = O_RDWR;
  68. break;
  69. case in | out | trunc:
  70. flags = O_RDWR | O_TRUNC | O_CREAT;
  71. break;
  72. case out:
  73. case out | trunc:
  74. flags = O_WRITE | O_TRUNC | O_CREAT;
  75. break;
  76. default:
  77. goto fail;
  78. }
  79. if (mode & ios::ate) {
  80. flags |= O_AT_END;
  81. }
  82. if (!FatFile::open(path, flags)) {
  83. goto fail;
  84. }
  85. setmode(mode);
  86. clear();
  87. return;
  88. fail:
  89. FatFile::close();
  90. setstate(failbit);
  91. return;
  92. }
  93. //------------------------------------------------------------------------------
  94. void FatStreamBase::putch(char c) {
  95. if (c == '\n' && !(getmode() & ios::binary)) {
  96. write('\r');
  97. }
  98. write(c);
  99. if (getWriteError()) {
  100. setstate(badbit);
  101. }
  102. }
  103. //------------------------------------------------------------------------------
  104. void FatStreamBase::putstr(const char* str) {
  105. size_t n = 0;
  106. while (1) {
  107. char c = str[n];
  108. if (c == '\0' || (c == '\n' && !(getmode() & ios::binary))) {
  109. if (n > 0) {
  110. write(str, n);
  111. }
  112. if (c == '\0') {
  113. break;
  114. }
  115. write('\r');
  116. str += n;
  117. n = 0;
  118. }
  119. n++;
  120. }
  121. if (getWriteError()) {
  122. setstate(badbit);
  123. }
  124. }
  125. //------------------------------------------------------------------------------
  126. /** Internal do not use
  127. * \param[in] off
  128. * \param[in] way
  129. */
  130. bool FatStreamBase::seekoff(off_type off, seekdir way) {
  131. pos_type pos;
  132. switch (way) {
  133. case beg:
  134. pos = off;
  135. break;
  136. case cur:
  137. pos = curPosition() + off;
  138. break;
  139. case end:
  140. pos = fileSize() + off;
  141. break;
  142. default:
  143. return false;
  144. }
  145. return seekpos(pos);
  146. }
  147. //------------------------------------------------------------------------------
  148. /** Internal do not use
  149. * \param[in] pos
  150. */
  151. bool FatStreamBase::seekpos(pos_type pos) {
  152. return seekSet(pos);
  153. }
  154. //------------------------------------------------------------------------------
  155. int FatStreamBase::write(const void* buf, size_t n) {
  156. return FatFile::write(buf, n);
  157. }
  158. //------------------------------------------------------------------------------
  159. void FatStreamBase::write(char c) {
  160. write(&c, 1);
  161. }
  162. /// @endcond