PixelLightAPI
.
|
00001 /*********************************************************\ 00002 * File: Url.h * 00003 * 00004 * Copyright (C) 2002-2011 The PixelLight Team (http://www.pixellight.org/) 00005 * 00006 * This file is part of PixelLight. 00007 * 00008 * PixelLight is free software: you can redistribute it and/or modify 00009 * it under the terms of the GNU Lesser General Public License as published by 00010 * the Free Software Foundation, either version 3 of the License, or 00011 * (at your option) any later version. 00012 * 00013 * PixelLight is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 * GNU Lesser General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU Lesser General Public License 00019 * along with PixelLight. If not, see <http://www.gnu.org/licenses/>. 00020 \*********************************************************/ 00021 00022 00023 #ifndef __PLCORE_URL_H__ 00024 #define __PLCORE_URL_H__ 00025 #pragma once 00026 00027 00028 //[-------------------------------------------------------] 00029 //[ Includes ] 00030 //[-------------------------------------------------------] 00031 #include "PLCore/String/String.h" 00032 00033 00034 //[-------------------------------------------------------] 00035 //[ Namespace ] 00036 //[-------------------------------------------------------] 00037 namespace PLCore { 00038 00039 00040 //[-------------------------------------------------------] 00041 //[ Classes ] 00042 //[-------------------------------------------------------] 00043 /** 00044 * @brief 00045 * Contains a path or URL (Uniform Resource Locator) 00046 * 00047 * @note 00048 * - An URL is always stored using a protocol ("file://" for a local path) and "/" as the delimiter. 00049 * - For getting the URL in native style, use the specific functions (GetWindowsPath(), GetUnixPath(), GetNativePath()) 00050 * - If there's a "/" at the end of a given path it will be kept, but no "/" is added automatically 00051 */ 00052 class Url { 00053 00054 00055 //[-------------------------------------------------------] 00056 //[ Public functions ] 00057 //[-------------------------------------------------------] 00058 public: 00059 /** 00060 * @brief 00061 * Constructor 00062 */ 00063 inline Url(); 00064 00065 /** 00066 * @brief 00067 * Copy constructor 00068 * 00069 * @param[in] cUrl 00070 * URL to copy 00071 */ 00072 inline Url(const Url &cUrl); 00073 00074 /** 00075 * @brief 00076 * Constructor 00077 * 00078 * @param[in] sUrl 00079 * URL as string 00080 */ 00081 inline Url(const String &sUrl); 00082 00083 /** 00084 * @brief 00085 * Destructor 00086 */ 00087 inline ~Url(); 00088 00089 /** 00090 * @brief 00091 * Copy an URL 00092 * 00093 * @param[in] cUrl 00094 * URL to copy 00095 * 00096 * @return 00097 * Reference to this URL 00098 */ 00099 inline Url &operator =(const Url &cUrl); 00100 00101 /** 00102 * @brief 00103 * Copy an URL (passed as a string) 00104 * 00105 * @param[in] sUrl 00106 * String containing URL to copy 00107 * 00108 * @return 00109 * Reference to this URL 00110 */ 00111 inline Url &operator =(const String &sUrl); 00112 00113 /** 00114 * @brief 00115 * Concatenate an URL 00116 * 00117 * @param[in] cUrl 00118 * URL to add 00119 * 00120 * @return 00121 * Resulting URL 00122 * 00123 * @note 00124 * - Both URLs must be valid 00125 * - The second URL must be relative 00126 * - The protocol of the second URL must match the first one or be empty 00127 */ 00128 inline Url operator +(const Url &cUrl) const; 00129 00130 /** 00131 * @brief 00132 * Concatenate an URL 00133 * 00134 * @param[in] cUrl 00135 * URL to add 00136 * 00137 * @return 00138 * Reference to this URL 00139 * 00140 * @see 00141 * - operator + 00142 */ 00143 inline Url &operator +=(const Url &cUrl); 00144 00145 /** 00146 * @brief 00147 * Get value in URL notation 00148 * 00149 * @return 00150 * Path in URL style 00151 * 00152 * @note 00153 * - If the value contains a local file path, a file:// URL is returned 00154 * - Any other URL (e.g. http://) is returned directly 00155 * - For example "Url("C:\\Programs\\App\\App.exe")" will result in "file://C:/Programs/App/App.exe" 00156 * - For example "Url("C:\\Programs\\App\\")" will result in "file://C:/Programs/App/" 00157 * - For example "Url("C:\\Programs\\App")" will result in "file://C:/Programs/App" 00158 */ 00159 inline String GetUrl() const; 00160 00161 /** 00162 * @brief 00163 * Get value in native notation 00164 * 00165 * @return 00166 * Path in native path format style 00167 * 00168 * @note 00169 * - Returns the URL in native (Windows/Unix) notation, if it is a local path 00170 * - If it is another URL (e.g. http://), the URL is returned 00171 * - On Windows for example "Url("C:\\Programs\\App\\App.exe")" will result in "C:\Programs\App\App.exe" 00172 * - On Windows for example "Url("C:\\Programs\\App\\")" will result in "C:\Programs\App\" 00173 * - On Windows for example "Url("C:\\Programs\\App")" will result in "C:\Programs\App" 00174 * - On Linux for example "Url("C:\\Programs\\App\\App.exe")" will result in "C:/Programs/App/App.exe" 00175 * - On Linux for example "Url("C:\\Programs\\App\\")" will result in "C:/Programs/App/" 00176 * - On Linux for example "Url("C:\\Programs\\App")" will result in "C:/Programs/App" 00177 */ 00178 inline String GetNativePath() const; 00179 00180 /** 00181 * @brief 00182 * Get value in Windows notation 00183 * 00184 * @return 00185 * Path in Windows format style 00186 * 00187 * @note 00188 * - If the value contains a valid Windows or Unix file path or an file:// URL, it is returned in Windows style 00189 * - If the value contains another URL (e.g. http://), the URL is returned without being converted 00190 * - For example "Url("C:\\Programs\\App\\App.exe")" will result in "C:\Programs\App\App.exe" 00191 * - For example "Url("C:\\Programs\\App\\")" will result in "C:\Programs\App\" 00192 * - For example "Url("C:\\Programs\\App")" will result in "C:\Programs\App" 00193 */ 00194 inline String GetWindowsPath() const; 00195 00196 /** 00197 * @brief 00198 * Get value in Unix notation 00199 * 00200 * @return 00201 * Path in Unix format style 00202 * 00203 * @note 00204 * - If the value contains a valid Windows or Unix file path or an file:// URL, it is returned in Unix style 00205 * - If the value contains another URL (e.g. http://), the URL is returned without being converted 00206 * - For example "Url("C:\\Programs\\App\\App.exe")" will result in "C:/Programs/App/App.exe" 00207 * - For example "Url("C:\\Programs\\App\\")" will result in "C:/Programs/App/" 00208 * - For example "Url("C:\\Programs\\App")" will result in "C:/Programs/App" 00209 */ 00210 inline String GetUnixPath() const; 00211 00212 /** 00213 * @brief 00214 * Returns if the URL is valid 00215 * 00216 * @return 00217 * 'true' if the value is valid, else 'false' 00218 */ 00219 inline bool IsValid() const; 00220 00221 /** 00222 * @brief 00223 * Check if the URL is a valid native file path 00224 * 00225 * @return 00226 * 'true', if the path is valid in the native path format style 00227 * 00228 * @note 00229 * - A wrapper for IsValidWindowsPath()/IsValidUnixPath() based on the used system 00230 */ 00231 inline bool IsValidNativePath() const; 00232 00233 /** 00234 * @brief 00235 * Check if the URL is valid as a Windows file path 00236 * 00237 * @return 00238 * 'true', if the path is valid in Windows format style 00239 * 00240 * @note 00241 * - 'false', if the protocol is any other than "file://" or empty 00242 * - 'false', if the file root is any other than "X:/" or empty ("/" is invalid!) 00243 */ 00244 inline bool IsValidWindowsPath() const; 00245 00246 /** 00247 * @brief 00248 * Check if the URL is valid as a Unix file path 00249 * 00250 * @return 00251 * 'true', if the path is valid in Unix format style 00252 * 00253 * @note 00254 * - 'false', if the protocol is any other than "file://" or empty 00255 * - 'false', if the file root is any other than "/" or empty ("X:/" is invalid!) 00256 */ 00257 inline bool IsValidUnixPath() const; 00258 00259 /** 00260 * @brief 00261 * Returns if the URL is empty 00262 * 00263 * @return 00264 * 'true' if the path is empty, else 'false' 00265 * 00266 * @note 00267 * - The protocol is ignored 00268 */ 00269 inline bool IsEmpty() const; 00270 00271 /** 00272 * @brief 00273 * Returns if the URL is absolute 00274 * 00275 * @return 00276 * 'true' if the path is absolute, else 'false' 00277 */ 00278 inline bool IsAbsolute() const; 00279 00280 /** 00281 * @brief 00282 * Returns if the URL is relative 00283 * 00284 * @return 00285 * 'true' if the path is relative, else 'false' 00286 */ 00287 inline bool IsRelative() const; 00288 00289 /** 00290 * @brief 00291 * Returns if the URL targets a directory 00292 * 00293 * @return 00294 * 'true' if the path targets a directory, else 'false' 00295 * 00296 * @note 00297 * - Returns true, if the URL ends with "/" which indicates a directory 00298 * - This is a consideration on pure syntax level - it does not mean that the path pointed 00299 * to is really a directory or that it even exists! 00300 */ 00301 inline bool IsDirectory() const; 00302 00303 /** 00304 * @brief 00305 * Returns the protocol part 00306 * 00307 * @return 00308 * Protocol part (e.g. "file://" or "http://") 00309 */ 00310 inline String GetProtocol() const; 00311 00312 /** 00313 * @brief 00314 * Returns the root part 00315 * 00316 * @return 00317 * Root part (e.g. "C:/" or "/") 00318 */ 00319 inline String GetRoot() const; 00320 00321 /** 00322 * @brief 00323 * Returns the path (without root and filename) 00324 * 00325 * @return 00326 * Path (e.g. "Programs/") 00327 */ 00328 inline String GetPath() const; 00329 00330 /** 00331 * @brief 00332 * Returns the filename (including it's extension) 00333 * 00334 * @return 00335 * Filename (e.g. "readme.txt") 00336 */ 00337 inline String GetFilename() const; 00338 00339 /** 00340 * @brief 00341 * Returns the path without the filename 00342 * 00343 * @return 00344 * Path without filename (e.g. "C:/Programs/App/App.exe"->"C:/Programs/App/") 00345 * 00346 * @note 00347 * - The protocol is omitted for a local path (e.g. the result will not be "file://C:/Programs/App/") 00348 */ 00349 inline String CutFilename() const; 00350 00351 /** 00352 * @brief 00353 * Returns the filename without it's complete extension (one may also call the result "basename) 00354 * 00355 * @remarks 00356 * @verbatim 00357 * Example showing the difference between "GetTitle()", "GetCompleteTitle()", "GetExtension()", "CutExtension()", "GetCompleteExtension()" and "CutCompleteExtension()": 00358 * Url cUrl("C:\\Programs\\App\\archive.tar.gz"); 00359 * String sTitle = cUrl.GetTitle(); // Result is "archive" 00360 * String sCompleteTitle = cUrl.GetCompleteTitle(); // Result is "archive.tar" 00361 * String sExtension = cUrl.GetExtension(); // Result is "gz" 00362 * String sCutExtension = cUrl.CutExtension(); // Result is "C:\Programs\App\archive.tar" 00363 * String sCompleteExtension = cUrl.GetCompleteExtension(); // Result is "tar.gz" 00364 * String sCutCompleteExtension = cUrl.CutCompleteExtension(); // Result is "C:\Programs\App\archive" 00365 * @endverbatim 00366 * 00367 * @return 00368 * Filename without complete extension (e.g. "readme" if the filename was "readme.txt", "archive" if the filename was "archive.tar.gz") 00369 */ 00370 inline String GetTitle() const; 00371 00372 /** 00373 * @brief 00374 * Returns the filename without it's extension (one may also call the result "basename) 00375 * 00376 * @return 00377 * Filename without extension (e.g. "readme" if the filename was "readme.txt", "archive.tar" if the filename was "archive.tar.gz") 00378 * 00379 * @see 00380 * - "GetTitle()" for an usage example 00381 */ 00382 inline String GetCompleteTitle() const; 00383 00384 /** 00385 * @brief 00386 * Returns the file extension (aka "suffix") 00387 * 00388 * @return 00389 * File extension (e.g. "txt" if the filename was "readme.txt", "gz" if the filename was "archive.tar.gz") 00390 * 00391 * @see 00392 * - "GetTitle()" for an usage example 00393 */ 00394 inline String GetExtension() const; 00395 00396 /** 00397 * @brief 00398 * Returns the path and filename without extension 00399 * 00400 * @return 00401 * Path and filename without extension (e.g. "C:\Programs\App\App.exe"->"C:\Programs\App\App" or "C:\Programs\App\archive.tar.gz"->"C:\Programs\App\archive.tar") 00402 * 00403 * @see 00404 * - "GetTitle()" for an usage example 00405 */ 00406 inline String CutExtension() const; 00407 00408 /** 00409 * @brief 00410 * Returns the complete file extension (aka "suffix") 00411 * 00412 * @return 00413 * File extension (e.g. "txt" if the filename was "readme.txt", "tar.gz" if the filename was "archive.tar.gz") 00414 * 00415 * @see 00416 * - "GetTitle()" for an usage example 00417 */ 00418 inline String GetCompleteExtension() const; 00419 00420 /** 00421 * @brief 00422 * Returns the path and filename without complete extension 00423 * 00424 * @return 00425 * Path and filename without complete extension (e.g. "C:\Programs\App\App.exe"->"C:\Programs\App\App" or "C:\Programs\App\archive.tar.gz"->"C:\Programs\App\archive") 00426 * 00427 * @see 00428 * - "GetTitle()" for an usage example 00429 */ 00430 inline String CutCompleteExtension() const; 00431 00432 /** 00433 * @brief 00434 * Returns the first part of the path 00435 * 00436 * @param[out] nPathPos 00437 * Will receive the new position within the path 00438 * 00439 * @return 00440 * First path 00441 * 00442 * @note 00443 * - Ignores the root (e.g. "C:/" or "/") 00444 * - Example: "test/foo.bar"->"test" 00445 */ 00446 inline String GetFirstPath(uint32 &nPathPos) const; 00447 00448 /** 00449 * @brief 00450 * Returns the first part of the path 00451 * 00452 * @return 00453 * First path 00454 * 00455 * @note 00456 * - Ignores the root (e.g. "C:/" or "/") 00457 * - Example: "test/foo.bar"->"test" 00458 */ 00459 inline String GetFirstPath() const; 00460 00461 /** 00462 * @brief 00463 * Returns the next part of the path 00464 * 00465 * @param[in, out] nPathPos 00466 * Position within the path 00467 * 00468 * @return 00469 * First path 00470 * 00471 * @note 00472 * - Call GetFirstPath() first 00473 */ 00474 PLCORE_API String GetNextPath(uint32 &nPathPos) const; 00475 00476 /** 00477 * @brief 00478 * Collapses the URL 00479 * 00480 * @return 00481 * Reference to this object 00482 * 00483 * @note 00484 * - Collapsing means, '..' and '.' inside the path will be resolved (on a syntactical level only!) 00485 * - If the URL is invalid, the function will fail 00486 */ 00487 PLCORE_API Url &Collapse(); 00488 00489 00490 //[-------------------------------------------------------] 00491 //[ Private functions ] 00492 //[-------------------------------------------------------] 00493 private: 00494 /** 00495 * @brief 00496 * Sets the value of the URL 00497 * 00498 * @param[in] sUrl 00499 * New URL value 00500 */ 00501 PLCORE_API void SetValue(const String &sUrl); 00502 00503 /** 00504 * @brief 00505 * Checks if the character is a letter (a..z, A..Z) 00506 * 00507 * @param[in] nChar 00508 * Character to check 00509 * 00510 * @return 00511 * 'true', if the character is a letter, else 'false' 00512 */ 00513 inline bool IsLetter(char nChar) const; 00514 00515 /** 00516 * @brief 00517 * Checks if the string is a valid name (does not contain ':', '/' or '\') 00518 * 00519 * @param[in] sString 00520 * String to check 00521 * 00522 * @return 00523 * 'true', if the string is a valid name, else 'false' 00524 */ 00525 inline bool IsName(const String &sString) const; 00526 00527 00528 //[-------------------------------------------------------] 00529 //[ Private data ] 00530 //[-------------------------------------------------------] 00531 private: 00532 String m_sUrl; /**< Path or URL */ 00533 String m_sProtocol; /**< Protocol part (e.g. "http://") */ 00534 String m_sRoot; /**< Root part (e.g. "/" or "C:\") */ 00535 String m_sPath; /**< Path (e.g. "Programs\") */ 00536 String m_sFilename; /**< Filename (e.g. "readme.txt") */ 00537 bool m_bValid; /**< Is the URL valid? */ 00538 00539 00540 }; 00541 00542 00543 //[-------------------------------------------------------] 00544 //[ Namespace ] 00545 //[-------------------------------------------------------] 00546 } // PLCore 00547 00548 00549 //[-------------------------------------------------------] 00550 //[ Implementation ] 00551 //[-------------------------------------------------------] 00552 #include "PLCore/File/Url.inl" 00553 00554 00555 #endif // __PLCORE_URL_H__
|