oculus1

view libovr/Src/Kernel/OVR_String_PathUtil.cpp @ 1:e2f9e4603129

added LibOVR and started a simple vr wrapper.
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 14 Sep 2013 16:14:59 +0300
parents
children b069a5c27388
line source
1 /************************************************************************************
3 Filename : OVR_String_PathUtil.cpp
4 Content : String filename/url helper function
5 Created : September 19, 2012
6 Notes :
8 Copyright : Copyright 2012 Oculus VR, Inc. All Rights reserved.
10 Use of this software is subject to the terms of the Oculus license
11 agreement provided at the time of installation or download, or which
12 otherwise accompanies this software in either electronic or hard copy form.
14 ************************************************************************************/
16 #include "OVR_String.h"
17 #include "OVR_UTF8Util.h"
19 namespace OVR {
21 //--------------------------------------------------------------------
22 // ***** Path-Scanner helper function
24 // Scans file path finding filename start and extension start, fills in their addess.
25 void ScanFilePath(const char* url, const char** pfilename, const char** pext)
26 {
27 const char* urlStart = url;
28 const char *filename = 0;
29 const char *lastDot = 0;
31 UInt32 charVal = UTF8Util::DecodeNextChar(&url);
33 while (charVal != 0)
34 {
35 if ((charVal == '/') || (charVal == '\\'))
36 {
37 filename = url;
38 lastDot = 0;
39 }
40 else if (charVal == '.')
41 {
42 lastDot = url - 1;
43 }
45 charVal = UTF8Util::DecodeNextChar(&url);
46 }
48 if (pfilename)
49 {
50 // It was a naked filename
51 if (urlStart && (*urlStart != '.') && *urlStart)
52 *pfilename = urlStart;
53 else
54 *pfilename = filename;
55 }
57 if (pext)
58 {
59 *pext = lastDot;
60 }
61 }
63 // Scans till the end of protocol. Returns first character past protocol,
64 // 0 if not found.
65 // - protocol: 'file://', 'http://'
66 const char* ScanPathProtocol(const char* url)
67 {
68 UInt32 charVal = UTF8Util::DecodeNextChar(&url);
69 UInt32 charVal2;
71 while (charVal != 0)
72 {
73 // Treat a colon followed by a slash as absolute.
74 if (charVal == ':')
75 {
76 charVal2 = UTF8Util::DecodeNextChar(&url);
77 charVal = UTF8Util::DecodeNextChar(&url);
78 if ((charVal == '/') && (charVal2 == '\\'))
79 return url;
80 }
81 charVal = UTF8Util::DecodeNextChar(&url);
82 }
83 return 0;
84 }
87 //--------------------------------------------------------------------
88 // ***** String Path API implementation
90 bool String::HasAbsolutePath(const char* url)
91 {
92 // Absolute paths can star with:
93 // - protocols: 'file://', 'http://'
94 // - windows drive: 'c:\'
95 // - UNC share name: '\\share'
96 // - unix root '/'
98 // On the other hand, relative paths are:
99 // - directory: 'directory/file'
100 // - this directory: './file'
101 // - parent directory: '../file'
102 //
103 // For now, we don't parse '.' or '..' out, but instead let it be concatenated
104 // to string and let the OS figure it out. This, however, is not good for file
105 // name matching in library/etc, so it should be improved.
107 if (!url || !*url)
108 return true; // Treat empty strings as absolute.
110 UInt32 charVal = UTF8Util::DecodeNextChar(&url);
112 // Fist character of '/' or '\\' means absolute url.
113 if ((charVal == '/') || (charVal == '\\'))
114 return true;
116 while (charVal != 0)
117 {
118 // Treat a colon followed by a slash as absolute.
119 if (charVal == ':')
120 {
121 charVal = UTF8Util::DecodeNextChar(&url);
122 // Protocol or windows drive. Absolute.
123 if ((charVal == '/') || (charVal == '\\'))
124 return true;
125 }
126 else if ((charVal == '/') || (charVal == '\\'))
127 {
128 // Not a first character (else 'if' above the loop would have caught it).
129 // Must be a relative url.
130 break;
131 }
133 charVal = UTF8Util::DecodeNextChar(&url);
134 }
136 // We get here for relative paths.
137 return false;
138 }
141 bool String::HasExtension(const char* path)
142 {
143 const char* ext = 0;
144 ScanFilePath(path, 0, &ext);
145 return ext != 0;
146 }
147 bool String::HasProtocol(const char* path)
148 {
149 return ScanPathProtocol(path) != 0;
150 }
153 String String::GetPath() const
154 {
155 const char* filename = 0;
156 ScanFilePath(ToCStr(), &filename, 0);
158 // Technically we can have extra logic somewhere for paths,
159 // such as enforcing protocol and '/' only based on flags,
160 // but we keep it simple for now.
161 return String(ToCStr(), filename ? (filename-ToCStr()) : GetSize());
162 }
164 String String::GetProtocol() const
165 {
166 const char* protocolEnd = ScanPathProtocol(ToCStr());
167 return String(ToCStr(), protocolEnd ? (protocolEnd-ToCStr()) : 0);
168 }
170 String String::GetFilename() const
171 {
172 const char* filename = 0;
173 ScanFilePath(ToCStr(), &filename, 0);
174 return String(filename);
175 }
176 String String::GetExtension() const
177 {
178 const char* ext = 0;
179 ScanFilePath(ToCStr(), 0, &ext);
180 return String(ext);
181 }
183 void String::StripExtension()
184 {
185 const char* ext = 0;
186 ScanFilePath(ToCStr(), 0, &ext);
187 if (ext)
188 {
189 *this = String(ToCStr(), ext-ToCStr());
190 }
191 }
193 void String::StripProtocol()
194 {
195 const char* protocol = ScanPathProtocol(ToCStr());
196 if (protocol)
197 AssignString(protocol, OVR_strlen(protocol));
198 }
200 } // OVR