commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mt...@apache.org
Subject svn commit: r813559 - in /commons/sandbox/runtime/trunk/src/main/native: include/arch/windows/acr_arch_private.h os/win32/wutil.c
Date Thu, 10 Sep 2009 19:38:33 GMT
Author: mturk
Date: Thu Sep 10 19:38:33 2009
New Revision: 813559

URL: http://svn.apache.org/viewvc?rev=813559&view=rev
Log:
Add strduped version of to/from utf8 path conversion

Modified:
    commons/sandbox/runtime/trunk/src/main/native/include/arch/windows/acr_arch_private.h
    commons/sandbox/runtime/trunk/src/main/native/os/win32/wutil.c

Modified: commons/sandbox/runtime/trunk/src/main/native/include/arch/windows/acr_arch_private.h
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/arch/windows/acr_arch_private.h?rev=813559&r1=813558&r2=813559&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/arch/windows/acr_arch_private.h
(original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/arch/windows/acr_arch_private.h
Thu Sep 10 19:38:33 2009
@@ -192,6 +192,8 @@
 
 int      utf8_to_unicode_path(wchar_t *, size_t, const char *);
 int      unicode_to_utf8_path(char *, size_t, const wchar_t *);
+char    *x_strdup_utf8_path(const wchar_t *);
+wchar_t *x_wcsdup_utf8_path(const char *);
 wchar_t *x_wcsdup_utf8(const char *);
 char    *x_strdup_utf8(const wchar_t *);
 

Modified: commons/sandbox/runtime/trunk/src/main/native/os/win32/wutil.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/wutil.c?rev=813559&r1=813558&r2=813559&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/win32/wutil.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/win32/wutil.c Thu Sep 10 19:38:33 2009
@@ -236,6 +236,30 @@
     return 0;
 }
 
+wchar_t *x_wcsdup_utf8_path(const char *str)
+{
+    int      rc;
+    int      len;
+    wchar_t *res;
+    if (!str)
+        return NULL;
+    if (!(len = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS,
+                                    str, -1, NULL, 0))) {
+        if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
+            ACR_SET_OS_ERROR(ACR_EILSEQ);
+        return NULL;
+    }
+    res = x_malloc(len * sizeof(wchar_t));
+    if (!res)
+        return NULL;
+    if ((rc = utf8_to_unicode_path(res, len, str))) {
+        x_free(res);
+        res = NULL;
+        SetLastError(rc);
+    }
+    return res;
+}
+
 wchar_t *x_wcsdup_utf8(const char *str)
 {
     int      len;
@@ -268,6 +292,40 @@
     }
 }
 
+char *x_strdup_utf8_path(const wchar_t *str)
+{
+    int   rc;
+    int   len;
+    char *res;
+
+    /*
+     * ###: We could use here the
+     * wcslen * 4 since any wchar can produce at most 4 chars.
+     * This is memory vs speed tradeof, and if
+     * conversion fails we'd need to destroy pre-allocated memory.
+     * Use Windows API to determine the required destination length.
+     *
+     */
+    if (!(len = WideCharToMultiByte(CP_UTF8, 0, str, -1, NULL, 0,
+                                    NULL, NULL))) {
+        if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
+            errno = EILSEQ;
+        return NULL;
+    }
+    if (!(res = x_malloc(len))) {
+        /* Make sure GetLastError returns ENOMEM
+         */
+        ACR_SET_OS_ERROR(ACR_ENOMEM);
+        return NULL;
+    }
+    if ((rc = unicode_to_utf8_path(res, len, str))) {
+        x_free(res);
+        res = NULL;
+        SetLastError(rc);
+    }
+    return res;
+}
+
 char *x_strdup_utf8(const wchar_t *str)
 {
     int   len;



Mime
View raw message