]>
git.saurik.com Git - apple/icu.git/blob - icuSources/io/ufile.c
2 ******************************************************************************
4 * Copyright (C) 1998-2014, 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 #if !UCONFIG_NO_CONVERSION
34 #include "unicode/uloc.h"
35 #include "unicode/ures.h"
36 #include "unicode/ucnv.h"
37 #include "unicode/ustring.h"
41 #if U_PLATFORM_USES_ONLY_WIN32_API && !defined(fileno)
42 /* Windows likes to rename Unix-like functions */
43 #define fileno _fileno
53 UErrorCode status
= U_ZERO_ERROR
;
58 result
= (UFILE
*) uprv_malloc(sizeof(UFILE
));
63 uprv_memset(result
, 0, sizeof(UFILE
));
64 result
->fFileno
= fileno(f
);
66 #if U_PLATFORM_USES_ONLY_WIN32_API
67 if (0 <= result
->fFileno
&& result
->fFileno
<= 2) {
68 /* stdin, stdout and stderr need to be special cased for Windows 98 */
70 result
->fFile
= &__iob_func()[_fileno(f
)];
72 result
->fFile
= &_iob
[_fileno(f
)];
81 result
->str
.fBuffer
= result
->fUCBuffer
;
82 result
->str
.fPos
= result
->fUCBuffer
;
83 result
->str
.fLimit
= result
->fUCBuffer
;
85 #if !UCONFIG_NO_FORMATTING
86 /* if locale is 0, use the default */
87 if(u_locbund_init(&result
->str
.fBundle
, locale
) == 0) {
88 /* DO NOT FCLOSE HERE! */
94 /* If the codepage is not "" use the ucnv_open default behavior */
95 if(codepage
== NULL
|| *codepage
!= '\0') {
96 result
->fConverter
= ucnv_open(codepage
, &status
);
98 /* else result->fConverter is already memset'd to NULL. */
100 if(U_SUCCESS(status
)) {
101 result
->fOwnFile
= takeOwnership
;
104 #if !UCONFIG_NO_FORMATTING
105 u_locbund_close(&result
->str
.fBundle
);
107 /* DO NOT fclose here!!!!!! */
115 U_CAPI UFILE
* U_EXPORT2
/* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
118 const char *codepage
)
120 return finit_owner(f
, locale
, codepage
, FALSE
);
123 U_CAPI UFILE
* U_EXPORT2
126 const char *codepage
)
128 return finit_owner(f
, locale
, codepage
, TRUE
);
131 U_CAPI UFILE
* U_EXPORT2
/* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
132 u_fopen(const char *filename
,
135 const char *codepage
)
138 FILE *systemFile
= fopen(filename
, perm
);
139 if(systemFile
== 0) {
143 result
= finit_owner(systemFile
, locale
, codepage
, TRUE
);
146 /* Something bad happened.
147 Maybe the converter couldn't be opened. */
151 return result
; /* not a file leak */
154 U_CAPI UFILE
* U_EXPORT2
155 u_fopen_u(const UChar
*filename
,
158 const char *codepage
)
163 u_austrcpy(buffer
, filename
);
165 result
= u_fopen(buffer
, perm
, locale
, codepage
);
166 #if U_PLATFORM_USES_ONLY_WIN32_API
167 /* Try Windows API _wfopen if the above fails. */
169 FILE *systemFile
= _wfopen(filename
, (UChar
*)perm
);
171 result
= finit_owner(systemFile
, locale
, codepage
, TRUE
);
174 /* Something bad happened.
175 Maybe the converter couldn't be opened. */
180 return result
; /* not a file leak */
183 U_CAPI UFILE
* U_EXPORT2
184 u_fstropen(UChar
*stringBuf
,
194 result
= (UFILE
*) uprv_malloc(sizeof(UFILE
));
195 /* Null pointer test */
196 if (result
== NULL
) {
197 return NULL
; /* Just get out. */
199 uprv_memset(result
, 0, sizeof(UFILE
));
200 result
->str
.fBuffer
= stringBuf
;
201 result
->str
.fPos
= stringBuf
;
202 result
->str
.fLimit
= stringBuf
+capacity
;
204 #if !UCONFIG_NO_FORMATTING
205 /* if locale is 0, use the default */
206 if(u_locbund_init(&result
->str
.fBundle
, locale
) == 0) {
207 /* DO NOT FCLOSE HERE! */
216 U_CAPI UBool U_EXPORT2
223 endOfBuffer
= (UBool
)(f
->str
.fPos
>= f
->str
.fLimit
);
224 if (f
->fFile
!= NULL
) {
225 return endOfBuffer
&& feof(f
->fFile
);
230 U_CAPI
void U_EXPORT2
231 u_fflush(UFILE
*file
)
233 ufile_flush_translit(file
);
234 ufile_flush_io(file
);
238 else if (file
->str
.fPos
< file
->str
.fLimit
) {
239 *(file
->str
.fPos
++) = 0;
241 /* TODO: flush input */
245 u_frewind(UFILE
*file
)
248 ucnv_reset(file
->fConverter
);
251 file
->str
.fLimit
= file
->fUCBuffer
;
252 file
->str
.fPos
= file
->fUCBuffer
;
255 file
->str
.fPos
= file
->str
.fBuffer
;
259 U_CAPI
void U_EXPORT2
/* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
260 u_fclose(UFILE
*file
)
264 ufile_close_translit(file
);
269 #if !UCONFIG_NO_FORMATTING
270 u_locbund_close(&file
->str
.fBundle
);
273 ucnv_close(file
->fConverter
);
278 U_CAPI
FILE* U_EXPORT2
/* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
279 u_fgetfile( UFILE
*f
)
284 #if !UCONFIG_NO_FORMATTING
286 U_CAPI
const char* U_EXPORT2
/* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
287 u_fgetlocale( UFILE
*file
)
289 return file
->str
.fBundle
.fLocale
;
292 U_CAPI
int32_t U_EXPORT2
/* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
293 u_fsetlocale(UFILE
*file
,
296 u_locbund_close(&file
->str
.fBundle
);
298 return u_locbund_init(&file
->str
.fBundle
, locale
) == 0 ? -1 : 0;
303 U_CAPI
const char* U_EXPORT2
/* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
304 u_fgetcodepage(UFILE
*file
)
306 UErrorCode status
= U_ZERO_ERROR
;
307 const char *codepage
= NULL
;
309 if (file
->fConverter
) {
310 codepage
= ucnv_getName(file
->fConverter
, &status
);
311 if(U_FAILURE(status
))
317 U_CAPI
int32_t U_EXPORT2
/* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
318 u_fsetcodepage( const char *codepage
,
321 UErrorCode status
= U_ZERO_ERROR
;
324 /* We use the normal default codepage for this system, and not the one for the locale. */
325 if ((file
->str
.fPos
== file
->str
.fBuffer
) && (file
->str
.fLimit
== file
->str
.fBuffer
)) {
326 ucnv_close(file
->fConverter
);
327 file
->fConverter
= ucnv_open(codepage
, &status
);
328 if(U_SUCCESS(status
)) {
336 U_CAPI UConverter
* U_EXPORT2
/* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
337 u_fgetConverter(UFILE
*file
)
339 return file
->fConverter
;
341 #if !UCONFIG_NO_FORMATTING
342 U_CAPI
const UNumberFormat
* U_EXPORT2
u_fgetNumberFormat(UFILE
*file
)
344 return u_locbund_getNumberFormat(&file
->str
.fBundle
, UNUM_DECIMAL
);