From 4247fb197e02a639c26dd75961c81d81e3e5b30a Mon Sep 17 00:00:00 2001 From: Grace Lu Date: Thu, 2 Nov 2023 19:47:12 -0400 Subject: [PATCH] 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 8e38f646..6e19f3bd 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 becc257e..6193cc9b 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