Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members  

url_encoder.cc

00001 /*
00002 *  Name:      url_encoder.hh
00003 *  Author:    Rafael Jesus Alcantara Perez
00004 *  Summary:   URL encoder class
00005 *  Date:      $Date: 2003/04/14 00:18:35 $
00006 *  Revision:  $Revision: 1.1 $
00007 *
00008 *  Copyright (C) 1994-2002  Rafael Jesus Alcantara Perez <rafa@dedalo-ing.com>
00009 *
00010 *  This program is free software; you can redistribute it and/or modify
00011 *  it under the terms of the GNU General Public License as published by
00012 *  the Free Software Foundation; either version 2 of the License, or
00013 *  (at your option) any later version.
00014 *
00015 *  This program is distributed in the hope that it will be useful,
00016 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 *  GNU General Public License for more details.
00019 *
00020 *  You should have received a copy of the GNU General Public License
00021 *  along with this program; if not, write to the Free Software
00022 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
00023 *  MA 02111-1307, USA.
00024 */
00025 
00026 #include <cctype>
00027 #include <cstdio>
00028 #include <mpcl/net/url_encoder.hh>
00029 #include <mpcl/text/string.hh>
00030 
00031 
00032 //
00033 //  S E L E C T O R S
00034 //
00035 
00036 mpcl::text::TString mpcl::net::TUrlEncoder::
00037 _encode (const text::TString& rkySOURCE_PLAIN)
00038 {
00039 
00040   using std::isalnum;
00041   using std::sprintf;
00042 
00043   TString       yEncoded;
00044   char          acConvertedBuffer[] = "  ";
00045   const char*   pkcSourcePlain      = rkySOURCE_PLAIN.c_str();
00046   
00047   //
00048   //  Replace hexadecimal escape patterns, by their
00049   //  ASCII values.
00050   //
00051   for (register size_t J = 0, zLength = rkySOURCE_PLAIN.size(); ( J < zLength ) ;++J)
00052   {
00053     if ( isalnum (*pkcSourcePlain) )
00054     {
00055       yEncoded += *pkcSourcePlain;
00056     }
00057     else
00058     {
00059       yEncoded += '%';
00060       sprintf (acConvertedBuffer, "%02x", *pkcSourcePlain);
00061       yEncoded += acConvertedBuffer;
00062     }
00063     ++pkcSourcePlain;
00064   }
00065   yEncoded.replaceAll (' ', '+');
00066   return yEncoded;
00067 
00068 }  // _encode()
00069 
00070 
00071 mpcl::text::TString mpcl::net::TUrlEncoder::
00072 _encode ( const util::collection::TStringToStringMap& rktSOURCE_PARAMETER_MAP ,
00073           bool                                        gEMPTY_PARAMETERS_TOO   )
00074 {
00075 
00076   using util::collection::TStringToStringMap;
00077 
00078   TString                              yParameter;
00079   TString                              yCgiParameters;
00080   TStringToStringMap::const_iterator   ktIter = rktSOURCE_PARAMETER_MAP.begin();
00081   TStringToStringMap::const_iterator   ktEnd  = rktSOURCE_PARAMETER_MAP.end();
00082 
00083   if ( gEMPTY_PARAMETERS_TOO )
00084   {
00085     for (; ( ktIter != ktEnd ) ;++ktIter)
00086     {
00087       yParameter     += ktIter->first;
00088       yParameter     += "=" ;
00089       yParameter     += _encode (ktIter->second);
00090       yCgiParameters += yParameter;
00091       yParameter      = "&";
00092     }
00093   }
00094   else
00095   {
00096     for (; ( ktIter != ktEnd ) ;++ktIter)
00097     {
00098       if ( !ktIter->second.empty() )
00099       {
00100         yParameter     += ktIter->first;
00101         yParameter     += "=" ;
00102         yParameter     += _encode (ktIter->second);
00103         yCgiParameters += yParameter;
00104         yParameter      = "&";
00105       }
00106     }
00107   }
00108   return yCgiParameters;
00109 
00110 }  // _encode()

Generated on Mon Oct 13 02:35:24 2003 for MPCL by doxygen1.2.18