[报道]重新报个到,申请个新人成长礼包~+ k+ @( D8 C. w' C* b; |( g
- s; i$ h! E2 i* y6 \- W灌个水吧...* r$ w6 i, E) C D0 A- c
/**$ \8 |9 J& }4 `, D4 J2 q& c% T$ H
*@file
|/ U- {* p4 X6 Q6 O) f; h) B*@author Jimmy
; P3 C& o9 o/ S6 N( j*@date 2007.1.165 l7 W8 E+ L) u j
*/8 B/ a6 U' A$ P0 `" m+ ]
#include "utf8conv.h"
+ c8 a) j9 F! y* V* } j5 [* `: W, k% G
- I. i' \# {( t+ X: R( m
: c/ j5 n7 K5 L( [0 V* N7 u' a! U. pint UTF8Conv::codepage(const char* code_page)# Q& W7 w. _# u! |7 A% |( ~" O6 K
{ " N" h- s! L& Y! s; Z
#ifdef WIN32
5 f$ n. \$ _! L: V if(code_page)( B% x% i v0 N/ M" A7 ~& y
{ " y: V9 |3 e7 ^
return GetACP();//既竚箇砞
" C# F) J! S8 j4 J$ n! \* @ } ( J) Y# |9 p' `3 Z: z4 G
#endif
! ]: y2 H8 \4 x! C7 ]$ S2 T return 950 ;//taiwan acp
( d7 j1 q; P: K( U2 O}
( E' u+ m0 H* h; a5 O* [7 o; l1 n
" g! p$ J" Z$ b1 F: q0 @
, X* M8 K" o$ ]3 S2 qint UTF8Conv::dbcs2wchar(const char* code_page,/*in*/const char* in,int in_len,/*out*/wchar_t* out,int out_max)
% y2 P, X% s# \6 |& s{, T$ W, h5 x" L7 p
#ifdef WIN32; L8 w6 m. Q: ~# g1 s3 d
return ::MultiByteToWideChar(codepage(code_page),0,in,in_len,out,out_max);# l* h$ J! H! ]* l3 f5 `& Q0 V0 T
#else $ z) W5 Y1 e9 I# x
size_t result;
$ v" q; ^6 l9 j; ]7 ~# s* R iconv_t env;9 B0 A8 E+ [; z7 {3 M4 ]% y
env = iconv_open("WCHAR_T",code_page);& P0 ]+ Z" b2 E: z" E
result = iconv(env,(char**)&in,(size_t*)&in_len,(char**)&out,(size_t*)&out_max);
2 v* S1 @5 ]8 ?2 ]4 N1 ^ iconv_close(env);
+ }7 k; ?2 K i3 e, ^$ g return (int) result;# N. B5 f' B; i7 G
3 o6 h) o+ c5 B* d& l. c1 V; `$ Q
#endif 6 z) z) `" d* l X2 n2 L
}2 t; _1 ~+ q [, {
# F' {! `" o! mint UTF8Conv::dbcs2wchar(const char* code_page,/*in*/const std::string& in,/*out*/std::wstring& out)
0 F, I; @# q" e6 a( V( c{0 ]2 G/ i5 s1 O* n3 S% B' A* B I
int len = in.length() + 1;5 C5 J' `' f4 G1 y/ \
int result;
1 x6 ?& j; M3 N- \ P* T W wchar_t* pBuffer = new wchar_t[len];
7 m( m1 l# s& f7 i$ ], {- y: c memset(pBuffer,0,len*sizeof(wchar_t));2 u6 ]3 c! M( W
result = dbcs2wchar(code_page,in.c_str(),in.length(),pBuffer,len*sizeof(wchar_t));
, h9 X7 }5 ^4 g- E, Y% H U if(pBuffer >= 0)
M3 @: }4 k9 f ?) t6 x3 y7 P {* H( G7 x, N o3 z6 D6 G
out = pBuffer;
1 `# Q1 S2 U; T5 b5 Z }
7 o8 x5 A2 w7 i! X. [ else
5 }6 c7 c. m4 f {2 b# \8 g9 c& f% u
out.clear();
7 w, f6 P: c& ^9 q& ^* N }
Q) ?3 L( ?6 P6 _. [' l4 c delete[] pBuffer;
: t5 t5 A5 n+ g0 B return result;
1 F5 n7 w' l2 w6 w2 \3 N}: {2 K" x2 J2 j3 v1 P
% M6 v' p2 U2 t. ]7 x! Z) s
int UTF8Conv::wchar2dbcs(const char* code_page,/*in*/const wchar_t* in,int in_len,/*out*/char* out,int out_max)
, V6 \& D7 X, [# l7 x! B& C9 ]{
- _2 ~7 c V4 \+ [& o! f* `#ifdef WIN32
$ A7 V& j. f+ O6 `) f BOOL use_def_char;2 y5 W8 L& c) b$ D4 d
use_def_char = FALSE;1 S4 F2 L& ]4 `9 F" K
return ::WideCharToMultiByte(codepage(code_page),0,in,in_len/sizeof(wchar_t),out,out_max,"?",&use_def_char);# o9 F; Y8 b6 t8 m" m: Y
#else
) y: C" i* z; e" v* u size_t result;4 b9 x% P. W8 \
iconv_t env;
+ ~; J& } ]! }% ~4 } env = iconv_open(code_page,"WCHAR_T");
) p% _ [, E* x* Y5 Z" x result = iconv(env,(char**)&in,(size_t*)&in_len,(char**)&out,(size_t*)&out_max);
0 S9 i( X+ l7 D" V0 j iconv_close(env);3 C5 r% o+ ^9 [8 E
return (int) result;
9 A1 Z% c3 f+ _( u" k8 t: L, z9 b#endif
d: ~, p0 n) @% B}
2 w1 B- B3 [' y# Q% x# K4 b" e
9 {* e/ ?0 T3 w* W/ f( i; kint UTF8Conv::wchar2dbcs(const char* code_page,/*in*/const std::wstring& in,/*out*/std::string& out)' \6 ^' u1 Y2 i# I% Z) B
{: B! ^) {2 ^" |2 m4 ~ {. z& ]" s+ h# W
int len = in.length() + 1;
* {7 }3 X( A1 G9 N& T, B int result;
6 o/ H" Y% J% F char* pBuffer = new char[len*3];
; X+ ?) l0 W- p memset(pBuffer,0,len*3);
) d; N& K5 [0 M4 g result = wchar2dbcs(code_page,in.c_str(),in.length() * sizeof(wchar_t),pBuffer,len*3);( R7 Z3 o+ G3 H% I2 i4 u
if(result >= 0)4 P1 t+ F- W, V& Y% C; D. }
{ D9 c# a. ~" m/ E8 T. a! O0 I
out = pBuffer;: l$ _' u" `: k) x4 s7 P- k) S
}& S+ a' A8 J# [, s, t! `. |
else+ t/ n% E5 G" N f9 M
{8 y% T( @1 N7 t/ i: i* r( ]
out = "";
6 j3 e! t4 r& q! o7 z }/ \9 P! v9 }1 D0 f, s9 x6 v
delete[] pBuffer;
/ R+ F8 R; C# v return result;5 K% d+ I8 ]" V( k( Q5 T
}# S U7 I( _7 r9 Z, y8 _! q
9 y6 O! N- U' H. s% x- Tint UTF8Conv::wchar2utf8(/*in*/const wchar_t* in,int in_len,/*out*/char* out,int out_max)2 d) K( C0 l4 e+ [5 Y8 V- [
{4 S2 I# L& m# ?2 s# ], W/ E7 O
#ifdef WIN32
3 A% l* W8 B; d8 ^+ W5 s3 d z: a# D; `- g: v* M
BOOL use_def_char;! @6 w- t: F0 l( e
use_def_char = FALSE;
' u0 L" n5 f7 }9 Y1 j) w3 m return ::WideCharToMultiByte(CP_UTF8,0,in,in_len/sizeof(wchar_t),out,out_max,NULL,NULL);
# g1 b% l# l- w' y#else: ?6 i6 g- X. p8 o) v
size_t result;8 K/ M- K9 t/ j4 c# ~ u/ V* |& \ Z4 k
iconv_t env;; r7 a# R0 {1 ~2 T7 ]( f
$ v( M4 I/ p4 d/ h& e0 c env = iconv_open("UTF8","WCHAR_T");( }3 A) d! ~! S; M' m3 G
result = iconv(env,(char**)&in,(size_t*)&in_len,(char**)&out,(size_t*)&out_max);
9 S; `( z! h/ k/ Q- d# [: E9 l iconv_close(env);; k; w7 v- {6 b, L% \! C: U
return (int) result;
& }6 B2 o# b% ~$ F1 `" W& I+ I#endif
) F0 x: B) N- l- q! i0 `3 c}5 f5 l# [. T6 x; o0 Q- b) ~( S
H! J. g% p9 Y8 c
int UTF8Conv::wchar2utf8(/*in*/const std::wstring& in,/*out*/std::string& out)
( w' K4 h2 r/ v* a& F{3 R8 a+ L3 ~8 s" E9 x W% \
int len = in.length() + 1;4 c/ i9 N j/ X! j5 {
int result;
* z& F* o1 y+ b+ K& T6 l char* pBuffer = new char[len*3];
1 {; J$ F+ s+ p# m s; e memset(pBuffer,0,len*3); 0 r6 C- e! x8 t* m+ J
result = wchar2utf8(in.c_str(),in.length() * sizeof(wchar_t),pBuffer,len*3);
! p! F: P- P! e2 R; \! e) s4 t if(result >= 0) C, ^* ` u7 b {
{
& E+ o) r2 l9 y6 f: n$ @" E out = pBuffer;( |) Q: O6 m' S, O2 c
}
5 A5 O. \: s' Y, i2 b' ~ else/ I* a& v9 f7 t4 Z8 N E0 I
{& W, F# g9 j2 L+ [3 d2 `, c
out = "";! z. f9 x$ R7 ~. @+ n
}
! f5 j7 G1 h! v# c/ |; E6 r8 T1 B0 k delete[] pBuffer;
. K6 }# [; n# B- w7 H return result;
q7 T! K. D6 `}* H |2 v! X# z# `" A
' u: l1 U& v- P+ v4 v
int UTF8Conv::utf82wchar(/*in*/const char* in,int in_len,/*out*/wchar_t* out,int out_max)9 J5 P4 B U) z* p8 r/ e
{, x8 I1 L+ A+ k8 p: h& ?9 M
#ifdef WIN32* i, n& h% h! J' z- c; s( h
return ::MultiByteToWideChar(CP_UTF8,0,in,in_len,out,out_max);) m2 F1 v: y7 X/ w* k+ ~( _
#else& W$ y5 u$ W7 L' w9 ]
size_t result;$ u0 V* D9 e& e- x. P+ n3 ~; p% \
iconv_t env;
& [% [2 y. c3 j1 _ env = iconv_open("WCHAR_T","UTF8");% h e, `/ N/ h* B; i, ], y& _8 O
result = iconv(env,(char**)&in,(size_t*)&in_len,(char**)&out,(size_t*)&out_max);
( ^. s, T- a {+ Q! O+ o, o' @4 s iconv_close(env);
( V: t S0 p9 v3 [% Q return (int) result;
1 Y' L- r4 v( Q J#endif
8 v% _& O2 }2 k; D}! c* _3 E+ S" j/ P
- X% U2 M1 \% ~: w
int UTF8Conv::utf82wchar(/*in*/const std::string& in,/*out*/std::wstring& out)
) P: Q" z- W* a0 h& Z{5 h: M' J6 y ^8 j( K% m
int len = in.length() + 1;2 ^! x; w7 V( l
int result;
# N2 A! D, X$ j' X% r# X3 U% h //wstring temp;
/ T, ?* I, l7 {1 Y9 b, i; c% u wchar_t* pBuffer = new wchar_t[len];& {- f0 \0 I9 `) k( t& \
memset(pBuffer,0,len*sizeof(wchar_t));
2 N+ D7 u! Z x- Y5 S3 n' O result = utf82wchar(in.c_str(),in.length(),pBuffer,len*sizeof(wchar_t));' Q& x) z( ^ z! G/ W- ]! U- b
//printf("utf82wchar result is %d,errno is %s\n",result,strerror(errno));
' b9 h, f4 N% f3 t8 V, P7 Q* h9 _ if(result >= 0)
W5 n' E+ k) x) J, | { {# Q1 N! Y. c% a1 l5 @7 E
out = pBuffer;
+ W; k$ E$ T w$ F5 H, @ }4 x5 h; e6 ?9 H. e: U
else0 p' v: y/ q2 H3 B8 _$ k/ {; ?! b
{
9 J" O) x& q' g3 X. k out.clear(); 3 H2 p9 ?# }/ j `2 B1 X8 _8 G
}: `% K5 Y& q1 p' H
delete[] pBuffer;1 \9 x- N+ Z7 k( q1 |5 z6 S
return result;
# m/ v' o( P4 x8 u$ H# I% Z: T}
+ `" R$ I, m7 y$ ~- t' e+ F: @3 R+ M: s7 J- q8 \$ f
/**0 R$ I, S' L& A( T
*@file 7 g% E+ R$ r0 l- q |) E3 h& o5 Q
*@author Jimmy9 \. }* k! v) X% c3 S% ~
*@date 2007.1.16
& p% e3 C9 h: O) _*/- U8 c( r- K" v
#ifndef _UTF8CONV_H_) F. D, Y F+ e( z
#define _UTF8CONV_H_
" E6 B) R. H1 Q6 J6 U( K1 p5 y& h" ~$ n
+ v7 L; Y9 F0 U#ifdef WIN32
! e+ V2 y5 ?$ p% s D- b#include <windows.h>& A" c' [( z9 s* m! _9 n
#include <winnls.h>
" E. y9 m5 D$ M; B8 b- \5 \#else) R7 R4 {# C9 O: n2 Z: w4 s- w' r& B
#include <stdio.h>+ C- C3 o3 U4 {* m- b' ]% u( ~- a
#include <iconv.h>5 t& P: E$ m: P a' z, K
#endif* z5 K( v5 j, k" P% ? F
' L! E5 m6 K$ n1 P3 y
#include<string>; J+ D9 X0 v* b P
) N/ C+ \+ X) |. g9 \/ F) I
7 ?0 }6 z( C# Cclass UTF8Conv, [% {; i0 p6 b4 d0 M5 G! S4 f- x
{
0 I! O5 H* ^2 C( X Y Q public: f5 e& \& x4 T: p" B5 T1 I
static int codepage(const char* code_page);
( w( S, L6 ^: N: Y, ^/ N
& F8 E5 z2 c, @% d, l; R. D) s static int dbcs2wchar(const char* code_page,/*in*/const char* in,int in_len,/*out*/wchar_t* out,int out_max) ;" T1 D. q) B2 s1 B6 b( y
+ m- |! P" x# N
static int dbcs2wchar(const char* code_page,/*in*/const std::string& in,/*out*/std::wstring& out);( ]' ?+ u8 U Y: J
8 N1 ?! N% {% Z8 j( V( ?
static int wchar2dbcs(const char* code_page,/*in*/const wchar_t* in,int in_len,/*out*/char* out,int out_max) ;/ T% v3 z6 h* H' a' Y1 e9 n- }
5 R) [4 F) y# |# J( D* ~ static int wchar2dbcs(const char* code_page,/*in*/const std::wstring& in,/*out*/std::string& out) ;
* u( @) D7 T. e; O) _# T- f' H1 b7 d1 \! g. n/ Y" E' `5 ]
static int wchar2utf8(/*in*/const wchar_t* in,int in_len,/*out*/char* out,int out_max);- T$ N1 V) f, P4 ?9 @: f8 {' U% b+ C7 `
, ~* I9 u$ w# h/ k static int wchar2utf8(/*in*/const std::wstring& in,/*out*/std::string& out);* i* S3 [+ n! l+ v/ I8 r+ o" h
, s z' w. Q, _) k+ G static int utf82wchar(/*in*/const char* in,int in_len,/*out*/wchar_t* out,int out_max);
8 T- t+ {+ G* C0 G6 ^; S1 U2 }. C; [- u& X( [+ X0 a
static int utf82wchar(/*in*/const std::string& in,/*out*/std::wstring& out);
" l- G- ~, u) V- P5 O2 j. w$ U};" U" j! X- u6 t: ~; J
9 ]# v7 ~( ?) L* I
3 r0 ]4 }8 Q& Z' e) Z" O' b
+ L; Y1 _# `7 x; `* V( g* T8 l4 c: a. v3 P+ p: `
; G9 a0 \* u2 D/ U7 c' ^1 O2 j$ l [
% z, C! c# ?) A% j: A: p- M j2 T8 Y1 z
( d- q$ u9 g: h$ X" E5 v
5 e r6 C0 W( v, D z! g& |9 p, Y2 R! b$ x. F. d3 w* h
$ s% d) @: p8 ]4 A: h% _0 U
& \% W- C7 i/ I8 ]
+ X/ ?$ o# Q7 e# ?0 l) T' \
3 R; Q) M" W0 y6 e
" g' t+ X" N. c- y
! p( T x: i7 b( U+ g5 S5 q
% \8 Q) B R: q! ?2 w
0 r1 J$ r' a+ \% a r8 P; u0 s- |4 \$ |
, u. y/ r) C3 e7 ]+ r& h
5 g& B9 `7 R1 w( Z% y( E2 B5 U
" G% O3 Y* b% C. P- o+ r. Y; m, S- ?' g# B8 w2 ~) ?' M" O* O
$ d* K9 \! v3 v$ k% h
* y( j# i+ v9 Z- t2 u
- N+ y0 G" p9 ~2 ^2 D" X! ]
2 f' R7 I9 h) Q; Z
: c4 w! \+ T/ M' L6 J: n
3 T% X2 f! Q7 T. P#endif |