冒险解谜游戏中文网 ChinaAVG

标题: [报道]重新报个到,申请个新人成长礼包~ [打印本页]

作者: firendless    时间: 2009-4-23 19:44
标题: [报道]重新报个到,申请个新人成长礼包~
[报道]重新报个到,申请个新人成长礼包~
$ ?5 ~% a* K7 s# o) w* X5 i  v: B3 T6 k* c0 `. P
灌个水吧...
# R  o; G3 ]' X* A/**: B8 y4 {  {5 T% |) |
*@file   1 ?) ^4 z4 A0 C8 w! i
*@author Jimmy) P( g" h2 |0 h5 O9 }1 [, w- e4 v
*@date   2007.1.16; j/ N0 S. \' t) [
*/
4 E: I3 s6 _3 r. A5 d9 y8 v7 c#include "utf8conv.h"5 t7 B% g4 v+ ]- K: F* i# k0 t
  s% z& g, N) H5 J& X

& Z+ M; e' Y2 t6 h+ M# B  h$ l/ ^& X$ ^( i/ _8 r
int UTF8Conv::codepage(const char* code_page)
+ R# p1 i0 O# C# A( u# s0 {{   
, B8 c& ^- e6 A- x( h#ifdef WIN32
; ]; \" {- q- y# s' d    if(code_page)
; G  P  c6 @% @- y    {         " j& p5 U% X' Q6 ^# {
        return GetACP();//既竚箇砞
$ n+ u2 h: |3 q% z' z    }      
3 j9 F/ S" Y8 S5 S: i#endif+ a# w6 T6 T5 }
    return 950 ;//taiwan acp
) V/ [, o* _9 s: n1 z}
  V- j! F+ H9 {* C2 D3 E( j/ W+ x6 z) k1 U- W2 c

7 l$ M6 n3 f1 ]5 Sint UTF8Conv::dbcs2wchar(const char* code_page,/*in*/const char* in,int in_len,/*out*/wchar_t* out,int out_max)
2 v* t, U# d3 [% w" E( w* b( w{
% \  k( p6 h0 w" u#ifdef WIN32; L6 a. X/ t: M
    return ::MultiByteToWideChar(codepage(code_page),0,in,in_len,out,out_max);3 ]5 b' K/ E% ?2 P
#else - b8 Y" n. B. x( F
    size_t result;
& G/ ~' r& x- V3 D  f. ?2 ^! Z6 m    iconv_t env;% X4 w+ s& g/ ]$ }: @: Y( i
    env = iconv_open("WCHAR_T",code_page);% m) P3 ?: i" {5 t8 ?2 j) u4 Q
    result = iconv(env,(char**)&in,(size_t*)&in_len,(char**)&out,(size_t*)&out_max);
- s9 [2 J8 I2 f    iconv_close(env);
8 z' L/ _1 v) B4 N* `    return (int) result;4 f% y+ j  k  K
5 |' H& O2 B, B1 s' W
#endif    , h  P, x$ B7 F& g' i7 B* X4 @
}, B. ?# c8 }( ]: Q. e9 `3 g! Q
  j, Y! t/ s2 s, H- f
