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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
/************************************************************************************
Filename : OVR_String_PathUtil.cpp
Content : String filename/url helper function
Created : September 19, 2012
Notes :
Copyright : Copyright 2013 Oculus VR, Inc. All Rights reserved.
Licensed under the Oculus VR SDK License Version 2.0 (the "License");
you may not use the Oculus VR SDK except in compliance with the License,
which is provided at the time of installation or download, or which
otherwise accompanies this software in either electronic or hard copy form.
You may obtain a copy of the License at
http://www.oculusvr.com/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, the Oculus VR SDK
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
************************************************************************************/
#include "OVR_String.h"
#include "OVR_UTF8Util.h"
namespace OVR {
//--------------------------------------------------------------------
// ***** Path-Scanner helper function
// Scans file path finding filename start and extension start, fills in their addess.
void ScanFilePath(const char* url, const char** pfilename, const char** pext)
{
const char* urlStart = url;
const char *filename = 0;
const char *lastDot = 0;
UInt32 charVal = UTF8Util::DecodeNextChar(&url);
while (charVal != 0)
{
if ((charVal == '/') || (charVal == '\\'))
{
filename = url;
lastDot = 0;
}
else if (charVal == '.')
{
lastDot = url - 1;
}
charVal = UTF8Util::DecodeNextChar(&url);
}
if (pfilename)
{
// It was a naked filename
if (urlStart && (*urlStart != '.') && *urlStart)
*pfilename = urlStart;
else
*pfilename = filename;
}
if (pext)
{
*pext = lastDot;
}
}
// Scans till the end of protocol. Returns first character past protocol,
// 0 if not found.
// - protocol: 'file://', 'http://'
const char* ScanPathProtocol(const char* url)
{
UInt32 charVal = UTF8Util::DecodeNextChar(&url);
UInt32 charVal2;
while (charVal != 0)
{
// Treat a colon followed by a slash as absolute.
if (charVal == ':')
{
charVal2 = UTF8Util::DecodeNextChar(&url);
charVal = UTF8Util::DecodeNextChar(&url);
if ((charVal == '/') && (charVal2 == '\\'))
return url;
}
charVal = UTF8Util::DecodeNextChar(&url);
}
return 0;
}
//--------------------------------------------------------------------
// ***** String Path API implementation
bool String::HasAbsolutePath(const char* url)
{
// Absolute paths can star with:
// - protocols: 'file://', 'http://'
// - windows drive: 'c:\'
// - UNC share name: '\\share'
// - unix root '/'
// On the other hand, relative paths are:
// - directory: 'directory/file'
// - this directory: './file'
// - parent directory: '../file'
//
// For now, we don't parse '.' or '..' out, but instead let it be concatenated
// to string and let the OS figure it out. This, however, is not good for file
// name matching in library/etc, so it should be improved.
if (!url || !*url)
return true; // Treat empty strings as absolute.
UInt32 charVal = UTF8Util::DecodeNextChar(&url);
// Fist character of '/' or '\\' means absolute url.
if ((charVal == '/') || (charVal == '\\'))
return true;
while (charVal != 0)
{
// Treat a colon followed by a slash as absolute.
if (charVal == ':')
{
charVal = UTF8Util::DecodeNextChar(&url);
// Protocol or windows drive. Absolute.
if ((charVal == '/') || (charVal == '\\'))
return true;
}
else if ((charVal == '/') || (charVal == '\\'))
{
// Not a first character (else 'if' above the loop would have caught it).
// Must be a relative url.
break;
}
charVal = UTF8Util::DecodeNextChar(&url);
}
// We get here for relative paths.
return false;
}
bool String::HasExtension(const char* path)
{
const char* ext = 0;
ScanFilePath(path, 0, &ext);
return ext != 0;
}
bool String::HasProtocol(const char* path)
{
return ScanPathProtocol(path) != 0;
}
String String::GetPath() const
{
const char* filename = 0;
ScanFilePath(ToCStr(), &filename, 0);
// Technically we can have extra logic somewhere for paths,
// such as enforcing protocol and '/' only based on flags,
// but we keep it simple for now.
return String(ToCStr(), filename ? (filename-ToCStr()) : GetSize());
}
String String::GetProtocol() const
{
const char* protocolEnd = ScanPathProtocol(ToCStr());
return String(ToCStr(), protocolEnd ? (protocolEnd-ToCStr()) : 0);
}
String String::GetFilename() const
{
const char* filename = 0;
ScanFilePath(ToCStr(), &filename, 0);
return String(filename);
}
String String::GetExtension() const
{
const char* ext = 0;
ScanFilePath(ToCStr(), 0, &ext);
return String(ext);
}
void String::StripExtension()
{
const char* ext = 0;
ScanFilePath(ToCStr(), 0, &ext);
if (ext)
{
*this = String(ToCStr(), ext-ToCStr());
}
}
void String::StripProtocol()
{
const char* protocol = ScanPathProtocol(ToCStr());
if (protocol)
AssignString(protocol, OVR_strlen(protocol));
}
} // OVR
|