[报道]重新报个到,申请个新人成长礼包~% k4 m# V- Z0 k: c
1 l/ ?, p2 g L v% @4 d, r, a7 ?
灌个水吧...4 `, T6 _' }( I1 H% c( o
/**
9 z$ @1 P0 ]; G1 G: c*@file
- L6 [/ \ Z- Q& p- G1 \1 h7 E*@author Jimmy9 j9 n. X6 v4 @2 G# N6 k
*@date 2007.1.16; o' F1 o% c0 B4 y
*/, H3 ]+ `5 W$ [7 a/ x5 p
#include "utf8conv.h"
2 T. _7 a1 d& D. J0 J) K- d& c6 `
9 s2 F& c+ H/ D/ l7 d1 b |
0 G; a9 l8 J" L3 T( |/ p2 j3 C4 @
" V4 u/ Z9 u0 ^# D8 U2 ]" }int UTF8Conv::codepage(const char* code_page)
7 ]5 Y) X% _5 @0 |7 r' v{ " N/ Q% O4 Y( G; q' y! y: p8 g' \5 l
#ifdef WIN32
( b3 i0 }+ ^. _' ]2 k5 ` if(code_page)1 E5 g2 _5 E. U: a2 o# d* |/ e! \
{
0 |) L+ ]# A2 R; _9 J1 P5 W- x return GetACP();//既竚箇砞% m) \! [" y) J) d5 I0 N/ }* I
} . a9 A. p) g9 ?6 e4 S: V$ I- J
#endif R4 I2 i+ o4 R g# Q) k& A
return 950 ;//taiwan acp. L3 g! }# ~& @# p* y0 [- i8 Y1 M
}3 |0 f# E7 `) G, D- ~
+ E2 O# K: J: K8 [( i
: u5 e+ Y) U U# w/ x
int UTF8Conv::dbcs2wchar(const char* code_page,/*in*/const char* in,int in_len,/*out*/wchar_t* out,int out_max)
0 w6 W1 s) [3 v% h{
_" ~" N6 d( @7 Y. k X#ifdef WIN32
. t) W) n5 N1 @) f+ \* h2 A return ::MultiByteToWideChar(codepage(code_page),0,in,in_len,out,out_max); L; e) h$ ^' l
#else
3 m$ S% x- v3 m! _, ^- \ size_t result;7 r( f5 n- D5 S$ h5 F: a! e
iconv_t env;
" v9 R( T' R: z s# I8 k4 ]! Q- f env = iconv_open("WCHAR_T",code_page);
9 q( A) h! i+ L result = iconv(env,(char**)&in,(size_t*)&in_len,(char**)&out,(size_t*)&out_max);: i- J& ]" Z7 c* K" U7 L, H6 M
iconv_close(env);' P7 D( [5 [6 |* n0 U
return (int) result;/ }$ q8 z1 [# o6 g7 D4 Z
: s% [$ P' i9 i& {3 C#endif
0 X$ f V3 _9 H* w2 N}
- R( L/ c! R8 m! E v& Z( V$ X( W8 ` ` J
int UTF8Conv::dbcs2wchar(const char* code_page,/*in*/const std::string& in,/*out*/std::wstring& out)4 t* A- {2 A! u' I
{% w9 L; V$ R+ D' p8 X5 U* b8 q
int len = in.length() + 1;
6 O0 b# L1 }% L5 i6 O5 ]8 E int result;
3 h: N+ J% |- V# o2 \) f/ o* N wchar_t* pBuffer = new wchar_t[len];
! {0 S6 r- \& o3 \+ q memset(pBuffer,0,len*sizeof(wchar_t));5 R; Q: b; U: @+ N; h7 p% V! d
result = dbcs2wchar(code_page,in.c_str(),in.length(),pBuffer,len*sizeof(wchar_t));
J$ D* S4 C1 p/ k0 y! L if(pBuffer >= 0)
7 Y% f" K I$ w {
. I- ^( Q$ O& n- ^# b- [2 U out = pBuffer;# R. e8 Q+ c& x1 G$ y% t
}# D6 u$ E( b, N9 U3 W" T. V
else
# v' X/ ?3 ~( W: S {: l/ ]3 ~* J# w# p/ a, |
out.clear();
! C4 m q6 w( o/ l: S }
5 @) g3 m" X4 ~! B# {% D1 M" r+ g delete[] pBuffer;
9 f; p- ?. x6 I2 _( R0 @. P return result;
2 ]" H: v5 u7 V" r+ ]' [}& |( r. c# t" h) x! e4 N9 w
% C9 D0 ~2 C0 f2 Uint UTF8Conv::wchar2dbcs(const char* code_page,/*in*/const wchar_t* in,int in_len,/*out*/char* out,int out_max)7 ]+ v& Y6 ~, Z
{8 ]8 O' |( V, W( Z3 u' p
#ifdef WIN32$ q7 U2 }. d' T4 l+ q/ x. t
BOOL use_def_char;7 ]( s1 R( A$ H$ K8 b" w8 Q9 W
use_def_char = FALSE;
/ X3 g! ?0 x8 A# d5 [ return ::WideCharToMultiByte(codepage(code_page),0,in,in_len/sizeof(wchar_t),out,out_max,"?",&use_def_char);
5 d8 G' r( b# k7 O" Y#else4 @& w) @( A( m# I* ~ X
size_t result;
6 a- o1 s0 L5 l iconv_t env;
8 a# V7 z( z; }1 }+ v# i env = iconv_open(code_page,"WCHAR_T");
# C$ ]/ C# G' M( I result = iconv(env,(char**)&in,(size_t*)&in_len,(char**)&out,(size_t*)&out_max);2 q. F. o+ M! P# ]) T1 F
iconv_close(env);% w f1 s1 ~; u. e
return (int) result;- P, M- \' k$ H: ?5 x/ y$ I
#endif
) M2 v$ A* T x( r- o4 A9 N% ?}
% s) j" L0 I3 c& }$ z0 ]! g; p. W$ ^& ]2 s8 A& T
int UTF8Conv::wchar2dbcs(const char* code_page,/*in*/const std::wstring& in,/*out*/std::string& out)+ l8 o: O# h5 o6 ^1 v. H
{# p! u$ D' Z. t+ l3 u
int len = in.length() + 1;
7 M: L6 Y4 V8 p [5 K int result;; i! _( D% d% M' v+ L0 v! v
char* pBuffer = new char[len*3];/ w$ ]$ c& W S: q
memset(pBuffer,0,len*3);
Y" }! I. M+ D: J result = wchar2dbcs(code_page,in.c_str(),in.length() * sizeof(wchar_t),pBuffer,len*3);
& Y4 U+ M, t* S* A if(result >= 0)
# i+ ?; F2 v- R$ ~ {
6 E( b( O( m4 A out = pBuffer;
; S# H4 H. E* }+ {/ v }1 z/ R- M1 ^, `
else
5 k) K( j) [ j( S* `; V {. X; n! ?( v* j$ y. }
out = "";% h0 `! w* w2 V. |+ Y
}8 Z! Q4 J3 f0 H! v1 b
delete[] pBuffer;
$ c# S5 X3 D) I return result;
9 j; X2 i: y. `* l4 \}
f) C( ^8 b4 M2 ?! J* P. M4 Z' ~- n' I' C1 R' T
int UTF8Conv::wchar2utf8(/*in*/const wchar_t* in,int in_len,/*out*/char* out,int out_max)
) U. s7 H0 H- w{
9 e4 }# a4 G6 n) N4 p3 K; t#ifdef WIN32
' V0 Z3 X0 D7 D6 g
. |, Q5 P" Y& @8 O BOOL use_def_char;/ H" x* V6 u5 k6 s5 O! F
use_def_char = FALSE;
+ L+ e3 i5 j) g% X) |9 [1 D return ::WideCharToMultiByte(CP_UTF8,0,in,in_len/sizeof(wchar_t),out,out_max,NULL,NULL);
3 f- f9 J, o, u4 P( Y#else( c1 j7 B" O6 a
size_t result;
3 O! U, @9 T" O: D" x/ R iconv_t env;
+ r& h2 @9 r9 B X4 s4 [- j+ z! S( J, Q
env = iconv_open("UTF8","WCHAR_T");
) |0 q- @4 u t% r6 _ result = iconv(env,(char**)&in,(size_t*)&in_len,(char**)&out,(size_t*)&out_max); / S1 N9 E. _; c* ?! F% p
iconv_close(env);
: g2 n0 A5 W1 U( Y" T) W$ m( L6 s return (int) result;
3 a5 _2 n2 E' a! E4 X2 U* s#endif
+ B% ?. {$ i$ @8 a# l/ q: ~/ |+ N( g}, H3 G; ~ k) @& g* y
! q! B) |: u( B4 F9 G7 C& O$ h% ^int UTF8Conv::wchar2utf8(/*in*/const std::wstring& in,/*out*/std::string& out)
, Z$ N: a) }+ d. @$ k v{
7 X/ V7 S1 u; G- ?; b0 s int len = in.length() + 1;
2 B5 A. G7 B/ _; J% U2 w2 ` int result;
# r3 D! w5 I5 a9 L* M7 F* } char* pBuffer = new char[len*3];( K9 M1 C5 J1 H: S8 ~
memset(pBuffer,0,len*3); 2 v% h% n6 s' o( y2 A3 Z" L
result = wchar2utf8(in.c_str(),in.length() * sizeof(wchar_t),pBuffer,len*3);
7 P) Z2 R }8 W6 K0 ] if(result >= 0); G; g) f* [3 S) j0 E4 J
{) c' u6 b6 Q" T( I# t9 m7 t4 W; C; X
out = pBuffer;$ ?$ x+ _. v$ E( `7 G
}4 Q: Y$ J* b# P2 u# u
else
! u( T8 @8 T: v# f# Q {5 z7 R% h# ?( Q
out = "";3 H0 x$ T. g! \$ l1 u- e
}: o6 l3 }) E w! N+ k, L5 W
delete[] pBuffer;
/ G3 e! g) @1 i+ \ return result;5 @- N$ k% Y$ |* o
}( P! Q/ `$ v+ t
5 h. S) X6 k" I/ ~4 ^ Y2 u- @
int UTF8Conv::utf82wchar(/*in*/const char* in,int in_len,/*out*/wchar_t* out,int out_max)
9 t0 r8 L3 y% D( u9 z' ^, a{4 g& U/ Z; Z, Z. B: @
#ifdef WIN32, S3 ~. v# n o( Y6 W9 w
return ::MultiByteToWideChar(CP_UTF8,0,in,in_len,out,out_max);
( q. U6 U! L. a* u8 X#else) x& b7 `+ q* l0 f$ M' R8 o
size_t result;
- V+ x4 O s+ m+ K5 ~ iconv_t env;9 g7 \+ N6 g2 h% I
env = iconv_open("WCHAR_T","UTF8");
* A+ ^, j0 I2 y& {4 } result = iconv(env,(char**)&in,(size_t*)&in_len,(char**)&out,(size_t*)&out_max);- H" M" a' ^4 i7 N$ t/ J5 G
iconv_close(env);
5 {, G6 W; C2 y( _" |5 r1 P0 }+ c0 H6 E return (int) result;* n- L8 m- U' ?4 d3 S8 Y- S. q
#endif
, L7 ~; Y+ c: i; A9 d9 X}
3 Y8 {+ O* ?4 \; R8 L6 Y0 F6 W/ C& o3 B& O1 F, g; i z
int UTF8Conv::utf82wchar(/*in*/const std::string& in,/*out*/std::wstring& out)/ d1 t* H+ v) y0 `- t
{
5 @, P5 S+ q" C7 X1 ` int len = in.length() + 1;$ T' z/ V7 l/ N5 p& R8 D) I# s
int result;7 N9 g' k- s- m
//wstring temp;. Q& h# @8 h; b# b! k9 p" J) R
wchar_t* pBuffer = new wchar_t[len];, j3 L3 i) h" t( a# ^ e
memset(pBuffer,0,len*sizeof(wchar_t));
4 {8 S2 H" c4 h result = utf82wchar(in.c_str(),in.length(),pBuffer,len*sizeof(wchar_t));
' d( C4 l( I% [8 \3 H //printf("utf82wchar result is %d,errno is %s\n",result,strerror(errno));
0 e6 q/ u& Z( I if(result >= 0)
) x2 Z0 j6 r+ u6 t3 B {$ {4 j& _1 w$ J* @
out = pBuffer;
+ I5 Q& k/ k" i/ A: @( b+ g1 ]2 I }& ^$ w$ Z! D x8 {3 S9 u7 W
else
" H |' f8 W* O' L% A: C {( [! H" y8 l0 X9 \ m$ ~
out.clear(); " h, _9 X( w; ?- W, B
}5 o7 Z& f8 ?$ P' K' ^8 g
delete[] pBuffer;* g. a/ C% a$ Z" Y M: k2 q5 y) {$ l
return result;
0 S! o2 L$ w8 F' M9 s ?$ o' t} Y& f) G( {' e3 x6 d. I' `
5 }& e4 U4 d+ ~) ]" Y/**+ D5 E8 |" {* S0 l/ r0 C
*@file 8 U) A: Q' [) S
*@author Jimmy6 D" o8 W' Z% b w! T7 t
*@date 2007.1.16
' v1 H2 p: h, X*/
( c) I* o1 }* I# f, k1 b0 _6 e, D" h#ifndef _UTF8CONV_H_# ]/ e. g9 e* x2 T6 K+ H
#define _UTF8CONV_H_ j7 F6 b+ l( G
& ^/ A- z8 k1 r) ]5 d
1 P9 Q2 |3 V* C# Z& W+ I* W1 o
#ifdef WIN32
, B4 g4 f1 i) N3 x, g#include <windows.h>5 I2 v5 Q5 V9 k: O) ^1 E
#include <winnls.h>
7 L) _2 t+ K2 B) C7 }1 W* p# T5 Z) @#else
5 a* M- z$ J% u#include <stdio.h>) T' U& R( a# B! u: ~" G9 A
#include <iconv.h>
+ i1 C4 D/ l O. q# ?, i! z#endif8 r' |; C: Y1 T. V( ]" ?) r7 M, X$ {
; x/ U8 W2 I3 r# ?#include<string>
7 L, ^1 [) U8 Q! n _1 r0 ~1 k0 Z
7 N8 T1 Q" d2 [( k5 `/ ?! Q7 oclass UTF8Conv4 Q) L4 F6 \9 \# U: x( d
{
0 H& a. T" @: P0 m1 _8 W public:$ o* k3 O: t1 Q/ P W. F
static int codepage(const char* code_page);$ Z; `8 v6 H" g- i6 z
" E4 T: I7 a. ~6 x static int dbcs2wchar(const char* code_page,/*in*/const char* in,int in_len,/*out*/wchar_t* out,int out_max) ;
3 d6 i% w' q `$ H
4 G7 R* O3 p) k; w/ X static int dbcs2wchar(const char* code_page,/*in*/const std::string& in,/*out*/std::wstring& out);3 f( \4 o/ J" R! k+ M
+ y7 U; \8 Z. }; f) p4 R
static int wchar2dbcs(const char* code_page,/*in*/const wchar_t* in,int in_len,/*out*/char* out,int out_max) ;
8 J2 S: e4 Z9 H
' r4 u9 z" S: o/ D5 X7 N( L static int wchar2dbcs(const char* code_page,/*in*/const std::wstring& in,/*out*/std::string& out) ;
, b' y( P% a8 j; o1 B* g' b! G1 N# i/ y8 E- s
static int wchar2utf8(/*in*/const wchar_t* in,int in_len,/*out*/char* out,int out_max);
) \" P7 K e$ P" q* E& y* C7 h+ o, i; F! w0 K6 A- C0 y7 i$ a6 P
static int wchar2utf8(/*in*/const std::wstring& in,/*out*/std::string& out);
1 e" Z$ C' ?4 S9 s: R/ R* d
* ^4 _5 F& n* F" m static int utf82wchar(/*in*/const char* in,int in_len,/*out*/wchar_t* out,int out_max);
: l, r6 [1 B" _3 L( o. T1 L/ b% {
static int utf82wchar(/*in*/const std::string& in,/*out*/std::wstring& out);
/ z7 S9 h7 |& @; }4 Q& `+ G};
' l: S5 S/ V5 R* G$ `( S% R9 O4 n" A
4 |) Y# A% n& j% d/ Y, Y; Y6 X# N* ?+ _2 i2 ]5 F! Y2 _
$ n% n6 c1 \7 W4 H! } o8 K
( l8 _" d! M" i* E4 {" L! C- K( ^* e9 o: I, h. z% S) @
/ O! `9 v8 w, |. A3 Q, ^# l' e, s" e& j/ Y! Y
) t- Y& C- R9 l
9 B: F l) b, U a" r% l/ h. L( n
; d2 R" R; Z5 P
2 a/ u7 X+ Z4 m7 \
- J. z! j1 \1 B1 W8 }; `8 e& j
9 O& S" I# d& F$ [
* V+ V2 t6 b' U
. {4 }; E9 ]+ L5 z s% W
# b: T# \% w$ x4 @8 C0 P
/ y: E+ U3 v, q1 N8 ?5 Y. Z; {: e, R1 N4 s
: D4 Q9 I2 l; f/ ]. p
. K; @: Z* X$ Z [& O
0 k9 Y% H. Z. i: w# u0 D6 ]8 }2 ], \2 G% Q7 O
* P& g8 S; u9 j3 l" G
% Y$ v# G [$ l$ P8 M( m e! |6 V% D [9 t
9 G0 [* F, @) H5 {4 S4 H9 I* s- h! O3 k7 o) `0 Q8 t( o
: j2 r# e1 ^6 C#endif |