ovr_sdk
diff LibOVR/Src/Kernel/OVR_String_PathUtil.cpp @ 0:1b39a1b46319
initial 0.4.4
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Wed, 14 Jan 2015 06:51:16 +0200 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/LibOVR/Src/Kernel/OVR_String_PathUtil.cpp Wed Jan 14 06:51:16 2015 +0200 1.3 @@ -0,0 +1,211 @@ 1.4 +/************************************************************************************ 1.5 + 1.6 +Filename : OVR_String_PathUtil.cpp 1.7 +Content : String filename/url helper function 1.8 +Created : September 19, 2012 1.9 +Notes : 1.10 + 1.11 +Copyright : Copyright 2014 Oculus VR, LLC All Rights reserved. 1.12 + 1.13 +Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License"); 1.14 +you may not use the Oculus VR Rift SDK except in compliance with the License, 1.15 +which is provided at the time of installation or download, or which 1.16 +otherwise accompanies this software in either electronic or hard copy form. 1.17 + 1.18 +You may obtain a copy of the License at 1.19 + 1.20 +http://www.oculusvr.com/licenses/LICENSE-3.2 1.21 + 1.22 +Unless required by applicable law or agreed to in writing, the Oculus VR SDK 1.23 +distributed under the License is distributed on an "AS IS" BASIS, 1.24 +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1.25 +See the License for the specific language governing permissions and 1.26 +limitations under the License. 1.27 + 1.28 +************************************************************************************/ 1.29 + 1.30 +#include "OVR_String.h" 1.31 +#include "OVR_UTF8Util.h" 1.32 + 1.33 +namespace OVR { 1.34 + 1.35 +//-------------------------------------------------------------------- 1.36 +// ***** Path-Scanner helper function 1.37 + 1.38 +// Scans file path finding filename start and extension start, fills in their addess. 1.39 +void ScanFilePath(const char* url, const char** pfilename, const char** pext) 1.40 +{ 1.41 + const char* urlStart = url; 1.42 + const char *filename = 0; 1.43 + const char *lastDot = 0; 1.44 + 1.45 + uint32_t charVal = UTF8Util::DecodeNextChar(&url); 1.46 + 1.47 + while (charVal != 0) 1.48 + { 1.49 + if ((charVal == '/') || (charVal == '\\')) 1.50 + { 1.51 + filename = url; 1.52 + lastDot = 0; 1.53 + } 1.54 + else if (charVal == '.') 1.55 + { 1.56 + lastDot = url - 1; 1.57 + } 1.58 + 1.59 + charVal = UTF8Util::DecodeNextChar(&url); 1.60 + } 1.61 + 1.62 + if (pfilename) 1.63 + { 1.64 + // It was a naked filename 1.65 + if (urlStart && (*urlStart != '.') && *urlStart) 1.66 + *pfilename = urlStart; 1.67 + else 1.68 + *pfilename = filename; 1.69 + } 1.70 + 1.71 + if (pext) 1.72 + { 1.73 + *pext = lastDot; 1.74 + } 1.75 +} 1.76 + 1.77 +// Scans till the end of protocol. Returns first character past protocol, 1.78 +// 0 if not found. 1.79 +// - protocol: 'file://', 'http://' 1.80 +const char* ScanPathProtocol(const char* url) 1.81 +{ 1.82 + uint32_t charVal = UTF8Util::DecodeNextChar(&url); 1.83 + uint32_t charVal2; 1.84 + 1.85 + while (charVal != 0) 1.86 + { 1.87 + // Treat a colon followed by a slash as absolute. 1.88 + if (charVal == ':') 1.89 + { 1.90 + charVal2 = UTF8Util::DecodeNextChar(&url); 1.91 + charVal = UTF8Util::DecodeNextChar(&url); 1.92 + if ((charVal == '/') && (charVal2 == '\\')) 1.93 + return url; 1.94 + } 1.95 + charVal = UTF8Util::DecodeNextChar(&url); 1.96 + } 1.97 + return 0; 1.98 +} 1.99 + 1.100 + 1.101 +//-------------------------------------------------------------------- 1.102 +// ***** String Path API implementation 1.103 + 1.104 +bool String::HasAbsolutePath(const char* url) 1.105 +{ 1.106 + // Absolute paths can star with: 1.107 + // - protocols: 'file://', 'http://' 1.108 + // - windows drive: 'c:\' 1.109 + // - UNC share name: '\\share' 1.110 + // - unix root '/' 1.111 + 1.112 + // On the other hand, relative paths are: 1.113 + // - directory: 'directory/file' 1.114 + // - this directory: './file' 1.115 + // - parent directory: '../file' 1.116 + // 1.117 + // For now, we don't parse '.' or '..' out, but instead let it be concatenated 1.118 + // to string and let the OS figure it out. This, however, is not good for file 1.119 + // name matching in library/etc, so it should be improved. 1.120 + 1.121 + if (!url || !*url) 1.122 + return true; // Treat empty strings as absolute. 1.123 + 1.124 + uint32_t charVal = UTF8Util::DecodeNextChar(&url); 1.125 + 1.126 + // Fist character of '/' or '\\' means absolute url. 1.127 + if ((charVal == '/') || (charVal == '\\')) 1.128 + return true; 1.129 + 1.130 + while (charVal != 0) 1.131 + { 1.132 + // Treat a colon followed by a slash as absolute. 1.133 + if (charVal == ':') 1.134 + { 1.135 + charVal = UTF8Util::DecodeNextChar(&url); 1.136 + // Protocol or windows drive. Absolute. 1.137 + if ((charVal == '/') || (charVal == '\\')) 1.138 + return true; 1.139 + } 1.140 + else if ((charVal == '/') || (charVal == '\\')) 1.141 + { 1.142 + // Not a first character (else 'if' above the loop would have caught it). 1.143 + // Must be a relative url. 1.144 + break; 1.145 + } 1.146 + 1.147 + charVal = UTF8Util::DecodeNextChar(&url); 1.148 + } 1.149 + 1.150 + // We get here for relative paths. 1.151 + return false; 1.152 +} 1.153 + 1.154 + 1.155 +bool String::HasExtension(const char* path) 1.156 +{ 1.157 + const char* ext = 0; 1.158 + ScanFilePath(path, 0, &ext); 1.159 + return ext != 0; 1.160 +} 1.161 +bool String::HasProtocol(const char* path) 1.162 +{ 1.163 + return ScanPathProtocol(path) != 0; 1.164 +} 1.165 + 1.166 + 1.167 +String String::GetPath() const 1.168 +{ 1.169 + const char* filename = 0; 1.170 + ScanFilePath(ToCStr(), &filename, 0); 1.171 + 1.172 + // Technically we can have extra logic somewhere for paths, 1.173 + // such as enforcing protocol and '/' only based on flags, 1.174 + // but we keep it simple for now. 1.175 + return String(ToCStr(), filename ? (filename-ToCStr()) : GetSize()); 1.176 +} 1.177 + 1.178 +String String::GetProtocol() const 1.179 +{ 1.180 + const char* protocolEnd = ScanPathProtocol(ToCStr()); 1.181 + return String(ToCStr(), protocolEnd ? (protocolEnd-ToCStr()) : 0); 1.182 +} 1.183 + 1.184 +String String::GetFilename() const 1.185 +{ 1.186 + const char* filename = 0; 1.187 + ScanFilePath(ToCStr(), &filename, 0); 1.188 + return String(filename); 1.189 +} 1.190 +String String::GetExtension() const 1.191 +{ 1.192 + const char* ext = 0; 1.193 + ScanFilePath(ToCStr(), 0, &ext); 1.194 + return String(ext); 1.195 +} 1.196 + 1.197 +void String::StripExtension() 1.198 +{ 1.199 + const char* ext = 0; 1.200 + ScanFilePath(ToCStr(), 0, &ext); 1.201 + if (ext) 1.202 + { 1.203 + *this = String(ToCStr(), ext-ToCStr()); 1.204 + } 1.205 +} 1.206 + 1.207 +void String::StripProtocol() 1.208 +{ 1.209 + const char* protocol = ScanPathProtocol(ToCStr()); 1.210 + if (protocol) 1.211 + AssignString(protocol, OVR_strlen(protocol)); 1.212 +} 1.213 + 1.214 +} // OVR