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-2009 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 Atmospheric 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 #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     //0x4f == 01001111 mask 
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 
Generated by  doxygen 1.6.2-20100208