]> granicus.if.org Git - imagemagick/blobdiff - MagickCore/utility-private.h
(no commit message)
[imagemagick] / MagickCore / utility-private.h
index be6fd7c8e431afb3918a7b08524f2fa28eb05132..d144bbc8eee44d88584371785ec7fe44e8c0f9a5 100644 (file)
@@ -46,224 +46,224 @@ extern MagickPrivate void
   Windows UTF8 compatibility methods.
 */
 
-#if defined(MAGICKCORE_WINDOWS_SUPPORT)\r
-static inline wchar_t *create_wchar_string(const char *utf8)\r
-{\r
-  int\r
-    count;\r
\r
-  wchar_t\r
-    *wideChar;\r
\r
-  count=MultiByteToWideChar(CP_UTF8,0,utf8,-1,NULL,0);\r
-  wideChar=(WCHAR *) AcquireQuantumMemory(count,sizeof(*wideChar));\r
-  if (wideChar == (WCHAR *) NULL)\r
-    return((WCHAR *) NULL);\r
-  count=MultiByteToWideChar(CP_UTF8,0,utf8,-1,wideChar,count);\r
-  if (count == 0)\r
-    {\r
-      wideChar=(WCHAR *) RelinquishMagickMemory(wideChar);\r
-      return((WCHAR *) NULL);\r
-    }\r
-  return(wideChar);\r
-}\r
-\r
-static inline char *create_utf8_string(const wchar_t *wideChar)\r
-{\r
-  char\r
-    *utf8;\r
-\r
-  int\r
-    count;\r
-\r
-  count=WideCharToMultiByte(CP_UTF8,0,wideChar,-1,NULL,0,NULL,NULL);\r
-  if (count < 0)\r
-    return((char *) NULL);\r
-  utf8=(char *) AcquireQuantumMemory(count+1,sizeof(*utf8));\r
-  if (utf8 == (char *) NULL)\r
-    return((char *) NULL);\r
-  count=WideCharToMultiByte(CP_UTF8,0,wideChar,-1,utf8,count,NULL,NULL);\r
-  if (count == 0)\r
-    {\r
-      utf8=DestroyString(utf8);\r
-      return((char *) NULL);\r
-    }\r
-  utf8[count]=0;\r
-  return(utf8);\r
-}\r
-#endif\r
-\r
-static inline int access_utf8(const char *path,int mode)\r
-{\r
-#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__)\r
-  return(access(path,mode));\r
-#else\r
-   int\r
-     status;\r
-\r
-   wchar_t\r
-     *path_wide;\r
-\r
-   path_wide=create_wchar_string(path);\r
-   if (path_wide == (wchar_t *) NULL)\r
-     return(-1);\r
-   status=_waccess(path_wide,mode);\r
-   path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);\r
-   return(status);\r
-#endif\r
-}\r
-\r
-static inline FILE *fopen_utf8(const char *path,const char *mode)\r
-{\r
-#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__)\r
-  return(fopen(path,mode));\r
-#else\r
-   FILE\r
-     *file;\r
-\r
-   wchar_t\r
-     *mode_wide,\r
-     *path_wide;\r
-\r
-   path_wide=create_wchar_string(path);\r
-   if (path_wide == (wchar_t *) NULL)\r
-     return((FILE *) NULL);\r
-   mode_wide=create_wchar_string(mode);\r
-   if (mode_wide == (wchar_t *) NULL)\r
-     {\r
-       path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);\r
-       return((FILE *) NULL);\r
-     }\r
-   file=_wfopen(path_wide,mode_wide);\r
-   mode_wide=(wchar_t *) RelinquishMagickMemory(mode_wide);\r
-   path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);\r
-   return(file);\r
-#endif\r
-}\r
-\r
-#if defined(MAGICKCORE_WINDOWS_SUPPORT) && !defined(__CYGWIN__) && !defined(__MINGW32__) && !defined(__MINGW64__)\r
-typedef int\r
-  mode_t;\r
-#endif\r
-\r
-static inline int open_utf8(const char *path,int flags,mode_t mode)\r
-{\r
-#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__)\r
-  return(open(path,flags,mode));\r
-#else\r
-   int\r
-     status;\r
-\r
-   wchar_t\r
-     *path_wide;\r
-\r
-   path_wide=create_wchar_string(path);\r
-   if (path_wide == (wchar_t *) NULL)\r
-     return(-1);\r
-   status=_wopen(path_wide,flags,mode);\r
-   path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);\r
-   return(status);\r
-#endif\r
-}\r
-\r
-static inline FILE *popen_utf8(const char *command,const char *type)\r
-{\r
-#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__)\r
-  return(popen(command,type));\r
-#else\r
-   FILE\r
-     *file;\r
-\r
-   wchar_t\r
-     *type_wide,\r
-     *command_wide;\r
-\r
-   command_wide=create_wchar_string(command);\r
-   if (command_wide == (wchar_t *) NULL)\r
-     return((FILE *) NULL);\r
-   type_wide=create_wchar_string(type);\r
-   if (type_wide == (wchar_t *) NULL)\r
-     {\r
-       command_wide=(wchar_t *) RelinquishMagickMemory(command_wide);\r
-       return((FILE *) NULL);\r
-     }\r
-   file=_wpopen(command_wide,type_wide);\r
-   type_wide=(wchar_t *) RelinquishMagickMemory(type_wide);\r
-   command_wide=(wchar_t *) RelinquishMagickMemory(command_wide);\r
-   return(file);\r
-#endif\r
-}\r
-\r
-static inline int remove_utf8(const char *path)\r
-{\r
-#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__)\r
-  return(unlink(path));\r
-#else\r
-   int\r
-     status;\r
-\r
-   wchar_t\r
-     *path_wide;\r
-\r
-   path_wide=create_wchar_string(path);\r
-   if (path_wide == (wchar_t *) NULL)\r
-     return(-1);\r
-   status=_wremove(path_wide);\r
-   path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);\r
-   return(status);\r
-#endif\r
-}\r
-\r
-static inline int rename_utf8(const char *source,const char *destination)\r
-{\r
-#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__)\r
-  return(rename(source,destination));\r
-#else\r
-   int\r
-     status;\r
-\r
-   wchar_t\r
-     *destination_wide,\r
-     *source_wide;\r
-\r
-   source_wide=create_wchar_string(source);\r
-   if (source_wide == (wchar_t *) NULL)\r
-     return(-1);\r
-   destination_wide=create_wchar_string(destination);\r
-   if (destination_wide == (wchar_t *) NULL)\r
-     {\r
-       source_wide=(wchar_t *) RelinquishMagickMemory(source_wide);\r
-       return(-1);\r
-     }\r
-   status=_wrename(source_wide,destination_wide);\r
-   destination_wide=(wchar_t *) RelinquishMagickMemory(destination_wide);\r
-   source_wide=(wchar_t *) RelinquishMagickMemory(source_wide);\r
-   return(status);\r
-#endif\r
-}\r
-\r
-static inline int stat_utf8(const char *path,struct stat *attributes)\r
-{\r
-#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__)\r
-  return(stat(path,attributes));\r
-#else\r
-   int\r
-     status;\r
-\r
-   wchar_t\r
-     *path_wide;\r
-\r
-   path_wide=create_wchar_string(path);\r
-   if (path_wide == (WCHAR *) NULL)\r
-     return(-1);\r
-   status=wstat(path_wide,attributes);\r
-   path_wide=(WCHAR *) RelinquishMagickMemory(path_wide);\r
-   return(status);\r
-#endif\r
-}\r
-\r
-#if defined(__cplusplus) || defined(c_plusplus)\r
-}\r
-#endif\r
-\r
-#endif\r
+#if defined(MAGICKCORE_WINDOWS_SUPPORT)
+static inline wchar_t *create_wchar_path(const char *utf8)
+{
+  int
+    count;
+  wchar_t
+    *wideChar;
+
+  count=MultiByteToWideChar(CP_UTF8,0,utf8,-1,NULL,0);
+  if (count > MAX_PATH)
+    {
+      char
+        buffer[MaxTextExtent];
+
+      wchar_t
+        shortPath[MAX_PATH],
+        *longPath;
+
+      (void) FormatLocaleString(buffer,MaxTextExtent,"\\\\?\\%s",utf8);
+      count+=4;
+      longPath=(wchar_t *) AcquireQuantumMemory(count,sizeof(*longPath));
+      if (longPath == (wchar_t *) NULL)
+        return((wchar_t *) NULL);
+      count=MultiByteToWideChar(CP_UTF8,0,buffer,-1,longPath,count);
+      if (count != 0)
+        count=GetShortPathNameW(longPath,shortPath,MAX_PATH);
+      longPath=(wchar_t *) RelinquishMagickMemory(longPath);
+      if (count < 5)
+        return((wchar_t *) NULL);
+      wideChar=(wchar_t *) AcquireQuantumMemory(count-3,sizeof(*wideChar));
+      wcscpy(wideChar,shortPath+4);
+      return(wideChar);
+    }
+  wideChar=(wchar_t *) AcquireQuantumMemory(count,sizeof(*wideChar));
+  if (wideChar == (wchar_t *) NULL)
+    return((wchar_t *) NULL);
+  count=MultiByteToWideChar(CP_UTF8,0,utf8,-1,wideChar,count);
+  if (count == 0)
+    {
+      wideChar=(wchar_t *) RelinquishMagickMemory(wideChar);
+      return((wchar_t *) NULL);
+    }
+  return(wideChar);
+}
+#endif
+
+static inline int access_utf8(const char *path,int mode)
+{
+#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__)
+  return(access(path,mode));
+#else
+   int
+     status;
+
+   wchar_t
+     *path_wide;
+
+   path_wide=create_wchar_path(path);
+   if (path_wide == (wchar_t *) NULL)
+     return(-1);
+   status=_waccess(path_wide,mode);
+   path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
+   return(status);
+#endif
+}
+
+static inline FILE *fopen_utf8(const char *path,const char *mode)
+{
+#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__)
+  return(fopen(path,mode));
+#else
+   FILE
+     *file;
+
+   wchar_t
+     *mode_wide,
+     *path_wide;
+
+   path_wide=create_wchar_path(path);
+   if (path_wide == (wchar_t *) NULL)
+     return((FILE *) NULL);
+   mode_wide=create_wchar_path(mode);
+   if (mode_wide == (wchar_t *) NULL)
+     {
+       path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
+       return((FILE *) NULL);
+     }
+   file=_wfopen(path_wide,mode_wide);
+   mode_wide=(wchar_t *) RelinquishMagickMemory(mode_wide);
+   path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
+   return(file);
+#endif
+}
+
+#if defined(MAGICKCORE_WINDOWS_SUPPORT) && !defined(__CYGWIN__) && !defined(__MINGW32__) && !defined(__MINGW64__)
+typedef int
+  mode_t;
+#endif
+
+static inline int open_utf8(const char *path,int flags,mode_t mode)
+{
+#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__)
+  return(open(path,flags,mode));
+#else
+   int
+     status;
+
+   wchar_t
+     *path_wide;
+
+   path_wide=create_wchar_path(path);
+   if (path_wide == (wchar_t *) NULL)
+     return(-1);
+   status=_wopen(path_wide,flags,mode);
+   path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
+   return(status);
+#endif
+}
+
+static inline FILE *popen_utf8(const char *command,const char *type)
+{
+#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__)
+  return(popen(command,type));
+#else
+   FILE
+     *file;
+
+   wchar_t
+     *type_wide,
+     *command_wide;
+
+   command_wide=create_wchar_path(command);
+   if (command_wide == (wchar_t *) NULL)
+     return((FILE *) NULL);
+   type_wide=create_wchar_path(type);
+   if (type_wide == (wchar_t *) NULL)
+     {
+       command_wide=(wchar_t *) RelinquishMagickMemory(command_wide);
+       return((FILE *) NULL);
+     }
+   file=_wpopen(command_wide,type_wide);
+   type_wide=(wchar_t *) RelinquishMagickMemory(type_wide);
+   command_wide=(wchar_t *) RelinquishMagickMemory(command_wide);
+   return(file);
+#endif
+}
+
+static inline int remove_utf8(const char *path)
+{
+#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__)
+  return(unlink(path));
+#else
+   int
+     status;
+
+   wchar_t
+     *path_wide;
+
+   path_wide=create_wchar_path(path);
+   if (path_wide == (wchar_t *) NULL)
+     return(-1);
+   status=_wremove(path_wide);
+   path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
+   return(status);
+#endif
+}
+
+static inline int rename_utf8(const char *source,const char *destination)
+{
+#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__)
+  return(rename(source,destination));
+#else
+   int
+     status;
+
+   wchar_t
+     *destination_wide,
+     *source_wide;
+
+   source_wide=create_wchar_path(source);
+   if (source_wide == (wchar_t *) NULL)
+     return(-1);
+   destination_wide=create_wchar_path(destination);
+   if (destination_wide == (wchar_t *) NULL)
+     {
+       source_wide=(wchar_t *) RelinquishMagickMemory(source_wide);
+       return(-1);
+     }
+   status=_wrename(source_wide,destination_wide);
+   destination_wide=(wchar_t *) RelinquishMagickMemory(destination_wide);
+   source_wide=(wchar_t *) RelinquishMagickMemory(source_wide);
+   return(status);
+#endif
+}
+
+static inline int stat_utf8(const char *path,struct stat *attributes)
+{
+#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__)
+  return(stat(path,attributes));
+#else
+   int
+     status;
+
+   wchar_t
+     *path_wide;
+
+   path_wide=create_wchar_path(path);
+   if (path_wide == (WCHAR *) NULL)
+     return(-1);
+   status=wstat(path_wide,attributes);
+   path_wide=(WCHAR *) RelinquishMagickMemory(path_wide);
+   return(status);
+#endif
+}
+
+#if defined(__cplusplus) || defined(c_plusplus)
+}
+#endif
+
+#endif