FreeBASIC  0.91.0
intl_core.c
Go to the documentation of this file.
1 /* Core i18n functions */
2 
3 #include "../fb.h"
4 #include "fb_private_intl.h"
5 
6 /* Convert a strings character set to another character set. */
7 static FBSTRING *fb_hIntlConvertToWC( FBSTRING *source, UINT source_cp )
8 {
9  FBSTRING *res;
10  int CharsRequired;
11 
12  FB_STRLOCK();
13 
14  CharsRequired =
15  MultiByteToWideChar( source_cp,
16  0,
17  source->data,
18  FB_STRSIZE(source),
19  NULL,
20  0 );
21 
22  res = fb_hStrAllocTemp_NoLock( NULL, (CharsRequired + 1) * sizeof(WCHAR) - 1 );
23  if( res!=NULL ) {
24  size_t idx = CharsRequired * sizeof(WCHAR);
25  MultiByteToWideChar( source_cp,
26  0,
27  (LPCSTR) source->data,
28  FB_STRSIZE(source),
29  (LPWSTR) res->data,
30  CharsRequired );
31  *((WCHAR*) (res->data + idx)) = 0;
32  } else {
33  res = &__fb_ctx.null_desc;
34  }
35 
36  fb_hStrDelTemp_NoLock( source );
37 
38  FB_STRUNLOCK();
39 
40  return res;
41 }
42 
43 static FBSTRING *fb_hIntlConvertFromWC( FBSTRING *source, UINT dest_cp )
44 {
45  FBSTRING *res;
46  int CharsRequired;
47 
48  FB_STRLOCK();
49 
50  CharsRequired =
51  WideCharToMultiByte( dest_cp,
52  0,
53  (LPCWSTR) source->data,
54  FB_STRSIZE(source) / sizeof(WCHAR),
55  (LPSTR) NULL,
56  0,
57  NULL,
58  NULL );
59 
60  res = fb_hStrAllocTemp_NoLock( NULL, CharsRequired );
61  if( res!=NULL ) {
62  WideCharToMultiByte( dest_cp,
63  0,
64  (LPCWSTR) source->data,
65  FB_STRSIZE(source) / sizeof(WCHAR),
66  (LPSTR) res->data,
67  CharsRequired,
68  NULL,
69  NULL );
70  res->data[CharsRequired] = 0;
71  } else {
72  res = &__fb_ctx.null_desc;
73  }
74 
75  fb_hStrDelTemp_NoLock( source );
76 
77  FB_STRUNLOCK();
78 
79  return res;
80 }
81 
82 FBSTRING *fb_hIntlConvertString( FBSTRING *source, int source_cp, int dest_cp )
83 {
84  return fb_hIntlConvertFromWC( fb_hIntlConvertToWC( source, source_cp ), dest_cp );
85 }
86 
87 char *fb_hGetLocaleInfo( LCID Locale, LCTYPE LCType, char *pszBuffer, size_t uiSize )
88 {
89  if( uiSize==0 ) {
90  uiSize = 64;
91  pszBuffer = NULL;
92  for(;;) {
93  pszBuffer = (char*) realloc( pszBuffer, uiSize <<= 1 );
94  if( pszBuffer==NULL )
95  break;
96  if( GetLocaleInfo( Locale, LCType, pszBuffer, uiSize - 1 )!=0 ) {
97  return pszBuffer;
98  }
99  if( GetLastError( ) != ERROR_INSUFFICIENT_BUFFER ) {
100  free( pszBuffer );
101  pszBuffer = NULL;
102  break;
103  }
104  }
105  } else {
106  if( GetLocaleInfo( Locale, LCType, pszBuffer, uiSize )!=0 )
107  return pszBuffer;
108  }
109  return NULL;
110 }