From 7fa0947d9d6b7dc21d52cfda20bb40ca0160ce62 Mon Sep 17 00:00:00 2001 From: noam987 Date: Mon, 23 Oct 2023 19:24:19 -0400 Subject: [PATCH 01/19] Add list_files --- include/pros/misc.h | 20 ++++++++++++++++++++ include/pros/misc.hpp | 19 +++++++++++++++++++ src/devices/vdml_usd.c | 12 ++++++++++++ src/devices/vdml_usd.cpp | 4 ++++ 4 files changed, 55 insertions(+) diff --git a/include/pros/misc.h b/include/pros/misc.h index d0ad6764b..a355661e1 100644 --- a/include/pros/misc.h +++ b/include/pros/misc.h @@ -723,6 +723,26 @@ double battery_get_capacity(void); */ int32_t usd_is_installed(void); +/** + * Lists the files in a directory specified by the path + * Puts the list of file names (NOT DIRECTORIES) into the buffer seperated by newlines + * + * \note use a path of "\" to list the files in the main directory + * + * \return 1 on success or PROS_ERR on failure setting errno + * + * \b Example + * \code + * void opcontrol() { + * char* test = (char*) malloc(128); + * pros::c::list_files("/", test, 128); + * pros::delay(200); + * printf("%s\n", test); + * } + * \endcode +*/ +int32_t list_files(const char* path, char* buffer, int32_t len); + /******************************************************************************/ /** Date and Time **/ /******************************************************************************/ diff --git a/include/pros/misc.hpp b/include/pros/misc.hpp index 8ade055ad..7a5b084ba 100644 --- a/include/pros/misc.hpp +++ b/include/pros/misc.hpp @@ -525,6 +525,25 @@ namespace usd { * \endcode */ std::int32_t is_installed(void); +/** + * Lists the files in a directory specified by the path + * Puts the list of file names (NOT DIRECTORIES) into the buffer seperated by newlines + * + * \note use a path of "\" to list the files in the main directory + * + * \return 1 on success or PROS_ERR on failure, setting errno + * + * \b Example + * \code + * void opcontrol() { + * char* test = (char*) malloc(128); + * pros::usd::list_files("/", test, 128); + * pros::delay(200); + * printf("%s\n", test); + * } + * \endcode +*/ +std::int32_t list_files(const char* path, char* buffer, std::int32_t len); } // namespace usd } // namespace pros diff --git a/src/devices/vdml_usd.c b/src/devices/vdml_usd.c index 879f6525f..8d443f047 100644 --- a/src/devices/vdml_usd.c +++ b/src/devices/vdml_usd.c @@ -17,3 +17,15 @@ int32_t usd_is_installed(void) { return vexFileDriveStatus(0); } +static const int FRESULTMAP[] = {0, EIO, EINVAL, EBUSY, ENOENT, ENOENT, EINVAL, EACCES, // FR_DENIED + EEXIST, EINVAL, EROFS, ENXIO, ENOBUFS, ENXIO, EIO, EACCES, // FR_LOCKED + ENOBUFS, ENFILE, EINVAL}; + +int32_t list_files(const char* path, char* buffer, int32_t len) { + FRESULT result = vexFileDirectoryGet(path, buffer, len); + if (result != F_OK) { + errno = FRESULTMAP[result]; + return PROS_ERR; + } + return PROS_SUCCESS; +} diff --git a/src/devices/vdml_usd.cpp b/src/devices/vdml_usd.cpp index 71318463c..c91c83378 100644 --- a/src/devices/vdml_usd.cpp +++ b/src/devices/vdml_usd.cpp @@ -21,5 +21,9 @@ std::int32_t is_installed(void) { return usd_is_installed(); } +int32_t list_files(const char* path, char* buffer, int32_t len) { + return list_files(path, buffer, len); +} + } // namespace usd } // namespace pros From 40aa222bc37fe83188f1940adfc229b4a861af85 Mon Sep 17 00:00:00 2001 From: noam987 Date: Mon, 23 Oct 2023 22:18:53 -0400 Subject: [PATCH 02/19] renamed c function --- include/pros/misc.h | 4 ++-- src/devices/vdml_usd.c | 2 +- src/devices/vdml_usd.cpp | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/include/pros/misc.h b/include/pros/misc.h index a355661e1..4018c8f2a 100644 --- a/include/pros/misc.h +++ b/include/pros/misc.h @@ -735,13 +735,13 @@ int32_t usd_is_installed(void); * \code * void opcontrol() { * char* test = (char*) malloc(128); - * pros::c::list_files("/", test, 128); + * pros::c::usd_list_files("/", test, 128); * pros::delay(200); * printf("%s\n", test); * } * \endcode */ -int32_t list_files(const char* path, char* buffer, int32_t len); +int32_t usd_list_files(const char* path, char* buffer, int32_t len); /******************************************************************************/ /** Date and Time **/ diff --git a/src/devices/vdml_usd.c b/src/devices/vdml_usd.c index 8d443f047..da60f3f93 100644 --- a/src/devices/vdml_usd.c +++ b/src/devices/vdml_usd.c @@ -21,7 +21,7 @@ static const int FRESULTMAP[] = {0, EIO, EINVAL, EBUSY, ENOENT, ENOENT EEXIST, EINVAL, EROFS, ENXIO, ENOBUFS, ENXIO, EIO, EACCES, // FR_LOCKED ENOBUFS, ENFILE, EINVAL}; -int32_t list_files(const char* path, char* buffer, int32_t len) { +int32_t usd_list_files(const char* path, char* buffer, int32_t len) { FRESULT result = vexFileDirectoryGet(path, buffer, len); if (result != F_OK) { errno = FRESULTMAP[result]; diff --git a/src/devices/vdml_usd.cpp b/src/devices/vdml_usd.cpp index c91c83378..d6ede6edd 100644 --- a/src/devices/vdml_usd.cpp +++ b/src/devices/vdml_usd.cpp @@ -22,7 +22,7 @@ std::int32_t is_installed(void) { } int32_t list_files(const char* path, char* buffer, int32_t len) { - return list_files(path, buffer, len); + return usd_list_files(path, buffer, len); } } // namespace usd From 90e0bba5a7f21e557bc5a50edf8d910f62aa359d Mon Sep 17 00:00:00 2001 From: noam987 Date: Tue, 24 Oct 2023 15:33:00 -0400 Subject: [PATCH 03/19] Clarify the docs --- include/pros/misc.h | 27 +++++++++++++++++++++++++-- include/pros/misc.hpp | 31 +++++++++++++++++++++++++++---- 2 files changed, 52 insertions(+), 6 deletions(-) diff --git a/include/pros/misc.h b/include/pros/misc.h index 4018c8f2a..ce84c17bb 100644 --- a/include/pros/misc.h +++ b/include/pros/misc.h @@ -727,7 +727,25 @@ int32_t usd_is_installed(void); * Lists the files in a directory specified by the path * Puts the list of file names (NOT DIRECTORIES) into the buffer seperated by newlines * - * \note use a path of "\" to list the files in the main directory + * This function uses the following values of errno when an error state is + * reached: + * + * EIO - Hard error occured in the low level disk I/O layer + * EINVAL - file or directory is invalid, or length is invalid + * EBUSY - THe physical drinve cannot work + * ENOENT - cannot find the path or file + * EINVAL - the path name format is invalid + * EACCES - Access denied or directory full + * EEXIST - Access denied + * EROFS - SD card is write protected + * ENXIO - drive number is invalid or not a FAT32 drive + * ENOBUFS - drive has no work area + * ENFILE - too many open files + * + * + * + * \note use a path of "\" to list the files in the main directory NOT "/usd/" + * DO NOT PREPEND YOUR PATHS WITH "/usd/" * * \return 1 on success or PROS_ERR on failure setting errno * @@ -737,7 +755,12 @@ int32_t usd_is_installed(void); * char* test = (char*) malloc(128); * pros::c::usd_list_files("/", test, 128); * pros::delay(200); - * printf("%s\n", test); + * printf("%s\n", test); //Prints the file names in the root directory seperated by newlines + * pros::delay(100); + * pros::c::usd_list_files("/test", test, 128); + * pros::delay(200); + * printf("%s\n", test); //Prints the names of files in the folder named test seperated by newlines + * pros::delay(100); * } * \endcode */ diff --git a/include/pros/misc.hpp b/include/pros/misc.hpp index 7a5b084ba..ce779b546 100644 --- a/include/pros/misc.hpp +++ b/include/pros/misc.hpp @@ -526,12 +526,30 @@ namespace usd { */ std::int32_t is_installed(void); /** - * Lists the files in a directory specified by the path +Lists the files in a directory specified by the path * Puts the list of file names (NOT DIRECTORIES) into the buffer seperated by newlines * - * \note use a path of "\" to list the files in the main directory + * This function uses the following values of errno when an error state is + * reached: + * + * EIO - Hard error occured in the low level disk I/O layer + * EINVAL - file or directory is invalid, or length is invalid + * EBUSY - THe physical drinve cannot work + * ENOENT - cannot find the path or file + * EINVAL - the path name format is invalid + * EACCES - Access denied or directory full + * EEXIST - Access denied + * EROFS - SD card is write protected + * ENXIO - drive number is invalid or not a FAT32 drive + * ENOBUFS - drive has no work area + * ENFILE - too many open files * - * \return 1 on success or PROS_ERR on failure, setting errno + * + * + * \note use a path of "\" to list the files in the main directory NOT "/usd/" + * DO NOT PREPEND YOUR PATHS WITH "/usd/" + * + * \return 1 on success or PROS_ERR on failure setting errno * * \b Example * \code @@ -539,7 +557,12 @@ std::int32_t is_installed(void); * char* test = (char*) malloc(128); * pros::usd::list_files("/", test, 128); * pros::delay(200); - * printf("%s\n", test); + * printf("%s\n", test); //Prints the file names in the root directory seperated by newlines + * pros::delay(100); + * pros::list_files("/test", test, 128); + * pros::delay(200); + * printf("%s\n", test); //Prints the names of files in the folder named test seperated by newlines + * pros::delay(100); * } * \endcode */ From a8ed32bd2ed361b0c071f4d1423a6aa2c35c7bf7 Mon Sep 17 00:00:00 2001 From: Grace Lu Date: Fri, 27 Oct 2023 00:30:41 -0400 Subject: [PATCH 04/19] Changed list_files function in vdml_usd.cpp to return vector of std:string of file names Will return a vector of std::strings of file names, will return a vector containing one std::string PROS_ERR if error occurs. --- include/pros/misc.hpp | 19 ++++++++++--------- src/devices/vdml_usd.cpp | 35 +++++++++++++++++++++++++++++++++-- 2 files changed, 43 insertions(+), 11 deletions(-) diff --git a/include/pros/misc.hpp b/include/pros/misc.hpp index ce779b546..769045ad2 100644 --- a/include/pros/misc.hpp +++ b/include/pros/misc.hpp @@ -549,24 +549,25 @@ Lists the files in a directory specified by the path * \note use a path of "\" to list the files in the main directory NOT "/usd/" * DO NOT PREPEND YOUR PATHS WITH "/usd/" * - * \return 1 on success or PROS_ERR on failure setting errno + * \return vector of std::string of file names, if error occurs, returns vector containing + * one element, PROS_ERR * * \b Example * \code * void opcontrol() { * char* test = (char*) malloc(128); - * pros::usd::list_files("/", test, 128); + * std::vector files = list_files("/", test, 128); * pros::delay(200); - * printf("%s\n", test); //Prints the file names in the root directory seperated by newlines - * pros::delay(100); - * pros::list_files("/test", test, 128); - * pros::delay(200); - * printf("%s\n", test); //Prints the names of files in the folder named test seperated by newlines - * pros::delay(100); + * // Given vector of std::string file names, print each file name + * // Note that if there is an error, the vector will contain one element, PROS_ERR + * // Print each file name + * for (std::string file : files) { + * std::cout << file << std::endl; + * } * } * \endcode */ -std::int32_t list_files(const char* path, char* buffer, std::int32_t len); +std::vector list_files(const char* path, char* buffer, int32_t len); } // namespace usd } // namespace pros diff --git a/src/devices/vdml_usd.cpp b/src/devices/vdml_usd.cpp index d6ede6edd..c3a3f47a7 100644 --- a/src/devices/vdml_usd.cpp +++ b/src/devices/vdml_usd.cpp @@ -21,8 +21,39 @@ std::int32_t is_installed(void) { return usd_is_installed(); } -int32_t list_files(const char* path, char* buffer, int32_t len) { - return usd_list_files(path, buffer, len); +std::vector list_files(const char* path, char* buffer, int32_t len) { + std::vector files = {}; + // Call the C function + int32_t success = usd_list_files(path, buffer, len); + // Check if call successful, if PROS_ERR return vector containing PROS_ERR + if (success == PROS_ERR) { + // push_back PROS_ERR to files vector + files.push_back(std::to_string(success)); + return files; + } + // Parse buffer given call successful, split by '/n' + // Store char * buffer as std::string in str + // Store delimiter in std::string, '\n' + std::string str(buffer); + std::string delimiter = "\n"; + + // position to keep track of position in str + // file_name used to store each file name + size_t position = 0; + std::string file_name; + + // Loop until delimiter '\n' can not be found anymore + while ((position = str.find(delimiter)) != std::string::npos) { + // file_name is the string from the beginning of str to the first '\n', excluding '\n' + file_name = str.substr(0, position); + // Add token to files vector + files.push_back(file_name); + // Erase file_name from str, would be pos + 1 if we wanted to include '\n' + str.erase(0, position + 1); + } + + // return vector of file names + return files; } } // namespace usd From 4983f3b64c809017a14579df298976dfc879b8dc Mon Sep 17 00:00:00 2001 From: Grace Lu Date: Fri, 27 Oct 2023 00:38:06 -0400 Subject: [PATCH 05/19] Added #include --- include/pros/misc.hpp | 1 + 1 file changed, 1 insertion(+) diff --git a/include/pros/misc.hpp b/include/pros/misc.hpp index 769045ad2..431a7340f 100644 --- a/include/pros/misc.hpp +++ b/include/pros/misc.hpp @@ -26,6 +26,7 @@ #include #include +#include namespace pros { inline namespace v5 { From c80a4aae196e9d98aea906b009eccedea3315852 Mon Sep 17 00:00:00 2001 From: Grace Lu Date: Fri, 27 Oct 2023 10:55:53 -0400 Subject: [PATCH 06/19] Readded original func as list_files_raw, made sure to update func in c API as well to raw --- include/pros/misc.h | 6 +++--- include/pros/misc.hpp | 45 ++++++++++++++++++++++++++++++++++++++++ src/devices/vdml_usd.c | 2 +- src/devices/vdml_usd.cpp | 6 +++++- 4 files changed, 54 insertions(+), 5 deletions(-) diff --git a/include/pros/misc.h b/include/pros/misc.h index ce84c17bb..5137b28c9 100644 --- a/include/pros/misc.h +++ b/include/pros/misc.h @@ -753,18 +753,18 @@ int32_t usd_is_installed(void); * \code * void opcontrol() { * char* test = (char*) malloc(128); - * pros::c::usd_list_files("/", test, 128); + * pros::c::usd_list_files_raw("/", test, 128); * pros::delay(200); * printf("%s\n", test); //Prints the file names in the root directory seperated by newlines * pros::delay(100); - * pros::c::usd_list_files("/test", test, 128); + * pros::c::usd_list_files_raw("/usd/test", test, 128); * pros::delay(200); * printf("%s\n", test); //Prints the names of files in the folder named test seperated by newlines * pros::delay(100); * } * \endcode */ -int32_t usd_list_files(const char* path, char* buffer, int32_t len); +int32_t usd_list_files_raw(const char* path, char* buffer, int32_t len); /******************************************************************************/ /** Date and Time **/ diff --git a/include/pros/misc.hpp b/include/pros/misc.hpp index 431a7340f..fe88e484b 100644 --- a/include/pros/misc.hpp +++ b/include/pros/misc.hpp @@ -526,6 +526,50 @@ namespace usd { * \endcode */ std::int32_t is_installed(void); + +/** +Lists the files in a directory specified by the path + * Puts the list of file names (NOT DIRECTORIES) into the buffer seperated by newlines + * + * This function uses the following values of errno when an error state is + * reached: + * + * EIO - Hard error occured in the low level disk I/O layer + * EINVAL - file or directory is invalid, or length is invalid + * EBUSY - THe physical drinve cannot work + * ENOENT - cannot find the path or file + * EINVAL - the path name format is invalid + * EACCES - Access denied or directory full + * EEXIST - Access denied + * EROFS - SD card is write protected + * ENXIO - drive number is invalid or not a FAT32 drive + * ENOBUFS - drive has no work area + * ENFILE - too many open files + * + * + * + * \note use a path of "\" to list the files in the main directory NOT "/usd/" + * DO NOT PREPEND YOUR PATHS WITH "/usd/" + * + * \return 1 on success or PROS_ERR on failure, setting errno + * + * \b Example + * \code + * void opcontrol() { + * char* test = (char*) malloc(128); + * pros::usd::list_files_raw("/", test, 128); + * pros::delay(200); + * printf("%s\n", test); //Prints the file names in the root directory seperated by newlines + * pros::delay(100); + * pros::list_files_raw("/usd/test", test, 128); + * pros::delay(200); + * printf("%s\n", test); //Prints the names of files in the folder named test seperated by newlines + * pros::delay(100); + * } + * \endcode +*/ +std::int32_t list_files_raw(const char* path, char* buffer, int32_t len); + /** Lists the files in a directory specified by the path * Puts the list of file names (NOT DIRECTORIES) into the buffer seperated by newlines @@ -557,6 +601,7 @@ Lists the files in a directory specified by the path * \code * void opcontrol() { * char* test = (char*) malloc(128); + * // Will return vector containing names of files in root directory * std::vector files = list_files("/", test, 128); * pros::delay(200); * // Given vector of std::string file names, print each file name diff --git a/src/devices/vdml_usd.c b/src/devices/vdml_usd.c index da60f3f93..c53558800 100644 --- a/src/devices/vdml_usd.c +++ b/src/devices/vdml_usd.c @@ -21,7 +21,7 @@ static const int FRESULTMAP[] = {0, EIO, EINVAL, EBUSY, ENOENT, ENOENT EEXIST, EINVAL, EROFS, ENXIO, ENOBUFS, ENXIO, EIO, EACCES, // FR_LOCKED ENOBUFS, ENFILE, EINVAL}; -int32_t usd_list_files(const char* path, char* buffer, int32_t len) { +int32_t usd_list_files_raw(const char* path, char* buffer, int32_t len) { FRESULT result = vexFileDirectoryGet(path, buffer, len); if (result != F_OK) { errno = FRESULTMAP[result]; diff --git a/src/devices/vdml_usd.cpp b/src/devices/vdml_usd.cpp index c3a3f47a7..9fce8c12f 100644 --- a/src/devices/vdml_usd.cpp +++ b/src/devices/vdml_usd.cpp @@ -21,10 +21,14 @@ std::int32_t is_installed(void) { return usd_is_installed(); } +std::int32_t list_files_raw(const char* path, char* buffer, int32_t len) { + return usd_list_files_raw(path, buffer, len); +} + std::vector list_files(const char* path, char* buffer, int32_t len) { std::vector files = {}; // Call the C function - int32_t success = usd_list_files(path, buffer, len); + int32_t success = usd_list_files_raw(path, buffer, len); // Check if call successful, if PROS_ERR return vector containing PROS_ERR if (success == PROS_ERR) { // push_back PROS_ERR to files vector From ba4a55bdbe728f345cd2a20eda42411ee588f068 Mon Sep 17 00:00:00 2001 From: Grace Lu Date: Fri, 27 Oct 2023 16:38:42 -0400 Subject: [PATCH 07/19] Altered how list_files func deals with error Function to return vector containing specific error state errno is set to, if none of the error states will set to FILE I/O error --- include/pros/misc.h | 2 +- include/pros/misc.hpp | 4 ++-- src/devices/vdml_usd.cpp | 42 +++++++++++++++++++++++++++++++++++++--- 3 files changed, 42 insertions(+), 6 deletions(-) diff --git a/include/pros/misc.h b/include/pros/misc.h index 5137b28c9..db83fcec5 100644 --- a/include/pros/misc.h +++ b/include/pros/misc.h @@ -757,7 +757,7 @@ int32_t usd_is_installed(void); * pros::delay(200); * printf("%s\n", test); //Prints the file names in the root directory seperated by newlines * pros::delay(100); - * pros::c::usd_list_files_raw("/usd/test", test, 128); + * pros::c::usd_list_files_raw("/test", test, 128); * pros::delay(200); * printf("%s\n", test); //Prints the names of files in the folder named test seperated by newlines * pros::delay(100); diff --git a/include/pros/misc.hpp b/include/pros/misc.hpp index fe88e484b..16d897078 100644 --- a/include/pros/misc.hpp +++ b/include/pros/misc.hpp @@ -561,7 +561,7 @@ Lists the files in a directory specified by the path * pros::delay(200); * printf("%s\n", test); //Prints the file names in the root directory seperated by newlines * pros::delay(100); - * pros::list_files_raw("/usd/test", test, 128); + * pros::list_files_raw("/test", test, 128); * pros::delay(200); * printf("%s\n", test); //Prints the names of files in the folder named test seperated by newlines * pros::delay(100); @@ -595,7 +595,7 @@ Lists the files in a directory specified by the path * DO NOT PREPEND YOUR PATHS WITH "/usd/" * * \return vector of std::string of file names, if error occurs, returns vector containing - * one element, PROS_ERR + * one element specifying the error state * * \b Example * \code diff --git a/src/devices/vdml_usd.cpp b/src/devices/vdml_usd.cpp index 9fce8c12f..0f1a9ad36 100644 --- a/src/devices/vdml_usd.cpp +++ b/src/devices/vdml_usd.cpp @@ -29,10 +29,46 @@ std::vector list_files(const char* path, char* buffer, int32_t len) std::vector files = {}; // Call the C function int32_t success = usd_list_files_raw(path, buffer, len); - // Check if call successful, if PROS_ERR return vector containing PROS_ERR + // Check if call successful, if error return vector containing error state if (success == PROS_ERR) { - // push_back PROS_ERR to files vector - files.push_back(std::to_string(success)); + // Check errno to see which error state occurred + // push back error state to files vector as std::string + switch (errno) { + case EIO: + files.push_back("EIO"); + break; + case EINVAL: + files.push_back("EINVAL"); + break; + case EBUSY: + files.push_back("EBUSY"); + break; + case ENOENT: + files.push_back("ENOENT"); + break; + case EACCES: + files.push_back("EACCES"); + break; + case EEXIST: + files.push_back("EEXIST"); + break; + case EROFS: + files.push_back("EROFS"); + break; + case ENXIO: + files.push_back("ENXIO"); + break; + case ENOBUFS: + files.push_back("ENOBUFS"); + break; + case ENFILE: + files.push_back("ENFILE"); + break; + default: + // If none of the above, will be FILE IO error + files.push_back("FILE I/O ERROR"); + break; + } return files; } // Parse buffer given call successful, split by '/n' From da47f28c238fe8dbd6b35790eb7b5b22c59353b2 Mon Sep 17 00:00:00 2001 From: Grace Lu Date: Fri, 27 Oct 2023 16:42:53 -0400 Subject: [PATCH 08/19] Updated header regarding example for list_files func --- include/pros/misc.hpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/include/pros/misc.hpp b/include/pros/misc.hpp index 16d897078..c8d804102 100644 --- a/include/pros/misc.hpp +++ b/include/pros/misc.hpp @@ -602,10 +602,11 @@ Lists the files in a directory specified by the path * void opcontrol() { * char* test = (char*) malloc(128); * // Will return vector containing names of files in root directory - * std::vector files = list_files("/", test, 128); + * std::vector files = pros::usd::list_files("/", test, 128); * pros::delay(200); * // Given vector of std::string file names, print each file name - * // Note that if there is an error, the vector will contain one element, PROS_ERR + * // Note that if there is an error, the vector will contain one element, which + * // is the error state * // Print each file name * for (std::string file : files) { * std::cout << file << std::endl; From 3d41ca62de29da852509b02fcd8ce85c94a92e60 Mon Sep 17 00:00:00 2001 From: Grace Lu Date: Mon, 30 Oct 2023 17:13:24 -0400 Subject: [PATCH 09/19] Edits to list_files func, function header --- include/pros/misc.h | 7 ++++++- include/pros/misc.hpp | 18 ++++++++++++++---- src/devices/vdml_usd.cpp | 31 ++++++++++++++++--------------- 3 files changed, 36 insertions(+), 20 deletions(-) diff --git a/include/pros/misc.h b/include/pros/misc.h index db83fcec5..94ca26948 100644 --- a/include/pros/misc.h +++ b/include/pros/misc.h @@ -742,7 +742,12 @@ int32_t usd_is_installed(void); * ENOBUFS - drive has no work area * ENFILE - too many open files * - * + * \param path + * The path to the directory to list files in + * \param buffer + * The buffer to put the file names into + * \param len + * The length of the buffer * * \note use a path of "\" to list the files in the main directory NOT "/usd/" * DO NOT PREPEND YOUR PATHS WITH "/usd/" diff --git a/include/pros/misc.hpp b/include/pros/misc.hpp index c8d804102..8e38f6466 100644 --- a/include/pros/misc.hpp +++ b/include/pros/misc.hpp @@ -546,7 +546,12 @@ Lists the files in a directory specified by the path * ENOBUFS - drive has no work area * ENFILE - too many open files * - * + * \param path + * The path to the directory to list files in + * \param buffer + * The buffer to put the file names into + * \param len + * The length of the buffer * * \note use a path of "\" to list the files in the main directory NOT "/usd/" * DO NOT PREPEND YOUR PATHS WITH "/usd/" @@ -589,7 +594,12 @@ Lists the files in a directory specified by the path * ENOBUFS - drive has no work area * ENFILE - too many open files * - * + * \param path + * The path to the directory to list files in + * \param buffer + * The buffer to put the file names into + * \param len + * The length of the buffer * * \note use a path of "\" to list the files in the main directory NOT "/usd/" * DO NOT PREPEND YOUR PATHS WITH "/usd/" @@ -606,9 +616,9 @@ Lists the files in a directory specified by the path * pros::delay(200); * // Given vector of std::string file names, print each file name * // Note that if there is an error, the vector will contain one element, which - * // is the error state + * // is the error state * // Print each file name - * for (std::string file : files) { + * for (std::string& file : files) { * std::cout << file << std::endl; * } * } diff --git a/src/devices/vdml_usd.cpp b/src/devices/vdml_usd.cpp index 0f1a9ad36..becc257e9 100644 --- a/src/devices/vdml_usd.cpp +++ b/src/devices/vdml_usd.cpp @@ -71,29 +71,30 @@ std::vector list_files(const char* path, char* buffer, int32_t len) } return files; } + // Parse buffer given call successful, split by '/n' - // Store char * buffer as std::string in str - // Store delimiter in std::string, '\n' - std::string str(buffer); - std::string delimiter = "\n"; + std::string_view str(buffer); - // position to keep track of position in str + // delimter_pos is the position of the delimiter '\n' + // index of which character to start substr from // file_name used to store each file name - size_t position = 0; - std::string file_name; + size_t delimiter_pos, index = 0; + std::string_view file_name; // Loop until delimiter '\n' can not be found anymore - while ((position = str.find(delimiter)) != std::string::npos) { + while ((delimiter_pos = str.find('\n', index)) != std::string::npos) { // file_name is the string from the beginning of str to the first '\n', excluding '\n' - file_name = str.substr(0, position); + file_name = std::string_view(str.data() + index, delimiter_pos - index); // Add token to files vector - files.push_back(file_name); - // Erase file_name from str, would be pos + 1 if we wanted to include '\n' - str.erase(0, position + 1); - } + files.emplace_back(file_name); + // Increment index to start substr from + index = delimiter_pos + 1; - // return vector of file names - return files; + // If index is greater than or equal to str length, break + if (index >= str.length()) { + break; + } + } } } // namespace usd From 4247fb197e02a639c26dd75961c81d81e3e5b30a Mon Sep 17 00:00:00 2001 From: Grace Lu Date: Thu, 2 Nov 2023 19:47:12 -0400 Subject: [PATCH 10/19] Edits to list_files function --- include/pros/misc.hpp | 11 +++-------- src/devices/vdml_usd.cpp | 12 ++++++++++-- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/include/pros/misc.hpp b/include/pros/misc.hpp index 8e38f6466..6e19f3bda 100644 --- a/include/pros/misc.hpp +++ b/include/pros/misc.hpp @@ -577,7 +577,7 @@ std::int32_t list_files_raw(const char* path, char* buffer, int32_t len); /** Lists the files in a directory specified by the path - * Puts the list of file names (NOT DIRECTORIES) into the buffer seperated by newlines + * Puts the list of file names (NOT DIRECTORIES) into a vector of std::string * * This function uses the following values of errno when an error state is * reached: @@ -596,10 +596,6 @@ Lists the files in a directory specified by the path * * \param path * The path to the directory to list files in - * \param buffer - * The buffer to put the file names into - * \param len - * The length of the buffer * * \note use a path of "\" to list the files in the main directory NOT "/usd/" * DO NOT PREPEND YOUR PATHS WITH "/usd/" @@ -610,9 +606,8 @@ Lists the files in a directory specified by the path * \b Example * \code * void opcontrol() { - * char* test = (char*) malloc(128); * // Will return vector containing names of files in root directory - * std::vector files = pros::usd::list_files("/", test, 128); + * std::vector files = pros::usd::list_files("/"); * pros::delay(200); * // Given vector of std::string file names, print each file name * // Note that if there is an error, the vector will contain one element, which @@ -624,7 +619,7 @@ Lists the files in a directory specified by the path * } * \endcode */ -std::vector list_files(const char* path, char* buffer, int32_t len); +std::vector list_files(const char* path); } // namespace usd } // namespace pros diff --git a/src/devices/vdml_usd.cpp b/src/devices/vdml_usd.cpp index becc257e9..6193cc9b3 100644 --- a/src/devices/vdml_usd.cpp +++ b/src/devices/vdml_usd.cpp @@ -25,10 +25,12 @@ std::int32_t list_files_raw(const char* path, char* buffer, int32_t len) { return usd_list_files_raw(path, buffer, len); } -std::vector list_files(const char* path, char* buffer, int32_t len) { +std::vector list_files(const char* path) { std::vector files = {}; + // malloc buffer to store file names + char *buffer = (char *) malloc(10000); // Call the C function - int32_t success = usd_list_files_raw(path, buffer, len); + int32_t success = usd_list_files_raw(path, buffer, 10000); // Check if call successful, if error return vector containing error state if (success == PROS_ERR) { // Check errno to see which error state occurred @@ -95,6 +97,12 @@ std::vector list_files(const char* path, char* buffer, int32_t len) break; } } + + // Free buffer + free(buffer); + + // Return vector of file names + return files; } } // namespace usd From 8df65db0e749dba5995fdffe1dc5cd488398f78d Mon Sep 17 00:00:00 2001 From: Grace Lu Date: Thu, 2 Nov 2023 19:54:06 -0400 Subject: [PATCH 11/19] Update header for list_files and list_files_raw function --- include/pros/misc.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/pros/misc.hpp b/include/pros/misc.hpp index 6e19f3bda..3c1427799 100644 --- a/include/pros/misc.hpp +++ b/include/pros/misc.hpp @@ -528,7 +528,7 @@ namespace usd { std::int32_t is_installed(void); /** -Lists the files in a directory specified by the path + * Lists the files in a directory specified by the path * Puts the list of file names (NOT DIRECTORIES) into the buffer seperated by newlines * * This function uses the following values of errno when an error state is @@ -576,7 +576,7 @@ Lists the files in a directory specified by the path std::int32_t list_files_raw(const char* path, char* buffer, int32_t len); /** -Lists the files in a directory specified by the path + * Lists the files in a directory specified by the path * Puts the list of file names (NOT DIRECTORIES) into a vector of std::string * * This function uses the following values of errno when an error state is From f4aa6202ad392205491789ac59a990724dd587a6 Mon Sep 17 00:00:00 2001 From: Grace Lu Date: Thu, 2 Nov 2023 20:00:38 -0400 Subject: [PATCH 12/19] List_files dealt with possible memory allocation error --- src/devices/vdml_usd.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/devices/vdml_usd.cpp b/src/devices/vdml_usd.cpp index 6193cc9b3..38cfb2003 100644 --- a/src/devices/vdml_usd.cpp +++ b/src/devices/vdml_usd.cpp @@ -29,6 +29,15 @@ std::vector list_files(const char* path) { std::vector files = {}; // malloc buffer to store file names char *buffer = (char *) malloc(10000); + if (buffer == NULL) { + // try again smaller buffer to see if that works + buffer = (char *) malloc(500); + if (buffer == NULL) { + // if still fails, return vector containing error state + files.push_back("ENOMEM"); + return files; + } + } // Call the C function int32_t success = usd_list_files_raw(path, buffer, 10000); // Check if call successful, if error return vector containing error state From db8c2eee4247466193775b8c607694abb3082acd Mon Sep 17 00:00:00 2001 From: Grace Lu Date: Thu, 2 Nov 2023 20:03:07 -0400 Subject: [PATCH 13/19] edit dealing with memory allocation error in list_files --- src/devices/vdml_usd.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/devices/vdml_usd.cpp b/src/devices/vdml_usd.cpp index 38cfb2003..36c19b91f 100644 --- a/src/devices/vdml_usd.cpp +++ b/src/devices/vdml_usd.cpp @@ -34,7 +34,9 @@ std::vector list_files(const char* path) { buffer = (char *) malloc(500); if (buffer == NULL) { // if still fails, return vector containing error state - files.push_back("ENOMEM"); + // set errno to ENOMEM + errno = ENOMEM; + files.push_back("not enough memory to get file names"); return files; } } From a4bc889403491638cda00b334b799da636ae9ce6 Mon Sep 17 00:00:00 2001 From: Grace Lu Date: Thu, 2 Nov 2023 20:10:48 -0400 Subject: [PATCH 14/19] Update on dealing with errors Either file i/o error or path not found --- src/devices/vdml_usd.cpp | 41 ++++++---------------------------------- 1 file changed, 6 insertions(+), 35 deletions(-) diff --git a/src/devices/vdml_usd.cpp b/src/devices/vdml_usd.cpp index 36c19b91f..8ababbe24 100644 --- a/src/devices/vdml_usd.cpp +++ b/src/devices/vdml_usd.cpp @@ -46,41 +46,12 @@ std::vector list_files(const char* path) { if (success == PROS_ERR) { // Check errno to see which error state occurred // push back error state to files vector as std::string - switch (errno) { - case EIO: - files.push_back("EIO"); - break; - case EINVAL: - files.push_back("EINVAL"); - break; - case EBUSY: - files.push_back("EBUSY"); - break; - case ENOENT: - files.push_back("ENOENT"); - break; - case EACCES: - files.push_back("EACCES"); - break; - case EEXIST: - files.push_back("EEXIST"); - break; - case EROFS: - files.push_back("EROFS"); - break; - case ENXIO: - files.push_back("ENXIO"); - break; - case ENOBUFS: - files.push_back("ENOBUFS"); - break; - case ENFILE: - files.push_back("ENFILE"); - break; - default: - // If none of the above, will be FILE IO error - files.push_back("FILE I/O ERROR"); - break; + if (errno == EINVAL || errno == ENOENT) { + // errors related to path not found + files.push_back("path not found"); + } else { + // other error stats related to file io + files.push_back("file i/o error"); } return files; } From 490a3ca83be775d3e2c97da582e00099efb6393a Mon Sep 17 00:00:00 2001 From: Grace Lu Date: Sat, 4 Nov 2023 16:42:35 -0400 Subject: [PATCH 15/19] Made stylistic edits to list_files function --- include/pros/misc.hpp | 22 +++++++++++++++------- src/devices/vdml_usd.cpp | 16 +++++++++++----- 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/include/pros/misc.hpp b/include/pros/misc.hpp index 3c1427799..0d323d2b8 100644 --- a/include/pros/misc.hpp +++ b/include/pros/misc.hpp @@ -601,7 +601,7 @@ std::int32_t list_files_raw(const char* path, char* buffer, int32_t len); * DO NOT PREPEND YOUR PATHS WITH "/usd/" * * \return vector of std::string of file names, if error occurs, returns vector containing - * one element specifying the error state + * two elements, first element is "ERROR" and second element is the error message * * \b Example * \code @@ -610,12 +610,20 @@ std::int32_t list_files_raw(const char* path, char* buffer, int32_t len); * std::vector files = pros::usd::list_files("/"); * pros::delay(200); * // Given vector of std::string file names, print each file name - * // Note that if there is an error, the vector will contain one element, which - * // is the error state - * // Print each file name - * for (std::string& file : files) { - * std::cout << file << std::endl; - * } + * // Note that if there is an error, the vector will contain two elements, + * // first element is "ERROR" and second element is the error message + * + * // Check if error occurred + * if (file_list.front().start_with("ERROR")) { + * // deal with error + * } + * else { + * // file list returned is valid + * // Print each file name + * for (std::string& file : files) { + * std::cout << file << std::endl; + * } + * } * } * \endcode */ diff --git a/src/devices/vdml_usd.cpp b/src/devices/vdml_usd.cpp index 8ababbe24..c4d72038a 100644 --- a/src/devices/vdml_usd.cpp +++ b/src/devices/vdml_usd.cpp @@ -28,22 +28,26 @@ std::int32_t list_files_raw(const char* path, char* buffer, int32_t len) { std::vector list_files(const char* path) { std::vector files = {}; // malloc buffer to store file names - char *buffer = (char *) malloc(10000); + size_t buffer_size = 10000; + char *buffer = (char *) malloc(buffer_size); if (buffer == NULL) { // try again smaller buffer to see if that works - buffer = (char *) malloc(500); + buffer_size = 500; + buffer = (char *) malloc(buffer_size); if (buffer == NULL) { // if still fails, return vector containing error state // set errno to ENOMEM errno = ENOMEM; + files.push_back("ERROR"); files.push_back("not enough memory to get file names"); return files; } } // Call the C function - int32_t success = usd_list_files_raw(path, buffer, 10000); + int32_t success = usd_list_files_raw(path, buffer, buffer_size); // Check if call successful, if error return vector containing error state if (success == PROS_ERR) { + files.push_back("ERROR"); // Check errno to see which error state occurred // push back error state to files vector as std::string if (errno == EINVAL || errno == ENOENT) { @@ -62,14 +66,16 @@ std::vector list_files(const char* path) { // delimter_pos is the position of the delimiter '\n' // index of which character to start substr from // file_name used to store each file name - size_t delimiter_pos, index = 0; + size_t delimiter_pos = 0; + size_t index = 0; std::string_view file_name; // Loop until delimiter '\n' can not be found anymore while ((delimiter_pos = str.find('\n', index)) != std::string::npos) { // file_name is the string from the beginning of str to the first '\n', excluding '\n' file_name = std::string_view(str.data() + index, delimiter_pos - index); - // Add token to files vector + // This is point where content of the std::string_view file name is copied to its + // own std::string and added to the files vector files.emplace_back(file_name); // Increment index to start substr from index = delimiter_pos + 1; From b7b773ec6f20e1cedd258d3a94d3ccc97f2646de Mon Sep 17 00:00:00 2001 From: Grace Lu Date: Thu, 9 Nov 2023 22:41:55 -0500 Subject: [PATCH 16/19] list_files function Altered to return the file path instead of file names. Ex. "/usd/path/file_name". --- include/pros/misc.hpp | 14 ++++++-------- src/devices/vdml_usd.cpp | 15 ++++++++++++++- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/include/pros/misc.hpp b/include/pros/misc.hpp index 0d323d2b8..6264452a0 100644 --- a/include/pros/misc.hpp +++ b/include/pros/misc.hpp @@ -577,7 +577,7 @@ std::int32_t list_files_raw(const char* path, char* buffer, int32_t len); /** * Lists the files in a directory specified by the path - * Puts the list of file names (NOT DIRECTORIES) into a vector of std::string + * Puts the list of file paths (NOT DIRECTORIES) into a vector of std::string * * This function uses the following values of errno when an error state is * reached: @@ -596,9 +596,6 @@ std::int32_t list_files_raw(const char* path, char* buffer, int32_t len); * * \param path * The path to the directory to list files in - * - * \note use a path of "\" to list the files in the main directory NOT "/usd/" - * DO NOT PREPEND YOUR PATHS WITH "/usd/" * * \return vector of std::string of file names, if error occurs, returns vector containing * two elements, first element is "ERROR" and second element is the error message @@ -606,10 +603,10 @@ std::int32_t list_files_raw(const char* path, char* buffer, int32_t len); * \b Example * \code * void opcontrol() { - * // Will return vector containing names of files in root directory - * std::vector files = pros::usd::list_files("/"); + * // Will return vector containing file paths of files in root directory + * std::vector files = pros::usd::list_files("/test"); * pros::delay(200); - * // Given vector of std::string file names, print each file name + * // Given vector of std::string file paths, print each file path * // Note that if there is an error, the vector will contain two elements, * // first element is "ERROR" and second element is the error message * @@ -619,7 +616,8 @@ std::int32_t list_files_raw(const char* path, char* buffer, int32_t len); * } * else { * // file list returned is valid - * // Print each file name + * // Print each file path + * // Each file path in format "/usd/path/file_name" * for (std::string& file : files) { * std::cout << file << std::endl; * } diff --git a/src/devices/vdml_usd.cpp b/src/devices/vdml_usd.cpp index c4d72038a..923bbd296 100644 --- a/src/devices/vdml_usd.cpp +++ b/src/devices/vdml_usd.cpp @@ -43,6 +43,19 @@ std::vector list_files(const char* path) { return files; } } + + // Check path user passed in + std::string_view path_sv(path); + size_t found = path_sv.find("usd"); + if (found == 0 || found == 1) { + // Deal with when user prepends path with usd + // as either "usd/..." or "/usd/..." + path_sv.remove_prefix(3); + } + + // set path to path_sv.data() + path = path_sv.data(); + // Call the C function int32_t success = usd_list_files_raw(path, buffer, buffer_size); // Check if call successful, if error return vector containing error state @@ -76,7 +89,7 @@ std::vector list_files(const char* path) { file_name = std::string_view(str.data() + index, delimiter_pos - index); // This is point where content of the std::string_view file name is copied to its // own std::string and added to the files vector - files.emplace_back(file_name); + files.emplace_back("/usd" + std::string(path) + "/" + std::string(file_name)); // Increment index to start substr from index = delimiter_pos + 1; From 1c42992e5b0a062e0d5030178e4d55137d96df6f Mon Sep 17 00:00:00 2001 From: Grace Lu Date: Fri, 10 Nov 2023 13:28:02 -0500 Subject: [PATCH 17/19] Edits to list_files function on handling file paths, inclusion of "usd" in file path --- src/devices/vdml_usd.cpp | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/devices/vdml_usd.cpp b/src/devices/vdml_usd.cpp index 923bbd296..d77787f17 100644 --- a/src/devices/vdml_usd.cpp +++ b/src/devices/vdml_usd.cpp @@ -46,11 +46,12 @@ std::vector list_files(const char* path) { // Check path user passed in std::string_view path_sv(path); - size_t found = path_sv.find("usd"); - if (found == 0 || found == 1) { + constexpr std::string_view usd_prefix {"usd"}; + const size_t usd_prefix_idx = path_sv.find(usd_prefix); + if (usd_prefix_idx == 0 || usd_prefix_idx == 1) { // Deal with when user prepends path with usd // as either "usd/..." or "/usd/..." - path_sv.remove_prefix(3); + path_sv.remove_prefix(usd_prefix.length() + usd_prefix_idx); } // set path to path_sv.data() @@ -89,7 +90,12 @@ std::vector list_files(const char* path) { file_name = std::string_view(str.data() + index, delimiter_pos - index); // This is point where content of the std::string_view file name is copied to its // own std::string and added to the files vector - files.emplace_back("/usd" + std::string(path) + "/" + std::string(file_name)); + const size_t path_size = 1 + usd_prefix.length() + strlen(path) + 1 + file_name.length() + 1; + std::string buffer; + buffer.reserve(path_size); + std::snprintf(buffer.data(), path_size, "/usd%s/%.*s", path, static_cast(file_name.length()), file_name.data()); + + files.push_back(std::move(buffer)); // Increment index to start substr from index = delimiter_pos + 1; From fc2a72fe6873eacf6c1cf9c070d5372ff3ff0dda Mon Sep 17 00:00:00 2001 From: Grace Lu Date: Tue, 22 Oct 2024 23:11:34 -0400 Subject: [PATCH 18/19] Updated list_files function --- include/pros/misc.hpp | 4 ++-- src/devices/vdml_usd.cpp | 14 ++++---------- 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/include/pros/misc.hpp b/include/pros/misc.hpp index 6264452a0..c40849e21 100644 --- a/include/pros/misc.hpp +++ b/include/pros/misc.hpp @@ -592,6 +592,7 @@ std::int32_t list_files_raw(const char* path, char* buffer, int32_t len); * EROFS - SD card is write protected * ENXIO - drive number is invalid or not a FAT32 drive * ENOBUFS - drive has no work area + * ETIMEDOUT - Operation timed out while accessing the volume * ENFILE - too many open files * * \param path @@ -616,8 +617,7 @@ std::int32_t list_files_raw(const char* path, char* buffer, int32_t len); * } * else { * // file list returned is valid - * // Print each file path - * // Each file path in format "/usd/path/file_name" + * // Print each file * for (std::string& file : files) { * std::cout << file << std::endl; * } diff --git a/src/devices/vdml_usd.cpp b/src/devices/vdml_usd.cpp index d77787f17..8de9c5eb6 100644 --- a/src/devices/vdml_usd.cpp +++ b/src/devices/vdml_usd.cpp @@ -82,20 +82,14 @@ std::vector list_files(const char* path) { // file_name used to store each file name size_t delimiter_pos = 0; size_t index = 0; - std::string_view file_name; + std::string file_name; // Loop until delimiter '\n' can not be found anymore while ((delimiter_pos = str.find('\n', index)) != std::string::npos) { // file_name is the string from the beginning of str to the first '\n', excluding '\n' - file_name = std::string_view(str.data() + index, delimiter_pos - index); - // This is point where content of the std::string_view file name is copied to its - // own std::string and added to the files vector - const size_t path_size = 1 + usd_prefix.length() + strlen(path) + 1 + file_name.length() + 1; - std::string buffer; - buffer.reserve(path_size); - std::snprintf(buffer.data(), path_size, "/usd%s/%.*s", path, static_cast(file_name.length()), file_name.data()); - - files.push_back(std::move(buffer)); + file_name = std::string(str.data() + index, delimiter_pos - index); + + files.push_back(file_name); // Increment index to start substr from index = delimiter_pos + 1; From b36e01c43695e968824ca180bf5f00bba4a085fa Mon Sep 17 00:00:00 2001 From: Grace Lu Date: Thu, 24 Oct 2024 19:32:24 -0400 Subject: [PATCH 19/19] Update misc.h --- include/pros/misc.h | 1 + 1 file changed, 1 insertion(+) diff --git a/include/pros/misc.h b/include/pros/misc.h index 94ca26948..56a2d8c17 100644 --- a/include/pros/misc.h +++ b/include/pros/misc.h @@ -740,6 +740,7 @@ int32_t usd_is_installed(void); * EROFS - SD card is write protected * ENXIO - drive number is invalid or not a FAT32 drive * ENOBUFS - drive has no work area + * ETIMEDOUT - Operation timed out while accessing the volume * ENFILE - too many open files * * \param path