BESProcessEncodedString.cc

Go to the documentation of this file.
00001 // BESProcessEncodedString.cc
00002 
00003 // This file is part of bes, A C++ back-end server implementation framework
00004 // for the OPeNDAP Data Access Protocol.
00005 
00006 // Copyright (c) 2004,2005 University Corporation for Atmospheric Research
00007 // Author: Patrick West <pwest@ucar.edu> and Jose Garcia <jgarcia@ucar.edu>
00008 //
00009 // This library is free software; you can redistribute it and/or
00010 // modify it under the terms of the GNU Lesser General Public
00011 // License as published by the Free Software Foundation; either
00012 // version 2.1 of the License, or (at your option) any later version.
00013 // 
00014 // This library is distributed in the hope that it will be useful,
00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017 // Lesser General Public License for more details.
00018 // 
00019 // You should have received a copy of the GNU Lesser General Public
00020 // License along with this library; if not, write to the Free Software
00021 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 //
00023 // You can contact University Corporation for Atmospheric Research at
00024 // 3080 Center Green Drive, Boulder, CO 80301
00025  
00026 // (c) COPYRIGHT University Corporation for Atmostpheric Research 2004-2005
00027 // Please read the full copyright statement in the file COPYRIGHT_UCAR.
00028 //
00029 // Authors:
00030 //      pwest       Patrick West <pwest@ucar.edu>
00031 //      jgarcia     Jose Garcia <jgarcia@ucar.edu>
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     //0x4f == 01001111 mask 
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 

Generated on Wed Aug 29 03:24:04 2007 for OPeNDAP Back End Server (BES) by  doxygen 1.5.2