PageRenderTime 74ms CodeModel.GetById 20ms app.highlight 36ms RepoModel.GetById 15ms app.codeStats 0ms

/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
 27#include "llviewerprecompiledheaders.h"
 28#include "llurl.h"
 29#include "llerror.h"
 30
 31LLURL::LLURL()
 32{
 33	init("");
 34}
 35
 36LLURL::LLURL(const LLURL &url)
 37{
 38	if (this != &url)
 39	{
 40		init(url.getFQURL());
 41	}
 42	else
 43	{
 44		init("");
 45	}
 46}
 47
 48LLURL::LLURL(const char * url)
 49{
 50	init(url);
 51}
 52
 53LLURL::~LLURL()
 54{
 55	cleanup();
 56}
 57
 58void LLURL::init(const char * url)
 59{
 60	mURI[0] = '\0';
 61	mAuthority[0] = '\0';
 62	mPath[0] = '\0';
 63	mFilename[0] = '\0';
 64	mExtension[0] = '\0';
 65	mTag[0] = '\0';
 66	
 67	char url_copy[MAX_STRING];		/* Flawfinder: ignore */
 68
 69	strncpy (url_copy,url, MAX_STRING -1);		/* Flawfinder: ignore */
 70	url_copy[MAX_STRING -1] = '\0';
 71
 72	char *parse;
 73	char *leftover_url = url_copy;
 74	S32 span = 0;
 75
 76	// copy and lop off tag
 77	if ((parse = strchr(url_copy,'#')))
 78	{
 79		strncpy(mTag,parse+1, LL_MAX_PATH -1);		/* Flawfinder: ignore */
 80		mTag[LL_MAX_PATH -1] = '\0';
 81		*parse = '\0';
 82	}
 83
 84	// copy out URI if it exists, update leftover_url 
 85	if ((parse = strchr(url_copy,':')))
 86	{
 87		*parse = '\0';
 88		strncpy(mURI,leftover_url, LL_MAX_PATH -1);		/* Flawfinder: ignore */
 89		mURI[LL_MAX_PATH -1] = '\0';
 90		leftover_url = parse + 1;
 91	}
 92
 93	// copy out authority if it exists, update leftover_url
 94	if ((leftover_url[0] == '/') && (leftover_url[1] == '/'))
 95	{
 96		leftover_url += 2; // skip the "//"
 97
 98		span = strcspn(leftover_url, "/"); 
 99		strncat(mAuthority,leftover_url,span);		/* Flawfinder: ignore */
100		leftover_url += span;
101	}
102	
103	if ((parse = strrchr(leftover_url,'.')))
104	{
105		// copy and lop off extension
106		strncpy(mExtension,parse+1, LL_MAX_PATH -1);		/* Flawfinder: ignore */
107		mExtension[LL_MAX_PATH -1] = '\0';
108		*parse = '\0';
109	}
110
111	if ((parse = strrchr(leftover_url,'/')))
112	{
113		parse++;
114	}
115	else
116	{
117		parse = leftover_url;
118	}
119
120	// copy and lop off filename
121	strncpy(mFilename,parse, LL_MAX_PATH -1);/* Flawfinder: ignore */
122	mFilename[LL_MAX_PATH -1] = '\0';
123	*parse = '\0';
124
125	// what's left should be the path
126	strncpy(mPath,leftover_url, LL_MAX_PATH -1);		/* Flawfinder: ignore */
127	mPath[LL_MAX_PATH -1] = '\0';
128
129//	llinfos << url << "  decomposed into: " << llendl;
130//	llinfos << "  URI : <" << mURI << ">" << llendl;
131//	llinfos << "  Auth: <" << mAuthority << ">" << llendl;
132//	llinfos << "  Path: <" << mPath << ">" << llendl;
133//	llinfos << "  File: <" << mFilename << ">" << llendl;
134//	llinfos << "  Ext : <" << mExtension << ">" << llendl;
135//	llinfos << "  Tag : <" << mTag << ">" << llendl;
136}
137
138void LLURL::cleanup()
139{
140}
141
142// Copy assignment
143LLURL &LLURL::operator=(const LLURL &rhs)
144{
145	if (this != &rhs)
146	{
147		this->init(rhs.getFQURL());
148	}
149	
150	return *this;
151}
152
153// Compare
154bool LLURL::operator==(const LLURL &rhs) const
155{
156	if ((strcmp(mURI, rhs.mURI))
157		|| (strcmp(mAuthority, rhs.mAuthority))
158		|| (strcmp(mPath, rhs.mPath))
159		|| (strcmp(mFilename, rhs.mFilename))
160		|| (strcmp(mExtension, rhs.mExtension))
161		|| (strcmp(mTag, rhs.mTag))
162		)
163	{
164		return FALSE;
165	}
166	return TRUE;
167}
168
169bool LLURL::operator!=(const LLURL& rhs) const
170{
171	return !(*this == rhs);
172}
173
174const char * LLURL::getFQURL() const
175{
176	char        fqurl[LL_MAX_PATH];		/* Flawfinder: ignore */
177
178	fqurl[0] = '\0';
179
180	if (mURI[0])
181	{
182		strncat(fqurl,mURI, LL_MAX_PATH - strlen(fqurl) -1);		/* Flawfinder: ignore */
183		strcat(fqurl,":");		/* Flawfinder: ignore */
184		if (mAuthority[0])
185		{
186			strcat(fqurl,"//");		/* Flawfinder: ignore */
187		}
188	}
189
190	if (mAuthority[0])
191	{
192		strncat(fqurl,mAuthority, LL_MAX_PATH - strlen(fqurl) -1);		/* Flawfinder: ignore */
193	}
194
195	strncat(fqurl,mPath, LL_MAX_PATH - strlen(fqurl) -1);		/* Flawfinder: ignore */
196
197	strncat(fqurl,mFilename, LL_MAX_PATH - strlen(fqurl) -1);		/* Flawfinder: ignore */
198	
199	if (mExtension[0])
200	{
201		strcat(fqurl,".");		/* Flawfinder: ignore */
202		strncat(fqurl,mExtension, LL_MAX_PATH - strlen(fqurl) -1);		/* Flawfinder: ignore */
203	}
204
205	if (mTag[0])
206	{
207		strcat(fqurl,"#");		/* Flawfinder: ignore */
208		strncat(fqurl,mTag, LL_MAX_PATH - strlen(fqurl) -1);		/* Flawfinder: ignore */
209	}
210
211	strncpy(LLURL::sReturnString,fqurl, LL_MAX_PATH -1);		/* Flawfinder: ignore */
212	LLURL::sReturnString[LL_MAX_PATH -1] = '\0';
213
214	return(LLURL::sReturnString);
215}
216
217
218const char* LLURL::updateRelativePath(const LLURL &url)
219{
220	char new_path[LL_MAX_PATH];		/* Flawfinder: ignore */
221	char tmp_path[LL_MAX_PATH];		/* Flawfinder: ignore */
222
223	char *parse;
224
225	if (mPath[0] != '/')
226	{
227		//start with existing path
228		strncpy (new_path,url.mPath, LL_MAX_PATH -1);		/* Flawfinder: ignore */
229		new_path[LL_MAX_PATH -1] = '\0';
230		strncpy (tmp_path,mPath, LL_MAX_PATH -1);		/* Flawfinder: ignore */
231		tmp_path[LL_MAX_PATH -1] = '\0';
232
233		parse = strtok(tmp_path,"/");
234		while (parse)
235		{
236			if (!strcmp(parse,"."))
237			{
238				// skip this, it's meaningless
239			}
240			else if (!strcmp(parse,".."))
241			{
242				if ((parse = strrchr(new_path, '/')))
243				{
244					*parse = '\0';
245					if ((parse = strrchr(new_path, '/')))
246					{
247						*(parse+1) = '\0';
248					}
249					else
250					{
251						new_path[0] = '\0';
252					}
253				}
254				else
255				{
256					strcat(new_path,"../");		/* Flawfinder: ignore */
257				}
258
259			}
260			else 
261			{
262				strncat(new_path,parse, LL_MAX_PATH - strlen(new_path) -1 );		/* Flawfinder: ignore */
263				strcat(new_path,"/");		/* Flawfinder: ignore */
264			}
265			parse = strtok(NULL,"/");
266		}
267		strncpy(mPath,new_path, LL_MAX_PATH -1);		/* Flawfinder: ignore */
268		mPath[LL_MAX_PATH -1] = '\0';
269	}
270	return mPath;
271}
272
273const char * LLURL::getFullPath()
274{
275	strncpy(LLURL::sReturnString,mPath, LL_MAX_PATH -1);		/* Flawfinder: ignore */
276	LLURL::sReturnString[LL_MAX_PATH -1] = '\0';
277	strncat(LLURL::sReturnString,mFilename, LL_MAX_PATH - strlen(LLURL::sReturnString) -1);		/* Flawfinder: ignore */
278	strcat(LLURL::sReturnString,".");		/* Flawfinder: ignore */
279	strncat(LLURL::sReturnString,mExtension, LL_MAX_PATH - strlen(LLURL::sReturnString) -1);		/* Flawfinder: ignore */
280	return(sReturnString);
281}
282
283const char * LLURL::getAuthority()
284{
285	strncpy(LLURL::sReturnString,mAuthority, LL_MAX_PATH -1);               /* Flawfinder: ignore */
286	LLURL::sReturnString[LL_MAX_PATH -1] = '\0';
287	return(sReturnString);
288}
289
290char LLURL::sReturnString[LL_MAX_PATH] = "";