-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcasecorrectpath.cpp
More file actions
66 lines (59 loc) · 1.86 KB
/
casecorrectpath.cpp
File metadata and controls
66 lines (59 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <stdio.h>
#include <string>
#ifdef _WIN32
#include <windows.h>
#else
#include <stdlib.h>
#endif
namespace {
static const int kPathBufferSize = 1024;
// Return the number of instances of the_char in str
int CountCharsInString(const char* str, char the_char){
int count = 0;
int str_length = strlen(str);
for(int i=0; i<str_length; ++i){
if(str[i] == the_char){
++count;
}
}
return count;
}
// 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(const char* str, char the_char, int n){
int count = 0;
int str_length = strlen(str);
for(int i= str_length-1; i>=0; --i){
if(str[i] == the_char){
++count;
if(count == n){
return i;
}
}
}
return -1;
}
void GetCaseCorrectPath(const char* input_path, char* correct_path){
#ifdef _WIN32 // Correct case by converting to short path and back to long
char short_path[kPathBufferSize];
GetShortPathName(input_path, short_path, kPathBufferSize);
GetLongPathName(short_path, correct_path, kPathBufferSize);
#else // Correct case using realpath() and cut off working directory
int num_dirs = CountCharsInString(input_path, '/');
char path[kPathBufferSize];
realpath(input_path, path);
char *cut_path = &path[FindNthCharFromBack(path,'/',num_dirs+1)+1];
strcpy(correct_path, cut_path);
#endif
}
} // namespace ""
bool IsPathCaseCorrect(const std::string& input_path, std::string *correct_case){
char correct_case_buf[kPathBufferSize];
GetCaseCorrectPath(input_path.c_str(), correct_case_buf);
*correct_case = correct_case_buf;
if(input_path != *correct_case){
return false;
} else {
return true;
}
}