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.

преди 10 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. /* for debugging file open/close leaks
  11. uint8_t nfilecount=0;
  12. */
  13. File::File(SdFile f, const char *n) {
  14. // oh man you are kidding me, new() doesnt exist? Ok we do it by hand!
  15. _file = (SdFile *)malloc(sizeof(SdFile));
  16. if (_file) {
  17. memcpy(_file, &f, sizeof(SdFile));
  18. strncpy(_name, n, 12);
  19. _name[12] = 0;
  20. /* for debugging file open/close leaks
  21. nfilecount++;
  22. Serial.print("Created \"");
  23. Serial.print(n);
  24. Serial.print("\": ");
  25. Serial.println(nfilecount, DEC);
  26. */
  27. }
  28. }
  29. File::File(void) {
  30. _file = 0;
  31. _name[0] = 0;
  32. //Serial.print("Created empty file object");
  33. }
  34. File::~File(void) {
  35. // Serial.print("Deleted file object");
  36. }
  37. // returns a pointer to the file name
  38. char *File::name(void) {
  39. return _name;
  40. }
  41. // a directory is a special type of file
  42. boolean File::isDirectory(void) {
  43. return (_file && _file->isDir());
  44. }
  45. size_t File::write(uint8_t val) {
  46. return write(&val, 1);
  47. }
  48. size_t File::write(const uint8_t *buf, size_t size) {
  49. size_t t;
  50. if (!_file) {
  51. setWriteError();
  52. return 0;
  53. }
  54. _file->clearWriteError();
  55. t = _file->write(buf, size);
  56. if (_file->getWriteError()) {
  57. setWriteError();
  58. return 0;
  59. }
  60. return t;
  61. }
  62. int File::peek() {
  63. if (! _file)
  64. return 0;
  65. int c = _file->read();
  66. if (c != -1) _file->seekCur(-1);
  67. return c;
  68. }
  69. int File::read() {
  70. if (_file)
  71. return _file->read();
  72. return -1;
  73. }
  74. // buffered read for more efficient, high speed reading
  75. int File::read(void *buf, uint16_t nbyte) {
  76. if (_file)
  77. return _file->read(buf, nbyte);
  78. return 0;
  79. }
  80. int File::available() {
  81. if (! _file) return 0;
  82. uint32_t n = size() - position();
  83. return n > 0X7FFF ? 0X7FFF : n;
  84. }
  85. void File::flush() {
  86. if (_file)
  87. _file->sync();
  88. }
  89. boolean File::seek(uint32_t pos) {
  90. if (! _file) return false;
  91. return _file->seekSet(pos);
  92. }
  93. uint32_t File::position() {
  94. if (! _file) return -1;
  95. return _file->curPosition();
  96. }
  97. uint32_t File::size() {
  98. if (! _file) return 0;
  99. return _file->fileSize();
  100. }
  101. void File::close() {
  102. if (_file) {
  103. _file->close();
  104. free(_file);
  105. _file = 0;
  106. /* for debugging file open/close leaks
  107. nfilecount--;
  108. Serial.print("Deleted ");
  109. Serial.println(nfilecount, DEC);
  110. */
  111. }
  112. }
  113. File::operator bool() {
  114. if (_file)
  115. return _file->isOpen();
  116. return false;
  117. }