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 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 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     //0x4f == 01001111 mask 
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 

Generated on Wed Jan 2 06:01:18 2008 for OPeNDAP Back End Server (BES) by  doxygen 1.5.4