utility-private.h

Go to the documentation of this file.
00001 /*
00002   Copyright 1999-2020 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 "MagickCore/memory_.h"
00022 #include "MagickCore/nt-base.h"
00023 #include "MagickCore/nt-base-private.h"
00024 
00025 #if defined(__cplusplus) || defined(c_plusplus)
00026 extern "C" {
00027 #endif
00028 
00029 extern MagickPrivate char
00030   **GetPathComponents(const char *,size_t *),
00031   **ListFiles(const char *,const char *,size_t *);
00032 
00033 extern MagickPrivate MagickBooleanType
00034   GetExecutionPath(char *,const size_t),
00035   ShredFile(const char *);
00036 
00037 extern MagickPrivate ssize_t
00038   GetMagickPageSize(void);
00039 
00040 extern MagickPrivate void
00041   ChopPathComponents(char *,const size_t),
00042   ExpandFilename(char *);
00043 
00044 static inline int MagickReadDirectory(DIR *directory,struct dirent *entry,
00045   struct dirent **result)
00046 {
00047 #if defined(MAGICKCORE_HAVE_READDIR_R)
00048   return(readdir_r(directory,entry,result));
00049 #else
00050   (void) entry;
00051   errno=0;
00052   *result=readdir(directory);
00053   return(errno);
00054 #endif
00055 }
00056 
00057 /*
00058   Windows UTF8 compatibility methods.
00059 */
00060 
00061 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
00062 static inline wchar_t *create_wchar_path(const char *utf8)
00063 {
00064   int
00065     count;
00066  
00067   wchar_t
00068     *wideChar;
00069 
00070   count=MultiByteToWideChar(CP_UTF8,0,utf8,-1,NULL,0);
00071   if ((count > MAX_PATH) && (NTLongPathsEnabled() == MagickFalse))
00072     {
00073       char
00074         buffer[MagickPathExtent];
00075 
00076       wchar_t
00077         shortPath[MAX_PATH],
00078         *longPath;
00079 
00080       (void) FormatLocaleString(buffer,MagickPathExtent,"\\\\?\\%s",utf8);
00081       count+=4;
00082       longPath=(wchar_t *) AcquireQuantumMemory(count,sizeof(*longPath));
00083       if (longPath == (wchar_t *) NULL)
00084         return((wchar_t *) NULL);
00085       count=MultiByteToWideChar(CP_UTF8,0,buffer,-1,longPath,count);
00086       if (count != 0)
00087         count=GetShortPathNameW(longPath,shortPath,MAX_PATH);
00088       longPath=(wchar_t *) RelinquishMagickMemory(longPath);
00089       if ((count < 5) || (count >= MAX_PATH))
00090         return((wchar_t *) NULL);
00091       wideChar=(wchar_t *) AcquireQuantumMemory(count-3,sizeof(*wideChar));
00092       wcscpy(wideChar,shortPath+4);
00093       return(wideChar);
00094     }
00095   wideChar=(wchar_t *) AcquireQuantumMemory(count,sizeof(*wideChar));
00096   if (wideChar == (wchar_t *) NULL)
00097     return((wchar_t *) NULL);
00098   count=MultiByteToWideChar(CP_UTF8,0,utf8,-1,wideChar,count);
00099   if (count == 0)
00100     {
00101       wideChar=(wchar_t *) RelinquishMagickMemory(wideChar);
00102       return((wchar_t *) NULL);
00103     }
00104   return(wideChar);
00105 }
00106 #endif
00107 
00108 static inline int access_utf8(const char *path,int mode)
00109 {
00110 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
00111   return(access(path,mode));
00112 #else
00113    int
00114      status;
00115 
00116    wchar_t
00117      *path_wide;
00118 
00119    path_wide=create_wchar_path(path);
00120    if (path_wide == (wchar_t *) NULL)
00121      return(-1);
00122    status=_waccess(path_wide,mode);
00123    path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
00124    return(status);
00125 #endif
00126 }
00127 
00128 static inline FILE *fopen_utf8(const char *path,const char *mode)
00129 {
00130 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
00131   return(fopen(path,mode));
00132 #else
00133    FILE
00134      *file;
00135 
00136    wchar_t
00137      *mode_wide,
00138      *path_wide;
00139 
00140    path_wide=create_wchar_path(path);
00141    if (path_wide == (wchar_t *) NULL)
00142      return((FILE *) NULL);
00143    mode_wide=create_wchar_path(mode);
00144    if (mode_wide == (wchar_t *) NULL)
00145      {
00146        path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
00147        return((FILE *) NULL);
00148      }
00149    file=_wfopen(path_wide,mode_wide);
00150    mode_wide=(wchar_t *) RelinquishMagickMemory(mode_wide);
00151    path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
00152    return(file);
00153 #endif
00154 }
00155 
00156 static inline void getcwd_utf8(char *path,size_t extent)
00157 {
00158 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
00159   char
00160     *directory;
00161 
00162    directory=getcwd(path,extent);
00163    (void) directory;
00164 #else
00165   wchar_t
00166     wide_path[MagickPathExtent];
00167 
00168   (void) _wgetcwd(wide_path,MagickPathExtent-1);
00169   (void) WideCharToMultiByte(CP_UTF8,0,wide_path,-1,path,(int) extent,NULL,NULL);
00170 #endif
00171 }
00172 
00173 #if defined(MAGICKCORE_WINDOWS_SUPPORT) && !defined(__CYGWIN__) && !defined(__MINGW32__)
00174 typedef int
00175   mode_t;
00176 #endif
00177 
00178 static inline int open_utf8(const char *path,int flags,mode_t mode)
00179 {
00180 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
00181   return(open(path,flags,mode));
00182 #else
00183    int
00184      status;
00185 
00186    wchar_t
00187      *path_wide;
00188 
00189    path_wide=create_wchar_path(path);
00190    if (path_wide == (wchar_t *) NULL)
00191      return(-1);
00192    status=_wopen(path_wide,flags,mode);
00193    path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
00194    return(status);
00195 #endif
00196 }
00197 
00198 static inline FILE *popen_utf8(const char *command,const char *type)
00199 {
00200 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
00201   return(popen(command,type));
00202 #else
00203    FILE
00204      *file;
00205 
00206    wchar_t
00207      *type_wide,
00208      *command_wide;
00209 
00210    command_wide=create_wchar_path(command);
00211    if (command_wide == (wchar_t *) NULL)
00212      return((FILE *) NULL);
00213    type_wide=create_wchar_path(type);
00214    if (type_wide == (wchar_t *) NULL)
00215      {
00216        command_wide=(wchar_t *) RelinquishMagickMemory(command_wide);
00217        return((FILE *) NULL);
00218      }
00219    file=_wpopen(command_wide,type_wide);
00220    type_wide=(wchar_t *) RelinquishMagickMemory(type_wide);
00221    command_wide=(wchar_t *) RelinquishMagickMemory(command_wide);
00222    return(file);
00223 #endif
00224 }
00225 
00226 static inline int remove_utf8(const char *path)
00227 {
00228 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
00229   return(unlink(path));
00230 #else
00231    int
00232      status;
00233 
00234    wchar_t
00235      *path_wide;
00236 
00237    path_wide=create_wchar_path(path);
00238    if (path_wide == (wchar_t *) NULL)
00239      return(-1);
00240    status=_wremove(path_wide);
00241    path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
00242    return(status);
00243 #endif
00244 }
00245 
00246 static inline int rename_utf8(const char *source,const char *destination)
00247 {
00248 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
00249   return(rename(source,destination));
00250 #else
00251    int
00252      status;
00253 
00254    wchar_t
00255      *destination_wide,
00256      *source_wide;
00257 
00258    source_wide=create_wchar_path(source);
00259    if (source_wide == (wchar_t *) NULL)
00260      return(-1);
00261    destination_wide=create_wchar_path(destination);
00262    if (destination_wide == (wchar_t *) NULL)
00263      {
00264        source_wide=(wchar_t *) RelinquishMagickMemory(source_wide);
00265        return(-1);
00266      }
00267    status=_wrename(source_wide,destination_wide);
00268    destination_wide=(wchar_t *) RelinquishMagickMemory(destination_wide);
00269    source_wide=(wchar_t *) RelinquishMagickMemory(source_wide);
00270    return(status);
00271 #endif
00272 }
00273 
00274 static inline int stat_utf8(const char *path,struct stat *attributes)
00275 {
00276 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
00277   return(stat(path,attributes));
00278 #else
00279    int
00280      status;
00281 
00282    wchar_t
00283      *path_wide;
00284 
00285    path_wide=create_wchar_path(path);
00286    if (path_wide == (WCHAR *) NULL)
00287      return(-1);
00288    status=wstat(path_wide,attributes);
00289    path_wide=(WCHAR *) RelinquishMagickMemory(path_wide);
00290    return(status);
00291 #endif
00292 }
00293 
00294 #if defined(__cplusplus) || defined(c_plusplus)
00295 }
00296 #endif
00297 
00298 #endif

Generated on 10 Aug 2020 for MagickCore by  doxygen 1.6.1