OPeNDAP Hyrax Back End Server (BES)  Updated for version 3.8.3
BESProcessEncodedString.cc
Go to the documentation of this file.
1 // BESProcessEncodedString.cc
2 
3 // This file is part of bes, A C++ back-end server implementation framework
4 // for the OPeNDAP Data Access Protocol.
5 
6 // Copyright (c) 2004-2009 University Corporation for Atmospheric Research
7 // Author: Patrick West <pwest@ucar.edu> and Jose Garcia <jgarcia@ucar.edu>
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 //
23 // You can contact University Corporation for Atmospheric Research at
24 // 3080 Center Green Drive, Boulder, CO 80301
25 
26 // (c) COPYRIGHT University Corporation for Atmospheric Research 2004-2005
27 // Please read the full copyright statement in the file COPYRIGHT_UCAR.
28 //
29 // Authors:
30 // pwest Patrick West <pwest@ucar.edu>
31 // jgarcia Jose Garcia <jgarcia@ucar.edu>
32 
34 
35 #include <cstring>
36 #include <cstdlib>
37 
38 using std::cerr ;
39 
41 {
42  if (s)
43  {
44  string key = "" ;
45  string value = "" ;
46  bool getting_key_data = true ;
47  size_t len = strlen( s ) ;
48  for( unsigned int j = 0; j < len; j++ )
49  {
50  if( getting_key_data )
51  {
52  if( s[j] != '=' )
53  {
54  key += s[j] ;
55  }
56  else
57  {
58  getting_key_data = false ;
59  value = "" ;
60  }
61  }
62  else
63  {
64  if( s[j] != '&' )
65  {
66  value += s[j] ;
67  }
68  else
69  {
70  _entries[parseHex( key.c_str(), key.length() )] = parseHex( value.c_str(), value.length() ) ;
71  getting_key_data = true ;
72  key = "" ;
73  }
74  }
75  }
76  if( getting_key_data )
77  cerr << "BESProcessEncodedString: parse error.\n" ;
78  else
79  {
80  _entries[parseHex( key.c_str(), key.length() )] = parseHex( value.c_str(), value.length() ) ;
81  }
82  }
83  else
84  {
85  cerr << "BESProcessEncodedString: Passing NULL pointer.\n" ;
86  exit( 1 ) ;
87  }
88 }
89 
90 string
91 BESProcessEncodedString::parseHex( const char *s, unsigned int len )
92 {
93  if( !s || !len )
94  return "" ;
95  char *hexstr = new char[len + 1] ;
96  if( hexstr == NULL )
97  return "" ;
98 
99  strncpy( hexstr, s, len ) ;
100  hexstr[len] = '\0'; // Must explicitly add null; strncpy might not. jhrg
101  if(strlen( hexstr ) == 0 )
102  {
103  delete [] hexstr ;
104  return "";
105  }
106 
107  register unsigned int x,y;
108  for( x = 0, y = 0; hexstr[y] && y < len && x < len; x++, y++ )
109  {
110  if( ( hexstr[x] = hexstr[y] ) == '+' )
111  {
112  hexstr[x] = ' ' ;
113  continue ;
114  }
115  else if( hexstr[x] == '%' )
116  {
117  hexstr[x] = convertHex( &hexstr[y+1] ) ;
118  y += 2 ;
119  }
120  }
121  hexstr[x] = '\0';
122  string w = hexstr ;
123  delete [] hexstr ;
124  return w ;
125 }
126 
127 const unsigned int
128 BESProcessEncodedString::convertHex( const char* what )
129 {
130  //0x4f == 01001111 mask
131 
132  register char digit;
133  digit = (what[0] >= 'A' ? ((what[0] & 0x4F) - 'A')+10 : (what[0] - '0'));
134  digit *= 16;
135  digit += (what[1] >='A' ? ((what[1] & 0x4F) - 'A')+10 : (what[1] - '0'));
136 
137  return (unsigned int)digit;
138 }
139 
140 string
142 {
143  map<string,string>::iterator i ;
144  i = _entries.find( s ) ;
145  if( i != _entries.end() )
146  return (*i).second ;
147  else
148  return "" ;
149 }
150 
158 void
159 BESProcessEncodedString::dump( ostream &strm ) const
160 {
161  strm << BESIndent::LMarg << "BESProcessEncodedString::dump - ("
162  << (void *)this << ")" << endl ;
164  if( _entries.size() )
165  {
166  strm << BESIndent::LMarg << "key|value pairs:" << endl ;
168  map<string,string>::const_iterator i ;
169  map<string,string>::const_iterator ie = _entries.end() ;
170  for( i = _entries.begin(); i != ie; ++i )
171  {
172  strm << BESIndent::LMarg << (*i).first << ": "
173  << (*i).second << endl ;
174  }
176  }
177  else
178  {
179  strm << BESIndent::LMarg << "key|value pairs: none" << endl ;
180  }
182 }
183 
static void Indent()
Definition: BESIndent.cc:38
static ostream & LMarg(ostream &strm)
Definition: BESIndent.cc:73
virtual void dump(ostream &strm) const
dumps information about this object
static void UnIndent()
Definition: BESIndent.cc:44
string get_key(const string &s)