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.

199 satır
5.5KB

  1. /* FatLib Library
  2. * Copyright (C) 2012 by William Greiman
  3. *
  4. * This file is part of the FatLib 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 FatLib Library. If not, see
  18. * <http://www.gnu.org/licenses/>.
  19. */
  20. #include <math.h>
  21. #include "FatFile.h"
  22. #include "FmtNumber.h"
  23. //------------------------------------------------------------------------------
  24. // print uint8_t with width 2
  25. static void print2u(print_t* pr, uint8_t v) {
  26. char c0 = '?';
  27. char c1 = '?';
  28. if (v < 100) {
  29. c1 = v/10;
  30. c0 = v - 10*c1 + '0';
  31. c1 += '0';
  32. }
  33. pr->write(c1);
  34. pr->write(c0);
  35. }
  36. //------------------------------------------------------------------------------
  37. static void printU32(print_t* pr, uint32_t v) {
  38. char buf[11];
  39. char* ptr = buf + sizeof(buf);
  40. *--ptr = 0;
  41. pr->write(fmtDec(v, ptr));
  42. }
  43. //------------------------------------------------------------------------------
  44. void FatFile::ls(print_t* pr, uint8_t flags, uint8_t indent) {
  45. FatFile file;
  46. rewind();
  47. while (file.openNext(this, O_READ)) {
  48. // indent for dir level
  49. if (!file.isHidden() && !(flags & LS_A)) {
  50. for (uint8_t i = 0; i < indent; i++) {
  51. pr->write(' ');
  52. }
  53. if (flags & LS_DATE) {
  54. file.printModifyDateTime(pr);
  55. pr->write(' ');
  56. }
  57. if (flags & LS_SIZE) {
  58. file.printFileSize(pr);
  59. pr->write(' ');
  60. }
  61. file.printName(pr);
  62. if (file.isDir()) {
  63. pr->write('/');
  64. }
  65. pr->write('\r');
  66. pr->write('\n');
  67. if ((flags & LS_R) && file.isDir()) {
  68. file.ls(pr, flags, indent + 2);
  69. }
  70. }
  71. file.close();
  72. }
  73. }
  74. //------------------------------------------------------------------------------
  75. bool FatFile::printCreateDateTime(print_t* pr) {
  76. dir_t dir;
  77. if (!dirEntry(&dir)) {
  78. DBG_FAIL_MACRO;
  79. goto fail;
  80. }
  81. printFatDate(pr, dir.creationDate);
  82. pr->write(' ');
  83. printFatTime(pr, dir.creationTime);
  84. return true;
  85. fail:
  86. return false;
  87. }
  88. //------------------------------------------------------------------------------
  89. void FatFile::printFatDate(print_t* pr, uint16_t fatDate) {
  90. printU32(pr, FAT_YEAR(fatDate));
  91. pr->write('-');
  92. print2u(pr, FAT_MONTH(fatDate));
  93. pr->write('-');
  94. print2u(pr, FAT_DAY(fatDate));
  95. }
  96. //------------------------------------------------------------------------------
  97. void FatFile::printFatTime(print_t* pr, uint16_t fatTime) {
  98. print2u(pr, FAT_HOUR(fatTime));
  99. pr->write(':');
  100. print2u(pr, FAT_MINUTE(fatTime));
  101. pr->write(':');
  102. print2u(pr, FAT_SECOND(fatTime));
  103. }
  104. //------------------------------------------------------------------------------
  105. /** Template for FatFile::printField() */
  106. template <typename Type>
  107. static int printFieldT(FatFile* file, char sign, Type value, char term) {
  108. char buf[3*sizeof(Type) + 3];
  109. char* str = &buf[sizeof(buf)];
  110. if (term) {
  111. *--str = term;
  112. if (term == '\n') {
  113. *--str = '\r';
  114. }
  115. }
  116. #ifdef OLD_FMT
  117. do {
  118. Type m = value;
  119. value /= 10;
  120. *--str = '0' + m - 10*value;
  121. } while (value);
  122. #else // OLD_FMT
  123. str = fmtDec(value, str);
  124. #endif // OLD_FMT
  125. if (sign) {
  126. *--str = sign;
  127. }
  128. return file->write(str, &buf[sizeof(buf)] - str);
  129. }
  130. //------------------------------------------------------------------------------
  131. int FatFile::printField(float value, char term, uint8_t prec) {
  132. char buf[24];
  133. char* str = &buf[sizeof(buf)];
  134. if (term) {
  135. *--str = term;
  136. if (term == '\n') {
  137. *--str = '\r';
  138. }
  139. }
  140. str = fmtFloat(value, str, prec);
  141. return write(str, buf + sizeof(buf) - str);
  142. }
  143. //------------------------------------------------------------------------------
  144. int FatFile::printField(uint16_t value, char term) {
  145. return printFieldT(this, 0, value, term);
  146. }
  147. //------------------------------------------------------------------------------
  148. int FatFile::printField(int16_t value, char term) {
  149. char sign = 0;
  150. if (value < 0) {
  151. sign = '-';
  152. value = -value;
  153. }
  154. return printFieldT(this, sign, (uint16_t)value, term);
  155. }
  156. //------------------------------------------------------------------------------
  157. int FatFile::printField(uint32_t value, char term) {
  158. return printFieldT(this, 0, value, term);
  159. }
  160. //------------------------------------------------------------------------------
  161. int FatFile::printField(int32_t value, char term) {
  162. char sign = 0;
  163. if (value < 0) {
  164. sign = '-';
  165. value = -value;
  166. }
  167. return printFieldT(this, sign, (uint32_t)value, term);
  168. }
  169. //------------------------------------------------------------------------------
  170. bool FatFile::printModifyDateTime(print_t* pr) {
  171. dir_t dir;
  172. if (!dirEntry(&dir)) {
  173. DBG_FAIL_MACRO;
  174. goto fail;
  175. }
  176. printFatDate(pr, dir.lastWriteDate);
  177. pr->write(' ');
  178. printFatTime(pr, dir.lastWriteTime);
  179. return true;
  180. fail:
  181. return false;
  182. }
  183. //------------------------------------------------------------------------------
  184. size_t FatFile::printFileSize(print_t* pr) {
  185. char buf[11];
  186. char *ptr = buf + sizeof(buf);
  187. *--ptr = 0;
  188. ptr = fmtDec(fileSize(), ptr);
  189. while (ptr > buf) {
  190. *--ptr = ' ';
  191. }
  192. return pr->write(buf);
  193. }