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 using std::cerr ;
00036
00037 BESProcessEncodedString::BESProcessEncodedString (const char *s)
00038 {
00039 if (s)
00040 {
00041 string key = "" ;
00042 string value = "" ;
00043 bool getting_key_data = true ;
00044 size_t len = strlen( s ) ;
00045 for( unsigned int j = 0; j < len; j++ )
00046 {
00047 if( getting_key_data )
00048 {
00049 if( s[j] != '=' )
00050 {
00051 key += s[j] ;
00052 }
00053 else
00054 {
00055 getting_key_data = false ;
00056 value = "" ;
00057 }
00058 }
00059 else
00060 {
00061 if( s[j] != '&' )
00062 {
00063 value += s[j] ;
00064 }
00065 else
00066 {
00067 _entries[parseHex( key.c_str(), key.length() )] = parseHex( value.c_str(), value.length() ) ;
00068 getting_key_data = true ;
00069 key = "" ;
00070 }
00071 }
00072 }
00073 if( getting_key_data )
00074 cerr << "BESProcessEncodedString: parse error.\n" ;
00075 else
00076 {
00077 _entries[parseHex( key.c_str(), key.length() )] = parseHex( value.c_str(), value.length() ) ;
00078 }
00079 }
00080 else
00081 {
00082 cerr << "BESProcessEncodedString: Passing NULL pointer.\n" ;
00083 exit( 1 ) ;
00084 }
00085 }
00086
00087 string
00088 BESProcessEncodedString::parseHex( const char *s, unsigned int len )
00089 {
00090 if( !s || !len )
00091 return "" ;
00092 char *hexstr = new char[len + 1] ;
00093 strncpy( hexstr, s, len ) ;
00094
00095 if(hexstr == NULL || strlen( hexstr ) == 0 )
00096 return "";
00097
00098 register unsigned int x,y;
00099 for( x = 0, y = 0; hexstr[y] && y < len && x < len; x++, y++ )
00100 {
00101 if( ( hexstr[x] = hexstr[y] ) == '+' )
00102 {
00103 hexstr[x] = ' ' ;
00104 continue ;
00105 }
00106 else if( hexstr[x] == '%' )
00107 {
00108 hexstr[x] = convertHex( &hexstr[y+1] ) ;
00109 y += 2 ;
00110 }
00111 }
00112 hexstr[x] = '\0';
00113 string w = hexstr ;
00114 delete [] hexstr ;
00115 return w ;
00116 }
00117
00118 const unsigned int
00119 BESProcessEncodedString::convertHex( const char* what )
00120 {
00121
00122
00123 register char digit;
00124 digit = (what[0] >= 'A' ? ((what[0] & 0x4F) - 'A')+10 : (what[0] - '0'));
00125 digit *= 16;
00126 digit += (what[1] >='A' ? ((what[1] & 0x4F) - 'A')+10 : (what[1] - '0'));
00127
00128 return digit;
00129 }
00130
00131 string
00132 BESProcessEncodedString::get_key( const string& s )
00133 {
00134 map<string,string>::iterator i ;
00135 i = _entries.find( s ) ;
00136 if( i != _entries.end() )
00137 return (*i).second ;
00138 else
00139 return "" ;
00140 }
00141
00149 void
00150 BESProcessEncodedString::dump( ostream &strm ) const
00151 {
00152 strm << BESIndent::LMarg << "BESProcessEncodedString::dump - ("
00153 << (void *)this << ")" << endl ;
00154 BESIndent::Indent() ;
00155 if( _entries.size() )
00156 {
00157 strm << BESIndent::LMarg << "key|value pairs:" << endl ;
00158 BESIndent::Indent() ;
00159 map<string,string>::const_iterator i ;
00160 map<string,string>::const_iterator ie = _entries.end() ;
00161 for( i = _entries.begin(); i != ie; ++i )
00162 {
00163 strm << BESIndent::LMarg << (*i).first << ": "
00164 << (*i).second << endl ;
00165 }
00166 BESIndent::UnIndent() ;
00167 }
00168 else
00169 {
00170 strm << BESIndent::LMarg << "key|value pairs: none" << endl ;
00171 }
00172 BESIndent::UnIndent() ;
00173 }
00174