string_urlescape.h

00001 /***************************************************************************
00002  *  string_urlescape.h - Fawkes string URL escape utils
00003  *
00004  *  Created: Fri Oct 24 09:31:39 2008
00005  *  Copyright  2006-2007  Tim Niemueller [www.niemueller.de]
00006  *
00007  ****************************************************************************/
00008 
00009 /*  This program is free software; you can redistribute it and/or modify
00010  *  it under the terms of the GNU General Public License as published by
00011  *  the Free Software Foundation; either version 2 of the License, or
00012  *  (at your option) any later version. A runtime exception applies to
00013  *  this software (see LICENSE.GPL_WRE file mentioned below for details).
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 Library General Public License for more details.
00019  *
00020  *  Read the full text in the LICENSE.GPL_WRE file in the doc directory.
00021  */
00022 
00023 #ifndef __UTILS_MISC_STRING_URLESCAPE_H_
00024 #define __UTILS_MISC_STRING_URLESCAPE_H_
00025 
00026 namespace fawkes {
00027 
00028 
00029 /** Transform hex to value.
00030  * @param c character
00031  * @return value of hex code as number
00032  */
00033 int
00034 unhex(char c)
00035 {
00036   return( c >= '0' && c <= '9' ? c - '0'
00037           : c >= 'A' && c <= 'F' ? c - 'A' + 10
00038           : c - 'a' + 10 );
00039 }
00040 
00041 /** Remove URL hex escapes from s in place.
00042  * @param s string to manipulate
00043  */
00044 void
00045 hex_unescape( char *s )
00046 {
00047   char  *p;
00048 
00049   for ( p = s; *s != '\0'; ++s ) {
00050     if ( *s == '%' ) {
00051       if ( *++s != '\0' ) {
00052         *p = unhex( *s ) << 4;
00053       }
00054       if ( *++s != '\0' ) {
00055         *p++ += unhex( *s );
00056       }
00057     } else {
00058       *p++ = *s;
00059     }
00060   }
00061 
00062   *p = '\0';
00063 }
00064 
00065 
00066 } // end namespace fawkes
00067 
00068 #endif

Generated on Tue Feb 22 13:32:28 2011 for Fawkes API by  doxygen 1.4.7