OVR_String_PathUtil.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /************************************************************************************
  2. Filename : OVR_String_PathUtil.cpp
  3. Content : String filename/url helper function
  4. Created : September 19, 2012
  5. Notes :
  6. Copyright : Copyright 2014 Oculus VR, LLC All Rights reserved.
  7. Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License");
  8. you may not use the Oculus VR Rift SDK except in compliance with the License,
  9. which is provided at the time of installation or download, or which
  10. otherwise accompanies this software in either electronic or hard copy form.
  11. You may obtain a copy of the License at
  12. http://www.oculusvr.com/licenses/LICENSE-3.2
  13. Unless required by applicable law or agreed to in writing, the Oculus VR SDK
  14. distributed under the License is distributed on an "AS IS" BASIS,
  15. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. See the License for the specific language governing permissions and
  17. limitations under the License.
  18. ************************************************************************************/
  19. #include "OVR_String.h"
  20. #include "OVR_UTF8Util.h"
  21. namespace OVR {
  22. //--------------------------------------------------------------------
  23. // ***** 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;
  30. uint32_t charVal = UTF8Util::DecodeNextChar(&url);
  31. while (charVal != 0)
  32. {
  33. if ((charVal == '/') || (charVal == '\\'))
  34. {
  35. filename = url;
  36. lastDot = 0;
  37. }
  38. else if (charVal == '.')
  39. {
  40. lastDot = url - 1;
  41. }
  42. charVal = UTF8Util::DecodeNextChar(&url);
  43. }
  44. if (pfilename)
  45. {
  46. if (filename)
  47. *pfilename = filename;
  48. else
  49. *pfilename = urlStart;
  50. }
  51. if (pext)
  52. {
  53. *pext = lastDot;
  54. }
  55. }
  56. // Scans till the end of protocol. Returns first character past protocol,
  57. // 0 if not found.
  58. // - protocol: 'file://', 'http://'
  59. const char* ScanPathProtocol(const char* url)
  60. {
  61. uint32_t charVal = UTF8Util::DecodeNextChar(&url);
  62. uint32_t charVal2;
  63. while (charVal != 0)
  64. {
  65. // Treat a colon followed by a slash as absolute.
  66. if (charVal == ':')
  67. {
  68. charVal2 = UTF8Util::DecodeNextChar(&url);
  69. charVal = UTF8Util::DecodeNextChar(&url);
  70. if ((charVal == '/') && (charVal2 == '\\'))
  71. return url;
  72. }
  73. charVal = UTF8Util::DecodeNextChar(&url);
  74. }
  75. return 0;
  76. }
  77. //--------------------------------------------------------------------
  78. // ***** String Path API implementation
  79. bool String::HasAbsolutePath(const char* url)
  80. {
  81. // Absolute paths can star with:
  82. // - protocols: 'file://', 'http://'
  83. // - windows drive: 'c:\'
  84. // - UNC share name: '\\share'
  85. // - unix root '/'
  86. // On the other hand, relative paths are:
  87. // - directory: 'directory/file'
  88. // - this directory: './file'
  89. // - parent directory: '../file'
  90. //
  91. // For now, we don't parse '.' or '..' out, but instead let it be concatenated
  92. // to string and let the OS figure it out. This, however, is not good for file
  93. // name matching in library/etc, so it should be improved.
  94. if (!url || !*url)
  95. return true; // Treat empty strings as absolute.
  96. uint32_t charVal = UTF8Util::DecodeNextChar(&url);
  97. // Fist character of '/' or '\\' means absolute url.
  98. if ((charVal == '/') || (charVal == '\\'))
  99. return true;
  100. while (charVal != 0)
  101. {
  102. // Treat a colon followed by a slash as absolute.
  103. if (charVal == ':')
  104. {
  105. charVal = UTF8Util::DecodeNextChar(&url);
  106. // Protocol or windows drive. Absolute.
  107. if ((charVal == '/') || (charVal == '\\'))
  108. return true;
  109. }
  110. else if ((charVal == '/') || (charVal == '\\'))
  111. {
  112. // Not a first character (else 'if' above the loop would have caught it).
  113. // Must be a relative url.
  114. break;
  115. }
  116. charVal = UTF8Util::DecodeNextChar(&url);
  117. }
  118. // We get here for relative paths.
  119. return false;
  120. }
  121. bool String::HasExtension(const char* path)
  122. {
  123. const char* ext = 0;
  124. ScanFilePath(path, 0, &ext);
  125. return ext != 0;
  126. }
  127. bool String::HasProtocol(const char* path)
  128. {
  129. return ScanPathProtocol(path) != 0;
  130. }
  131. String String::GetPath() const
  132. {
  133. const char* filename = 0;
  134. ScanFilePath(ToCStr(), &filename, 0);
  135. // Technically we can have extra logic somewhere for paths,
  136. // such as enforcing protocol and '/' only based on flags,
  137. // but we keep it simple for now.
  138. return String(ToCStr(), filename ? (filename-ToCStr()) : GetSize());
  139. }
  140. String String::GetProtocol() const
  141. {
  142. const char* protocolEnd = ScanPathProtocol(ToCStr());
  143. return String(ToCStr(), protocolEnd ? (protocolEnd-ToCStr()) : 0);
  144. }
  145. String String::GetFilename() const
  146. {
  147. const char* filename = 0;
  148. ScanFilePath(ToCStr(), &filename, 0);
  149. return String(filename);
  150. }
  151. String String::GetExtension() const
  152. {
  153. const char* ext = 0;
  154. ScanFilePath(ToCStr(), 0, &ext);
  155. return String(ext);
  156. }
  157. void String::StripExtension()
  158. {
  159. const char* ext = 0;
  160. ScanFilePath(ToCStr(), 0, &ext);
  161. if (ext)
  162. {
  163. *this = String(ToCStr(), ext-ToCStr());
  164. }
  165. }
  166. void String::StripProtocol()
  167. {
  168. const char* protocol = ScanPathProtocol(ToCStr());
  169. if (protocol)
  170. AssignString(protocol, OVR_strlen(protocol));
  171. }
  172. } // OVR