]>
git.saurik.com Git - apple/icu.git/blob - icuSources/io/ufile.c
2 ******************************************************************************
4 * Copyright (C) 1998-2004, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 ******************************************************************************
11 * Modification History:
13 * Date Name Description
14 * 11/19/98 stephen Creation.
15 * 03/12/99 stephen Modified for new C API.
16 * 06/16/99 stephen Changed T_LocaleBundle to u_locbund
17 * 07/19/99 stephen Fixed to use ucnv's default codepage.
18 ******************************************************************************
21 /* define for fileno. */
23 #define _XOPEN_SOURCE 4
27 #include "unicode/ustdio.h"
29 #include "unicode/uloc.h"
30 #include "unicode/ures.h"
31 #include "unicode/ucnv.h"
36 /* Windows likes to rename Unix-like functions */
37 #define fileno _fileno
40 U_CAPI UFILE
* U_EXPORT2
/* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
45 UErrorCode status
= U_ZERO_ERROR
;
46 UFILE
*result
= (UFILE
*) uprv_malloc(sizeof(UFILE
));
47 if(result
== NULL
|| f
== NULL
) {
51 uprv_memset(result
, 0, sizeof(UFILE
));
52 result
->fFileno
= fileno(f
);
55 if (0 <= result
->fFileno
&& result
->fFileno
<= 2) {
56 /* stdin, stdout and stderr need to be special cased for Windows 98 */
57 result
->fFile
= &_iob
[_fileno(f
)];
65 result
->str
.fBuffer
= result
->fUCBuffer
;
66 result
->str
.fPos
= result
->fUCBuffer
;
67 result
->str
.fLimit
= result
->fUCBuffer
;
69 #if !UCONFIG_NO_FORMATTING
70 /* if locale is 0, use the default */
72 locale
= uloc_getDefault();
75 if(u_locbund_init(&result
->str
.fBundle
, locale
) == 0) {
76 /* DO NOT FCLOSE HERE! */
82 /* If the codepage is not "" use the ucnv_open default behavior */
83 if(codepage
== NULL
|| *codepage
!= '\0') {
84 result
->fConverter
= ucnv_open(codepage
, &status
);
86 /* else result->fConverter is already memset'd to NULL. */
88 if(U_FAILURE(status
)) {
89 #if !UCONFIG_NO_FORMATTING
90 u_locbund_close(&result
->str
.fBundle
);
92 /* DO NOT fclose here!!!!!! */
100 U_CAPI UFILE
* U_EXPORT2
/* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
101 u_fopen(const char *filename
,
104 const char *codepage
)
107 FILE *systemFile
= fopen(filename
, perm
);
108 if(systemFile
== 0) {
112 result
= u_finit(systemFile
, locale
, codepage
);
115 result
->fOwnFile
= TRUE
;
118 /* Something bad happened.
119 Maybe the converter couldn't be opened. */
126 U_CAPI UFILE
* U_EXPORT2
127 u_fstropen(UChar
*stringBuf
,
137 result
= (UFILE
*) uprv_malloc(sizeof(UFILE
));
138 uprv_memset(result
, 0, sizeof(UFILE
));
139 result
->str
.fBuffer
= stringBuf
;
140 result
->str
.fPos
= stringBuf
;
141 result
->str
.fLimit
= stringBuf
+capacity
;
143 #if !UCONFIG_NO_FORMATTING
144 /* if locale is 0, use the default */
146 locale
= uloc_getDefault();
149 if(u_locbund_init(&result
->str
.fBundle
, locale
) == 0) {
150 /* DO NOT FCLOSE HERE! */
159 U_CAPI UBool U_EXPORT2
166 endOfBuffer
= (UBool
)(f
->str
.fPos
>= f
->str
.fLimit
);
167 if (f
->fFile
!= NULL
) {
168 return endOfBuffer
&& feof(f
->fFile
);
173 U_CAPI
void U_EXPORT2
174 u_fflush(UFILE
*file
)
176 ufile_flush_translit(file
);
180 else if (file
->str
.fPos
< file
->str
.fLimit
) {
181 *(file
->str
.fPos
++) = 0;
183 /* TODO: flush input */
187 u_frewind(UFILE
*file
)
190 ucnv_reset(file
->fConverter
);
193 file
->str
.fLimit
= file
->fUCBuffer
;
194 file
->str
.fPos
= file
->fUCBuffer
;
197 file
->str
.fPos
= file
->str
.fBuffer
;
201 U_CAPI
void U_EXPORT2
/* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
202 u_fclose(UFILE
*file
)
205 ufile_close_translit(file
);
210 #if !UCONFIG_NO_FORMATTING
211 u_locbund_close(&file
->str
.fBundle
);
214 ucnv_close(file
->fConverter
);
218 U_CAPI
FILE* U_EXPORT2
/* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
219 u_fgetfile( UFILE
*f
)
224 #if !UCONFIG_NO_FORMATTING
226 U_CAPI
const char* U_EXPORT2
/* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
227 u_fgetlocale( UFILE
*file
)
229 return file
->str
.fBundle
.fLocale
;
232 U_CAPI
int32_t U_EXPORT2
/* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
233 u_fsetlocale(UFILE
*file
,
236 u_locbund_close(&file
->str
.fBundle
);
238 return u_locbund_init(&file
->str
.fBundle
, locale
) == 0 ? -1 : 0;
243 U_CAPI
const char* U_EXPORT2
/* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
244 u_fgetcodepage(UFILE
*file
)
246 UErrorCode status
= U_ZERO_ERROR
;
247 const char *codepage
= NULL
;
249 if (file
->fConverter
) {
250 codepage
= ucnv_getName(file
->fConverter
, &status
);
251 if(U_FAILURE(status
))
257 U_CAPI
int32_t U_EXPORT2
/* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
258 u_fsetcodepage( const char *codepage
,
261 UErrorCode status
= U_ZERO_ERROR
;
264 /* We use the normal default codepage for this system, and not the one for the locale. */
265 if ((file
->str
.fPos
== file
->str
.fBuffer
) && (file
->str
.fLimit
== file
->str
.fBuffer
)) {
266 ucnv_close(file
->fConverter
);
267 file
->fConverter
= ucnv_open(codepage
, &status
);
268 if(U_SUCCESS(status
)) {
276 U_CAPI UConverter
* U_EXPORT2
/* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
277 u_fgetConverter(UFILE
*file
)
279 return file
->fConverter
;