/indra/newview/llurl.cpp

https://bitbucket.org/lindenlab/viewer-beta/ · C++ · 290 lines · 206 code · 42 blank · 42 comment · 33 complexity · a4843be6476387115d4d836a53903f7d MD5 · raw file

  1. /**
  2. * @file llurl.cpp
  3. * @brief Text url class
  4. *
  5. * $LicenseInfo:firstyear=2001&license=viewerlgpl$
  6. * Second Life Viewer Source Code
  7. * Copyright (C) 2010, Linden Research, Inc.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation;
  12. * version 2.1 of the License only.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  24. * $/LicenseInfo$
  25. */
  26. #include "llviewerprecompiledheaders.h"
  27. #include "llurl.h"
  28. #include "llerror.h"
  29. LLURL::LLURL()
  30. {
  31. init("");
  32. }
  33. LLURL::LLURL(const LLURL &url)
  34. {
  35. if (this != &url)
  36. {
  37. init(url.getFQURL());
  38. }
  39. else
  40. {
  41. init("");
  42. }
  43. }
  44. LLURL::LLURL(const char * url)
  45. {
  46. init(url);
  47. }
  48. LLURL::~LLURL()
  49. {
  50. cleanup();
  51. }
  52. void LLURL::init(const char * url)
  53. {
  54. mURI[0] = '\0';
  55. mAuthority[0] = '\0';
  56. mPath[0] = '\0';
  57. mFilename[0] = '\0';
  58. mExtension[0] = '\0';
  59. mTag[0] = '\0';
  60. char url_copy[MAX_STRING]; /* Flawfinder: ignore */
  61. strncpy (url_copy,url, MAX_STRING -1); /* Flawfinder: ignore */
  62. url_copy[MAX_STRING -1] = '\0';
  63. char *parse;
  64. char *leftover_url = url_copy;
  65. S32 span = 0;
  66. // copy and lop off tag
  67. if ((parse = strchr(url_copy,'#')))
  68. {
  69. strncpy(mTag,parse+1, LL_MAX_PATH -1); /* Flawfinder: ignore */
  70. mTag[LL_MAX_PATH -1] = '\0';
  71. *parse = '\0';
  72. }
  73. // copy out URI if it exists, update leftover_url
  74. if ((parse = strchr(url_copy,':')))
  75. {
  76. *parse = '\0';
  77. strncpy(mURI,leftover_url, LL_MAX_PATH -1); /* Flawfinder: ignore */
  78. mURI[LL_MAX_PATH -1] = '\0';
  79. leftover_url = parse + 1;
  80. }
  81. // copy out authority if it exists, update leftover_url
  82. if ((leftover_url[0] == '/') && (leftover_url[1] == '/'))
  83. {
  84. leftover_url += 2; // skip the "//"
  85. span = strcspn(leftover_url, "/");
  86. strncat(mAuthority,leftover_url,span); /* Flawfinder: ignore */
  87. leftover_url += span;
  88. }
  89. if ((parse = strrchr(leftover_url,'.')))
  90. {
  91. // copy and lop off extension
  92. strncpy(mExtension,parse+1, LL_MAX_PATH -1); /* Flawfinder: ignore */
  93. mExtension[LL_MAX_PATH -1] = '\0';
  94. *parse = '\0';
  95. }
  96. if ((parse = strrchr(leftover_url,'/')))
  97. {
  98. parse++;
  99. }
  100. else
  101. {
  102. parse = leftover_url;
  103. }
  104. // copy and lop off filename
  105. strncpy(mFilename,parse, LL_MAX_PATH -1);/* Flawfinder: ignore */
  106. mFilename[LL_MAX_PATH -1] = '\0';
  107. *parse = '\0';
  108. // what's left should be the path
  109. strncpy(mPath,leftover_url, LL_MAX_PATH -1); /* Flawfinder: ignore */
  110. mPath[LL_MAX_PATH -1] = '\0';
  111. // llinfos << url << " decomposed into: " << llendl;
  112. // llinfos << " URI : <" << mURI << ">" << llendl;
  113. // llinfos << " Auth: <" << mAuthority << ">" << llendl;
  114. // llinfos << " Path: <" << mPath << ">" << llendl;
  115. // llinfos << " File: <" << mFilename << ">" << llendl;
  116. // llinfos << " Ext : <" << mExtension << ">" << llendl;
  117. // llinfos << " Tag : <" << mTag << ">" << llendl;
  118. }
  119. void LLURL::cleanup()
  120. {
  121. }
  122. // Copy assignment
  123. LLURL &LLURL::operator=(const LLURL &rhs)
  124. {
  125. if (this != &rhs)
  126. {
  127. this->init(rhs.getFQURL());
  128. }
  129. return *this;
  130. }
  131. // Compare
  132. bool LLURL::operator==(const LLURL &rhs) const
  133. {
  134. if ((strcmp(mURI, rhs.mURI))
  135. || (strcmp(mAuthority, rhs.mAuthority))
  136. || (strcmp(mPath, rhs.mPath))
  137. || (strcmp(mFilename, rhs.mFilename))
  138. || (strcmp(mExtension, rhs.mExtension))
  139. || (strcmp(mTag, rhs.mTag))
  140. )
  141. {
  142. return FALSE;
  143. }
  144. return TRUE;
  145. }
  146. bool LLURL::operator!=(const LLURL& rhs) const
  147. {
  148. return !(*this == rhs);
  149. }
  150. const char * LLURL::getFQURL() const
  151. {
  152. char fqurl[LL_MAX_PATH]; /* Flawfinder: ignore */
  153. fqurl[0] = '\0';
  154. if (mURI[0])
  155. {
  156. strncat(fqurl,mURI, LL_MAX_PATH - strlen(fqurl) -1); /* Flawfinder: ignore */
  157. strcat(fqurl,":"); /* Flawfinder: ignore */
  158. if (mAuthority[0])
  159. {
  160. strcat(fqurl,"//"); /* Flawfinder: ignore */
  161. }
  162. }
  163. if (mAuthority[0])
  164. {
  165. strncat(fqurl,mAuthority, LL_MAX_PATH - strlen(fqurl) -1); /* Flawfinder: ignore */
  166. }
  167. strncat(fqurl,mPath, LL_MAX_PATH - strlen(fqurl) -1); /* Flawfinder: ignore */
  168. strncat(fqurl,mFilename, LL_MAX_PATH - strlen(fqurl) -1); /* Flawfinder: ignore */
  169. if (mExtension[0])
  170. {
  171. strcat(fqurl,"."); /* Flawfinder: ignore */
  172. strncat(fqurl,mExtension, LL_MAX_PATH - strlen(fqurl) -1); /* Flawfinder: ignore */
  173. }
  174. if (mTag[0])
  175. {
  176. strcat(fqurl,"#"); /* Flawfinder: ignore */
  177. strncat(fqurl,mTag, LL_MAX_PATH - strlen(fqurl) -1); /* Flawfinder: ignore */
  178. }
  179. strncpy(LLURL::sReturnString,fqurl, LL_MAX_PATH -1); /* Flawfinder: ignore */
  180. LLURL::sReturnString[LL_MAX_PATH -1] = '\0';
  181. return(LLURL::sReturnString);
  182. }
  183. const char* LLURL::updateRelativePath(const LLURL &url)
  184. {
  185. char new_path[LL_MAX_PATH]; /* Flawfinder: ignore */
  186. char tmp_path[LL_MAX_PATH]; /* Flawfinder: ignore */
  187. char *parse;
  188. if (mPath[0] != '/')
  189. {
  190. //start with existing path
  191. strncpy (new_path,url.mPath, LL_MAX_PATH -1); /* Flawfinder: ignore */
  192. new_path[LL_MAX_PATH -1] = '\0';
  193. strncpy (tmp_path,mPath, LL_MAX_PATH -1); /* Flawfinder: ignore */
  194. tmp_path[LL_MAX_PATH -1] = '\0';
  195. parse = strtok(tmp_path,"/");
  196. while (parse)
  197. {
  198. if (!strcmp(parse,"."))
  199. {
  200. // skip this, it's meaningless
  201. }
  202. else if (!strcmp(parse,".."))
  203. {
  204. if ((parse = strrchr(new_path, '/')))
  205. {
  206. *parse = '\0';
  207. if ((parse = strrchr(new_path, '/')))
  208. {
  209. *(parse+1) = '\0';
  210. }
  211. else
  212. {
  213. new_path[0] = '\0';
  214. }
  215. }
  216. else
  217. {
  218. strcat(new_path,"../"); /* Flawfinder: ignore */
  219. }
  220. }
  221. else
  222. {
  223. strncat(new_path,parse, LL_MAX_PATH - strlen(new_path) -1 ); /* Flawfinder: ignore */
  224. strcat(new_path,"/"); /* Flawfinder: ignore */
  225. }
  226. parse = strtok(NULL,"/");
  227. }
  228. strncpy(mPath,new_path, LL_MAX_PATH -1); /* Flawfinder: ignore */
  229. mPath[LL_MAX_PATH -1] = '\0';
  230. }
  231. return mPath;
  232. }
  233. const char * LLURL::getFullPath()
  234. {
  235. strncpy(LLURL::sReturnString,mPath, LL_MAX_PATH -1); /* Flawfinder: ignore */
  236. LLURL::sReturnString[LL_MAX_PATH -1] = '\0';
  237. strncat(LLURL::sReturnString,mFilename, LL_MAX_PATH - strlen(LLURL::sReturnString) -1); /* Flawfinder: ignore */
  238. strcat(LLURL::sReturnString,"."); /* Flawfinder: ignore */
  239. strncat(LLURL::sReturnString,mExtension, LL_MAX_PATH - strlen(LLURL::sReturnString) -1); /* Flawfinder: ignore */
  240. return(sReturnString);
  241. }
  242. const char * LLURL::getAuthority()
  243. {
  244. strncpy(LLURL::sReturnString,mAuthority, LL_MAX_PATH -1); /* Flawfinder: ignore */
  245. LLURL::sReturnString[LL_MAX_PATH -1] = '\0';
  246. return(sReturnString);
  247. }
  248. char LLURL::sReturnString[LL_MAX_PATH] = "";