diff common/portpath.c @ 1157:fd7d04bb37cb

(issue36) Add encoding aware port_fopen function and use it
author Andre Heinecke <andre.heinecke@intevation.de>
date Thu, 18 Sep 2014 15:43:22 +0200
parents f110a3f6e387
children 0a803c3fb5a6
line wrap: on
line diff
--- a/common/portpath.c	Thu Sep 18 12:56:31 2014 +0200
+++ b/common/portpath.c	Thu Sep 18 15:43:22 2014 +0200
@@ -19,6 +19,10 @@
 #include <unistd.h>
 #include <string.h>
 
+#ifdef WIN32
+#include <share.h>
+#endif
+
 char *
 port_dirname(char *path)
 {
@@ -140,3 +144,53 @@
   else
     return false;
 }
+
+FILE*
+port_fopen_rb(const char *path, bool exclusive)
+{
+  FILE *f = NULL;
+  if (!path)
+    {
+      return NULL;
+    }
+#ifdef WIN32
+    {
+      wchar_t *wFilename = utf8_to_wchar(path, strlen(path));
+      if (!wFilename)
+        {
+          ERRORPRINTF ("Invalid encoding\n");
+          return NULL;
+        }
+      /* We open and write protect the file here so that
+         as long as the file is open we can be sure that
+         it was not modified and can use it in subsequent
+         calls based on the filename. */
+      OutputDebugStringW(wFilename);
+      if (exclusive)
+        {
+          f = _wfsopen(wFilename, L"rb", _SH_DENYWR);
+        }
+      else
+        {
+          f = _wfopen(wFilename, L"rb");
+        }
+      xfree(wFilename);
+      if (f == NULL)
+        {
+          /* Fall back to local8 bit encoding */
+          if (exclusive)
+            {
+              f = _fsopen(path, "rb", _SH_DENYWR);
+            }
+          else
+            {
+              f = fopen(path, "rb");
+            }
+        }
+    }
+#else
+  (void)(exclusive);
+  f = fopen(path, "rb");
+#endif
+  return f;
+}

http://wald.intevation.org/projects/trustbridge/