Skip to content

Commit

Permalink
Made stylistic edits to list_files function
Browse files Browse the repository at this point in the history
  • Loading branch information
Gracelu128 committed Nov 4, 2023
1 parent a4bc889 commit 490a3ca
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
22 changes: 15 additions & 7 deletions include/pros/misc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -610,12 +610,20 @@ std::int32_t list_files_raw(const char* path, char* buffer, int32_t len);
* 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
* // 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
*/
Expand Down
16 changes: 11 additions & 5 deletions src/devices/vdml_usd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,26 @@ std::int32_t list_files_raw(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);
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) {
Expand All @@ -62,14 +66,16 @@ std::vector<std::string> 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;
Expand Down

0 comments on commit 490a3ca

Please sign in to comment.