1 /*
2 ******************************************************************************
3 *
4 *   Copyright (C) 1998-2014, 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 /*
22  * fileno is not declared when building with GCC in strict mode.
23  */
24 #if defined(__GNUC__) && defined(__STRICT_ANSI__)
25 #undef __STRICT_ANSI__
26 #endif
27 
28 #include "locmap.h"
29 #include "unicode/ustdio.h"
30 
31 #if !UCONFIG_NO_CONVERSION
32 
33 #include "ufile.h"
34 #include "unicode/uloc.h"
35 #include "unicode/ures.h"
36 #include "unicode/ucnv.h"
37 #include "unicode/ustring.h"
38 #include "cstring.h"
39 #include "cmemory.h"
40 
41 #if U_PLATFORM_USES_ONLY_WIN32_API && !defined(fileno)
42 /* Windows likes to rename Unix-like functions */
43 #define fileno _fileno
44 #endif
45 
46 static UFILE*
finit_owner(FILE * f,const char * locale,const char * codepage,UBool takeOwnership)47 finit_owner(FILE         *f,
48               const char *locale,
49               const char *codepage,
50               UBool       takeOwnership
51               )
52 {
53     UErrorCode status = U_ZERO_ERROR;
54     UFILE     *result;
55     if(f == NULL) {
56         return 0;
57     }
58     result = (UFILE*) uprv_malloc(sizeof(UFILE));
59     if(result == NULL) {
60         return 0;
61     }
62 
63     uprv_memset(result, 0, sizeof(UFILE));
64     result->fFileno = fileno(f);
65 
66 #if U_PLATFORM_USES_ONLY_WIN32_API
67     if (0 <= result->fFileno && result->fFileno <= 2) {
68         /* stdin, stdout and stderr need to be special cased for Windows 98 */
69 #if _MSC_VER >= 1400
70         result->fFile = &__iob_func()[_fileno(f)];
71 #else
72         result->fFile = &_iob[_fileno(f)];
73 #endif
74     }
75     else
76 #endif
77     {
78         result->fFile = f;
79     }
80 
81     result->str.fBuffer = result->fUCBuffer;
82     result->str.fPos    = result->fUCBuffer;
83     result->str.fLimit  = result->fUCBuffer;
84 
85 #if !UCONFIG_NO_FORMATTING
86         /* if locale is 0, use the default */
87         if(u_locbund_init(&result->str.fBundle, locale) == 0) {
88             /* DO NOT FCLOSE HERE! */
89             uprv_free(result);
90             return 0;
91         }
92 #endif
93 
94     /* If the codepage is not "" use the ucnv_open default behavior */
95     if(codepage == NULL || *codepage != '\0') {
96         result->fConverter = ucnv_open(codepage, &status);
97     }
98     /* else result->fConverter is already memset'd to NULL. */
99 
100     if(U_SUCCESS(status)) {
101         result->fOwnFile = takeOwnership;
102     }
103     else {
104 #if !UCONFIG_NO_FORMATTING
105         u_locbund_close(&result->str.fBundle);
106 #endif
107         /* DO NOT fclose here!!!!!! */
108         uprv_free(result);
109         result = NULL;
110     }
111 
112     return result;
113 }
114 
115 U_CAPI UFILE* U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
u_finit(FILE * f,const char * locale,const char * codepage)116 u_finit(FILE          *f,
117         const char    *locale,
118         const char    *codepage)
119 {
120     return finit_owner(f, locale, codepage, FALSE);
121 }
122 
123 U_CAPI UFILE* U_EXPORT2
u_fadopt(FILE * f,const char * locale,const char * codepage)124 u_fadopt(FILE         *f,
125         const char    *locale,
126         const char    *codepage)
127 {
128     return finit_owner(f, locale, codepage, TRUE);
129 }
130 
131 U_CAPI UFILE* U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
u_fopen(const char * filename,const char * perm,const char * locale,const char * codepage)132 u_fopen(const char    *filename,
133         const char    *perm,
134         const char    *locale,
135         const char    *codepage)
136 {
137     UFILE     *result;
138     FILE     *systemFile = fopen(filename, perm);
139     if(systemFile == 0) {
140         return 0;
141     }
142 
143     result = finit_owner(systemFile, locale, codepage, TRUE);
144 
145     if (!result) {
146         /* Something bad happened.
147            Maybe the converter couldn't be opened. */
148         fclose(systemFile);
149     }
150 
151     return result; /* not a file leak */
152 }
153 
154 U_CAPI UFILE* U_EXPORT2
u_fopen_u(const UChar * filename,const char * perm,const char * locale,const char * codepage)155 u_fopen_u(const UChar   *filename,
156         const char    *perm,
157         const char    *locale,
158         const char    *codepage)
159 {
160     UFILE     *result;
161     char buffer[256];
162 
163     u_austrcpy(buffer, filename);
164 
165     result = u_fopen(buffer, perm, locale, codepage);
166 #if U_PLATFORM_USES_ONLY_WIN32_API
167     /* Try Windows API _wfopen if the above fails. */
168     if (!result) {
169         FILE *systemFile = _wfopen(filename, (UChar*)perm);
170         if (systemFile) {
171             result = finit_owner(systemFile, locale, codepage, TRUE);
172         }
173         if (!result) {
174             /* Something bad happened.
175                Maybe the converter couldn't be opened. */
176             fclose(systemFile);
177         }
178     }
179 #endif
180     return result; /* not a file leak */
181 }
182 
183 U_CAPI UFILE* U_EXPORT2
u_fstropen(UChar * stringBuf,int32_t capacity,const char * locale)184 u_fstropen(UChar *stringBuf,
185            int32_t      capacity,
186            const char  *locale)
187 {
188     UFILE *result;
189 
190     if (capacity < 0) {
191         return NULL;
192     }
193 
194     result = (UFILE*) uprv_malloc(sizeof(UFILE));
195     /* Null pointer test */
196     if (result == NULL) {
197     	return NULL; /* Just get out. */
198     }
199     uprv_memset(result, 0, sizeof(UFILE));
200     result->str.fBuffer = stringBuf;
201     result->str.fPos    = stringBuf;
202     result->str.fLimit  = stringBuf+capacity;
203 
204 #if !UCONFIG_NO_FORMATTING
205     /* if locale is 0, use the default */
206     if(u_locbund_init(&result->str.fBundle, locale) == 0) {
207         /* DO NOT FCLOSE HERE! */
208         uprv_free(result);
209         return 0;
210     }
211 #endif
212 
213     return result;
214 }
215 
216 U_CAPI UBool U_EXPORT2
u_feof(UFILE * f)217 u_feof(UFILE  *f)
218 {
219     UBool endOfBuffer;
220     if (f == NULL) {
221         return TRUE;
222     }
223     endOfBuffer = (UBool)(f->str.fPos >= f->str.fLimit);
224     if (f->fFile != NULL) {
225         return endOfBuffer && feof(f->fFile);
226     }
227     return endOfBuffer;
228 }
229 
230 U_CAPI void U_EXPORT2
u_fflush(UFILE * file)231 u_fflush(UFILE *file)
232 {
233     ufile_flush_translit(file);
234     ufile_flush_io(file);
235     if (file->fFile) {
236         fflush(file->fFile);
237     }
238     else if (file->str.fPos < file->str.fLimit) {
239         *(file->str.fPos++) = 0;
240     }
241     /* TODO: flush input */
242 }
243 
244 U_CAPI void
u_frewind(UFILE * file)245 u_frewind(UFILE *file)
246 {
247     u_fflush(file);
248     ucnv_reset(file->fConverter);
249     if (file->fFile) {
250         rewind(file->fFile);
251         file->str.fLimit = file->fUCBuffer;
252         file->str.fPos   = file->fUCBuffer;
253     }
254     else {
255         file->str.fPos = file->str.fBuffer;
256     }
257 }
258 
259 U_CAPI void U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
u_fclose(UFILE * file)260 u_fclose(UFILE *file)
261 {
262     if (file) {
263         u_fflush(file);
264         ufile_close_translit(file);
265 
266         if(file->fOwnFile)
267             fclose(file->fFile);
268 
269 #if !UCONFIG_NO_FORMATTING
270         u_locbund_close(&file->str.fBundle);
271 #endif
272 
273         ucnv_close(file->fConverter);
274         uprv_free(file);
275     }
276 }
277 
278 U_CAPI FILE* U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
u_fgetfile(UFILE * f)279 u_fgetfile(    UFILE         *f)
280 {
281     return f->fFile;
282 }
283 
284 #if !UCONFIG_NO_FORMATTING
285 
286 U_CAPI const char*  U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
u_fgetlocale(UFILE * file)287 u_fgetlocale(    UFILE        *file)
288 {
289     return file->str.fBundle.fLocale;
290 }
291 
292 U_CAPI int32_t U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
u_fsetlocale(UFILE * file,const char * locale)293 u_fsetlocale(UFILE      *file,
294              const char *locale)
295 {
296     u_locbund_close(&file->str.fBundle);
297 
298     return u_locbund_init(&file->str.fBundle, locale) == 0 ? -1 : 0;
299 }
300 
301 #endif
302 
303 U_CAPI const char* U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
u_fgetcodepage(UFILE * file)304 u_fgetcodepage(UFILE        *file)
305 {
306     UErrorCode     status = U_ZERO_ERROR;
307     const char     *codepage = NULL;
308 
309     if (file->fConverter) {
310         codepage = ucnv_getName(file->fConverter, &status);
311         if(U_FAILURE(status))
312             return 0;
313     }
314     return codepage;
315 }
316 
317 U_CAPI int32_t U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
u_fsetcodepage(const char * codepage,UFILE * file)318 u_fsetcodepage(    const char    *codepage,
319                UFILE        *file)
320 {
321     UErrorCode status = U_ZERO_ERROR;
322     int32_t retVal = -1;
323 
324     /* We use the normal default codepage for this system, and not the one for the locale. */
325     if ((file->str.fPos == file->str.fBuffer) && (file->str.fLimit == file->str.fBuffer)) {
326         ucnv_close(file->fConverter);
327         file->fConverter = ucnv_open(codepage, &status);
328         if(U_SUCCESS(status)) {
329             retVal = 0;
330         }
331     }
332     return retVal;
333 }
334 
335 
336 U_CAPI UConverter * U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
u_fgetConverter(UFILE * file)337 u_fgetConverter(UFILE *file)
338 {
339     return file->fConverter;
340 }
341 #if !UCONFIG_NO_FORMATTING
u_fgetNumberFormat(UFILE * file)342 U_CAPI const UNumberFormat* U_EXPORT2 u_fgetNumberFormat(UFILE *file)
343 {
344     return u_locbund_getNumberFormat(&file->str.fBundle, UNUM_DECIMAL);
345 }
346 #endif
347 
348 #endif
349