Skip to content

Commit

Permalink
Add a path split function
Browse files Browse the repository at this point in the history
  • Loading branch information
RobDangerous committed Feb 14, 2024
1 parent e082e94 commit fa2d028
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions glob/src/glob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
#include "log.h"

#include <string>
#include <vector>

const char *pattern = "../../kmake/**";

using std::string;
using std::vector;

string find_base_dir(const string &pattern) {
size_t last_break = pattern.size();
Expand All @@ -24,6 +26,19 @@ string to_absolute(const string &path) {
return string(working_dir()) + "/" + path;
}

vector<string> split(const string &path) {
vector<string> parts;
size_t last = 0;
for (size_t i = 0; i < path.size(); ++i) {
if (path[i] == '/' || path[i] == '\\') {
parts.push_back(path.substr(last, i - last));
last = i + 1;
}
}
parts.push_back(path.substr(last));
return parts;
}

int main(int argc, char **argv) {
string absolute = to_absolute(pattern);

Expand All @@ -34,5 +49,11 @@ int main(int argc, char **argv) {
kmake_log(LOG_LEVEL_INFO, "Visited %s", f.name);
f = read_next_file(&dir);
}

auto parts = split(pattern);
for (auto &part : parts) {
kmake_log(LOG_LEVEL_INFO, "Part: %s", part.c_str());
}

return 0;
}

0 comments on commit fa2d028

Please sign in to comment.