oculus1

diff 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 diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/libovr/Src/Kernel/OVR_String_PathUtil.cpp	Sat Sep 14 16:14:59 2013 +0300
     1.3 @@ -0,0 +1,1 @@
     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