]>
git.saurik.com Git - apple/icu.git/blob - icuSources/io/ufile.c
2 ******************************************************************************
4 * Copyright (C) 1998-2013, 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 ******************************************************************************
22 * fileno is not declared when building with GCC in strict mode.
24 #if defined(__GNUC__) && defined(__STRICT_ANSI__)
25 #undef __STRICT_ANSI__
29 #include "unicode/ustdio.h"
31 #include "unicode/uloc.h"
32 #include "unicode/ures.h"
33 #include "unicode/ucnv.h"
37 #if U_PLATFORM_USES_ONLY_WIN32_API && !defined(fileno)
38 /* Windows likes to rename Unix-like functions */
39 #define fileno _fileno
49 UErrorCode status
= U_ZERO_ERROR
;
54 result
= (UFILE
*) uprv_malloc(sizeof(UFILE
));
59 uprv_memset(result
, 0, sizeof(UFILE
));
60 result
->fFileno
= fileno(f
);
62 #if U_PLATFORM_USES_ONLY_WIN32_API
63 if (0 <= result
->fFileno
&& result
->fFileno
<= 2) {
64 /* stdin, stdout and stderr need to be special cased for Windows 98 */
66 result
->fFile
= &__iob_func()[_fileno(f
)];
68 result
->fFile
= &_iob
[_fileno(f
)];
77 result
->str
.fBuffer
= result
->fUCBuffer
;
78 result
->str
.fPos
= result
->fUCBuffer
;
79 result
->str
.fLimit
= result
->fUCBuffer
;
81 #if !UCONFIG_NO_FORMATTING
82 /* if locale is 0, use the default */
83 if(u_locbund_init(&result
->str
.fBundle
, locale
) == 0) {
84 /* DO NOT FCLOSE HERE! */
90 /* If the codepage is not "" use the ucnv_open default behavior */
91 if(codepage
== NULL
|| *codepage
!= '\0') {
92 result
->fConverter
= ucnv_open(codepage
, &status
);
94 /* else result->fConverter is already memset'd to NULL. */
96 if(U_SUCCESS(status
)) {
97 result
->fOwnFile
= takeOwnership
;
100 #if !UCONFIG_NO_FORMATTING
101 u_locbund_close(&result
->str
.fBundle
);
103 /* DO NOT fclose here!!!!!! */
111 U_CAPI UFILE
* U_EXPORT2
/* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
114 const char *codepage
)
116 return finit_owner(f
, locale
, codepage
, FALSE
);
119 U_CAPI UFILE
* U_EXPORT2
122 const char *codepage
)
124 return finit_owner(f
, locale
, codepage
, TRUE
);
127 U_CAPI UFILE
* U_EXPORT2
/* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
128 u_fopen(const char *filename
,
131 const char *codepage
)
134 FILE *systemFile
= fopen(filename
, perm
);
135 if(systemFile
== 0) {
139 result
= finit_owner(systemFile
, locale
, codepage
, TRUE
);
142 /* Something bad happened.
143 Maybe the converter couldn't be opened. */
147 return result
; /* not a file leak */
150 U_CAPI UFILE
* U_EXPORT2
151 u_fstropen(UChar
*stringBuf
,
161 result
= (UFILE
*) uprv_malloc(sizeof(UFILE
));
162 /* Null pointer test */
163 if (result
== NULL
) {
164 return NULL
; /* Just get out. */
166 uprv_memset(result
, 0, sizeof(UFILE
));
167 result
->str
.fBuffer
= stringBuf
;
168 result
->str
.fPos
= stringBuf
;
169 result
->str
.fLimit
= stringBuf
+capacity
;
171 #if !UCONFIG_NO_FORMATTING
172 /* if locale is 0, use the default */
173 if(u_locbund_init(&result
->str
.fBundle
, locale
) == 0) {
174 /* DO NOT FCLOSE HERE! */
183 U_CAPI UBool U_EXPORT2
190 endOfBuffer
= (UBool
)(f
->str
.fPos
>= f
->str
.fLimit
);
191 if (f
->fFile
!= NULL
) {
192 return endOfBuffer
&& feof(f
->fFile
);
197 U_CAPI
void U_EXPORT2
198 u_fflush(UFILE
*file
)
200 ufile_flush_translit(file
);
201 ufile_flush_io(file
);
205 else if (file
->str
.fPos
< file
->str
.fLimit
) {
206 *(file
->str
.fPos
++) = 0;
208 /* TODO: flush input */
212 u_frewind(UFILE
*file
)
215 ucnv_reset(file
->fConverter
);
218 file
->str
.fLimit
= file
->fUCBuffer
;
219 file
->str
.fPos
= file
->fUCBuffer
;
222 file
->str
.fPos
= file
->str
.fBuffer
;
226 U_CAPI
void U_EXPORT2
/* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
227 u_fclose(UFILE
*file
)
231 ufile_close_translit(file
);
236 #if !UCONFIG_NO_FORMATTING
237 u_locbund_close(&file
->str
.fBundle
);
240 ucnv_close(file
->fConverter
);
245 U_CAPI
FILE* U_EXPORT2
/* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
246 u_fgetfile( UFILE
*f
)
251 #if !UCONFIG_NO_FORMATTING
253 U_CAPI
const char* U_EXPORT2
/* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
254 u_fgetlocale( UFILE
*file
)
256 return file
->str
.fBundle
.fLocale
;
259 U_CAPI
int32_t U_EXPORT2
/* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
260 u_fsetlocale(UFILE
*file
,
263 u_locbund_close(&file
->str
.fBundle
);
265 return u_locbund_init(&file
->str
.fBundle
, locale
) == 0 ? -1 : 0;
270 U_CAPI
const char* U_EXPORT2
/* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
271 u_fgetcodepage(UFILE
*file
)
273 UErrorCode status
= U_ZERO_ERROR
;
274 const char *codepage
= NULL
;
276 if (file
->fConverter
) {
277 codepage
= ucnv_getName(file
->fConverter
, &status
);
278 if(U_FAILURE(status
))
284 U_CAPI
int32_t U_EXPORT2
/* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
285 u_fsetcodepage( const char *codepage
,
288 UErrorCode status
= U_ZERO_ERROR
;
291 /* We use the normal default codepage for this system, and not the one for the locale. */
292 if ((file
->str
.fPos
== file
->str
.fBuffer
) && (file
->str
.fLimit
== file
->str
.fBuffer
)) {
293 ucnv_close(file
->fConverter
);
294 file
->fConverter
= ucnv_open(codepage
, &status
);
295 if(U_SUCCESS(status
)) {
303 U_CAPI UConverter
* U_EXPORT2
/* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
304 u_fgetConverter(UFILE
*file
)
306 return file
->fConverter
;
308 #if !UCONFIG_NO_FORMATTING
309 U_CAPI
const UNumberFormat
* U_EXPORT2
u_fgetNumberFormat(UFILE
*file
)
311 return u_locbund_getNumberFormat(&file
->str
.fBundle
, UNUM_DECIMAL
);