llurl.cpp

Go to the documentation of this file.
00001 
00032 #include "llviewerprecompiledheaders.h"
00033 #include "llurl.h"
00034 #include "llerror.h"
00035 
00036 LLURL::LLURL()
00037 {
00038         init("");
00039 }
00040 
00041 LLURL::LLURL(const LLURL &url)
00042 {
00043         if (this != &url)
00044         {
00045                 init(url.getFQURL());
00046         }
00047         else
00048         {
00049                 init("");
00050         }
00051 }
00052 
00053 LLURL::LLURL(const char * url)
00054 {
00055         init(url);
00056 }
00057 
00058 LLURL::~LLURL()
00059 {
00060         free();
00061 }
00062 
00063 void LLURL::init(const char * url)
00064 {
00065         mURI[0] = '\0';
00066         mAuthority[0] = '\0';
00067         mPath[0] = '\0';
00068         mFilename[0] = '\0';
00069         mExtension[0] = '\0';
00070         mTag[0] = '\0';
00071         
00072         char url_copy[MAX_STRING];              /* Flawfinder: ignore */
00073 
00074         strncpy (url_copy,url, MAX_STRING -1);          /* Flawfinder: ignore */
00075         url_copy[MAX_STRING -1] = '\0';
00076 
00077         char *parse;
00078         char *leftover_url = url_copy;
00079         S32 span = 0;
00080 
00081         // copy and lop off tag
00082         if ((parse = strchr(url_copy,'#')))
00083         {
00084                 strncpy(mTag,parse+1, LL_MAX_PATH -1);          /* Flawfinder: ignore */
00085                 mTag[LL_MAX_PATH -1] = '\0';
00086                 *parse = '\0';
00087         }
00088 
00089         // copy out URI if it exists, update leftover_url 
00090         if ((parse = strchr(url_copy,':')))
00091         {
00092                 *parse = '\0';
00093                 strncpy(mURI,leftover_url, LL_MAX_PATH -1);             /* Flawfinder: ignore */
00094                 mURI[LL_MAX_PATH -1] = '\0';
00095                 leftover_url = parse + 1;
00096         }
00097 
00098         // copy out authority if it exists, update leftover_url
00099         if ((leftover_url[0] == '/') && (leftover_url[1] == '/'))
00100         {
00101                 leftover_url += 2; // skip the "//"
00102 
00103                 span = strcspn(leftover_url, "/"); 
00104                 strncat(mAuthority,leftover_url,span);          /* Flawfinder: ignore */
00105                 leftover_url += span;
00106         }
00107         
00108         if ((parse = strrchr(leftover_url,'.')))
00109         {
00110                 // copy and lop off extension
00111                 strncpy(mExtension,parse+1, LL_MAX_PATH -1);            /* Flawfinder: ignore */
00112                 mExtension[LL_MAX_PATH -1] = '\0';
00113                 *parse = '\0';
00114         }
00115 
00116         if ((parse = strrchr(leftover_url,'/')))
00117         {
00118                 parse++;
00119         }
00120         else
00121         {
00122                 parse = leftover_url;
00123         }
00124 
00125         // copy and lop off filename
00126         strncpy(mFilename,parse, LL_MAX_PATH -1);/* Flawfinder: ignore */
00127         mFilename[LL_MAX_PATH -1] = '\0';
00128         *parse = '\0';
00129 
00130         // what's left should be the path
00131         strncpy(mPath,leftover_url, LL_MAX_PATH -1);            /* Flawfinder: ignore */
00132         mPath[LL_MAX_PATH -1] = '\0';
00133 
00134 //      llinfos << url << "  decomposed into: " << llendl;
00135 //      llinfos << "  URI : <" << mURI << ">" << llendl;
00136 //      llinfos << "  Auth: <" << mAuthority << ">" << llendl;
00137 //      llinfos << "  Path: <" << mPath << ">" << llendl;
00138 //      llinfos << "  File: <" << mFilename << ">" << llendl;
00139 //      llinfos << "  Ext : <" << mExtension << ">" << llendl;
00140 //      llinfos << "  Tag : <" << mTag << ">" << llendl;
00141 }
00142 
00143 void LLURL::free()
00144 {
00145 }
00146 
00147 // Copy assignment
00148 LLURL &LLURL::operator=(const LLURL &rhs)
00149 {
00150         if (this != &rhs)
00151         {
00152                 this->init(rhs.getFQURL());
00153         }
00154         
00155         return *this;
00156 }
00157 
00158 // Compare
00159 bool LLURL::operator==(const LLURL &rhs) const
00160 {
00161         if ((strcmp(mURI, rhs.mURI))
00162                 || (strcmp(mAuthority, rhs.mAuthority))
00163                 || (strcmp(mPath, rhs.mPath))
00164                 || (strcmp(mFilename, rhs.mFilename))
00165                 || (strcmp(mExtension, rhs.mExtension))
00166                 || (strcmp(mTag, rhs.mTag))
00167                 )
00168         {
00169                 return FALSE;
00170         }
00171         return TRUE;
00172 }
00173 
00174 bool LLURL::operator!=(const LLURL& rhs) const
00175 {
00176         return !(*this == rhs);
00177 }
00178 
00179 const char * LLURL::getFQURL() const
00180 {
00181         char        fqurl[LL_MAX_PATH];         /* Flawfinder: ignore */
00182 
00183         fqurl[0] = '\0';
00184 
00185         if (mURI[0])
00186         {
00187                 strncat(fqurl,mURI, LL_MAX_PATH - strlen(fqurl) -1);            /* Flawfinder: ignore */
00188                 strcat(fqurl,":");              /* Flawfinder: ignore */
00189                 if (mAuthority[0])
00190                 {
00191                         strcat(fqurl,"//");             /* Flawfinder: ignore */
00192                 }
00193         }
00194 
00195         if (mAuthority[0])
00196         {
00197                 strncat(fqurl,mAuthority, LL_MAX_PATH - strlen(fqurl) -1);              /* Flawfinder: ignore */
00198         }
00199 
00200         strncat(fqurl,mPath, LL_MAX_PATH - strlen(fqurl) -1);           /* Flawfinder: ignore */
00201 
00202         strncat(fqurl,mFilename, LL_MAX_PATH - strlen(fqurl) -1);               /* Flawfinder: ignore */
00203         
00204         if (mExtension[0])
00205         {
00206                 strcat(fqurl,".");              /* Flawfinder: ignore */
00207                 strncat(fqurl,mExtension, LL_MAX_PATH - strlen(fqurl) -1);              /* Flawfinder: ignore */
00208         }
00209 
00210         if (mTag[0])
00211         {
00212                 strcat(fqurl,"#");              /* Flawfinder: ignore */
00213                 strncat(fqurl,mTag, LL_MAX_PATH - strlen(fqurl) -1);            /* Flawfinder: ignore */
00214         }
00215 
00216         strncpy(LLURL::sReturnString,fqurl, LL_MAX_PATH -1);            /* Flawfinder: ignore */
00217         LLURL::sReturnString[LL_MAX_PATH -1] = '\0';
00218 
00219         return(LLURL::sReturnString);
00220 }
00221 
00222 
00223 const char* LLURL::updateRelativePath(const LLURL &url)
00224 {
00225         char new_path[LL_MAX_PATH];             /* Flawfinder: ignore */
00226         char tmp_path[LL_MAX_PATH];             /* Flawfinder: ignore */
00227 
00228         char *parse;
00229 
00230         if (mPath[0] != '/')
00231         {
00232                 //start with existing path
00233                 strncpy (new_path,url.mPath, LL_MAX_PATH -1);           /* Flawfinder: ignore */
00234                 new_path[LL_MAX_PATH -1] = '\0';
00235                 strncpy (tmp_path,mPath, LL_MAX_PATH -1);               /* Flawfinder: ignore */
00236                 tmp_path[LL_MAX_PATH -1] = '\0';
00237 
00238                 parse = strtok(tmp_path,"/");
00239                 while (parse)
00240                 {
00241                         if (!strcmp(parse,"."))
00242                         {
00243                                 // skip this, it's meaningless
00244                         }
00245                         else if (!strcmp(parse,".."))
00246                         {
00247                                 if ((parse = strrchr(new_path, '/')))
00248                                 {
00249                                         *parse = '\0';
00250                                         if ((parse = strrchr(new_path, '/')))
00251                                         {
00252                                                 *(parse+1) = '\0';
00253                                         }
00254                                         else
00255                                         {
00256                                                 new_path[0] = '\0';
00257                                         }
00258                                 }
00259                                 else
00260                                 {
00261                                         strcat(new_path,"../");         /* Flawfinder: ignore */
00262                                 }
00263 
00264                         }
00265                         else 
00266                         {
00267                                 strncat(new_path,parse, LL_MAX_PATH - strlen(new_path) -1 );            /* Flawfinder: ignore */
00268                                 strcat(new_path,"/");           /* Flawfinder: ignore */
00269                         }
00270                         parse = strtok(NULL,"/");
00271                 }
00272                 strncpy(mPath,new_path, LL_MAX_PATH -1);                /* Flawfinder: ignore */
00273                 mPath[LL_MAX_PATH -1] = '\0';
00274         }
00275         return mPath;
00276 }
00277 
00278 const char * LLURL::getFullPath()
00279 {
00280         strncpy(LLURL::sReturnString,mPath, LL_MAX_PATH -1);            /* Flawfinder: ignore */
00281         LLURL::sReturnString[LL_MAX_PATH -1] = '\0';
00282         strncat(LLURL::sReturnString,mFilename, LL_MAX_PATH - strlen(LLURL::sReturnString) -1);         /* Flawfinder: ignore */
00283         strcat(LLURL::sReturnString,".");               /* Flawfinder: ignore */
00284         strncat(LLURL::sReturnString,mExtension, LL_MAX_PATH - strlen(LLURL::sReturnString) -1);                /* Flawfinder: ignore */
00285         return(sReturnString);
00286 }
00287 
00288 
00289 char LLURL::sReturnString[LL_MAX_PATH] = "";

Generated on Thu Jul 1 06:09:24 2010 for Second Life Viewer by  doxygen 1.4.7