FatFilePrint.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. #include <math.h>
  26. #define DBG_FILE "FatFilePrint.cpp"
  27. #include "../common/DebugMacros.h"
  28. #include "FatLib.h"
  29. //------------------------------------------------------------------------------
  30. bool FatFile::ls(print_t* pr, uint8_t flags, uint8_t indent) {
  31. FatFile file;
  32. if (!isDir()) {
  33. DBG_FAIL_MACRO;
  34. goto fail;
  35. }
  36. rewind();
  37. while (file.openNext(this, O_RDONLY)) {
  38. // indent for dir level
  39. if (!file.isHidden() || (flags & LS_A)) {
  40. for (uint8_t i = 0; i < indent; i++) {
  41. pr->write(' ');
  42. }
  43. if (flags & LS_DATE) {
  44. file.printModifyDateTime(pr);
  45. pr->write(' ');
  46. }
  47. if (flags & LS_SIZE) {
  48. file.printFileSize(pr);
  49. pr->write(' ');
  50. }
  51. file.printName(pr);
  52. if (file.isDir()) {
  53. pr->write('/');
  54. }
  55. pr->write('\r');
  56. pr->write('\n');
  57. if ((flags & LS_R) && file.isDir()) {
  58. file.ls(pr, flags, indent + 2);
  59. }
  60. }
  61. file.close();
  62. }
  63. if (getError()) {
  64. DBG_FAIL_MACRO;
  65. goto fail;
  66. }
  67. return true;
  68. fail:
  69. return false;
  70. }
  71. //------------------------------------------------------------------------------
  72. size_t FatFile::printAccessDate(print_t* pr) {
  73. uint16_t date;
  74. if (getAccessDate(&date)) {
  75. return fsPrintDate(pr, date);
  76. }
  77. return 0;
  78. }
  79. //------------------------------------------------------------------------------
  80. size_t FatFile::printCreateDateTime(print_t* pr) {
  81. uint16_t date;
  82. uint16_t time;
  83. if (getCreateDateTime(&date, &time)) {
  84. return fsPrintDateTime(pr, date, time);
  85. }
  86. return 0;
  87. }
  88. //------------------------------------------------------------------------------
  89. size_t FatFile::printModifyDateTime(print_t* pr) {
  90. uint16_t date;
  91. uint16_t time;
  92. if (getModifyDateTime(&date, &time)) {
  93. return fsPrintDateTime(pr, date, time);
  94. }
  95. return 0;
  96. }
  97. //------------------------------------------------------------------------------
  98. size_t FatFile::printFileSize(print_t* pr) {
  99. char buf[11];
  100. char *ptr = buf + sizeof(buf);
  101. *--ptr = 0;
  102. ptr = fmtBase10(ptr, fileSize());
  103. while (ptr > buf) {
  104. *--ptr = ' ';
  105. }
  106. return pr->write(buf);
  107. }