diff options
author | Sascha Volkenandt <sascha (at) akv-soft (dot) de> | 2007-05-29 17:40:37 +0000 |
---|---|---|
committer | Sascha Volkenandt <sascha (at) akv-soft (dot) de> | 2007-05-29 17:40:37 +0000 |
commit | ae66c91c938b8e7c6d5b62479f8b65c287fcbf1a (patch) | |
tree | 44d40f78b3778eaa53822b36a455090a43e8e416 | |
parent | 7d6b30829ff8ca1b0cf404a4945fb9ef87fafa99 (diff) | |
download | vdr-plugin-live-ae66c91c938b8e7c6d5b62479f8b65c287fcbf1a.tar.gz vdr-plugin-live-ae66c91c938b8e7c6d5b62479f8b65c287fcbf1a.tar.bz2 |
- function StringUrlEncode
-rw-r--r-- | tools.cpp | 38 | ||||
-rw-r--r-- | tools.h | 1 |
2 files changed, 39 insertions, 0 deletions
@@ -1,3 +1,4 @@ +#include <algorithm> #include <stdexcept> #include <iomanip> #include <tnt/ecpp.h> @@ -184,4 +185,41 @@ time_t GetTimeT(std::string timestring) // timestring in HH:MM return mktime(tmnow); } +struct urlencoder +{ + ostream& ostr_; + + urlencoder( ostream& ostr ): ostr_( ostr ) {} + + void operator()( char ch ) + { + static const char allowedChars[] = { + // 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , A , B , C , D , E , F , + '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', //00 + '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', //10 + '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', 0x2D,0x2E,0x2F, //20 + 0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,'_', '_', '_', '_', '_', '_', //30 + '_', 0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4A,0x4B,0x4C,0x4D,0x4E,0x4F, //40 + 0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5A,'_', '_', '_', '_', '_', //50 + '_', 0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6A,0x6B,0x6C,0x6D,0x6E,0x6F, //60 + 0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7A,'_', '_', '_', '_', '_' //70 + // everything above 127 (for signed char, below zero) is replaced with '_' + }; + + if ( ch == ' ' ) + ostr_ << '+'; + else if ( allowedChars[ size_t( ch ) ] == '_' ) + ostr_ << '%' << setw( 2 ) << setfill( '0' ) << hex << int( ch ); + else + ostr_ << ch; + } +}; + +string StringUrlEncode( string const& input ) +{ + ostringstream ostr; + for_each( input.begin(), input.end(), urlencoder( ostr ) ); + return ostr.str(); +} + } // namespace vdrlive @@ -34,6 +34,7 @@ std::string ZeroPad(int number); std::string MD5Hash(std::string const& str); time_t GetTimeT(std::string timestring); std::string ExpandTimeString(std::string timestring); +std::string StringUrlEncode( std::string const& input ); struct bad_lexical_cast: std::runtime_error { |