Main Page | Namespace List | Alphabetical List | Data Structures | File List | Namespace Members | Data Fields | Globals

NWWWCodec.cp

Go to the documentation of this file.
00001 00024 00025 #define MW_CGI monsterworks::cgi 00026 00027 #include <NWWWCodec.h> 00028 using MW_CGI::FormPairs; 00029 using MW_CGI::QueryWords; 00030 using MW_CGI::NWWWCodec; 00031 using std::string; 00032 00033 #include <cctype> 00034 using std::toupper; 00035 using std::isxdigit; 00036 using std::isdigit; 00037 using std::iscntrl; 00038 using std::isalnum; 00039 using std::sprintf; 00040 00051 std::string MW_CGI::NWWWCodec::ToWWW( 00052 const std::string& inPlainText) 00053 { 00054 string theResult = ""; 00055 00056 for ( string::const_iterator theIterator (inPlainText.begin()); 00057 theIterator != inPlainText.end(); 00058 ++theIterator) 00059 { 00060 if (*theIterator == ' ') 00061 { 00062 theResult += '+'; 00063 } 00064 else if (isalnum(*theIterator)) 00065 { 00066 theResult += *theIterator; 00067 } 00068 else 00069 { 00070 theResult += '%'; 00071 char theString[2]; 00072 sprintf(theString, "%02X", *theIterator); 00073 theResult += theString; 00074 } 00075 } 00076 return theResult; 00077 } 00078 00094 std::string MW_CGI::NWWWCodec::FromWWW( 00095 const std::string& inCypherText) 00096 { 00097 string theResult (""); 00098 for ( string::const_iterator theIterator = inCypherText.begin(); 00099 theIterator != inCypherText.end(); 00100 ++theIterator) 00101 { 00102 if (*theIterator == '+') 00103 { 00104 theResult += ' '; 00105 } 00106 else if (*theIterator == '%') 00107 { 00108 int c1 = toupper(*(++theIterator)); 00109 int c2 = toupper(*(++theIterator)); 00110 00111 //check validity of 3 char code 00112 if (!isxdigit(c1) || !isxdigit(c2)) throw XInvalidString(inCypherText); 00113 00114 //convert c1 to integer value 00115 if (isdigit(c1)) 00116 { 00117 c1 -= '0'; 00118 } 00119 else 00120 { 00121 c1 += (10 - 'A'); 00122 } 00123 00124 //convert c2 to integer value 00125 if (isdigit(c2)) 00126 { 00127 c2 -= '0'; 00128 } 00129 else 00130 { 00131 c2 += (10 - 'A'); 00132 } 00133 00134 //combine values and cast to char 00135 theResult += static_cast<char>(((c1 << 4) | c2)); 00136 } 00137 else if ( *theIterator == 0x0D 00138 || *theIterator == 0x0A) 00139 { 00140 //check to see if this is a sequence and skip as needed 00141 while ( *(theIterator + 1) == 0x0D 00142 || *(theIterator + 1) == 0x0A) 00143 { 00144 ++theIterator; 00145 } 00146 theResult += '\n'; 00147 } 00148 else if (iscntrl(*theIterator) ) 00149 { 00150 //skip this character 00151 } 00152 else 00153 { 00154 theResult += *theIterator; 00155 } 00156 } 00157 return theResult; 00158 } 00159 00173 std::string MW_CGI::NWWWCodec::EscapeHTML( 00174 const std::string& inPlainText) 00175 { 00176 string theResult = ""; 00177 for (string::const_iterator theIterator = inPlainText.begin(); 00178 theIterator != inPlainText.end(); 00179 ++theIterator) 00180 { 00181 if (*theIterator == '<') 00182 { 00183 theResult += "&lt;"; 00184 } 00185 else if (*theIterator == '>') 00186 { 00187 theResult += "&gt;"; 00188 } 00189 else if (*theIterator == '&') 00190 { 00191 theResult += "&amp;"; 00192 } 00193 else if (*theIterator == '"') 00194 { 00195 theResult += "&quot;"; 00196 } 00197 else 00198 { 00199 theResult += *theIterator; 00200 } 00201 } 00202 00203 return theResult; 00204 } 00205 00213 MW_CGI::QueryWords MW_CGI::NWWWCodec::StringToWords( 00214 const std::string& inEncodedString) 00215 { 00216 QueryWords theResult; 00217 string theName; 00218 string::const_iterator theIterator = inEncodedString.begin(); 00219 00220 while (theIterator != inEncodedString.end()) 00221 { 00222 theName = ""; 00223 00224 while ( (*theIterator != '+') 00225 && (theIterator != inEncodedString.end()) ) 00226 { 00227 theName += *theIterator; 00228 ++theIterator; 00229 } 00230 if (theIterator != inEncodedString.end()) 00231 { // skip the + 00232 ++theIterator; 00233 } 00234 00235 theName = FromWWW(theName); 00236 theResult.insert(theName); 00237 } 00238 return theResult; 00239 } 00240 00247 MW_CGI::FormPairs MW_CGI::NWWWCodec::StringToPairs( 00248 const std::string& inEncodedString) 00249 { 00250 00251 FormPairs theResult; 00252 string theName = ""; 00253 string theValue = ""; 00254 string::const_iterator theIterator = inEncodedString.begin(); 00255 00256 while (theIterator != inEncodedString.end()) 00257 { 00258 theName = ""; 00259 theValue = ""; 00260 00261 //get the name 00262 while ( (*theIterator != '=') 00263 && (theIterator != inEncodedString.end()) ) 00264 { 00265 theName += *theIterator; 00266 ++theIterator; 00267 } 00268 if (theIterator != inEncodedString.end()) 00269 { //skip the = 00270 ++theIterator; 00271 } 00272 00273 //get the value 00274 while ( (*theIterator != '&') 00275 && (theIterator != inEncodedString.end()) ) 00276 { 00277 theValue += *theIterator; 00278 ++theIterator; 00279 } 00280 if (theIterator != inEncodedString.end()) 00281 { //skip the & 00282 ++theIterator; 00283 } 00284 00285 theName = FromWWW(theName); 00286 theValue = FromWWW(theValue); 00287 theResult.insert(std::pair<const string,string>(theName,theValue)); 00288 } 00289 return theResult; 00290 }

monsterworks Code by Monsterworks. Made available under the GNU Public License.
Documentation by doxygen.