]>
git.saurik.com Git - apple/icu.git/blob - icuSources/io/ufile.c
2 ******************************************************************************
4 * Copyright (C) 1998-2008, 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
45 U_CAPI UFILE
* U_EXPORT2
/* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
50 UErrorCode status
= U_ZERO_ERROR
;
55 result
= (UFILE
*) uprv_malloc(sizeof(UFILE
));
60 uprv_memset(result
, 0, sizeof(UFILE
));
61 result
->fFileno
= fileno(f
);
64 if (0 <= result
->fFileno
&& result
->fFileno
<= 2) {
65 /* stdin, stdout and stderr need to be special cased for Windows 98 */
67 result
->fFile
= &__iob_func()[_fileno(f
)];
69 result
->fFile
= &_iob
[_fileno(f
)];
78 result
->str
.fBuffer
= result
->fUCBuffer
;
79 result
->str
.fPos
= result
->fUCBuffer
;
80 result
->str
.fLimit
= result
->fUCBuffer
;
82 #if !UCONFIG_NO_FORMATTING
83 /* if locale is 0, use the default */
84 if(u_locbund_init(&result
->str
.fBundle
, locale
) == 0) {
85 /* DO NOT FCLOSE HERE! */
91 /* If the codepage is not "" use the ucnv_open default behavior */
92 if(codepage
== NULL
|| *codepage
!= '\0') {
93 result
->fConverter
= ucnv_open(codepage
, &status
);
95 /* else result->fConverter is already memset'd to NULL. */
97 if(U_FAILURE(status
)) {
98 #if !UCONFIG_NO_FORMATTING
99 u_locbund_close(&result
->str
.fBundle
);
101 /* DO NOT fclose here!!!!!! */
109 U_CAPI UFILE
* U_EXPORT2
/* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
110 u_fopen(const char *filename
,
113 const char *codepage
)
116 FILE *systemFile
= fopen(filename
, perm
);
117 if(systemFile
== 0) {
121 result
= u_finit(systemFile
, locale
, codepage
);
124 result
->fOwnFile
= TRUE
;
127 /* Something bad happened.
128 Maybe the converter couldn't be opened. */
135 U_CAPI UFILE
* U_EXPORT2
136 u_fstropen(UChar
*stringBuf
,
146 result
= (UFILE
*) uprv_malloc(sizeof(UFILE
));
147 /* Null pointer test */
148 if (result
== NULL
) {
149 return NULL
; /* Just get out. */
151 uprv_memset(result
, 0, sizeof(UFILE
));
152 result
->str
.fBuffer
= stringBuf
;
153 result
->str
.fPos
= stringBuf
;
154 result
->str
.fLimit
= stringBuf
+capacity
;
156 #if !UCONFIG_NO_FORMATTING
157 /* if locale is 0, use the default */
158 if(u_locbund_init(&result
->str
.fBundle
, locale
) == 0) {
159 /* DO NOT FCLOSE HERE! */
168 U_CAPI UBool U_EXPORT2
175 endOfBuffer
= (UBool
)(f
->str
.fPos
>= f
->str
.fLimit
);
176 if (f
->fFile
!= NULL
) {
177 return endOfBuffer
&& feof(f
->fFile
);
182 U_CAPI
void U_EXPORT2
183 u_fflush(UFILE
*file
)
185 ufile_flush_translit(file
);
189 else if (file
->str
.fPos
< file
->str
.fLimit
) {
190 *(file
->str
.fPos
++) = 0;
192 /* TODO: flush input */
196 u_frewind(UFILE
*file
)
199 ucnv_reset(file
->fConverter
);
202 file
->str
.fLimit
= file
->fUCBuffer
;
203 file
->str
.fPos
= file
->fUCBuffer
;
206 file
->str
.fPos
= file
->str
.fBuffer
;
210 U_CAPI
void U_EXPORT2
/* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
211 u_fclose(UFILE
*file
)
214 ufile_close_translit(file
);
219 #if !UCONFIG_NO_FORMATTING
220 u_locbund_close(&file
->str
.fBundle
);
223 ucnv_close(file
->fConverter
);
227 U_CAPI
FILE* U_EXPORT2
/* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
228 u_fgetfile( UFILE
*f
)
233 #if !UCONFIG_NO_FORMATTING
235 U_CAPI
const char* U_EXPORT2
/* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
236 u_fgetlocale( UFILE
*file
)
238 return file
->str
.fBundle
.fLocale
;
241 U_CAPI
int32_t U_EXPORT2
/* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
242 u_fsetlocale(UFILE
*file
,
245 u_locbund_close(&file
->str
.fBundle
);
247 return u_locbund_init(&file
->str
.fBundle
, locale
) == 0 ? -1 : 0;
252 U_CAPI
const char* U_EXPORT2
/* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
253 u_fgetcodepage(UFILE
*file
)
255 UErrorCode status
= U_ZERO_ERROR
;
256 const char *codepage
= NULL
;
258 if (file
->fConverter
) {
259 codepage
= ucnv_getName(file
->fConverter
, &status
);
260 if(U_FAILURE(status
))
266 U_CAPI
int32_t U_EXPORT2
/* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
267 u_fsetcodepage( const char *codepage
,
270 UErrorCode status
= U_ZERO_ERROR
;
273 /* We use the normal default codepage for this system, and not the one for the locale. */
274 if ((file
->str
.fPos
== file
->str
.fBuffer
) && (file
->str
.fLimit
== file
->str
.fBuffer
)) {
275 ucnv_close(file
->fConverter
);
276 file
->fConverter
= ucnv_open(codepage
, &status
);
277 if(U_SUCCESS(status
)) {
285 U_CAPI UConverter
* U_EXPORT2
/* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
286 u_fgetConverter(UFILE
*file
)
288 return file
->fConverter
;