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