Skip to content

Commit

Permalink
Edits to list_files function
Browse files Browse the repository at this point in the history
  • Loading branch information
Gracelu128 committed Nov 2, 2023
1 parent 3d41ca6 commit 4247fb1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
11 changes: 3 additions & 8 deletions include/pros/misc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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/"
Expand All @@ -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<std::string> files = pros::usd::list_files("/", test, 128);
* std::vector<std::string> 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
Expand All @@ -624,7 +619,7 @@ Lists the files in a directory specified by the path
* }
* \endcode
*/
std::vector<std::string> list_files(const char* path, char* buffer, int32_t len);
std::vector<std::string> list_files(const char* path);
} // namespace usd

} // namespace pros
Expand Down
12 changes: 10 additions & 2 deletions src/devices/vdml_usd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> list_files(const char* path, char* buffer, int32_t len) {
std::vector<std::string> list_files(const char* path) {
std::vector<std::string> 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
Expand Down Expand Up @@ -95,6 +97,12 @@ std::vector<std::string> list_files(const char* path, char* buffer, int32_t len)
break;
}
}

// Free buffer
free(buffer);

// Return vector of file names
return files;
}

} // namespace usd
Expand Down

0 comments on commit 4247fb1

Please sign in to comment.