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.

152 lines
3.7KB

  1. /* Arduino SdFat Library
  2. * Copyright (C) 2012 by William Greiman
  3. *
  4. * This file is part of the Arduino SdFat 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 Arduino SdFat Library. If not, see
  18. * <http://www.gnu.org/licenses/>.
  19. */
  20. #include <SdFat.h>
  21. //==============================================================================
  22. /// @cond SHOW_PROTECTED
  23. int16_t SdStreamBase::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)) return c;
  35. s = read(&c, 1);
  36. if (s == 1 && c == '\n') return c;
  37. if (s == 1) seekCur(-1);
  38. return '\r';
  39. }
  40. //------------------------------------------------------------------------------
  41. void SdStreamBase::open(const char* path, ios::openmode mode) {
  42. uint8_t flags;
  43. switch (mode & (app | in | out | trunc)) {
  44. case app | in:
  45. case app | in | out:
  46. flags = O_RDWR | O_APPEND | O_CREAT;
  47. break;
  48. case app:
  49. case app | out:
  50. flags = O_WRITE | O_APPEND | O_CREAT;
  51. break;
  52. case in:
  53. flags = O_READ;
  54. break;
  55. case in | out:
  56. flags = O_RDWR;
  57. break;
  58. case in | out | trunc:
  59. flags = O_RDWR | O_TRUNC | O_CREAT;
  60. break;
  61. case out:
  62. case out | trunc:
  63. flags = O_WRITE | O_TRUNC | O_CREAT;
  64. break;
  65. default:
  66. goto fail;
  67. }
  68. if (mode & ios::ate) flags |= O_AT_END;
  69. if (!SdBaseFile::open(path, flags)) goto fail;
  70. setmode(mode);
  71. clear();
  72. return;
  73. fail:
  74. SdBaseFile::close();
  75. setstate(failbit);
  76. return;
  77. }
  78. //------------------------------------------------------------------------------
  79. void SdStreamBase::putch(char c) {
  80. if (c == '\n' && !(getmode() & ios::binary)) {
  81. write('\r');
  82. }
  83. write(c);
  84. if (writeError) setstate(badbit);
  85. }
  86. //------------------------------------------------------------------------------
  87. void SdStreamBase::putstr(const char* str) {
  88. size_t n = 0;
  89. while (1) {
  90. char c = str[n];
  91. if (c == '\0' || (c == '\n' && !(getmode() & ios::binary))) {
  92. if (n > 0) write(str, n);
  93. if (c == '\0') break;
  94. write('\r');
  95. str += n;
  96. n = 0;
  97. }
  98. n++;
  99. }
  100. if (writeError) setstate(badbit);
  101. }
  102. //------------------------------------------------------------------------------
  103. /** Internal do not use
  104. * \param[in] off
  105. * \param[in] way
  106. */
  107. bool SdStreamBase::seekoff(off_type off, seekdir way) {
  108. pos_type pos;
  109. switch (way) {
  110. case beg:
  111. pos = off;
  112. break;
  113. case cur:
  114. pos = curPosition() + off;
  115. break;
  116. case end:
  117. pos = fileSize() + off;
  118. break;
  119. default:
  120. return false;
  121. }
  122. return seekpos(pos);
  123. }
  124. //------------------------------------------------------------------------------
  125. /** Internal do not use
  126. * \param[in] pos
  127. */
  128. bool SdStreamBase::seekpos(pos_type pos) {
  129. return seekSet(pos);
  130. }
  131. //------------------------------------------------------------------------------
  132. int SdStreamBase::write(const void* buf, size_t n) {
  133. return SdBaseFile::write(buf, n);
  134. }
  135. //------------------------------------------------------------------------------
  136. void SdStreamBase::write(char c) {
  137. write(&c, 1);
  138. }
  139. /// @endcond