]>
Commit | Line | Data |
---|---|---|
b75a7d8f A |
1 | /* |
2 | ****************************************************************************** | |
3 | * | |
46f4442e | 4 | * Copyright (C) 1998-2008, 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 | |
73c04bcf A |
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 | |
374ca955 A |
27 | #define _XOPEN_SOURCE 4 |
28 | #endif | |
73c04bcf | 29 | #endif |
374ca955 | 30 | |
b75a7d8f A |
31 | #include "locmap.h" |
32 | #include "unicode/ustdio.h" | |
33 | #include "ufile.h" | |
34 | #include "unicode/uloc.h" | |
b75a7d8f A |
35 | #include "unicode/ures.h" |
36 | #include "unicode/ucnv.h" | |
37 | #include "cstring.h" | |
38 | #include "cmemory.h" | |
39 | ||
73c04bcf | 40 | #if defined(U_WINDOWS) && !defined(fileno) |
374ca955 A |
41 | /* Windows likes to rename Unix-like functions */ |
42 | #define fileno _fileno | |
43 | #endif | |
b75a7d8f | 44 | |
374ca955 A |
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 | { | |
73c04bcf A |
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) { | |
374ca955 A |
57 | return 0; |
58 | } | |
b75a7d8f | 59 | |
374ca955 A |
60 | uprv_memset(result, 0, sizeof(UFILE)); |
61 | result->fFileno = fileno(f); | |
62 | ||
73c04bcf | 63 | #ifdef U_WINDOWS |
374ca955 A |
64 | if (0 <= result->fFileno && result->fFileno <= 2) { |
65 | /* stdin, stdout and stderr need to be special cased for Windows 98 */ | |
73c04bcf A |
66 | #if _MSC_VER >= 1400 |
67 | result->fFile = &__iob_func()[_fileno(f)]; | |
68 | #else | |
374ca955 | 69 | result->fFile = &_iob[_fileno(f)]; |
73c04bcf | 70 | #endif |
b75a7d8f | 71 | } |
374ca955 | 72 | else |
b75a7d8f | 73 | #endif |
374ca955 A |
74 | { |
75 | result->fFile = f; | |
b75a7d8f A |
76 | } |
77 | ||
374ca955 A |
78 | result->str.fBuffer = result->fUCBuffer; |
79 | result->str.fPos = result->fUCBuffer; | |
80 | result->str.fLimit = result->fUCBuffer; | |
b75a7d8f | 81 | |
374ca955 A |
82 | #if !UCONFIG_NO_FORMATTING |
83 | /* if locale is 0, use the default */ | |
374ca955 A |
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 | } | |
b75a7d8f A |
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 | } | |
374ca955 A |
126 | else { |
127 | /* Something bad happened. | |
128 | Maybe the converter couldn't be opened. */ | |
129 | fclose(systemFile); | |
130 | } | |
b75a7d8f A |
131 | |
132 | return result; | |
133 | } | |
134 | ||
374ca955 A |
135 | U_CAPI UFILE* U_EXPORT2 |
136 | u_fstropen(UChar *stringBuf, | |
137 | int32_t capacity, | |
138 | const char *locale) | |
b75a7d8f | 139 | { |
374ca955 A |
140 | UFILE *result; |
141 | ||
142 | if (capacity < 0) { | |
143 | return NULL; | |
b75a7d8f A |
144 | } |
145 | ||
374ca955 | 146 | result = (UFILE*) uprv_malloc(sizeof(UFILE)); |
46f4442e A |
147 | /* Null pointer test */ |
148 | if (result == NULL) { | |
149 | return NULL; /* Just get out. */ | |
150 | } | |
b75a7d8f | 151 | uprv_memset(result, 0, sizeof(UFILE)); |
374ca955 A |
152 | result->str.fBuffer = stringBuf; |
153 | result->str.fPos = stringBuf; | |
154 | result->str.fLimit = stringBuf+capacity; | |
b75a7d8f | 155 | |
b75a7d8f | 156 | #if !UCONFIG_NO_FORMATTING |
374ca955 | 157 | /* if locale is 0, use the default */ |
374ca955 A |
158 | if(u_locbund_init(&result->str.fBundle, locale) == 0) { |
159 | /* DO NOT FCLOSE HERE! */ | |
160 | uprv_free(result); | |
161 | return 0; | |
b75a7d8f | 162 | } |
374ca955 A |
163 | #endif |
164 | ||
b75a7d8f A |
165 | return result; |
166 | } | |
167 | ||
374ca955 A |
168 | U_CAPI UBool U_EXPORT2 |
169 | u_feof(UFILE *f) | |
170 | { | |
171 | UBool endOfBuffer; | |
172 | if (f == NULL) { | |
173 | return TRUE; | |
174 | } | |
175 | endOfBuffer = (UBool)(f->str.fPos >= f->str.fLimit); | |
176 | if (f->fFile != NULL) { | |
177 | return endOfBuffer && feof(f->fFile); | |
178 | } | |
179 | return endOfBuffer; | |
180 | } | |
b75a7d8f A |
181 | |
182 | U_CAPI void U_EXPORT2 | |
183 | u_fflush(UFILE *file) | |
184 | { | |
374ca955 A |
185 | ufile_flush_translit(file); |
186 | if (file->fFile) { | |
187 | fflush(file->fFile); | |
188 | } | |
189 | else if (file->str.fPos < file->str.fLimit) { | |
190 | *(file->str.fPos++) = 0; | |
191 | } | |
192 | /* TODO: flush input */ | |
193 | } | |
194 | ||
195 | U_CAPI void | |
196 | u_frewind(UFILE *file) | |
197 | { | |
198 | u_fflush(file); | |
199 | ucnv_reset(file->fConverter); | |
200 | if (file->fFile) { | |
201 | rewind(file->fFile); | |
202 | file->str.fLimit = file->fUCBuffer; | |
203 | file->str.fPos = file->fUCBuffer; | |
204 | } | |
205 | else { | |
206 | file->str.fPos = file->str.fBuffer; | |
207 | } | |
b75a7d8f A |
208 | } |
209 | ||
210 | U_CAPI void U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */ | |
211 | u_fclose(UFILE *file) | |
212 | { | |
213 | u_fflush(file); | |
214 | ufile_close_translit(file); | |
215 | ||
216 | if(file->fOwnFile) | |
217 | fclose(file->fFile); | |
218 | ||
219 | #if !UCONFIG_NO_FORMATTING | |
374ca955 | 220 | u_locbund_close(&file->str.fBundle); |
b75a7d8f A |
221 | #endif |
222 | ||
223 | ucnv_close(file->fConverter); | |
224 | uprv_free(file); | |
225 | } | |
226 | ||
227 | U_CAPI FILE* U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */ | |
228 | u_fgetfile( UFILE *f) | |
229 | { | |
230 | return f->fFile; | |
231 | } | |
232 | ||
233 | #if !UCONFIG_NO_FORMATTING | |
234 | ||
235 | U_CAPI const char* U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */ | |
236 | u_fgetlocale( UFILE *file) | |
237 | { | |
374ca955 | 238 | return file->str.fBundle.fLocale; |
b75a7d8f A |
239 | } |
240 | ||
241 | U_CAPI int32_t U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */ | |
374ca955 A |
242 | u_fsetlocale(UFILE *file, |
243 | const char *locale) | |
b75a7d8f | 244 | { |
374ca955 | 245 | u_locbund_close(&file->str.fBundle); |
b75a7d8f | 246 | |
374ca955 | 247 | return u_locbund_init(&file->str.fBundle, locale) == 0 ? -1 : 0; |
b75a7d8f A |
248 | } |
249 | ||
250 | #endif | |
251 | ||
252 | U_CAPI const char* U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */ | |
253 | u_fgetcodepage(UFILE *file) | |
254 | { | |
255 | UErrorCode status = U_ZERO_ERROR; | |
256 | const char *codepage = NULL; | |
257 | ||
258 | if (file->fConverter) { | |
259 | codepage = ucnv_getName(file->fConverter, &status); | |
260 | if(U_FAILURE(status)) | |
261 | return 0; | |
262 | } | |
263 | return codepage; | |
264 | } | |
265 | ||
266 | U_CAPI int32_t U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */ | |
267 | u_fsetcodepage( const char *codepage, | |
268 | UFILE *file) | |
269 | { | |
270 | UErrorCode status = U_ZERO_ERROR; | |
374ca955 A |
271 | int32_t retVal = -1; |
272 | ||
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)) { | |
278 | retVal = 0; | |
279 | } | |
b75a7d8f | 280 | } |
374ca955 | 281 | return retVal; |
b75a7d8f A |
282 | } |
283 | ||
284 | ||
285 | U_CAPI UConverter * U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */ | |
286 | u_fgetConverter(UFILE *file) | |
287 | { | |
288 | return file->fConverter; | |
289 | } | |
374ca955 | 290 |