/** * Get the name of a file in the current directory by index */ void CardReader::getfilename(uint16_t nr, const char * const match/*=NULL*/) { curDir = &workDir; lsAction = LS_GetFilename; nrFiles = nr; curDir->rewind(); lsDive("", *curDir, match); }
uint16_t CardReader::getnrfilenames() { curDir = &workDir; lsAction = LS_Count; nrFiles = 0; curDir->rewind(); lsDive("", *curDir); return nrFiles; }
void CardReader::getfilename(const uint8_t nr) { curDir=&workDir; lsAction=LS_GetFilename; nrFiles=nr; curDir->rewind(); lsDive("",*curDir); }
void CardReader::ls() { lsAction=LS_SerialPrint; if(lsAction==LS_Count) nrFiles=0; root.rewind(); lsDive("",root); }
/** * Get a long pretty path based on a DOS 8.3 path */ void CardReader::printLongPath(char *path) { lsAction = LS_GetFilename; int i, pathLen = strlen(path); // SERIAL_ECHOPGM("Full Path: "); SERIAL_ECHOLN(path); // Zero out slashes to make segments for (i = 0; i < pathLen; i++) if (path[i] == '/') path[i] = '\0'; SdFile diveDir = root; // start from the root for segment 1 for (i = 0; i < pathLen;) { if (path[i] == '\0') i++; // move past a single nul char *segment = &path[i]; // The segment after most slashes // If a segment is empty (extra-slash) then exit if (!*segment) break; // Go to the next segment while (path[++i]) { } // SERIAL_ECHOPGM("Looking for segment: "); SERIAL_ECHOLN(segment); // Find the item, setting the long filename diveDir.rewind(); lsDive("", diveDir, segment); // Print /LongNamePart to serial output SERIAL_PROTOCOLCHAR('/'); SERIAL_PROTOCOL(longFilename[0] ? longFilename : "???"); // If the filename was printed then that's it if (!filenameIsDir) break; // SERIAL_ECHOPGM("Opening dir: "); SERIAL_ECHOLN(segment); // Open the sub-item as the new dive parent SdFile dir; if (!dir.open(diveDir, segment, O_READ)) { SERIAL_EOL; SERIAL_ECHO_START; SERIAL_ECHOPGM(MSG_SD_CANT_OPEN_SUBDIR); SERIAL_ECHO(segment); break; } diveDir.close(); diveDir = dir; } // while i<pathLen SERIAL_EOL; }
void CardReader::lsDive(const char *prepend,SdFile parent) { dir_t p; uint8_t cnt=0; while (parent.readDir(p, longFilename) > 0) { if( DIR_IS_SUBDIR(&p) && lsAction!=LS_Count && lsAction!=LS_GetFilename) // hence LS_SerialPrint { char path[13*2]; char lfilename[13]; createFilename(lfilename,p); path[0]=0; if(strlen(prepend)==0) //avoid leading / if already in prepend { strcat(path,"/"); } strcat(path,prepend); strcat(path,lfilename); strcat(path,"/"); //Serial.print(path); SdFile dir; if(!dir.open(parent,lfilename, O_READ)) { if(lsAction==LS_SerialPrint) { SERIAL_ECHO_START; SERIAL_ECHOLN(MSG_SD_CANT_OPEN_SUBDIR); SERIAL_ECHOLN(lfilename); } } lsDive(path,dir); //close done automatically by destructor of SdFile } else { if (p.name[0] == DIR_NAME_FREE) break; if (p.name[0] == DIR_NAME_DELETED || p.name[0] == '.'|| p.name[0] == '_') continue; if ( p.name[0] == '.') { if ( p.name[1] != '.') continue; } if (!DIR_IS_FILE_OR_SUBDIR(&p)) continue; filenameIsDir=DIR_IS_SUBDIR(&p); if(!filenameIsDir) { if(p.name[8]!='G') continue; if(p.name[9]=='~') continue; } //if(cnt++!=nr) continue; createFilename(filename,p); if(lsAction==LS_SerialPrint) { SERIAL_PROTOCOL(prepend); SERIAL_PROTOCOLLN(filename); } else if(lsAction==LS_Count) { nrFiles++; } else if(lsAction==LS_GetFilename) { if(cnt==nrFiles) return; cnt++; } } } }
void CardReader::ls() { lsAction = LS_SerialPrint; root.rewind(); lsDive("", root); }
/** * Dive into a folder and recurse depth-first to perform a pre-set operation lsAction: * LS_Count - Add +1 to nrFiles for every file within the parent * LS_GetFilename - Get the filename of the file indexed by nrFiles * LS_SerialPrint - Print the full path of each file to serial output */ void CardReader::lsDive(const char *prepend, SdFile parent, const char * const match/*=NULL*/) { dir_t p; uint8_t cnt = 0; // Read the next entry from a directory while (parent.readDir(p, longFilename) > 0) { // If the entry is a directory and the action is LS_SerialPrint if (DIR_IS_SUBDIR(&p) && lsAction != LS_Count && lsAction != LS_GetFilename) { // Get the short name for the item, which we know is a folder char lfilename[FILENAME_LENGTH]; createFilename(lfilename, p); // Allocate enough stack space for the full path to a folder, trailing slash, and nul boolean prepend_is_empty = (prepend[0] == '\0'); int len = (prepend_is_empty ? 1 : strlen(prepend)) + strlen(lfilename) + 1 + 1; char path[len]; // Append the FOLDERNAME12/ to the passed string. // It contains the full path to the "parent" argument. // We now have the full path to the item in this folder. strcpy(path, prepend_is_empty ? "/" : prepend); // root slash if prepend is empty strcat(path, lfilename); // FILENAME_LENGTH-1 characters maximum strcat(path, "/"); // 1 character // Serial.print(path); // Get a new directory object using the full path // and dive recursively into it. SdFile dir; if (!dir.open(parent, lfilename, O_READ)) { if (lsAction == LS_SerialPrint) { ECHO_LMV(ER, MSG_SD_CANT_OPEN_SUBDIR, lfilename); } } lsDive(path, dir); // close() is done automatically by destructor of SdFile } else { char pn0 = p.name[0]; if (pn0 == DIR_NAME_FREE) break; if (pn0 == DIR_NAME_DELETED || pn0 == '.') continue; if (longFilename[0] == '.') continue; if (!DIR_IS_FILE_OR_SUBDIR(&p)) continue; filenameIsDir = DIR_IS_SUBDIR(&p); if (!filenameIsDir && (p.name[8] != 'G' || p.name[9] == '~')) continue; switch (lsAction) { case LS_Count: nrFiles++; break; case LS_SerialPrint: createFilename(filename, p); ECHO_V(prepend); ECHO_EV(filename); break; case LS_GetFilename: createFilename(filename, p); if (match != NULL) { if (strcasecmp(match, filename) == 0) return; } else if (cnt == nrFiles) return; cnt++; break; } } } // while readDir }