Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fancy file directory loading stuff!

  • Loading branch information...
commit 9e86aa84f6914df1452e6f57c96757cf0aee3812 1 parent ad8709a
@andre-d authored
Showing with 80 additions and 22 deletions.
  1. +72 −18 casecorrectpath.cpp
  2. +8 −4 test.cpp
View
90 casecorrectpath.cpp
@@ -5,27 +5,85 @@
#else
#include <algorithm>
#include <cstdlib>
+ #include <dirent.h>
#endif
namespace {
-// Return the index of the nth instance of the_char in str, starting from the back
-// Returns -1 if there is no nth instance
-int FindNthCharFromBack(std::string& str, char the_char, int n){
- int count = 0;
- int str_length = str.length();
- for(int i = str_length-1; i>=0; --i){
- if(str[i] == the_char){
- ++count;
- if(count == n){
- return i;
+
+std::string BuildRealPath(std::string& input_path) {
+ std::string path = "";
+ size_t i = 0;
+ std::string next_child = ".";
+ bool first = true;
+
+ if(input_path.compare(0, next_child.length(), "./") == 0) {
+ i += 2;
+ path = "./";
+ } else if(input_path[0] == '/') {
+ i += 1;
+ path = "/";
+ next_child = "/";
+ }
+
+ bool done = false;
+ for(;!done && i < input_path.length();) {
+ DIR *dp;
+ struct dirent *ep;
+
+ std::string dir;
+
+ if(first) {
+ dir = next_child;
+ first = false;
+ } else {
+ dir = path;
+ }
+ dp = opendir(dir.c_str());
+
+ if(!dp) {
+ return ""; // Unexpected, raise exception?
+ }
+
+ size_t dir_end = input_path.find_first_of('/', i + 1);
+
+ if(dir_end == std::string::npos) {
+ next_child = input_path.substr(i);
+ done = true;
+ } else {
+ next_child = input_path.substr(i, dir_end - i) ;
+ i = dir_end + 1;
+ }
+
+ std::transform(next_child.begin(), next_child.end(), next_child.begin(), ::tolower);
+
+ bool found = false;
+ while(!found && (ep = readdir (dp))) {
+ std::string cur(ep->d_name);
+ std::transform(cur.begin(), cur.end(), cur.begin(), ::tolower);
+ if(next_child == cur) {
+ found = true;
+ path += ep->d_name;
}
+ free(ep);
+ }
+ closedir(dp);
+ free(dp);
+
+
+ if(!found) {
+ return ""; // Unexpected, raise exception?
+ }
+
+ if(!done) {
+ path += "/";
}
}
- return -1;
+
+ return path;
+
}
-
std::string GetCaseCorrectPath(std::string input_path){
#ifdef _WIN32 // Correct case by converting to short path and back to long
long kPathBufferSize = GetLongPathName(input_path.c_str(), NULL, 0);
@@ -37,12 +95,8 @@ std::string GetCaseCorrectPath(std::string input_path){
delete short_path;
delete correct_case_buf;
return corrected;
- #else // Correct case using realpath() and cut off working directory
- std::ptrdiff_t num_dirs = std::count(input_path.begin(), input_path.end(), '/');
- char* real_path = realpath(input_path.c_str(), NULL);
- std::string path(real_path);
- free(real_path);
- return path.substr(FindNthCharFromBack(path,'/',num_dirs+1)+1);
+ #else // Correct case by traversing the directory(s) manually
+ return BuildRealPath(input_path);
#endif
}
View
12 test.cpp
@@ -20,11 +20,15 @@ void CheckCase(const char* path){
}
int main(int argc, char* argv[]){
- CheckCase("casecorrectpath.cpp");
- CheckCase("CaseCorrectPath.H");
- CheckCase("CASECORRECTPATH.CPP");
+ CheckCase("/mnt/DATA/TEST");
+ CheckCase("/mnt/data/TEST");
+ CheckCase("./DATA/test");
+ CheckCase("./data/TEST");
+ CheckCase("DATA/test");
+ CheckCase("data/TEST");
+
#ifdef _WIN32
printf("\nTest completed, press 'enter' to quit\n");
getchar();
#endif
-}
+}
Please sign in to comment.
Something went wrong with that request. Please try again.