ExFatFilePrint.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /**
  2. * Copyright (c) 2011-2022 Bill Greiman
  3. * This file is part of the SdFat library for SD memory cards.
  4. *
  5. * MIT License
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a
  8. * copy of this software and associated documentation files (the "Software"),
  9. * to deal in the Software without restriction, including without limitation
  10. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  11. * and/or sell copies of the Software, and to permit persons to whom the
  12. * Software is furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included
  15. * in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  18. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. */
  25. #define DBG_FILE "ExFatFilePrint.cpp"
  26. #include "../common/DebugMacros.h"
  27. #include "ExFatLib.h"
  28. #include "../common/FsUtf.h"
  29. //------------------------------------------------------------------------------
  30. bool ExFatFile::ls(print_t* pr) {
  31. ExFatFile file;
  32. if (!isDir()) {
  33. DBG_FAIL_MACRO;
  34. goto fail;
  35. }
  36. rewind();
  37. while (file.openNext(this, O_RDONLY)) {
  38. if (!file.isHidden()) {
  39. file.printName(pr);
  40. if (file.isDir()) {
  41. pr->write('/');
  42. }
  43. pr->write('\r');
  44. pr->write('\n');
  45. }
  46. file.close();
  47. }
  48. if (getError()) {
  49. DBG_FAIL_MACRO;
  50. goto fail;
  51. }
  52. return true;
  53. fail:
  54. return false;
  55. }
  56. //------------------------------------------------------------------------------
  57. bool ExFatFile::ls(print_t* pr, uint8_t flags, uint8_t indent) {
  58. ExFatFile file;
  59. if (!isDir()) {
  60. DBG_FAIL_MACRO;
  61. goto fail;
  62. }
  63. rewind();
  64. while (file.openNext(this, O_RDONLY)) {
  65. // indent for dir level
  66. if (!file.isHidden() || (flags & LS_A)) {
  67. for (uint8_t i = 0; i < indent; i++) {
  68. pr->write(' ');
  69. }
  70. if (flags & LS_DATE) {
  71. file.printModifyDateTime(pr);
  72. pr->write(' ');
  73. }
  74. if (flags & LS_SIZE) {
  75. file.printFileSize(pr);
  76. pr->write(' ');
  77. }
  78. file.printName(pr);
  79. if (file.isDir()) {
  80. pr->write('/');
  81. }
  82. pr->write('\r');
  83. pr->write('\n');
  84. if ((flags & LS_R) && file.isDir()) {
  85. file.ls(pr, flags, indent + 2);
  86. }
  87. }
  88. file.close();
  89. }
  90. if (getError()) {
  91. DBG_FAIL_MACRO;
  92. goto fail;
  93. }
  94. return true;
  95. fail:
  96. return false;
  97. }
  98. //------------------------------------------------------------------------------
  99. size_t ExFatFile::printAccessDateTime(print_t* pr) {
  100. uint16_t date;
  101. uint16_t time;
  102. if (getAccessDateTime(&date, &time)) {
  103. return fsPrintDateTime(pr, date, time);
  104. }
  105. return 0;
  106. }
  107. //------------------------------------------------------------------------------
  108. size_t ExFatFile::printCreateDateTime(print_t* pr) {
  109. uint16_t date;
  110. uint16_t time;
  111. if (getCreateDateTime(&date, &time)) {
  112. return fsPrintDateTime(pr, date, time);
  113. }
  114. return 0;
  115. }
  116. //------------------------------------------------------------------------------
  117. size_t ExFatFile::printFileSize(print_t* pr) {
  118. uint64_t n = m_validLength;
  119. char buf[21];
  120. char *str = &buf[sizeof(buf) - 1];
  121. char *bgn = str - 12;
  122. *str = '\0';
  123. do {
  124. uint64_t m = n;
  125. n /= 10;
  126. *--str = m - 10*n + '0';
  127. } while (n);
  128. while (str > bgn) {
  129. *--str = ' ';
  130. }
  131. return pr->write(str);
  132. }
  133. //------------------------------------------------------------------------------
  134. size_t ExFatFile::printModifyDateTime(print_t* pr) {
  135. uint16_t date;
  136. uint16_t time;
  137. if (getModifyDateTime(&date, &time)) {
  138. return fsPrintDateTime(pr, date, time);
  139. }
  140. return 0;
  141. }
  142. //------------------------------------------------------------------------------
  143. size_t ExFatFile::printName7(print_t* pr) {
  144. DirName_t* dn;
  145. size_t n = 0;
  146. uint8_t in;
  147. uint8_t buf[15];
  148. if (!isOpen()) {
  149. DBG_FAIL_MACRO;
  150. goto fail;
  151. }
  152. for (uint8_t is = 2; is <= m_setCount; is++) {
  153. dn = reinterpret_cast<DirName_t*>
  154. (dirCache(is, FsCache::CACHE_FOR_READ));
  155. if (!dn || dn->type != EXFAT_TYPE_NAME) {
  156. DBG_FAIL_MACRO;
  157. goto fail;
  158. }
  159. for (in = 0; in < 15; in++) {
  160. uint16_t c = getLe16(dn->unicode + 2*in);
  161. if (!c) {
  162. break;
  163. }
  164. buf[in] = c < 0X7F ? c : '?';
  165. n++;
  166. }
  167. pr->write(buf, in);
  168. }
  169. return n;
  170. fail:
  171. return 0;
  172. }
  173. //------------------------------------------------------------------------------
  174. size_t ExFatFile::printName8(print_t *pr) {
  175. DirName_t* dn;
  176. uint16_t hs = 0;
  177. uint32_t cp;
  178. size_t n = 0;
  179. uint8_t in;
  180. char buf[5];
  181. if (!isOpen()) {
  182. DBG_FAIL_MACRO;
  183. goto fail;
  184. }
  185. for (uint8_t is = 2; is <= m_setCount; is++) {
  186. dn = reinterpret_cast<DirName_t*>
  187. (dirCache(is, FsCache::CACHE_FOR_READ));
  188. if (!dn || dn->type != EXFAT_TYPE_NAME) {
  189. DBG_FAIL_MACRO;
  190. goto fail;
  191. }
  192. for (in = 0; in < 15; in++) {
  193. uint16_t c = getLe16(dn->unicode + 2*in);
  194. if (hs) {
  195. if (!FsUtf::isLowSurrogate(c)) {
  196. DBG_FAIL_MACRO;
  197. goto fail;
  198. }
  199. cp = FsUtf::u16ToCp(hs, c);
  200. hs = 0;
  201. } else if (!FsUtf::isSurrogate(c)) {
  202. if (c == 0) {
  203. break;
  204. }
  205. cp = c;
  206. } else if (FsUtf::isHighSurrogate(c)) {
  207. hs = c;
  208. continue;
  209. } else {
  210. DBG_FAIL_MACRO;
  211. goto fail;
  212. }
  213. char* str = FsUtf::cpToMb(cp, buf, buf + sizeof(buf));
  214. if (!str) {
  215. DBG_FAIL_MACRO;
  216. goto fail;
  217. }
  218. n += pr->write(buf, str - buf);
  219. }
  220. }
  221. return n;
  222. fail:
  223. return 0;
  224. }