-
Notifications
You must be signed in to change notification settings - Fork 0
/
PathStringArg.cpp
85 lines (60 loc) · 2.58 KB
/
PathStringArg.cpp
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include "StdAfx.h"
#include "PathStringArg.h"
#include <boost\algorithm\string\case_conv.hpp>
//using namespace std;
PathStringArg::~PathStringArg(void){
}
//Normalized
string PathStringArg::GetNormalizedPath() const{
return NormalizePath(s, len);
}
string PathStringArg::GetExtension() const{
if(len == 0 || s[0] == 0)return string();
const char* dot = strchr(s, '.');
if(dot == NULL){
return string();
}
int ExtIndex = (int)(dot-s);
string ext(s+ExtIndex, len-ExtIndex);
boost::to_lower(ext);
return ext;
}
const PlatformString& PathStringArg::GetConvertedNativePath()const{
//the lua interfacing functions should be calling ConvertAndValidate on us before they pass it onto the real function
_ASSERT(CachedConvertedString.empty() == false || len == 0);
return CachedConvertedString;
}
PlatformPath PathStringArg::CreatePath(const PlatformPath& Root)const{
//the lua interfacing functions should be calling ConvertAndValidate on us before they pass it onto the real function
_ASSERT(CachedConvertedString.empty() == false || len == 0);
return Root/CachedConvertedString;
}
PlatformPath PathStringArg::CreateSearchPath(const PlatformPath& Root, const PathStringArg& SearchPath)const{
//the lua interfacing functions should be calling ConvertAndValidate on us before they pass it onto the real function
_ASSERT(CachedConvertedString.empty() == false || len == 0);
const PlatformPath& Path = CachedConvertedString.empty() ? Root : Root/CachedConvertedString;
if(SearchPath.len == 0){
return Path/_T("*");
}else{
return Path/SearchPath.GetConvertedNativePath();
}
}
const PlatformString& PathStringArg::ConvertAndValidate(const char* VaribleName){
#ifdef UTF8Paths
UTF8ToWString(s, len, CachedConvertedString);
#else
CachedConvertedString = AsciiToWString(s, len);
#endif
if(CachedConvertedString.find_first_of(InvalidPathChars) != PathString::npos){
throw std::runtime_error(string(VaribleName)+" cannot contain any of these chars :%|<>\"");
}
//size_t dotindex = CachedConvertedString.find('.');
//check thats theres not more than 1 dot in the string
if(CachedConvertedString.find(L"..") != string::npos){
throw std::runtime_error(string(VaribleName)+" cannot contain the directory up string \"..\"");
}
if(!CachedConvertedString.empty() && (CachedConvertedString[0] == L'.' || CachedConvertedString.back() == L'.')){
throw std::runtime_error(string(VaribleName)+" cannot contain dot . at the start or the end of the string");
}
return CachedConvertedString;
}