]>
git.saurik.com Git - apple/icu.git/blob - icuSources/io/ufile.c
2 ******************************************************************************
4 * Copyright (C) 1998-2011, 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 * Defines _XOPEN_SOURCE for access to POSIX functions.
23 * Must be before any other #includes.
25 #include "uposixdefs.h"
28 #include "unicode/ustdio.h"
30 #include "unicode/uloc.h"
31 #include "unicode/ures.h"
32 #include "unicode/ucnv.h"
36 #if U_PLATFORM_USES_ONLY_WIN32_API && !defined(fileno)
37 /* Windows likes to rename Unix-like functions */
38 #define fileno _fileno
48 UErrorCode status
= U_ZERO_ERROR
;
53 result
= (UFILE
*) uprv_malloc(sizeof(UFILE
));
58 uprv_memset(result
, 0, sizeof(UFILE
));
59 result
->fFileno
= fileno(f
);
61 #if U_PLATFORM_USES_ONLY_WIN32_API
62 if (0 <= result
->fFileno
&& result
->fFileno
<= 2) {
63 /* stdin, stdout and stderr need to be special cased for Windows 98 */
65 result
->fFile
= &__iob_func()[_fileno(f
)];
67 result
->fFile
= &_iob
[_fileno(f
)];
76 result
->str
.fBuffer
= result
->fUCBuffer
;
77 result
->str
.fPos
= result
->fUCBuffer
;
78 result
->str
.fLimit
= result
->fUCBuffer
;
80 #if !UCONFIG_NO_FORMATTING
81 /* if locale is 0, use the default */
82 if(u_locbund_init(&result
->str
.fBundle
, locale
) == 0) {
83 /* DO NOT FCLOSE HERE! */
89 /* If the codepage is not "" use the ucnv_open default behavior */
90 if(codepage
== NULL
|| *codepage
!= '\0') {
91 result
->fConverter
= ucnv_open(codepage
, &status
);
93 /* else result->fConverter is already memset'd to NULL. */
95 if(U_SUCCESS(status
)) {
96 result
->fOwnFile
= takeOwnership
;
99 #if !UCONFIG_NO_FORMATTING
100 u_locbund_close(&result
->str
.fBundle
);
102 /* DO NOT fclose here!!!!!! */
110 U_CAPI UFILE
* U_EXPORT2
/* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
113 const char *codepage
)
115 return finit_owner(f
, locale
, codepage
, FALSE
);
118 U_CAPI UFILE
* U_EXPORT2
121 const char *codepage
)
123 return finit_owner(f
, locale
, codepage
, TRUE
);
126 U_CAPI UFILE
* U_EXPORT2
/* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
127 u_fopen(const char *filename
,
130 const char *codepage
)
133 FILE *systemFile
= fopen(filename
, perm
);
134 if(systemFile
== 0) {
138 result
= finit_owner(systemFile
, locale
, codepage
, TRUE
);
141 /* Something bad happened.
142 Maybe the converter couldn't be opened. */
146 return result
; /* not a file leak */
149 U_CAPI UFILE
* U_EXPORT2
150 u_fstropen(UChar
*stringBuf
,
160 result
= (UFILE
*) uprv_malloc(sizeof(UFILE
));
161 /* Null pointer test */
162 if (result
== NULL
) {
163 return NULL
; /* Just get out. */
165 uprv_memset(result
, 0, sizeof(UFILE
));
166 result
->str
.fBuffer
= stringBuf
;
167 result
->str
.fPos
= stringBuf
;
168 result
->str
.fLimit
= stringBuf
+capacity
;
170 #if !UCONFIG_NO_FORMATTING
171 /* if locale is 0, use the default */
172 if(u_locbund_init(&result
->str
.fBundle
, locale
) == 0) {
173 /* DO NOT FCLOSE HERE! */
182 U_CAPI UBool U_EXPORT2
189 endOfBuffer
= (UBool
)(f
->str
.fPos
>= f
->str
.fLimit
);
190 if (f
->fFile
!= NULL
) {
191 return endOfBuffer
&& feof(f
->fFile
);
196 U_CAPI
void U_EXPORT2
197 u_fflush(UFILE
*file
)
199 ufile_flush_translit(file
);
200 ufile_flush_io(file
);
204 else if (file
->str
.fPos
< file
->str
.fLimit
) {
205 *(file
->str
.fPos
++) = 0;
207 /* TODO: flush input */
211 u_frewind(UFILE
*file
)
214 ucnv_reset(file
->fConverter
);
217 file
->str
.fLimit
= file
->fUCBuffer
;
218 file
->str
.fPos
= file
->fUCBuffer
;
221 file
->str
.fPos
= file
->str
.fBuffer
;
225 U_CAPI
void U_EXPORT2
/* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
226 u_fclose(UFILE
*file
)
230 ufile_close_translit(file
);
235 #if !UCONFIG_NO_FORMATTING
236 u_locbund_close(&file
->str
.fBundle
);
239 ucnv_close(file
->fConverter
);
244 U_CAPI
FILE* U_EXPORT2
/* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
245 u_fgetfile( UFILE
*f
)
250 #if !UCONFIG_NO_FORMATTING
252 U_CAPI
const char* U_EXPORT2
/* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
253 u_fgetlocale( UFILE
*file
)
255 return file
->str
.fBundle
.fLocale
;
258 U_CAPI
int32_t U_EXPORT2
/* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
259 u_fsetlocale(UFILE
*file
,
262 u_locbund_close(&file
->str
.fBundle
);
264 return u_locbund_init(&file
->str
.fBundle
, locale
) == 0 ? -1 : 0;
269 U_CAPI
const char* U_EXPORT2
/* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
270 u_fgetcodepage(UFILE
*file
)
272 UErrorCode status
= U_ZERO_ERROR
;
273 const char *codepage
= NULL
;
275 if (file
->fConverter
) {
276 codepage
= ucnv_getName(file
->fConverter
, &status
);
277 if(U_FAILURE(status
))
283 U_CAPI
int32_t U_EXPORT2
/* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
284 u_fsetcodepage( const char *codepage
,
287 UErrorCode status
= U_ZERO_ERROR
;
290 /* We use the normal default codepage for this system, and not the one for the locale. */
291 if ((file
->str
.fPos
== file
->str
.fBuffer
) && (file
->str
.fLimit
== file
->str
.fBuffer
)) {
292 ucnv_close(file
->fConverter
);
293 file
->fConverter
= ucnv_open(codepage
, &status
);
294 if(U_SUCCESS(status
)) {
302 U_CAPI UConverter
* U_EXPORT2
/* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
303 u_fgetConverter(UFILE
*file
)
305 return file
->fConverter
;