CPPSERV


Home Projects Jobs Clientele Contact
CPPSERV Documentation Download TODO Mailing lists Bug tracker News RSS Feed Browse source

Util.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2006 by Ilya A. Volynets-Evenbakh                       *
00003  *   ilya@total-knowledge.com                                              *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 
00021 #include <servlet/Util.h>
00022 #include <stdio.h>
00023 #include <stdlib.h>
00024 
00025 namespace servlet
00026 {
00027 namespace util
00028 {
00029 
00030 std::string urlDecode(const std::string& s)
00031 {
00032     std::string result(s);
00033     urlInPlaceDecode(result);
00034     return result;
00035 }
00036 
00037 
00041 void urlInPlaceDecode(std::string& s)
00042 {
00043     std::string::size_type w=0;
00044     for (std::string::size_type p=0; p < s.size(); p++,w++) {
00045         if (s[p] == '+'){
00046             s[w]= ' ';  // translate '+' to ' ':
00047         } else if ((s[p] == '%') && (p + 2 < s.size())) {  // check length
00048             char str[]={s[p+1],s[p+2],0};
00049             char *pp;
00050             unsigned char c=(unsigned char)strtoul(str,&pp,16);
00051             if(pp==str+2){
00052                 s[w]=c;
00053                 p+=2;
00054             } else {
00055                 s[w]=s[p];
00056             }
00057         }else {
00058             s[w]=s[p];
00059         }
00060     }
00061     s.erase(w);
00062 }
00063 
00070 std::string& urlEncode(const std::string& source, std::string& dest)
00071 {
00072     dest.clear();
00073     for(std::string::size_type r=0; r< source.length(); r++) {
00074         const char c=source[r];
00075         if((c>'0' && c<'9')||(c>'a' && c<'z')
00076            ||(c>'A' && c<'Z') || c=='_')
00077         {
00078             dest+=c;
00079         }
00080         else
00081         {
00082             char hex[4];
00083             sprintf(hex,"%%%x",(unsigned int)c);
00084             dest.append(hex);
00085         }
00086     }
00087     return dest;
00088 }
00089 
00090 }
00091 }

SourceForge.net Logo