BESProcessEncodedString.cc
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include "BESProcessEncodedString.h"
00034
00035 #include <cstring>
00036 #include <cstdlib>
00037
00038 using std::cerr ;
00039
00040 BESProcessEncodedString::BESProcessEncodedString (const char *s)
00041 {
00042 if (s)
00043 {
00044 string key = "" ;
00045 string value = "" ;
00046 bool getting_key_data = true ;
00047 size_t len = strlen( s ) ;
00048 for( unsigned int j = 0; j < len; j++ )
00049 {
00050 if( getting_key_data )
00051 {
00052 if( s[j] != '=' )
00053 {
00054 key += s[j] ;
00055 }
00056 else
00057 {
00058 getting_key_data = false ;
00059 value = "" ;
00060 }
00061 }
00062 else
00063 {
00064 if( s[j] != '&' )
00065 {
00066 value += s[j] ;
00067 }
00068 else
00069 {
00070 _entries[parseHex( key.c_str(), key.length() )] = parseHex( value.c_str(), value.length() ) ;
00071 getting_key_data = true ;
00072 key = "" ;
00073 }
00074 }
00075 }
00076 if( getting_key_data )
00077 cerr << "BESProcessEncodedString: parse error.\n" ;
00078 else
00079 {
00080 _entries[parseHex( key.c_str(), key.length() )] = parseHex( value.c_str(), value.length() ) ;
00081 }
00082 }
00083 else
00084 {
00085 cerr << "BESProcessEncodedString: Passing NULL pointer.\n" ;
00086 exit( 1 ) ;
00087 }
00088 }
00089
00090 string
00091 BESProcessEncodedString::parseHex( const char *s, unsigned int len )
00092 {
00093 if( !s || !len )
00094 return "" ;
00095 char *hexstr = new char[len + 1] ;
00096 if( hexstr == NULL )
00097 return "" ;
00098
00099 strncpy( hexstr, s, len ) ;
00100
00101 if(strlen( hexstr ) == 0 )
00102 {
00103 delete [] hexstr ;
00104 return "";
00105 }
00106
00107 register unsigned int x,y;
00108 for( x = 0, y = 0; hexstr[y] && y < len && x < len; x++, y++ )
00109 {
00110 if( ( hexstr[x] = hexstr[y] ) == '+' )
00111 {
00112 hexstr[x] = ' ' ;
00113 continue ;
00114 }
00115 else if( hexstr[x] == '%' )
00116 {
00117 hexstr[x] = convertHex( &hexstr[y+1] ) ;
00118 y += 2 ;
00119 }
00120 }
00121 hexstr[x] = '\0';
00122 string w = hexstr ;
00123 delete [] hexstr ;
00124 return w ;
00125 }
00126
00127 const unsigned int
00128 BESProcessEncodedString::convertHex( const char* what )
00129 {
00130
00131
00132 register char digit;
00133 digit = (what[0] >= 'A' ? ((what[0] & 0x4F) - 'A')+10 : (what[0] - '0'));
00134 digit *= 16;
00135 digit += (what[1] >='A' ? ((what[1] & 0x4F) - 'A')+10 : (what[1] - '0'));
00136
00137 return (unsigned int)digit;
00138 }
00139
00140 string
00141 BESProcessEncodedString::get_key( const string& s )
00142 {
00143 map<string,string>::iterator i ;
00144 i = _entries.find( s ) ;
00145 if( i != _entries.end() )
00146 return (*i).second ;
00147 else
00148 return "" ;
00149 }
00150
00158 void
00159 BESProcessEncodedString::dump( ostream &strm ) const
00160 {
00161 strm << BESIndent::LMarg << "BESProcessEncodedString::dump - ("
00162 << (void *)this << ")" << endl ;
00163 BESIndent::Indent() ;
00164 if( _entries.size() )
00165 {
00166 strm << BESIndent::LMarg << "key|value pairs:" << endl ;
00167 BESIndent::Indent() ;
00168 map<string,string>::const_iterator i ;
00169 map<string,string>::const_iterator ie = _entries.end() ;
00170 for( i = _entries.begin(); i != ie; ++i )
00171 {
00172 strm << BESIndent::LMarg << (*i).first << ": "
00173 << (*i).second << endl ;
00174 }
00175 BESIndent::UnIndent() ;
00176 }
00177 else
00178 {
00179 strm << BESIndent::LMarg << "key|value pairs: none" << endl ;
00180 }
00181 BESIndent::UnIndent() ;
00182 }
00183