www.pudn.com > fanccMSNr.src.rar > Charset.hpp


#pragma once 
 
#include  
#include  
using namespace std; 
 
namespace poral { 
	/** 
	* An utility class which has static functions that are convert the  
	* characterset of string or doing etc. 
	*/ 
	class Charset { 
	public: 
		static void UTF8ToLocal(string &local, const string &UTF8); 
		static void localToUTF8(string &UTF8, const string &local); 
		static void URLQuote(string &out, const string &in); 
		static void URLUnquote(string &out, const string &in); 
 
		/**  
		* Convert the string encoded UTF-8 and URL-quoted to 
		* string encoded local characterset and URL-unquoted. 
		*/ 
		static void fromServer(string &out, const string &in) { 
			string unquoted; 
			URLUnquote(unquoted, in); 
			UTF8ToLocal(out, unquoted); 
		} 
 
		/**  
		* Convert the string encoded local characterset and URL-unquoted to 
		* string encoded UTF-8 and URL-quoted. 
		*/ 
		static void toServer(string &out, const string &in) { 
			string UTF8; 
			localToUTF8(UTF8, in); 
			URLQuote(out, UTF8); 
		} 
 
		/** 
		* Trim the string by whitespace only. (does not consider \r, \t, \n, etc...) 
		* outstr and instr can be same. 
		*/ 
		static void trim(string &outstr, const string &instr) { 
			size_t start=instr.find_first_not_of(' '); 
			if(start!=string::npos) { 
				size_t end=instr.find_last_not_of(' '); 
				outstr.assign(instr,start, end-start+1); 
			} else { 
				outstr.erase(outstr.begin(), outstr.end()); 
			} 
		} 
	}; 
}