00001 00033 #include "llviewerprecompiledheaders.h" 00034 #include "llurlwhitelist.h" 00035 00036 #include <iostream> 00037 #include <fstream> 00038 00039 LLUrlWhiteList* LLUrlWhiteList::sInstance = 0; 00040 00042 // 00043 LLUrlWhiteList::LLUrlWhiteList () : 00044 mLoaded ( false ), 00045 mFilename ( "url_whitelist.ini" ), 00046 mUrlList ( 0 ), 00047 mCurIndex ( 0 ) 00048 { 00049 } 00050 00052 // 00053 LLUrlWhiteList::~LLUrlWhiteList () 00054 { 00055 } 00056 00058 00059 //static 00060 void LLUrlWhiteList::initClass () 00061 { 00062 if ( ! sInstance ) 00063 { 00064 sInstance = new LLUrlWhiteList (); 00065 } 00066 } 00067 00068 //static 00069 void LLUrlWhiteList::cleanupClass () 00070 { 00071 delete sInstance; 00072 sInstance = NULL; 00073 } 00074 00075 LLUrlWhiteList* LLUrlWhiteList::getInstance () 00076 { 00077 return sInstance; 00078 } 00079 00081 // 00082 bool LLUrlWhiteList::load () 00083 { 00084 // don't load if we're already loaded 00085 if ( mLoaded ) 00086 return ( true ); 00087 00088 // remove current entries before we load over them 00089 clear (); 00090 00091 // build filename for each user 00092 LLString resolvedFilename = gDirUtilp->getExpandedFilename ( LL_PATH_PER_SL_ACCOUNT, mFilename.c_str () ); 00093 00094 // open a file for reading 00095 llifstream file ( resolvedFilename.c_str () ); 00096 if ( file.is_open () ) 00097 { 00098 // add each line in the file to the list 00099 std::string line; 00100 while ( std::getline ( file, line ) ) 00101 { 00102 addItem ( line, false ); 00103 }; 00104 00105 file.close (); 00106 00107 // flag as loaded 00108 mLoaded = true; 00109 00110 return true; 00111 }; 00112 00113 return false; 00114 } 00115 00117 // 00118 bool LLUrlWhiteList::save () 00119 { 00120 // build filename for each user 00121 LLString resolvedFilename = gDirUtilp->getExpandedFilename ( LL_PATH_PER_SL_ACCOUNT, mFilename.c_str () ); 00122 00123 // open a file for writing 00124 llofstream file ( resolvedFilename.c_str () ); 00125 if ( file.is_open () ) 00126 { 00127 // for each entry we have 00128 for ( string_list_t::iterator iter = mUrlList.begin (); iter != mUrlList.end (); ++iter ) 00129 { 00130 file << ( *iter ) << std::endl; 00131 } 00132 00133 file.close (); 00134 00135 return true; 00136 }; 00137 00138 return false; 00139 } 00140 00142 // 00143 bool LLUrlWhiteList::clear () 00144 { 00145 mUrlList.clear (); 00146 00147 mCurIndex = 0; 00148 00149 return true; 00150 } 00151 00152 LLString url_cleanup(LLString pattern) 00153 { 00154 LLString::trim(pattern); 00155 S32 length = pattern.length(); 00156 S32 position = 0; 00157 std::string::reverse_iterator it = pattern.rbegin(); 00158 ++it; // skip last char, might be '/' 00159 ++position; 00160 for (; it < pattern.rend(); ++it) 00161 { 00162 char c = *it; 00163 if (c == '/') 00164 { 00165 // found second to last '/' 00166 S32 desired_length = length - position; 00167 LLString::truncate(pattern, desired_length); 00168 break; 00169 } 00170 ++position; 00171 } 00172 return pattern; 00173 } 00174 00176 // 00177 bool LLUrlWhiteList::addItem ( const LLString& itemIn, bool saveAfterAdd ) 00178 { 00179 LLString item = url_cleanup(itemIn); 00180 00181 mUrlList.push_back ( item ); 00182 00183 // use this when all you want to do is call addItem ( ... ) where necessary 00184 if ( saveAfterAdd ) 00185 save (); 00186 00187 return true; 00188 } 00189 00191 // 00192 bool LLUrlWhiteList::getFirst ( LLString& valueOut ) 00193 { 00194 if ( mUrlList.size () == 0 ) 00195 return false; 00196 00197 mCurIndex = 0; 00198 valueOut = mUrlList[mCurIndex++]; 00199 00200 return true; 00201 } 00202 00204 // 00205 bool LLUrlWhiteList::getNext ( LLString& valueOut ) 00206 { 00207 if ( mCurIndex >= mUrlList.size () ) 00208 return false; 00209 00210 valueOut = mUrlList[mCurIndex++]; 00211 00212 return true; 00213 } 00214 00216 // 00217 bool LLUrlWhiteList::containsMatch ( const LLString& patternIn ) 00218 { 00219 return false; 00220 }