]> git.saurik.com Git - apple/icu.git/blob - icuSources/io/ufile.c
ICU-6.2.4.tar.gz
[apple/icu.git] / icuSources / io / ufile.c
1 /*
2 ******************************************************************************
3 *
4 * Copyright (C) 1998-2004, International Business Machines
5 * Corporation and others. All Rights Reserved.
6 *
7 ******************************************************************************
8 *
9 * File ufile.c
10 *
11 * Modification History:
12 *
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 ******************************************************************************
19 */
20
21 /* define for fileno. */
22 #ifndef _XOPEN_SOURCE
23 #define _XOPEN_SOURCE 4
24 #endif
25
26 #include "locmap.h"
27 #include "unicode/ustdio.h"
28 #include "ufile.h"
29 #include "unicode/uloc.h"
30 #include "unicode/ures.h"
31 #include "unicode/ucnv.h"
32 #include "cstring.h"
33 #include "cmemory.h"
34
35 #ifdef WIN32
36 /* Windows likes to rename Unix-like functions */
37 #define fileno _fileno
38 #endif
39
40 U_CAPI UFILE* U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
41 u_finit(FILE *f,
42 const char *locale,
43 const char *codepage)
44 {
45 UErrorCode status = U_ZERO_ERROR;
46 UFILE *result = (UFILE*) uprv_malloc(sizeof(UFILE));
47 if(result == NULL || f == NULL) {
48 return 0;
49 }
50
51 uprv_memset(result, 0, sizeof(UFILE));
52 result->fFileno = fileno(f);
53
54 #ifdef WIN32
55 if (0 <= result->fFileno && result->fFileno <= 2) {
56 /* stdin, stdout and stderr need to be special cased for Windows 98 */
57 result->fFile = &_iob[_fileno(f)];
58 }
59 else
60 #endif
61 {
62 result->fFile = f;
63 }
64
65 result->str.fBuffer = result->fUCBuffer;
66 result->str.fPos = result->fUCBuffer;
67 result->str.fLimit = result->fUCBuffer;
68
69 #if !UCONFIG_NO_FORMATTING
70 /* if locale is 0, use the default */
71 if(locale == 0) {
72 locale = uloc_getDefault();
73 }
74
75 if(u_locbund_init(&result->str.fBundle, locale) == 0) {
76 /* DO NOT FCLOSE HERE! */
77 uprv_free(result);
78 return 0;
79 }
80 #endif
81
82 /* If the codepage is not "" use the ucnv_open default behavior */
83 if(codepage == NULL || *codepage != '\0') {
84 result->fConverter = ucnv_open(codepage, &status);
85 }
86 /* else result->fConverter is already memset'd to NULL. */
87
88 if(U_FAILURE(status)) {
89 #if !UCONFIG_NO_FORMATTING
90 u_locbund_close(&result->str.fBundle);
91 #endif
92 /* DO NOT fclose here!!!!!! */
93 uprv_free(result);
94 result = NULL;
95 }
96
97 return result;
98 }
99
100 U_CAPI UFILE* U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
101 u_fopen(const char *filename,
102 const char *perm,
103 const char *locale,
104 const char *codepage)
105 {
106 UFILE *result;
107 FILE *systemFile = fopen(filename, perm);
108 if(systemFile == 0) {
109 return 0;
110 }
111
112 result = u_finit(systemFile, locale, codepage);
113
114 if (result) {
115 result->fOwnFile = TRUE;
116 }
117 else {
118 /* Something bad happened.
119 Maybe the converter couldn't be opened. */
120 fclose(systemFile);
121 }
122
123 return result;
124 }
125
126 U_CAPI UFILE* U_EXPORT2
127 u_fstropen(UChar *stringBuf,
128 int32_t capacity,
129 const char *locale)
130 {
131 UFILE *result;
132
133 if (capacity < 0) {
134 return NULL;
135 }
136
137 result = (UFILE*) uprv_malloc(sizeof(UFILE));
138 uprv_memset(result, 0, sizeof(UFILE));
139 result->str.fBuffer = stringBuf;
140 result->str.fPos = stringBuf;
141 result->str.fLimit = stringBuf+capacity;
142
143 #if !UCONFIG_NO_FORMATTING
144 /* if locale is 0, use the default */
145 if(locale == 0) {
146 locale = uloc_getDefault();
147 }
148
149 if(u_locbund_init(&result->str.fBundle, locale) == 0) {
150 /* DO NOT FCLOSE HERE! */
151 uprv_free(result);
152 return 0;
153 }
154 #endif
155
156 return result;
157 }
158
159 U_CAPI UBool U_EXPORT2
160 u_feof(UFILE *f)
161 {
162 UBool endOfBuffer;
163 if (f == NULL) {
164 return TRUE;
165 }
166 endOfBuffer = (UBool)(f->str.fPos >= f->str.fLimit);
167 if (f->fFile != NULL) {
168 return endOfBuffer && feof(f->fFile);
169 }
170 return endOfBuffer;
171 }
172
173 U_CAPI void U_EXPORT2
174 u_fflush(UFILE *file)
175 {
176 ufile_flush_translit(file);
177 if (file->fFile) {
178 fflush(file->fFile);
179 }
180 else if (file->str.fPos < file->str.fLimit) {
181 *(file->str.fPos++) = 0;
182 }
183 /* TODO: flush input */
184 }
185
186 U_CAPI void
187 u_frewind(UFILE *file)
188 {
189 u_fflush(file);
190 ucnv_reset(file->fConverter);
191 if (file->fFile) {
192 rewind(file->fFile);
193 file->str.fLimit = file->fUCBuffer;
194 file->str.fPos = file->fUCBuffer;
195 }
196 else {
197 file->str.fPos = file->str.fBuffer;
198 }
199 }
200
201 U_CAPI void U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
202 u_fclose(UFILE *file)
203 {
204 u_fflush(file);
205 ufile_close_translit(file);
206
207 if(file->fOwnFile)
208 fclose(file->fFile);
209
210 #if !UCONFIG_NO_FORMATTING
211 u_locbund_close(&file->str.fBundle);
212 #endif
213
214 ucnv_close(file->fConverter);
215 uprv_free(file);
216 }
217
218 U_CAPI FILE* U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
219 u_fgetfile( UFILE *f)
220 {
221 return f->fFile;
222 }
223
224 #if !UCONFIG_NO_FORMATTING
225
226 U_CAPI const char* U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
227 u_fgetlocale( UFILE *file)
228 {
229 return file->str.fBundle.fLocale;
230 }
231
232 U_CAPI int32_t U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
233 u_fsetlocale(UFILE *file,
234 const char *locale)
235 {
236 u_locbund_close(&file->str.fBundle);
237
238 return u_locbund_init(&file->str.fBundle, locale) == 0 ? -1 : 0;
239 }
240
241 #endif
242
243 U_CAPI const char* U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
244 u_fgetcodepage(UFILE *file)
245 {
246 UErrorCode status = U_ZERO_ERROR;
247 const char *codepage = NULL;
248
249 if (file->fConverter) {
250 codepage = ucnv_getName(file->fConverter, &status);
251 if(U_FAILURE(status))
252 return 0;
253 }
254 return codepage;
255 }
256
257 U_CAPI int32_t U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
258 u_fsetcodepage( const char *codepage,
259 UFILE *file)
260 {
261 UErrorCode status = U_ZERO_ERROR;
262 int32_t retVal = -1;
263
264 /* We use the normal default codepage for this system, and not the one for the locale. */
265 if ((file->str.fPos == file->str.fBuffer) && (file->str.fLimit == file->str.fBuffer)) {
266 ucnv_close(file->fConverter);
267 file->fConverter = ucnv_open(codepage, &status);
268 if(U_SUCCESS(status)) {
269 retVal = 0;
270 }
271 }
272 return retVal;
273 }
274
275
276 U_CAPI UConverter * U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
277 u_fgetConverter(UFILE *file)
278 {
279 return file->fConverter;
280 }
281