oculus1

diff libovr/Src/Kernel/OVR_String_PathUtil.cpp @ 3:b069a5c27388

added a couple more stuff, fixed all the LibOVR line endings
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 15 Sep 2013 04:10:05 +0300
parents e2f9e4603129
children
line diff
     1.1 --- a/libovr/Src/Kernel/OVR_String_PathUtil.cpp	Sat Sep 14 17:51:03 2013 +0300
     1.2 +++ b/libovr/Src/Kernel/OVR_String_PathUtil.cpp	Sun Sep 15 04:10:05 2013 +0300
     1.3 @@ -1,1 +1,200 @@
     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 2012 Oculus VR, Inc. All Rights reserved.
    1.12 
    1.13 Use of this software is subject to the terms of the Oculus license
    1.14 agreement provided at the time of installation or download, or which
    1.15 otherwise accompanies this software in either electronic or hard copy form.
    1.16 
    1.17 ************************************************************************************/
    1.18 
    1.19 #include "OVR_String.h"
    1.20 #include "OVR_UTF8Util.h"
    1.21 
    1.22 namespace OVR {
    1.23 
    1.24 //--------------------------------------------------------------------
    1.25 // ***** Path-Scanner helper function 
    1.26 
    1.27 // Scans file path finding filename start and extension start, fills in their addess.
    1.28 void ScanFilePath(const char* url, const char** pfilename, const char** pext)
    1.29 { 
    1.30     const char* urlStart = url;
    1.31     const char *filename = 0;
    1.32     const char *lastDot = 0;
    1.33 
    1.34     UInt32 charVal = UTF8Util::DecodeNextChar(&url);
    1.35 
    1.36     while (charVal != 0)
    1.37     {
    1.38         if ((charVal == '/') || (charVal == '\\'))
    1.39         {
    1.40             filename = url;
    1.41             lastDot  = 0;
    1.42         }
    1.43         else if (charVal == '.')
    1.44         {
    1.45             lastDot = url - 1;
    1.46         }
    1.47         
    1.48         charVal = UTF8Util::DecodeNextChar(&url);
    1.49     }
    1.50 
    1.51     if (pfilename)
    1.52     {
    1.53         // It was a naked filename
    1.54         if (urlStart && (*urlStart != '.') && *urlStart)
    1.55             *pfilename = urlStart;
    1.56         else
    1.57             *pfilename = filename;
    1.58     }
    1.59 
    1.60     if (pext)
    1.61     {
    1.62         *pext = lastDot;
    1.63     }
    1.64 }
    1.65 
    1.66 // Scans till the end of protocol. Returns first character past protocol,
    1.67 // 0 if not found.
    1.68 //  - protocol: 'file://', 'http://'
    1.69 const char* ScanPathProtocol(const char* url)
    1.70 {    
    1.71     UInt32 charVal = UTF8Util::DecodeNextChar(&url);
    1.72     UInt32 charVal2;
    1.73    
    1.74     while (charVal != 0)
    1.75     {
    1.76         // Treat a colon followed by a slash as absolute.
    1.77         if (charVal == ':')
    1.78         {
    1.79             charVal2 = UTF8Util::DecodeNextChar(&url);
    1.80             charVal  = UTF8Util::DecodeNextChar(&url);
    1.81             if ((charVal == '/') && (charVal2 == '\\'))
    1.82                 return url;
    1.83         }
    1.84         charVal = UTF8Util::DecodeNextChar(&url);
    1.85     }
    1.86     return 0;
    1.87 }
    1.88 
    1.89 
    1.90 //--------------------------------------------------------------------
    1.91 // ***** String Path API implementation
    1.92 
    1.93 bool String::HasAbsolutePath(const char* url)
    1.94 {
    1.95     // Absolute paths can star with:
    1.96     //  - protocols:        'file://', 'http://'
    1.97     //  - windows drive:    'c:\'
    1.98     //  - UNC share name:   '\\share'
    1.99     //  - unix root         '/'
   1.100 
   1.101     // On the other hand, relative paths are:
   1.102     //  - directory:        'directory/file'
   1.103     //  - this directory:   './file'
   1.104     //  - parent directory: '../file'
   1.105     // 
   1.106     // For now, we don't parse '.' or '..' out, but instead let it be concatenated
   1.107     // to string and let the OS figure it out. This, however, is not good for file
   1.108     // name matching in library/etc, so it should be improved.
   1.109 
   1.110     if (!url || !*url)
   1.111         return true; // Treat empty strings as absolute.    
   1.112 
   1.113     UInt32 charVal = UTF8Util::DecodeNextChar(&url);
   1.114 
   1.115     // Fist character of '/' or '\\' means absolute url.
   1.116     if ((charVal == '/') || (charVal == '\\'))
   1.117         return true;
   1.118 
   1.119     while (charVal != 0)
   1.120     {
   1.121         // Treat a colon followed by a slash as absolute.
   1.122         if (charVal == ':')
   1.123         {
   1.124             charVal = UTF8Util::DecodeNextChar(&url);
   1.125             // Protocol or windows drive. Absolute.
   1.126             if ((charVal == '/') || (charVal == '\\'))
   1.127                 return true;
   1.128         }
   1.129         else if ((charVal == '/') || (charVal == '\\'))
   1.130         {
   1.131             // Not a first character (else 'if' above the loop would have caught it).
   1.132             // Must be a relative url.
   1.133             break;
   1.134         }
   1.135 
   1.136         charVal = UTF8Util::DecodeNextChar(&url);
   1.137     }
   1.138 
   1.139     // We get here for relative paths.
   1.140     return false;    
   1.141 }
   1.142 
   1.143 
   1.144 bool String::HasExtension(const char* path)
   1.145 {
   1.146     const char* ext = 0;
   1.147     ScanFilePath(path, 0, &ext);
   1.148     return ext != 0;
   1.149 }
   1.150 bool String::HasProtocol(const char* path)
   1.151 {
   1.152     return ScanPathProtocol(path) != 0;
   1.153 }
   1.154 
   1.155 
   1.156 String  String::GetPath() const
   1.157 {
   1.158     const char* filename = 0;
   1.159     ScanFilePath(ToCStr(), &filename, 0);
   1.160 
   1.161     // Technically we can have extra logic somewhere for paths,
   1.162     // such as enforcing protocol and '/' only based on flags,
   1.163     // but we keep it simple for now.
   1.164     return String(ToCStr(), filename ? (filename-ToCStr()) : GetSize());
   1.165 }
   1.166 
   1.167 String  String::GetProtocol() const
   1.168 {
   1.169     const char* protocolEnd = ScanPathProtocol(ToCStr());
   1.170     return String(ToCStr(), protocolEnd ? (protocolEnd-ToCStr()) : 0);
   1.171 }
   1.172 
   1.173 String  String::GetFilename() const
   1.174 {
   1.175     const char* filename = 0;
   1.176     ScanFilePath(ToCStr(), &filename, 0);
   1.177     return String(filename);
   1.178 }
   1.179 String  String::GetExtension() const
   1.180 {
   1.181     const char* ext = 0;
   1.182     ScanFilePath(ToCStr(), 0, &ext);
   1.183     return String(ext);
   1.184 }
   1.185 
   1.186 void    String::StripExtension()
   1.187 {
   1.188     const char* ext = 0;
   1.189     ScanFilePath(ToCStr(), 0, &ext);    
   1.190     if (ext)
   1.191     {
   1.192         *this = String(ToCStr(), ext-ToCStr());
   1.193     }
   1.194 }
   1.195 
   1.196 void    String::StripProtocol()
   1.197 {
   1.198     const char* protocol = ScanPathProtocol(ToCStr());
   1.199     if (protocol)
   1.200         AssignString(protocol, OVR_strlen(protocol));
   1.201 }
   1.202 
   1.203 } // OVR
   1.204 \ No newline at end of file
   1.205 +/************************************************************************************
   1.206 +
   1.207 +Filename    :   OVR_String_PathUtil.cpp
   1.208 +Content     :   String filename/url helper function
   1.209 +Created     :   September 19, 2012
   1.210 +Notes       : 
   1.211 +
   1.212 +Copyright   :   Copyright 2012 Oculus VR, Inc. All Rights reserved.
   1.213 +
   1.214 +Use of this software is subject to the terms of the Oculus license
   1.215 +agreement provided at the time of installation or download, or which
   1.216 +otherwise accompanies this software in either electronic or hard copy form.
   1.217 +
   1.218 +************************************************************************************/
   1.219 +
   1.220 +#include "OVR_String.h"
   1.221 +#include "OVR_UTF8Util.h"
   1.222 +
   1.223 +namespace OVR {
   1.224 +
   1.225 +//--------------------------------------------------------------------
   1.226 +// ***** Path-Scanner helper function 
   1.227 +
   1.228 +// Scans file path finding filename start and extension start, fills in their addess.
   1.229 +void ScanFilePath(const char* url, const char** pfilename, const char** pext)
   1.230 +{ 
   1.231 +    const char* urlStart = url;
   1.232 +    const char *filename = 0;
   1.233 +    const char *lastDot = 0;
   1.234 +
   1.235 +    UInt32 charVal = UTF8Util::DecodeNextChar(&url);
   1.236 +
   1.237 +    while (charVal != 0)
   1.238 +    {
   1.239 +        if ((charVal == '/') || (charVal == '\\'))
   1.240 +        {
   1.241 +            filename = url;
   1.242 +            lastDot  = 0;
   1.243 +        }
   1.244 +        else if (charVal == '.')
   1.245 +        {
   1.246 +            lastDot = url - 1;
   1.247 +        }
   1.248 +        
   1.249 +        charVal = UTF8Util::DecodeNextChar(&url);
   1.250 +    }
   1.251 +
   1.252 +    if (pfilename)
   1.253 +    {
   1.254 +        // It was a naked filename
   1.255 +        if (urlStart && (*urlStart != '.') && *urlStart)
   1.256 +            *pfilename = urlStart;
   1.257 +        else
   1.258 +            *pfilename = filename;
   1.259 +    }
   1.260 +
   1.261 +    if (pext)
   1.262 +    {
   1.263 +        *pext = lastDot;
   1.264 +    }
   1.265 +}
   1.266 +
   1.267 +// Scans till the end of protocol. Returns first character past protocol,
   1.268 +// 0 if not found.
   1.269 +//  - protocol: 'file://', 'http://'
   1.270 +const char* ScanPathProtocol(const char* url)
   1.271 +{    
   1.272 +    UInt32 charVal = UTF8Util::DecodeNextChar(&url);
   1.273 +    UInt32 charVal2;
   1.274 +   
   1.275 +    while (charVal != 0)
   1.276 +    {
   1.277 +        // Treat a colon followed by a slash as absolute.
   1.278 +        if (charVal == ':')
   1.279 +        {
   1.280 +            charVal2 = UTF8Util::DecodeNextChar(&url);
   1.281 +            charVal  = UTF8Util::DecodeNextChar(&url);
   1.282 +            if ((charVal == '/') && (charVal2 == '\\'))
   1.283 +                return url;
   1.284 +        }
   1.285 +        charVal = UTF8Util::DecodeNextChar(&url);
   1.286 +    }
   1.287 +    return 0;
   1.288 +}
   1.289 +
   1.290 +
   1.291 +//--------------------------------------------------------------------
   1.292 +// ***** String Path API implementation
   1.293 +
   1.294 +bool String::HasAbsolutePath(const char* url)
   1.295 +{
   1.296 +    // Absolute paths can star with:
   1.297 +    //  - protocols:        'file://', 'http://'
   1.298 +    //  - windows drive:    'c:\'
   1.299 +    //  - UNC share name:   '\\share'
   1.300 +    //  - unix root         '/'
   1.301 +
   1.302 +    // On the other hand, relative paths are:
   1.303 +    //  - directory:        'directory/file'
   1.304 +    //  - this directory:   './file'
   1.305 +    //  - parent directory: '../file'
   1.306 +    // 
   1.307 +    // For now, we don't parse '.' or '..' out, but instead let it be concatenated
   1.308 +    // to string and let the OS figure it out. This, however, is not good for file
   1.309 +    // name matching in library/etc, so it should be improved.
   1.310 +
   1.311 +    if (!url || !*url)
   1.312 +        return true; // Treat empty strings as absolute.    
   1.313 +
   1.314 +    UInt32 charVal = UTF8Util::DecodeNextChar(&url);
   1.315 +
   1.316 +    // Fist character of '/' or '\\' means absolute url.
   1.317 +    if ((charVal == '/') || (charVal == '\\'))
   1.318 +        return true;
   1.319 +
   1.320 +    while (charVal != 0)
   1.321 +    {
   1.322 +        // Treat a colon followed by a slash as absolute.
   1.323 +        if (charVal == ':')
   1.324 +        {
   1.325 +            charVal = UTF8Util::DecodeNextChar(&url);
   1.326 +            // Protocol or windows drive. Absolute.
   1.327 +            if ((charVal == '/') || (charVal == '\\'))
   1.328 +                return true;
   1.329 +        }
   1.330 +        else if ((charVal == '/') || (charVal == '\\'))
   1.331 +        {
   1.332 +            // Not a first character (else 'if' above the loop would have caught it).
   1.333 +            // Must be a relative url.
   1.334 +            break;
   1.335 +        }
   1.336 +
   1.337 +        charVal = UTF8Util::DecodeNextChar(&url);
   1.338 +    }
   1.339 +
   1.340 +    // We get here for relative paths.
   1.341 +    return false;    
   1.342 +}
   1.343 +
   1.344 +
   1.345 +bool String::HasExtension(const char* path)
   1.346 +{
   1.347 +    const char* ext = 0;
   1.348 +    ScanFilePath(path, 0, &ext);
   1.349 +    return ext != 0;
   1.350 +}
   1.351 +bool String::HasProtocol(const char* path)
   1.352 +{
   1.353 +    return ScanPathProtocol(path) != 0;
   1.354 +}
   1.355 +
   1.356 +
   1.357 +String  String::GetPath() const
   1.358 +{
   1.359 +    const char* filename = 0;
   1.360 +    ScanFilePath(ToCStr(), &filename, 0);
   1.361 +
   1.362 +    // Technically we can have extra logic somewhere for paths,
   1.363 +    // such as enforcing protocol and '/' only based on flags,
   1.364 +    // but we keep it simple for now.
   1.365 +    return String(ToCStr(), filename ? (filename-ToCStr()) : GetSize());
   1.366 +}
   1.367 +
   1.368 +String  String::GetProtocol() const
   1.369 +{
   1.370 +    const char* protocolEnd = ScanPathProtocol(ToCStr());
   1.371 +    return String(ToCStr(), protocolEnd ? (protocolEnd-ToCStr()) : 0);
   1.372 +}
   1.373 +
   1.374 +String  String::GetFilename() const
   1.375 +{
   1.376 +    const char* filename = 0;
   1.377 +    ScanFilePath(ToCStr(), &filename, 0);
   1.378 +    return String(filename);
   1.379 +}
   1.380 +String  String::GetExtension() const
   1.381 +{
   1.382 +    const char* ext = 0;
   1.383 +    ScanFilePath(ToCStr(), 0, &ext);
   1.384 +    return String(ext);
   1.385 +}
   1.386 +
   1.387 +void    String::StripExtension()
   1.388 +{
   1.389 +    const char* ext = 0;
   1.390 +    ScanFilePath(ToCStr(), 0, &ext);    
   1.391 +    if (ext)
   1.392 +    {
   1.393 +        *this = String(ToCStr(), ext-ToCStr());
   1.394 +    }
   1.395 +}
   1.396 +
   1.397 +void    String::StripProtocol()
   1.398 +{
   1.399 +    const char* protocol = ScanPathProtocol(ToCStr());
   1.400 +    if (protocol)
   1.401 +        AssignString(protocol, OVR_strlen(protocol));
   1.402 +}
   1.403 +
   1.404 +} // OVR