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
2.6KB

  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