[报道]重新报个到,申请个新人成长礼包~
2 G+ t) g& F% [9 u' X! {+ ]$ A+ [, {
灌个水吧...
j1 w& ?+ g: a) _: h4 c* u/**. M+ {! d+ X) S) I
*@file
( ~: w5 U- b' C5 Y2 f+ ?4 |*@author Jimmy% j7 g4 g% ?( g4 a' D
*@date 2007.1.16
. m7 c0 S7 ~$ E* e) O' a5 h# v+ J*/
) |. |! a: c4 t/ u9 t8 ~8 l#include "utf8conv.h"
4 i7 s1 ~3 F3 o0 l- U- ^( L) m2 ?
) p V4 v4 |) Q' ~0 n+ ^8 S- `; u$ |
6 r0 v% P, t( n: V& {0 ?5 Q$ `
int UTF8Conv::codepage(const char* code_page)1 t6 V3 G5 q! k& \# i
{
7 ]- d& f$ t9 G n, I#ifdef WIN32" E+ j& Q+ I, a/ u. d
if(code_page)8 S$ Q1 l: a; A y9 ?$ _
{
$ q7 Y8 Q8 g- n! C% ^5 }8 l4 h return GetACP();//既竚箇砞
" [% T. }- q3 g, F* B/ I }
5 D: l w. n) l8 i#endif1 Y# `# P4 G" }6 c8 V
return 950 ;//taiwan acp N4 g1 _ a' n' b; }5 [
}
* m3 y- h5 e( e6 n
9 I" O5 F& W3 [0 u& h9 ^- V& R {! x8 U' r7 P+ ~
int UTF8Conv::dbcs2wchar(const char* code_page,/*in*/const char* in,int in_len,/*out*/wchar_t* out,int out_max)
, ^2 L7 o3 G! m& ~# ~( ]) n{6 V& x; W# x5 |5 u
#ifdef WIN32
4 V1 |0 t! I2 Y% |3 `5 O return ::MultiByteToWideChar(codepage(code_page),0,in,in_len,out,out_max);1 J7 V! H3 `. u7 o* S% U
#else & h+ \+ _4 q/ r3 x8 O8 t, L
size_t result;
; p( I( v: F l$ h2 c1 |' ? ? iconv_t env;, A8 V* w" q' z# P) U6 p1 w/ j( ~5 j5 V
env = iconv_open("WCHAR_T",code_page);
; I4 b' Z) h9 f+ M7 R( S' M9 g result = iconv(env,(char**)&in,(size_t*)&in_len,(char**)&out,(size_t*)&out_max); [6 `' l" {8 U3 S
iconv_close(env);
; y+ N1 C& b$ L b' h return (int) result;. k; J, ^5 b6 g8 Z; y# w
. O0 f* ~- P) l, v. p1 \
#endif
2 X2 [' x! @0 _" K}) X3 }7 M. x' V m: I
0 A; P. O7 L; z$ b7 ~
int UTF8Conv::dbcs2wchar(const char* code_page,/*in*/const std::string& in,/*out*/std::wstring& out)/ w% |; \: h0 |3 X+ `3 \
{
5 w! \- \. A2 i* I+ ?3 h int len = in.length() + 1;
, U! z! y r1 ?/ W1 L# n5 N7 L: s" Q0 P int result;
/ A* T3 G H: ^* G! x1 Y wchar_t* pBuffer = new wchar_t[len];
- S! P% q) o3 W" Y5 \- m memset(pBuffer,0,len*sizeof(wchar_t));" L0 `: f0 I8 Y9 p( f4 c
result = dbcs2wchar(code_page,in.c_str(),in.length(),pBuffer,len*sizeof(wchar_t));+ r: g: F4 V% y3 d/ d- \7 ~
if(pBuffer >= 0)
0 [) ]& J# T+ b% u- q {
* X1 j8 g! A! s" D: o5 h( U out = pBuffer;
% K# d: E3 }/ e$ c8 g7 l- L }0 Q4 R: _ e" X+ k# ~
else
! M% I; `! G6 T/ @' ]6 M( n {
+ |) J5 J% m( i out.clear();
) [5 e& C% {/ m( x3 y. j. F6 O }
) T; e% _! T* a# r& j" t ? delete[] pBuffer;- {8 |) G* @# e
return result;
/ L% x9 w3 U7 [1 @9 y$ l}1 S1 O9 C% ]/ F6 z5 Q
3 M# C6 ?1 r5 G% zint UTF8Conv::wchar2dbcs(const char* code_page,/*in*/const wchar_t* in,int in_len,/*out*/char* out,int out_max)7 K9 J, t, t2 w7 p( ~
{1 t) k- m4 D4 p! n+ q' \
#ifdef WIN32" T; C" o: ~2 U* ~
BOOL use_def_char;
' C8 U/ c- E0 e) `# F) w use_def_char = FALSE;
* G' G z' e% y0 a# d1 I return ::WideCharToMultiByte(codepage(code_page),0,in,in_len/sizeof(wchar_t),out,out_max,"?",&use_def_char);% { y# }0 L) |% N K, b! u3 J
#else
9 K# _: q" e: b5 N+ J7 l size_t result;/ F6 ]7 X& D8 p$ P$ V" g- A
iconv_t env;
4 C) l+ K) U) S, Z- c% Q4 g1 c env = iconv_open(code_page,"WCHAR_T");
Z2 ]9 M$ q/ M v4 e result = iconv(env,(char**)&in,(size_t*)&in_len,(char**)&out,(size_t*)&out_max);3 F6 z ]1 @8 d+ I* E$ Z. w+ v
iconv_close(env);
6 }. E3 T. C6 ] return (int) result;4 O+ N" [0 \" m" A' _. ?+ L0 d4 S
#endif
. I7 i. `! ^6 @: j}$ O) |% o0 V2 k) X; w
9 l+ s8 I. \6 v6 u8 f, C4 s
int UTF8Conv::wchar2dbcs(const char* code_page,/*in*/const std::wstring& in,/*out*/std::string& out)* N& f9 l# U: q, I+ D) L, W% e7 T
{
4 P2 ^9 D% V( z$ W" A- g5 _+ o int len = in.length() + 1;+ w: T6 S( M6 h. f" n
int result;1 @7 g b# x9 B
char* pBuffer = new char[len*3];9 ~" I5 l8 R3 ^: W8 y
memset(pBuffer,0,len*3);: }1 m% e/ `1 D. h
result = wchar2dbcs(code_page,in.c_str(),in.length() * sizeof(wchar_t),pBuffer,len*3);" m* z/ w: X) v
if(result >= 0) K/ G% e! B3 o/ \4 p
{
+ V) P/ }; u4 J' w; p* c out = pBuffer;
2 }1 ^' S& ^9 E q) ~ }" F. |; v9 s U; O" T+ ]
else& \* M3 m! S* T' c; \
{
) r& U9 l' f! f2 F [& j5 k0 s out = "";, i Q- `0 h) B, v0 v- o3 b( I p4 l
} q9 ^) E+ d" X
delete[] pBuffer;
$ |. a0 d9 C9 C9 Z ^& X! I return result; e# @7 S1 K h) Y6 ~
}. G7 b8 }# i9 p& y
7 o1 Y2 i$ x- A0 Q% {- J# X1 b5 A
int UTF8Conv::wchar2utf8(/*in*/const wchar_t* in,int in_len,/*out*/char* out,int out_max)
' m; v2 X; u a, m/ e( y{# f: P: q4 m h/ \/ }8 d4 M
#ifdef WIN32- w. Q% I4 i6 I# Z: @9 W& f1 [3 u% J
. D- m, h! v! ?* s( a2 R
BOOL use_def_char;
2 w- l* i* H. ? ~9 a0 s use_def_char = FALSE;# U7 |; l+ Z& ^! _2 [0 N
return ::WideCharToMultiByte(CP_UTF8,0,in,in_len/sizeof(wchar_t),out,out_max,NULL,NULL);
1 T" I/ [# q ?2 f5 ]- H0 Y#else/ N7 g$ J" D1 @; C; s1 M* [9 _& @
size_t result;
) K4 f( d' y% s iconv_t env;& s( a- X1 h' e% R7 R/ p* w4 d
4 K! S4 S# S9 {1 b# x
env = iconv_open("UTF8","WCHAR_T");8 y* T; @: C) q9 j
result = iconv(env,(char**)&in,(size_t*)&in_len,(char**)&out,(size_t*)&out_max); ]" L1 E1 [: d2 }, I; i ^
iconv_close(env);; E6 I" y5 S7 J" |1 [0 A/ E
return (int) result;
( T) {' R! X. ?, m5 H#endif# S9 b; P! T3 D( w7 ^) o" @
}/ m" U! T! a! }# \
2 V( o$ {& u/ X# e) Z6 E
int UTF8Conv::wchar2utf8(/*in*/const std::wstring& in,/*out*/std::string& out)( t4 f# O" x! R2 d0 B& V3 @/ m
{
- @! i3 H9 B/ O# S( B8 m int len = in.length() + 1;
0 b3 a; t: _3 t' }2 j int result;
7 s- D/ M, }$ x% T3 G# E char* pBuffer = new char[len*3];: C. z% O8 [6 e" C; P
memset(pBuffer,0,len*3); 8 `$ |* n, h) X4 S" V( q7 z
result = wchar2utf8(in.c_str(),in.length() * sizeof(wchar_t),pBuffer,len*3); / \* j8 V0 Y. a
if(result >= 0)
, F9 \1 i' U7 g4 ? {
- n/ V: ]1 m4 j; e7 X1 N0 O1 U out = pBuffer;
" P5 i2 T6 B: [9 A2 Q }% S/ l$ v. b/ Q9 n9 p' F/ m U
else
{4 t3 z, w' n( A {
, D) R& D9 a7 r out = ""; P6 |# i/ |5 z+ D, o1 D
}9 E3 s3 M8 h. V+ I# R* r) [9 F1 a
delete[] pBuffer;
1 B7 x6 R7 X; w; j- p/ j* g return result;6 s c9 ?8 D0 a9 z" c7 D9 \) i
}( S& `- v& X* A! f$ G0 }
* k. Z U1 x1 Q% W# i) D; s8 o7 I
int UTF8Conv::utf82wchar(/*in*/const char* in,int in_len,/*out*/wchar_t* out,int out_max)
% I2 S' S5 ?& P4 p6 _( h- W{- B; A6 N# O( N- I) P
#ifdef WIN32* [6 \4 |# [, [1 H) \
return ::MultiByteToWideChar(CP_UTF8,0,in,in_len,out,out_max);' j" z7 p7 _7 z, B# F) G$ s4 g6 z5 h
#else
) a( `. {1 H: }3 V3 o! X/ I size_t result;) ?: C E! D* g& |3 f
iconv_t env;
# d' M+ V( f2 J env = iconv_open("WCHAR_T","UTF8");
8 R1 L& k" ~8 M! J( [9 p! n+ ? result = iconv(env,(char**)&in,(size_t*)&in_len,(char**)&out,(size_t*)&out_max);
) j5 @; ~5 N0 O* s1 F9 G iconv_close(env);, A; J, m, G: i2 C2 R" ?: S0 I: U
return (int) result;: }- ?- }, L/ ?8 A2 v% r
#endif8 z7 u7 G: y! t
}* o* s% k" b* o
' `. _2 k7 O0 i5 F' F# nint UTF8Conv::utf82wchar(/*in*/const std::string& in,/*out*/std::wstring& out)
+ y% @/ j. h* p O$ z( z7 w* G- o8 C{
, L( {/ q6 a! O% S2 b9 b! M int len = in.length() + 1;
( `8 Z2 C: `2 V, D5 {" r int result;3 T% `+ Q- `! ]; Q8 z+ ]5 q3 T: B/ |
//wstring temp;
8 H* Y" ]. n0 ?4 R1 `: n wchar_t* pBuffer = new wchar_t[len];) I$ p6 T2 d; N3 B, Q
memset(pBuffer,0,len*sizeof(wchar_t));
* O% {8 N: }" A$ l2 L result = utf82wchar(in.c_str(),in.length(),pBuffer,len*sizeof(wchar_t));2 g) x) v! f; I7 k' r/ }+ n& W
//printf("utf82wchar result is %d,errno is %s\n",result,strerror(errno));' W$ J9 j! N$ v, h8 S# i
if(result >= 0)1 N, W# m: r" B$ ]; Q7 k$ O
{0 A f# `7 l/ G. B3 S
out = pBuffer;
( T; d) o3 Z9 ^9 B# I7 b. ` }: F; U$ C1 [4 `# Q ]% z
else
& H% _/ v( i: |2 X {
% D" h" {! F* Y& L0 M out.clear(); 4 P; |& M) H9 M8 |
}
1 f" Z4 {! Q- H$ E8 [2 `" { delete[] pBuffer;
& J2 ~/ }' Q& O" F5 p8 ?4 W1 r return result;' F. o( c5 ]: c. a& F. B. r
}
/ @7 g4 X" V0 N+ @ H0 s( E0 X1 Q$ ]' ]. \+ m% t) P
/**. Z+ Y0 ~" W6 E
*@file
7 M% x- M: d$ ?$ g*@author Jimmy6 k$ V1 ^3 y# D, P, J+ i$ V
*@date 2007.1.16
% i% _ V+ z' [# A7 ~: ]*/
* Q& S- S2 }; ?. p; j0 K2 _#ifndef _UTF8CONV_H_1 K1 R; h7 \7 v$ V7 p9 ~ }
#define _UTF8CONV_H_
. E: O; |# a+ P- O8 ~& W8 r1 j2 f( D0 u/ H) h
3 j( s- J: v9 s
#ifdef WIN32
$ M8 g. G# I1 x! Z) w#include <windows.h>
7 N ^1 L3 A; v& I#include <winnls.h>
8 y0 {2 |' {2 G7 |. i6 y9 X" ?#else5 `+ ?% s$ S9 g9 I
#include <stdio.h>
1 \7 S' M) `) H$ C, }+ c#include <iconv.h>
5 O! L1 T' l/ j2 [#endif8 N2 x6 K4 Y9 H3 Z0 O
5 a2 ]/ n6 I. Z" Z+ B#include<string>3 U/ ]* ]5 r$ O' R% X" y3 p
3 _3 k* [8 y8 F; Q |' q$ p! v* Q" p7 a& H
class UTF8Conv
/ ]+ w3 B \" S$ } {' N/ H( x3 t% U" ~3 l
public:
) x% ?% I$ a3 T6 O( g: m static int codepage(const char* code_page); c- M: A% ~7 a8 }
3 B# k$ `( j- s
static int dbcs2wchar(const char* code_page,/*in*/const char* in,int in_len,/*out*/wchar_t* out,int out_max) ;
( B0 ], y2 w* A7 Q! j7 D
/ K/ g+ F' K! L static int dbcs2wchar(const char* code_page,/*in*/const std::string& in,/*out*/std::wstring& out);. p/ J9 D0 ]2 h2 F$ h
* [6 l5 G8 E% b" K7 i static int wchar2dbcs(const char* code_page,/*in*/const wchar_t* in,int in_len,/*out*/char* out,int out_max) ;" L- }9 x) \* U
# F' V( B$ ]6 m- ]3 v O+ J$ C
static int wchar2dbcs(const char* code_page,/*in*/const std::wstring& in,/*out*/std::string& out) ;
3 h( P. m0 Y, f
& A2 k$ T8 N/ P static int wchar2utf8(/*in*/const wchar_t* in,int in_len,/*out*/char* out,int out_max);
& i3 E% i! U5 Y1 ~5 d# J: g/ Q5 T: e
static int wchar2utf8(/*in*/const std::wstring& in,/*out*/std::string& out);
! m( M4 b5 N! d' f$ J+ [* h9 C- \) D, |6 e* p! j
static int utf82wchar(/*in*/const char* in,int in_len,/*out*/wchar_t* out,int out_max);. [7 S, ?* z i+ Q2 _% v0 p, w
- X! l0 ~- d: b+ A4 d3 I! i% {8 w/ o static int utf82wchar(/*in*/const std::string& in,/*out*/std::wstring& out);
4 W8 m. Q b: |1 h! q};
; B% _2 `. O$ y) a. `0 D& b5 }& ^, U# q" l9 a/ L5 }% X$ E0 i
) o% [7 t$ T, j$ o2 e. ]# S# |7 R
- D* S c: ]( g
8 g3 a" c- ^0 ?" t V, ^: j$ _
6 M( \! b; X! w) r& R6 T: x- r7 _8 C* ^* ^' H2 Y+ q e. b. @
# Q* ` Q4 w* R2 K- x9 R6 s3 i9 d J, {0 d* _
: r" w8 n' V' w$ R+ s7 u
1 s3 f& }2 j4 Z/ v
) y0 N8 k( B& c1 M" W# Y9 p" h$ g, F$ @6 A H0 v9 F
) T' d0 s4 p4 o, J0 z
* ^: _* U& {( l [. D
% c ^, }, j2 _2 B, p" U
' X6 [+ t h" g: c8 B5 z& `7 }7 B, i. o5 {7 i
/ l5 f, K3 A( l6 @0 y7 I0 e
4 k; b% A9 ^$ j
9 l5 x; k" G6 B" ^
1 m* @" V6 T% y& m" Y
& i; B0 N* d0 Z" P! N( ?3 a! p, {) P7 V
: D6 w0 s% e5 |& K: W6 J1 K
( S" _4 t7 N ?' P( m
- h. W! L/ ?8 |( H2 V+ h! T1 D) M) y6 C. {2 W" n
1 \( V1 C% Q' F+ \8 [$ ^
* ~* z5 i2 X1 P7 D2 @. [
#endif |