]> git.saurik.com Git - apple/icu.git/blobdiff - icuSources/io/ufile.c
ICU-511.31.tar.gz
[apple/icu.git] / icuSources / io / ufile.c
index c1870ecbcef06c7947d6a271136d4a999a0feda5..820071f1bcfc7a60d0286b0aa550d7f2ecf934a4 100644 (file)
@@ -1,7 +1,7 @@
 /*
 ******************************************************************************
 *
-*   Copyright (C) 1998-2004, International Business Machines
+*   Copyright (C) 1998-2013, International Business Machines
 *   Corporation and others.  All Rights Reserved.
 *
 ******************************************************************************
 ******************************************************************************
 */
 
-/* define for fileno.  */
-#ifndef _XOPEN_SOURCE
-#define _XOPEN_SOURCE 4
+/*
+ * fileno is not declared when building with GCC in strict mode.
+ */
+#if defined(__GNUC__) && defined(__STRICT_ANSI__)
+#undef __STRICT_ANSI__
 #endif
 
 #include "locmap.h"
 #include "cstring.h"
 #include "cmemory.h"
 
-#ifdef WIN32
+#if U_PLATFORM_USES_ONLY_WIN32_API && !defined(fileno)
 /* Windows likes to rename Unix-like functions */
 #define fileno _fileno
 #endif
 
-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)
+static UFILE*
+finit_owner(FILE         *f,
+              const char *locale,
+              const char *codepage,
+              UBool       takeOwnership
+              )
 {
-    UErrorCode status     = U_ZERO_ERROR;
-    UFILE     *result     = (UFILE*) uprv_malloc(sizeof(UFILE));
-    if(result == NULL || f == NULL) {
+    UErrorCode status = U_ZERO_ERROR;
+    UFILE     *result;
+    if(f == NULL) {
+        return 0;
+    }
+    result = (UFILE*) uprv_malloc(sizeof(UFILE));
+    if(result == NULL) {
         return 0;
     }
 
     uprv_memset(result, 0, sizeof(UFILE));
     result->fFileno = fileno(f);
 
-#ifdef WIN32
+#if U_PLATFORM_USES_ONLY_WIN32_API
     if (0 <= result->fFileno && result->fFileno <= 2) {
         /* stdin, stdout and stderr need to be special cased for Windows 98 */
+#if _MSC_VER >= 1400
+        result->fFile = &__iob_func()[_fileno(f)];
+#else
         result->fFile = &_iob[_fileno(f)];
+#endif
     }
     else
 #endif
@@ -68,10 +80,6 @@ u_finit(FILE        *f,
 
 #if !UCONFIG_NO_FORMATTING
         /* if locale is 0, use the default */
-        if(locale == 0) {
-            locale = uloc_getDefault();
-        }
-
         if(u_locbund_init(&result->str.fBundle, locale) == 0) {
             /* DO NOT FCLOSE HERE! */
             uprv_free(result);
@@ -85,7 +93,10 @@ u_finit(FILE        *f,
     }
     /* else result->fConverter is already memset'd to NULL. */
 
-    if(U_FAILURE(status)) {
+    if(U_SUCCESS(status)) {
+        result->fOwnFile = takeOwnership;
+    }
+    else {
 #if !UCONFIG_NO_FORMATTING
         u_locbund_close(&result->str.fBundle);
 #endif
@@ -97,6 +108,22 @@ u_finit(FILE        *f,
     return result;
 }
 
+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)
+{
+    return finit_owner(f, locale, codepage, FALSE);
+}
+
+U_CAPI UFILE* U_EXPORT2
+u_fadopt(FILE         *f,
+        const char    *locale,
+        const char    *codepage)
+{
+    return finit_owner(f, locale, codepage, TRUE);
+}
+
 U_CAPI UFILE* U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
 u_fopen(const char    *filename,
         const char    *perm,
@@ -109,18 +136,15 @@ u_fopen(const char    *filename,
         return 0;
     }
 
-    result = u_finit(systemFile, locale, codepage);
+    result = finit_owner(systemFile, locale, codepage, TRUE);
 
-    if (result) {
-        result->fOwnFile = TRUE;
-    }
-    else {
+    if (!result) {
         /* Something bad happened.
            Maybe the converter couldn't be opened. */
         fclose(systemFile);
     }
 
-    return result;
+    return result; /* not a file leak */
 }
 
 U_CAPI UFILE* U_EXPORT2
@@ -135,6 +159,10 @@ u_fstropen(UChar *stringBuf,
     }
 
     result = (UFILE*) uprv_malloc(sizeof(UFILE));
+    /* Null pointer test */
+    if (result == NULL) {
+       return NULL; /* Just get out. */
+    }
     uprv_memset(result, 0, sizeof(UFILE));
     result->str.fBuffer = stringBuf;
     result->str.fPos    = stringBuf;
@@ -142,10 +170,6 @@ u_fstropen(UChar *stringBuf,
 
 #if !UCONFIG_NO_FORMATTING
     /* if locale is 0, use the default */
-    if(locale == 0) {
-        locale = uloc_getDefault();
-    }
-
     if(u_locbund_init(&result->str.fBundle, locale) == 0) {
         /* DO NOT FCLOSE HERE! */
         uprv_free(result);
@@ -174,6 +198,7 @@ U_CAPI void U_EXPORT2
 u_fflush(UFILE *file)
 {
     ufile_flush_translit(file);
+    ufile_flush_io(file);
     if (file->fFile) {
         fflush(file->fFile);
     }
@@ -201,18 +226,20 @@ u_frewind(UFILE *file)
 U_CAPI void U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
 u_fclose(UFILE *file)
 {
-    u_fflush(file);
-    ufile_close_translit(file);
+    if (file) {
+        u_fflush(file);
+        ufile_close_translit(file);
 
-    if(file->fOwnFile)
-        fclose(file->fFile);
+        if(file->fOwnFile)
+            fclose(file->fFile);
 
 #if !UCONFIG_NO_FORMATTING
-    u_locbund_close(&file->str.fBundle);
+        u_locbund_close(&file->str.fBundle);
 #endif
 
-    ucnv_close(file->fConverter);
-    uprv_free(file);
+        ucnv_close(file->fConverter);
+        uprv_free(file);
+    }
 }
 
 U_CAPI FILE* U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
@@ -278,4 +305,10 @@ u_fgetConverter(UFILE *file)
 {
     return file->fConverter;
 }
+#if !UCONFIG_NO_FORMATTING
+U_CAPI const UNumberFormat* U_EXPORT2 u_fgetNumberFormat(UFILE *file)
+{
+    return u_locbund_getNumberFormat(&file->str.fBundle, UNUM_DECIMAL);
+}
+#endif