int UTF8Conv::dbcs2wchar(const char* code_page,/*in*/const std::string&  in,/*out*/std::wstring& out)
+ j7 J1 }' E6 O{
, u( e& Q, R! H- r    int len = in.length() + 1;
1 E. A! V( b  l: E# H/ V    int result;
! G- c" A) l6 \& a/ Y" M9 l    wchar_t* pBuffer = new wchar_t[len];
+ Q# A5 R" |8 [0 H; I5 o2 M    memset(pBuffer,0,len*sizeof(wchar_t));
, {+ \5 U/ O6 \7 b+ g9 C1 u+ \    result = dbcs2wchar(code_page,in.c_str(),in.length(),pBuffer,len*sizeof(wchar_t));: y1 |  l6 z1 m: b, c& d
    if(pBuffer >= 0)+ R# O' ^9 r) N' h. J1 u
    {
& w( m5 U: x$ v3 f        out = pBuffer;, x: E* B. e: o  e
    }! m, M5 n  g& `' b' y. D: @
    else
$ S% T2 c3 v/ v  L" H2 Y& W; ?  `    {
/ |+ [; d0 b0 c2 w        out.clear();
0 R9 h3 h4 o; {/ k    }# d$ R2 j) ^( i6 n  E
    delete[] pBuffer;- c8 I6 n" y& ~! Q" T+ I, l5 `
    return result;* q/ g! n) q2 Z0 z
}
9 F0 J! L  y5 V+ J& W' j  e6 q2 E% G* M3 I! M
int UTF8Conv::wchar2dbcs(const char* code_page,/*in*/const wchar_t* in,int in_len,/*out*/char* out,int out_max)
* c' I- j, x% _5 D3 C+ y' ^  X{! E6 [! p) i. `2 i0 K9 i
#ifdef WIN32
: V% V0 T, G( E; f    BOOL use_def_char;
1 c2 v6 D8 F' i, |& V    use_def_char = FALSE;
: e, U3 W( J9 H    return ::WideCharToMultiByte(codepage(code_page),0,in,in_len/sizeof(wchar_t),out,out_max,"?",&use_def_char);* u% L  e, j% b" A! ^
#else/ a  U  M3 O, o4 ~1 h4 y& d8 u
    size_t result;/ K% U/ ?% j0 n
    iconv_t env;, _& M0 }7 {- U
    env = iconv_open(code_page,"WCHAR_T");
4 s5 u* K! c1 B/ T% Y3 P, Z& Z    result = iconv(env,(char**)&in,(size_t*)&in_len,(char**)&out,(size_t*)&out_max);
0 N0 `$ V8 d' L/ r! M6 X. |    iconv_close(env);9 I; k3 m7 M, ]  O" q0 x
    return (int) result;7 g% B+ U) }* r7 i3 I7 k. x
#endif   , `7 S$ w, J5 _$ }: }$ W; i+ @
}
6 h* \6 O( j4 P5 Y; V) j4 y3 h2 n( j% q
int UTF8Conv::wchar2dbcs(const char* code_page,/*in*/const std::wstring& in,/*out*/std::string& out)
/ D* e$ ~- d! c5 f+ L" g{, G8 |& m6 C/ D) p5 C/ a- p$ b9 B
    int len = in.length() + 1;
* N/ s: G3 _/ q3 l    int result;
8 f! q9 C& ?! a/ Z- k4 k6 I    char* pBuffer = new char[len*3];8 \. d# G& U4 j, O7 L  I
    memset(pBuffer,0,len*3);
1 _5 L, p8 \, ]- I5 {    result = wchar2dbcs(code_page,in.c_str(),in.length() * sizeof(wchar_t),pBuffer,len*3);
$ Q' T; N$ j4 o- Z  Y    if(result >= 0), c2 v, b1 b7 o3 g
    {  
4 Q* f2 m0 G$ |8 h/ y        out = pBuffer;
+ w0 p5 V0 ]4 p9 X' f" O4 f9 c4 Q    }
5 h: A4 u+ Z  h( D9 F    else
# f; F+ v8 H5 @3 t% B! M& {0 z* [$ ^    {. L6 I- F2 V8 x  k
        out = "";% g! O* B! i2 v
    }" e: ~5 `7 w+ z3 T
    delete[] pBuffer;( \* r9 r2 d- c
    return result;! ]( B0 Z! G5 X, T5 m- Q
}* r6 L0 k" o( N- }4 M2 d

: z. ?3 e2 C1 \6 N  J. Vint UTF8Conv::wchar2utf8(/*in*/const wchar_t* in,int in_len,/*out*/char* out,int out_max)
3 H% _. J6 @5 {; ~0 w{
8 `% O, c3 C; x/ ~3 d) u- u* v#ifdef WIN329 N# Q5 W' j: o/ L5 X
7 Z8 D, u1 k8 N' \6 ?
    BOOL use_def_char;  e5 q% o3 l) Z2 }2 j) ~( t
    use_def_char = FALSE;
5 o5 D* J% n+ K) s4 W+ [  g    return ::WideCharToMultiByte(CP_UTF8,0,in,in_len/sizeof(wchar_t),out,out_max,NULL,NULL);+ X0 P- A7 j' b  Y" t. Q
#else, x: s* f' s" m" s
    size_t result;
4 W, s2 U! J0 H) D, o2 Y    iconv_t env;# g4 a' g. R9 S' E" c

/ |0 i- O6 f+ x, G( ?; b! }. h    env = iconv_open("UTF8","WCHAR_T");
1 o6 e. O; B+ `1 r1 V( U5 S    result = iconv(env,(char**)&in,(size_t*)&in_len,(char**)&out,(size_t*)&out_max);     : N; ?1 A( ]3 f4 p0 s* J. x1 W; P
    iconv_close(env);
' R# D$ {0 h. u, d    return (int) result;
% s1 M" }, e% z; k! b#endif) t+ ~' }  W" W! X1 i
}. I7 D$ i0 H& i4 [9 m

. }8 G7 ?, I# U' yint UTF8Conv::wchar2utf8(/*in*/const std::wstring& in,/*out*/std::string& out)8 E1 s  m" H0 y0 T! N) N
{8 D* Y) x) V8 G3 [3 ^/ r" |$ Y9 C
    int len = in.length() + 1;1 u) Y' m/ w. [; F5 g' |
    int result;0 T1 e/ O/ [! I+ P7 \
    char* pBuffer = new char[len*3];  C& m1 f4 V: }2 K7 z
    memset(pBuffer,0,len*3);              4 o! [  p  B" C& x0 y5 g& ]  w
    result = wchar2utf8(in.c_str(),in.length() * sizeof(wchar_t),pBuffer,len*3);           
- B) k# G8 ?  H. N& W- o/ r    if(result >= 0); P' a" N8 z/ V7 F* A
    {" F7 T$ d$ h3 ]2 C
        out = pBuffer;% q  N( S* I7 s7 h
    }
0 t9 P" ^6 g4 u2 _    else
" p/ }! `( i5 M    {1 Z: }  k! {0 u) w% j
        out = "";
. b" `/ w0 s' i" P+ E: z& W" H    }& u0 O8 v/ A; u+ q3 o( Y; M
    delete[] pBuffer;
/ a* w* g/ o3 l4 Q: w7 O6 e    return result;2 F( m5 v5 ^; p& {2 l( P* F$ y9 Q& G
}! C8 X1 k8 b- J/ Z1 P
* i( d/ n8 {( _9 r( U. w; p4 f3 ^
int UTF8Conv::utf82wchar(/*in*/const char* in,int in_len,/*out*/wchar_t* out,int out_max)2 _& r" h$ A, \# @" T
{7 X( ~" R" o/ u0 K9 |
#ifdef WIN32& E/ G1 \. v5 V# m
    return ::MultiByteToWideChar(CP_UTF8,0,in,in_len,out,out_max);
2 d2 w1 L" t) D' [" D4 N#else
! t: }+ s+ V8 F) w" T$ \8 z( _    size_t result;
" H0 r1 P2 z+ }5 G2 V    iconv_t env;
( X  R6 `) H# K; m# p+ i2 y6 o* C    env = iconv_open("WCHAR_T","UTF8");: f9 m8 {3 I. G, C7 R" N0 a9 d5 x* Y
    result = iconv(env,(char**)&in,(size_t*)&in_len,(char**)&out,(size_t*)&out_max);
+ k; S- J2 ~+ h9 r0 k2 c    iconv_close(env);
/ S, q4 [" H. N! X    return (int) result;. h4 F4 R3 C. O: z6 [
#endif% n; c! N- `" ]3 ?; D# I+ G
}
6 w5 P: W; l! N# L+ @2 k; V
. Y; T) [2 T6 c# \+ yint UTF8Conv::utf82wchar(/*in*/const std::string& in,/*out*/std::wstring& out)3 U6 {; C5 j( ~4 }2 z8 V
{
! f" \* N2 b: |& T3 |; U5 C8 W2 d: G    int len = in.length() + 1;
+ T% x& u: o" ?% l+ k0 x6 P    int result;
; [- J5 [+ M, F0 }5 s" F3 W, V- ^    //wstring temp;5 g( U: v: |% L
    wchar_t* pBuffer = new wchar_t[len];
6 x+ p# _7 n$ M' a: n1 \% G) G/ J    memset(pBuffer,0,len*sizeof(wchar_t));
' M2 v! f# b; u$ J! o- q7 c    result = utf82wchar(in.c_str(),in.length(),pBuffer,len*sizeof(wchar_t));9 U7 h% _# ~* a
    //printf("utf82wchar result is %d,errno is %s\n",result,strerror(errno));5 ^2 E7 v5 k- c$ H0 R! ]; l( K' K! v1 Q2 ~
    if(result >= 0)9 T. v6 l0 X2 c6 q
    {
  x% l- i& J7 ~( f        out = pBuffer;
, r6 V" y% h! M: t+ }- o$ f    }1 `, K% s1 s" Y  J: L9 v: K
    else$ N' O8 n+ S2 Y1 B, h$ [
    {8 l: V+ D9 m8 }5 @& I5 n; J
        out.clear();      
; i: ]4 n$ ]4 t    }
& h, {1 L4 t) f    delete[] pBuffer;/ ~3 K5 n# O! g# F  P! b
    return result;, ~. A6 q! K& u  {
}
. X. k, |2 |) {4 T6 y# }( u: V% [+ F" m
/**1 t& G6 a, C9 C8 W
*@file   
8 f, B$ C( [& R+ l) }+ B*@author Jimmy1 e; [2 _% ?% }; X, [$ I
*@date   2007.1.16
0 G+ i; l% d" L+ a*/$ f9 S  d- [0 K. |
#ifndef _UTF8CONV_H_  q  c9 r7 F" U8 c! z
#define _UTF8CONV_H_
. C  R: v2 |# P; F% a* @% ?4 `) b* o+ r6 @) V

* s. W/ G8 T3 ]0 h9 z#ifdef WIN32
7 A' c; W  }' q& g# T' N  K6 ?$ Y#include <windows.h>" Y" K9 G& {* i* ^; _2 j
#include <winnls.h>
2 w( I7 N! `. W- O# L" B#else
& o$ C3 [3 I0 k. ~#include <stdio.h>. o+ P  [% _/ `) _% i
#include <iconv.h>
; d  z, d+ ]5 K1 H#endif7 ]/ z2 d! }6 A

6 Q, I1 c' V# A" \1 T# ~* r4 ?- k9 f#include<string>5 R  G7 c9 |) R6 [! Z0 n

+ b5 k5 ?$ J! M9 |% f- @% g1 y0 o" E# H
class UTF8Conv
! O/ `8 p7 n* H, C {
* i5 t' V) E1 c public:
( f$ \: H) X  |& W    static  int codepage(const char* code_page);4 o7 X2 ]1 r  j% R: S

7 D* p$ T  [0 c! Y) D    static  int dbcs2wchar(const char* code_page,/*in*/const char* in,int in_len,/*out*/wchar_t* out,int out_max) ;
% d& Q6 G; ?! s4 J: \% n4 U0 s  c4 {( b1 j+ f- a) I* E! I
    static  int dbcs2wchar(const char* code_page,/*in*/const std::string&  in,/*out*/std::wstring& out);
1 H0 W9 m  q6 A& s! B3 t) O" ]' `2 T9 f6 A
    static  int wchar2dbcs(const char* code_page,/*in*/const wchar_t* in,int in_len,/*out*/char* out,int out_max) ;* I6 E1 m( g' s$ z" s/ N4 k* W

) w; N! |" e2 f# M" r/ T3 r    static  int wchar2dbcs(const char* code_page,/*in*/const std::wstring& in,/*out*/std::string& out)   ;
. h1 B) B: r0 X# d/ S9 X" D- Q. E5 Q: E8 L( h- a
    static  int wchar2utf8(/*in*/const wchar_t* in,int in_len,/*out*/char* out,int out_max);. S7 g' P' P6 T5 L; L
! s) P! i5 T3 I4 o% S6 j
    static  int wchar2utf8(/*in*/const std::wstring& in,/*out*/std::string& out);
$ \3 v# T' a# w- y% o3 a" r+ z$ J8 e! m! j
    static  int utf82wchar(/*in*/const char* in,int in_len,/*out*/wchar_t* out,int out_max);" y( @, i+ J: q( E
' o/ [" B/ C" E
    static  int utf82wchar(/*in*/const std::string& in,/*out*/std::wstring& out);$ r9 J$ p9 d8 e4 j& @. @+ _+ a
};( ~+ e, ~5 q: K

& g2 t2 G/ U0 |- ]* `
9 |- N+ L* h( X- X% k, y" k# J+ k$ L6 {5 ^- S* b

8 o, }0 \6 L3 O5 N8 X$ ~7 A# o8 ]& _# U- O2 p
/ T/ [8 O  d3 K, s# d& i4 b
! B) e4 x: ~8 g* t( O0 C- G) O
3 p' F, Y) D. Z& N9 j
$ }' a- ]9 C! @# F1 j+ k

! m# Z3 e' O! e; |9 v$ ^% i. t$ C" j
3 l& r0 D6 Q' ~" i7 X5 _
% N9 V" V! C2 R. B5 R

. x. I6 H4 ^1 O5 S- [
/ j  n5 d/ g6 H% Z
7 Y& O* c3 w) J% z' d" X% L: {' }! ~! B5 \- \6 s! R
% |# ^2 ]0 k$ F8 L3 l

) k! }% @6 K% X
, ^: R, U  G, B& K7 b% l
2 h& i$ H0 \) ^" x& v2 W( K! V' J3 `- E# R; s) X: s
& `. {; k$ f7 ?7 u1 N
% h8 d8 q/ E4 \  K" V& D

7 n, ^5 `8 p4 t. T% C
5 ^( ]! {) A- a  V: X$ _% f. `- a) b2 O2 q4 }9 W6 ]

+ T8 i6 x- ~$ ?2 K* }+ b1 i. J1 I5 r' a' j8 ?, Y* N, S
#endif
作者: 6875538348    时间: 2009-4-23 21:08
这水快赶上黄河了····· [s:2]
作者: caesarzx    时间: 2009-4-23 21:11
但这点还不如我打飞机的成果多。
作者: cherrytea    时间: 2009-4-23 21:29
版主楼主忒不厚道了 , t% q. |- e. p! W$ C
这样的话我要申请平民新人年度成长超级大礼包 [s:2]
作者: firendless    时间: 2009-4-24 00:33
引用第2楼caesarzx于2009-04-23 21:11发表的  :9 Z' R$ R- V+ N+ d$ z  t3 O0 k0 [
但这点还不如我打飞机的成果多。
莫非CZX造就了一个民族?呵呵呵~~
: l* _/ K6 P1 m7 ^- X' T
, ^( Q$ t* k. {3 ?$ C. vand 楼上的童鞋,你要领奖,俺一定帮你加,嘿嘿~~




欢迎光临 冒险解谜游戏中文网 ChinaAVG (https://chinaavg.com/) Powered by Discuz! X3.2