]> git.saurik.com Git - apple/icu.git/blame - icuSources/io/ufile.c
ICU-6.2.22.tar.gz
[apple/icu.git] / icuSources / io / ufile.c
CommitLineData
b75a7d8f
A
1/*
2******************************************************************************
3*
374ca955 4* Copyright (C) 1998-2004, International Business Machines
b75a7d8f
A
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
374ca955
A
21/* define for fileno. */
22#ifndef _XOPEN_SOURCE
23#define _XOPEN_SOURCE 4
24#endif
25
b75a7d8f
A
26#include "locmap.h"
27#include "unicode/ustdio.h"
28#include "ufile.h"
29#include "unicode/uloc.h"
b75a7d8f
A
30#include "unicode/ures.h"
31#include "unicode/ucnv.h"
32#include "cstring.h"
33#include "cmemory.h"
34
374ca955
A
35#ifdef WIN32
36/* Windows likes to rename Unix-like functions */
37#define fileno _fileno
38#endif
b75a7d8f 39
374ca955
A
40U_CAPI UFILE* U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
41u_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 }
b75a7d8f 50
374ca955
A
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)];
b75a7d8f 58 }
374ca955 59 else
b75a7d8f 60#endif
374ca955
A
61 {
62 result->fFile = f;
b75a7d8f
A
63 }
64
374ca955
A
65 result->str.fBuffer = result->fUCBuffer;
66 result->str.fPos = result->fUCBuffer;
67 result->str.fLimit = result->fUCBuffer;
b75a7d8f 68
374ca955
A
69#if !UCONFIG_NO_FORMATTING
70 /* if locale is 0, use the default */
71 if(locale == 0) {
72 locale = uloc_getDefault();
73 }
b75a7d8f 74
374ca955
A
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}
b75a7d8f
A
99
100U_CAPI UFILE* U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
101u_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 }
374ca955
A
117 else {
118 /* Something bad happened.
119 Maybe the converter couldn't be opened. */
120 fclose(systemFile);
121 }
b75a7d8f
A
122
123 return result;
124}
125
374ca955
A
126U_CAPI UFILE* U_EXPORT2
127u_fstropen(UChar *stringBuf,
128 int32_t capacity,
129 const char *locale)
b75a7d8f 130{
374ca955
A
131 UFILE *result;
132
133 if (capacity < 0) {
134 return NULL;
b75a7d8f
A
135 }
136
374ca955 137 result = (UFILE*) uprv_malloc(sizeof(UFILE));
b75a7d8f 138 uprv_memset(result, 0, sizeof(UFILE));
374ca955
A
139 result->str.fBuffer = stringBuf;
140 result->str.fPos = stringBuf;
141 result->str.fLimit = stringBuf+capacity;
b75a7d8f 142
b75a7d8f 143#if !UCONFIG_NO_FORMATTING
374ca955
A
144 /* if locale is 0, use the default */
145 if(locale == 0) {
146 locale = uloc_getDefault();
b75a7d8f
A
147 }
148
374ca955
A
149 if(u_locbund_init(&result->str.fBundle, locale) == 0) {
150 /* DO NOT FCLOSE HERE! */
151 uprv_free(result);
152 return 0;
b75a7d8f 153 }
374ca955
A
154#endif
155
b75a7d8f
A
156 return result;
157}
158
374ca955
A
159U_CAPI UBool U_EXPORT2
160u_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}
b75a7d8f
A
172
173U_CAPI void U_EXPORT2
174u_fflush(UFILE *file)
175{
374ca955
A
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
186U_CAPI void
187u_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 }
b75a7d8f
A
199}
200
201U_CAPI void U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
202u_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
374ca955 211 u_locbund_close(&file->str.fBundle);
b75a7d8f
A
212#endif
213
214 ucnv_close(file->fConverter);
215 uprv_free(file);
216}
217
218U_CAPI FILE* U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
219u_fgetfile( UFILE *f)
220{
221 return f->fFile;
222}
223
224#if !UCONFIG_NO_FORMATTING
225
226U_CAPI const char* U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
227u_fgetlocale( UFILE *file)
228{
374ca955 229 return file->str.fBundle.fLocale;
b75a7d8f
A
230}
231
232U_CAPI int32_t U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
374ca955
A
233u_fsetlocale(UFILE *file,
234 const char *locale)
b75a7d8f 235{
374ca955 236 u_locbund_close(&file->str.fBundle);
b75a7d8f 237
374ca955 238 return u_locbund_init(&file->str.fBundle, locale) == 0 ? -1 : 0;
b75a7d8f
A
239}
240
241#endif
242
243U_CAPI const char* U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
244u_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
257U_CAPI int32_t U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
258u_fsetcodepage( const char *codepage,
259 UFILE *file)
260{
261 UErrorCode status = U_ZERO_ERROR;
374ca955
A
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 }
b75a7d8f 271 }
374ca955 272 return retVal;
b75a7d8f
A
273}
274
275
276U_CAPI UConverter * U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
277u_fgetConverter(UFILE *file)
278{
279 return file->fConverter;
280}
374ca955 281