utility-private.h

Go to the documentation of this file.
00001 /*
00002   Copyright 1999-2019 ImageMagick Studio LLC, a non-profit organization
00003   dedicated to making software imaging solutions freely available.
00004 
00005   You may not use this file except in compliance with the License.  You may
00006   obtain a copy of the License at
00007 
00008     https://imagemagick.org/script/license.php
00009 
00010   Unless required by applicable law or agreed to in writing, software
00011   distributed under the License is distributed on an "AS IS" BASIS,
00012   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013   See the License for the specific language governing permissions and
00014   limitations under the License.
00015 
00016   MagickCore private utility methods.
00017 */
00018 #ifndef MAGICKCORE_UTILITY_PRIVATE_H
00019 #define MAGICKCORE_UTILITY_PRIVATE_H
00020 
00021 #include "magick/memory_.h"
00022 #include "magick/nt-base.h"
00023 #include "magick/nt-base-private.h"
00024 
00025 #if defined(__cplusplus) || defined(c_plusplus)
00026 extern "C" {
00027 #endif
00028 
00029 extern MagickPrivate MagickBooleanType
00030   ShredFile(const char *);
00031 
00032 static inline int MagickReadDirectory(DIR *directory,struct dirent *entry,
00033   struct dirent **result)
00034 {
00035 #if defined(MAGICKCORE_HAVE_READDIR_R)
00036   return(readdir_r(directory,entry,result));
00037 #else
00038   (void) entry;
00039   errno=0;
00040   *result=readdir(directory);
00041   return(errno);
00042 #endif
00043 }
00044 
00045 /*
00046   Windows UTF8 compatibility methods.
00047 */
00048 
00049 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
00050 static inline wchar_t *create_wchar_path(const char *utf8)
00051 {
00052   int
00053     count;
00054  
00055   wchar_t
00056     *wideChar;
00057 
00058   count=MultiByteToWideChar(CP_UTF8,0,utf8,-1,NULL,0);
00059   if ((count > MAX_PATH) && (NTLongPathsEnabled() == MagickFalse))
00060     {
00061       char
00062         buffer[MaxTextExtent];
00063 
00064       wchar_t
00065         shortPath[MAX_PATH],
00066         *longPath;
00067 
00068       (void) FormatLocaleString(buffer,MaxTextExtent,"\\\\?\\%s",utf8);
00069       count+=4;
00070       longPath=(wchar_t *) AcquireQuantumMemory(count,sizeof(*longPath));
00071       if (longPath == (wchar_t *) NULL)
00072         return((wchar_t *) NULL);
00073       count=MultiByteToWideChar(CP_UTF8,0,buffer,-1,longPath,count);
00074       if (count != 0)
00075         count=GetShortPathNameW(longPath,shortPath,MAX_PATH);
00076       longPath=(wchar_t *) RelinquishMagickMemory(longPath);
00077       if ((count < 5) || (count >= MAX_PATH))
00078         return((wchar_t *) NULL);
00079       wideChar=(wchar_t *) AcquireQuantumMemory(count-3,sizeof(*wideChar));
00080       wcscpy(wideChar,shortPath+4);
00081       return(wideChar);
00082     }
00083   wideChar=(wchar_t *) AcquireQuantumMemory(count,sizeof(*wideChar));
00084   if (wideChar == (wchar_t *) NULL)
00085     return((wchar_t *) NULL);
00086   count=MultiByteToWideChar(CP_UTF8,0,utf8,-1,wideChar,count);
00087   if (count == 0)
00088     {
00089       wideChar=(wchar_t *) RelinquishMagickMemory(wideChar);
00090       return((wchar_t *) NULL);
00091     }
00092   return(wideChar);
00093 }
00094 #endif
00095 
00096 static inline int access_utf8(const char *path,int mode)
00097 {
00098 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
00099   return(access(path,mode));
00100 #else
00101    int
00102      status;
00103 
00104    wchar_t
00105      *path_wide;
00106 
00107    path_wide=create_wchar_path(path);
00108    if (path_wide == (wchar_t *) NULL)
00109      return(-1);
00110    status=_waccess(path_wide,mode);
00111    path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
00112    return(status);
00113 #endif
00114 }
00115 
00116 static inline FILE *fopen_utf8(const char *path,const char *mode)
00117 {
00118 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
00119   return(fopen(path,mode));
00120 #else
00121    FILE
00122      *file;
00123 
00124    wchar_t
00125      *mode_wide,
00126      *path_wide;
00127 
00128    path_wide=create_wchar_path(path);
00129    if (path_wide == (wchar_t *) NULL)
00130      return((FILE *) NULL);
00131    mode_wide=create_wchar_path(mode);
00132    if (mode_wide == (wchar_t *) NULL)
00133      {
00134        path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
00135        return((FILE *) NULL);
00136      }
00137    file=_wfopen(path_wide,mode_wide);
00138    mode_wide=(wchar_t *) RelinquishMagickMemory(mode_wide);
00139    path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
00140    return(file);
00141 #endif
00142 }
00143 
00144 static inline void getcwd_utf8(char *path,size_t extent)
00145 {
00146 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
00147   char
00148     *directory;
00149 
00150    directory=getcwd(path,extent);
00151    (void) directory;
00152 #else
00153   wchar_t
00154     wide_path[MaxTextExtent];
00155 
00156   (void) _wgetcwd(wide_path,MaxTextExtent-1);
00157   (void) WideCharToMultiByte(CP_UTF8,0,wide_path,-1,path,(int) extent,NULL,NULL);
00158 #endif
00159 }
00160 
00161 #if defined(MAGICKCORE_WINDOWS_SUPPORT) && !defined(__CYGWIN__) && !defined(__MINGW32__)
00162 typedef int
00163   mode_t;
00164 #endif
00165 
00166 static inline int open_utf8(const char *path,int flags,mode_t mode)
00167 {
00168 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
00169   return(open(path,flags,mode));
00170 #else
00171    int
00172      status;
00173 
00174    wchar_t
00175      *path_wide;
00176 
00177    path_wide=create_wchar_path(path);
00178    if (path_wide == (wchar_t *) NULL)
00179      return(-1);
00180    status=_wopen(path_wide,flags,mode);
00181    path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
00182    return(status);
00183 #endif
00184 }
00185 
00186 static inline FILE *popen_utf8(const char *command,const char *type)
00187 {
00188 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
00189   return(popen(command,type));
00190 #else
00191    FILE
00192      *file;
00193 
00194    wchar_t
00195      *type_wide,
00196      *command_wide;
00197 
00198    command_wide=create_wchar_path(command);
00199    if (command_wide == (wchar_t *) NULL)
00200      return((FILE *) NULL);
00201    type_wide=create_wchar_path(type);
00202    if (type_wide == (wchar_t *) NULL)
00203      {
00204        command_wide=(wchar_t *) RelinquishMagickMemory(command_wide);
00205        return((FILE *) NULL);
00206      }
00207    file=_wpopen(command_wide,type_wide);
00208    type_wide=(wchar_t *) RelinquishMagickMemory(type_wide);
00209    command_wide=(wchar_t *) RelinquishMagickMemory(command_wide);
00210    return(file);
00211 #endif
00212 }
00213 
00214 static inline int remove_utf8(const char *path)
00215 {
00216 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
00217   return(unlink(path));
00218 #else
00219    int
00220      status;
00221 
00222    wchar_t
00223      *path_wide;
00224 
00225    path_wide=create_wchar_path(path);
00226    if (path_wide == (wchar_t *) NULL)
00227      return(-1);
00228    status=_wremove(path_wide);
00229    path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
00230    return(status);
00231 #endif
00232 }
00233 
00234 static inline int rename_utf8(const char *source,const char *destination)
00235 {
00236 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
00237   return(rename(source,destination));
00238 #else
00239    int
00240      status;
00241 
00242    wchar_t
00243      *destination_wide,
00244      *source_wide;
00245 
00246    source_wide=create_wchar_path(source);
00247    if (source_wide == (wchar_t *) NULL)
00248      return(-1);
00249    destination_wide=create_wchar_path(destination);
00250    if (destination_wide == (wchar_t *) NULL)
00251      {
00252        source_wide=(wchar_t *) RelinquishMagickMemory(source_wide);
00253        return(-1);
00254      }
00255    status=_wrename(source_wide,destination_wide);
00256    destination_wide=(wchar_t *) RelinquishMagickMemory(destination_wide);
00257    source_wide=(wchar_t *) RelinquishMagickMemory(source_wide);
00258    return(status);
00259 #endif
00260 }
00261 
00262 static inline int stat_utf8(const char *path,struct stat *attributes)
00263 {
00264 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
00265   return(stat(path,attributes));
00266 #else
00267    int
00268      status;
00269 
00270    wchar_t
00271      *path_wide;
00272 
00273    path_wide=create_wchar_path(path);
00274    if (path_wide == (WCHAR *) NULL)
00275      return(-1);
00276    status=wstat(path_wide,attributes);
00277    path_wide=(WCHAR *) RelinquishMagickMemory(path_wide);
00278    return(status);
00279 #endif
00280 }
00281 
00282 #if defined(__cplusplus) || defined(c_plusplus)
00283 }
00284 #endif
00285 
00286 #endif

Generated on 2 Jul 2019 for MagickCore by  doxygen 1.6.1