PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
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.

File.cpp 2.6KB

3 yıl önce
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. SD - a slightly more friendly wrapper for sdfatlib
  3. This library aims to expose a subset of SD card functionality
  4. in the form of a higher level "wrapper" object.
  5. License: GNU General Public License V3
  6. (Because sdfatlib is licensed with this.)
  7. (C) Copyright 2010 SparkFun Electronics
  8. */
  9. #include <SD.h>
  10. #ifndef __SD_t3_H__
  11. /* for debugging file open/close leaks
  12. uint8_t nfilecount=0;
  13. */
  14. File::File(SdFile f, const char *n) {
  15. // oh man you are kidding me, new() doesnt exist? Ok we do it by hand!
  16. _file = (SdFile *)malloc(sizeof(SdFile));
  17. if (_file) {
  18. memcpy(_file, &f, sizeof(SdFile));
  19. strncpy(_name, n, 12);
  20. _name[12] = 0;
  21. /* for debugging file open/close leaks
  22. nfilecount++;
  23. Serial.print("Created \"");
  24. Serial.print(n);
  25. Serial.print("\": ");
  26. Serial.println(nfilecount, DEC);
  27. */
  28. }
  29. }
  30. File::File(void) {
  31. _file = 0;
  32. _name[0] = 0;
  33. //Serial.print("Created empty file object");
  34. }
  35. File::~File(void) {
  36. // Serial.print("Deleted file object");
  37. }
  38. // returns a pointer to the file name
  39. char *File::name(void) {
  40. return _name;
  41. }
  42. // a directory is a special type of file
  43. boolean File::isDirectory(void) {
  44. return (_file && _file->isDir());
  45. }
  46. size_t File::write(uint8_t val) {
  47. return write(&val, 1);
  48. }
  49. size_t File::write(const uint8_t *buf, size_t size) {
  50. size_t t;
  51. if (!_file) {
  52. setWriteError();
  53. return 0;
  54. }
  55. _file->clearWriteError();
  56. t = _file->write(buf, size);
  57. if (_file->getWriteError()) {
  58. setWriteError();
  59. return 0;
  60. }
  61. return t;
  62. }
  63. int File::peek() {
  64. if (! _file)
  65. return 0;
  66. int c = _file->read();
  67. if (c != -1) _file->seekCur(-1);
  68. return c;
  69. }
  70. int File::read() {
  71. if (_file)
  72. return _file->read();
  73. return -1;
  74. }
  75. // buffered read for more efficient, high speed reading
  76. int File::read(void *buf, uint16_t nbyte) {
  77. if (_file)
  78. return _file->read(buf, nbyte);
  79. return 0;
  80. }
  81. int File::available() {
  82. if (! _file) return 0;
  83. uint32_t n = size() - position();
  84. return n > 0X7FFF ? 0X7FFF : n;
  85. }
  86. void File::flush() {
  87. if (_file)
  88. _file->sync();
  89. }
  90. boolean File::seek(uint32_t pos) {
  91. if (! _file) return false;
  92. return _file->seekSet(pos);
  93. }
  94. uint32_t File::position() {
  95. if (! _file) return -1;
  96. return _file->curPosition();
  97. }
  98. uint32_t File::size() {
  99. if (! _file) return 0;
  100. return _file->fileSize();
  101. }
  102. void File::close() {
  103. if (_file) {
  104. _file->close();
  105. free(_file);
  106. _file = 0;
  107. /* for debugging file open/close leaks
  108. nfilecount--;
  109. Serial.print("Deleted ");
  110. Serial.println(nfilecount, DEC);
  111. */
  112. }
  113. }
  114. File::operator bool() {
  115. if (_file)
  116. return _file->isOpen();
  117. return false;
  118. }
  119. #endif