Skip to content

Commit

Permalink
Edits to list_files func, function header
Browse files Browse the repository at this point in the history
  • Loading branch information
Gracelu128 committed Oct 30, 2023
1 parent da47f28 commit 3d41ca6
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 20 deletions.
7 changes: 6 additions & 1 deletion include/pros/misc.h
Original file line number Diff line number Diff line change
Expand Up @@ -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/"
Expand Down
18 changes: 14 additions & 4 deletions include/pros/misc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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/"
Expand Down Expand Up @@ -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/"
Expand All @@ -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;
* }
* }
Expand Down
31 changes: 16 additions & 15 deletions src/devices/vdml_usd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,29 +71,30 @@ std::vector<std::string> 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
Expand Down

0 comments on commit 3d41ca6

Please sign in to comment.