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


#include "Charset.hpp" 
#include "urldecode.h" 
 
#include "iconv.h" 
 
namespace poral { 
	void Charset::localToUTF8(string &UTF8, const string &local) { 
		static iconv_t cd=iconv_open("UTF-8", "CP949"); 
 
		size_t nLocal=local.size(); 
		const char *localStr=local.data(); 
 
		size_t nUTF=nLocal*3;	// UTF character can be 6 bytes maximum! 
		char *UTFStr=new char[nUTF+1];	// +1 for termination char 
		memset(UTFStr, 0, nUTF+1); 
		char *out=UTFStr; 
		iconv(cd, &localStr, &nLocal, &out, &nUTF); 
		 
		UTF8=UTFStr; 
		delete UTFStr; 
	} 
	void Charset::UTF8ToLocal(string &local, const string &UTF8) { 
		static iconv_t cd=iconv_open("CP949", "UTF-8"); 
 
		size_t nUTF=UTF8.size(); 
		const char *UTFStr=UTF8.data(); 
 
		size_t nLocal=nUTF;	// local string length is less than UTF8.size() 
		char *localStr=new char[nLocal+1];	// +1 for termination char 
		memset(localStr, 0, nLocal+1); 
		char *out=localStr; 
		iconv(cd, &UTFStr, &nUTF, &out, &nLocal); 
		 
		local=localStr; 
		delete localStr; 
	} 
 
	void Charset::URLQuote(string &out, const string &in) { 
		// TODO: very unefficient code. reform this. 
		out.erase(out.begin(), out.end()); 
		const char *str=in.data(); 
		while(*str!='\0') { 
			if(*str==' ') { 
				out.append("%20"); 
			}else { 
				out.append(1, *str); 
			} 
			str++; 
		} 
        		 
	} 
	void Charset::URLUnquote(string &out, const string &in) { 
		// length of unquoted string never exceeds that of quoted 
		char *URLquoted = new char[in.size()+1];
		strcpy(URLquoted, in.data());
		php_url_decode(URLquoted, (int)in.size());
		out=URLquoted;
		delete[] URLquoted;
	} 
 
}