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() )] = parseHex( value.c_str() ) ;
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() )] = parseHex( value.c_str() ) ;
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 )
00089 {
00090 char *hexstr = new char[strlen( s ) + 1] ;
00091 strcpy( hexstr, s ) ;
00092
00093 if(hexstr == NULL || strlen( hexstr ) == 0 )
00094 return "";
00095
00096 register unsigned int x,y;
00097 for( x = 0, y = 0; hexstr[y]; x++, y++ )
00098 {
00099 if( ( hexstr[x] = hexstr[y] ) == '+' )
00100 {
00101 hexstr[x] = ' ' ;
00102 continue ;
00103 }
00104 else if( hexstr[x] == '%' )
00105 {
00106 hexstr[x] = convertHex( &hexstr[y+1] ) ;
00107 y += 2 ;
00108 }
00109 }
00110 hexstr[x] = '\0';
00111 string w = hexstr ;
00112 delete [] hexstr ;
00113 return w ;
00114 }
00115
00116 const unsigned int
00117 BESProcessEncodedString::convertHex( const char* what )
00118 {
00119
00120
00121 register char digit;
00122 digit = (what[0] >= 'A' ? ((what[0] & 0x4F) - 'A')+10 : (what[0] - '0'));
00123 digit *= 16;
00124 digit += (what[1] >='A' ? ((what[1] & 0x4F) - 'A')+10 : (what[1] - '0'));
00125
00126 return digit;
00127 }
00128
00129 string
00130 BESProcessEncodedString::get_key( const string& s )
00131 {
00132 map<string,string>::iterator i ;
00133 i = _entries.find( s ) ;
00134 if( i != _entries.end() )
00135 return (*i).second ;
00136 else
00137 return "" ;
00138 }
00139
00140 void
00141 BESProcessEncodedString::show_keys()
00142 {
00143 map<string,string>::iterator i ;
00144 map<string,string>::iterator ie = _entries.end() ;
00145 for( i = _entries.begin(); i != ie; ++i )
00146 cout << "key: " << (*i).first << ", value: " << (*i).second << endl ;
00147 }
00148
00156 void
00157 BESProcessEncodedString::dump( ostream &strm ) const
00158 {
00159 strm << BESIndent::LMarg << "BESProcessEncodedString::dump - ("
00160 << (void *)this << ")" << endl ;
00161 BESIndent::Indent() ;
00162 if( _entries.size() )
00163 {
00164 strm << BESIndent::LMarg << "key|value pairs:" << endl ;
00165 BESIndent::Indent() ;
00166 map<string,string>::const_iterator i ;
00167 map<string,string>::const_iterator ie = _entries.end() ;
00168 for( i = _entries.begin(); i != ie; ++i )
00169 {
00170 strm << BESIndent::LMarg << (*i).first << ": "
00171 << (*i).second << endl ;
00172 }
00173 BESIndent::UnIndent() ;
00174 }
00175 else
00176 {
00177 strm << BESIndent::LMarg << "key|value pairs: none" << endl ;
00178 }
00179 BESIndent::UnIndent() ;
00180 }
00181