]>
git.saurik.com Git - apple/icu.git/blob - icuSources/io/ufile.c
2 ******************************************************************************
4 * Copyright (C) 1998-2010, 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 #if __STDC_VERSION__ >= 199901L
24 /* It is invalid to compile an XPG3, XPG4, XPG4v2 or XPG5 application using c99 */
25 #define _XOPEN_SOURCE 600
27 #define _XOPEN_SOURCE 4
32 #include "unicode/ustdio.h"
34 #include "unicode/uloc.h"
35 #include "unicode/ures.h"
36 #include "unicode/ucnv.h"
40 #if defined(U_WINDOWS) && !defined(fileno)
41 /* Windows likes to rename Unix-like functions */
42 #define fileno _fileno
52 UErrorCode status
= U_ZERO_ERROR
;
57 result
= (UFILE
*) uprv_malloc(sizeof(UFILE
));
62 uprv_memset(result
, 0, sizeof(UFILE
));
63 result
->fFileno
= fileno(f
);
66 if (0 <= result
->fFileno
&& result
->fFileno
<= 2) {
67 /* stdin, stdout and stderr need to be special cased for Windows 98 */
69 result
->fFile
= &__iob_func()[_fileno(f
)];
71 result
->fFile
= &_iob
[_fileno(f
)];
80 result
->str
.fBuffer
= result
->fUCBuffer
;
81 result
->str
.fPos
= result
->fUCBuffer
;
82 result
->str
.fLimit
= result
->fUCBuffer
;
84 #if !UCONFIG_NO_FORMATTING
85 /* if locale is 0, use the default */
86 if(u_locbund_init(&result
->str
.fBundle
, locale
) == 0) {
87 /* DO NOT FCLOSE HERE! */
93 /* If the codepage is not "" use the ucnv_open default behavior */
94 if(codepage
== NULL
|| *codepage
!= '\0') {
95 result
->fConverter
= ucnv_open(codepage
, &status
);
97 /* else result->fConverter is already memset'd to NULL. */
99 if(U_SUCCESS(status
)) {
100 result
->fOwnFile
= takeOwnership
;
103 #if !UCONFIG_NO_FORMATTING
104 u_locbund_close(&result
->str
.fBundle
);
106 /* DO NOT fclose here!!!!!! */
114 U_CAPI UFILE
* U_EXPORT2
/* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
117 const char *codepage
)
119 return finit_owner(f
, locale
, codepage
, FALSE
);
122 U_CAPI UFILE
* U_EXPORT2
125 const char *codepage
)
127 return finit_owner(f
, locale
, codepage
, TRUE
);
130 U_CAPI UFILE
* U_EXPORT2
/* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
131 u_fopen(const char *filename
,
134 const char *codepage
)
137 FILE *systemFile
= fopen(filename
, perm
);
138 if(systemFile
== 0) {
142 result
= finit_owner(systemFile
, locale
, codepage
, TRUE
);
145 /* Something bad happened.
146 Maybe the converter couldn't be opened. */
153 U_CAPI UFILE
* U_EXPORT2
154 u_fstropen(UChar
*stringBuf
,
164 result
= (UFILE
*) uprv_malloc(sizeof(UFILE
));
165 /* Null pointer test */
166 if (result
== NULL
) {
167 return NULL
; /* Just get out. */
169 uprv_memset(result
, 0, sizeof(UFILE
));
170 result
->str
.fBuffer
= stringBuf
;
171 result
->str
.fPos
= stringBuf
;
172 result
->str
.fLimit
= stringBuf
+capacity
;
174 #if !UCONFIG_NO_FORMATTING
175 /* if locale is 0, use the default */
176 if(u_locbund_init(&result
->str
.fBundle
, locale
) == 0) {
177 /* DO NOT FCLOSE HERE! */
186 U_CAPI UBool U_EXPORT2
193 endOfBuffer
= (UBool
)(f
->str
.fPos
>= f
->str
.fLimit
);
194 if (f
->fFile
!= NULL
) {
195 return endOfBuffer
&& feof(f
->fFile
);
200 U_CAPI
void U_EXPORT2
201 u_fflush(UFILE
*file
)
203 ufile_flush_translit(file
);
204 ufile_flush_io(file
);
208 else if (file
->str
.fPos
< file
->str
.fLimit
) {
209 *(file
->str
.fPos
++) = 0;
211 /* TODO: flush input */
215 u_frewind(UFILE
*file
)
218 ucnv_reset(file
->fConverter
);
221 file
->str
.fLimit
= file
->fUCBuffer
;
222 file
->str
.fPos
= file
->fUCBuffer
;
225 file
->str
.fPos
= file
->str
.fBuffer
;
229 U_CAPI
void U_EXPORT2
/* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
230 u_fclose(UFILE
*file
)
234 ufile_close_translit(file
);
239 #if !UCONFIG_NO_FORMATTING
240 u_locbund_close(&file
->str
.fBundle
);
243 ucnv_close(file
->fConverter
);
248 U_CAPI
FILE* U_EXPORT2
/* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
249 u_fgetfile( UFILE
*f
)
254 #if !UCONFIG_NO_FORMATTING
256 U_CAPI
const char* U_EXPORT2
/* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
257 u_fgetlocale( UFILE
*file
)
259 return file
->str
.fBundle
.fLocale
;
262 U_CAPI
int32_t U_EXPORT2
/* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
263 u_fsetlocale(UFILE
*file
,
266 u_locbund_close(&file
->str
.fBundle
);
268 return u_locbund_init(&file
->str
.fBundle
, locale
) == 0 ? -1 : 0;
273 U_CAPI
const char* U_EXPORT2
/* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
274 u_fgetcodepage(UFILE
*file
)
276 UErrorCode status
= U_ZERO_ERROR
;
277 const char *codepage
= NULL
;
279 if (file
->fConverter
) {
280 codepage
= ucnv_getName(file
->fConverter
, &status
);
281 if(U_FAILURE(status
))
287 U_CAPI
int32_t U_EXPORT2
/* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
288 u_fsetcodepage( const char *codepage
,
291 UErrorCode status
= U_ZERO_ERROR
;
294 /* We use the normal default codepage for this system, and not the one for the locale. */
295 if ((file
->str
.fPos
== file
->str
.fBuffer
) && (file
->str
.fLimit
== file
->str
.fBuffer
)) {
296 ucnv_close(file
->fConverter
);
297 file
->fConverter
= ucnv_open(codepage
, &status
);
298 if(U_SUCCESS(status
)) {
306 U_CAPI UConverter
* U_EXPORT2
/* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
307 u_fgetConverter(UFILE
*file
)
309 return file
->fConverter
;