WizardsToolkit  1.0.7
utility-private.h
Go to the documentation of this file.
00001 /*
00002   Copyright 1999-2012 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.
00006   obtain a copy of the License at
00007 
00008     http://www.wizard-toolkit.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   Wizard Toolkit private utility methods.
00017 */
00018 #ifndef _WIZARDSTOOLKIT_UTILITY_PRIVATE_H
00019 #define _WIZARDSTOOLKIT_UTILITY_PRIVATE_H
00020 
00021 #if defined(__cplusplus) || defined(c_plusplus)
00022 extern "C" {
00023 #endif
00024 
00025 #include "wizard/memory_.h"
00026 #if defined(WIZARDSTOOLKIT_WINDOWS_SUPPORT)
00027 #include "wizard/nt-base.h"
00028 #endif
00029 
00030 extern WizardExport char
00031   **GetPathComponents(const char *,size_t *);
00032 
00033 extern WizardExport WizardBooleanType
00034   GetExecutionPath(char *,const size_t);
00035 
00036 extern WizardExport ssize_t
00037   GetMagickPageSize(void);
00038 
00039 extern WizardExport void
00040   ChopPathComponents(char *,const size_t),
00041   WizardDelay(const WizardSizeType);
00042 
00043 /*
00044   Windows UTF8 compatibility methods.
00045 */
00046 
00047 static inline int access_utf8(const char *path,int mode)
00048 {
00049 #if !defined(WIZARDSTOOLKIT_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
00050   return(access(path,mode));
00051 #else
00052    int
00053      count,
00054      status;
00055 
00056    WCHAR
00057      *path_wide;
00058 
00059    path_wide=(WCHAR *) NULL;
00060    count=MultiByteToWideChar(CP_UTF8,0,path,-1,NULL,0);
00061    path_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*path_wide));
00062    if (path_wide == (WCHAR *) NULL)
00063      return(-1);
00064    count=MultiByteToWideChar(CP_UTF8,0,path,-1,path_wide,count);
00065    status=_waccess(path_wide,mode);
00066    path_wide=(WCHAR *) RelinquishWizardMemory(path_wide);
00067    return(status);
00068 #endif
00069 }
00070 
00071 static inline FILE *fopen_utf8(const char *path,const char *mode)
00072 {
00073 #if !defined(WIZARDSTOOLKIT_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
00074   return(fopen(path,mode));
00075 #else
00076    FILE
00077      *file;
00078 
00079    int
00080      count;
00081 
00082    WCHAR
00083      *mode_wide,
00084      *path_wide;
00085 
00086    path_wide=(WCHAR *) NULL;
00087    count=MultiByteToWideChar(CP_UTF8,0,path,-1,NULL,0);
00088    path_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*path_wide));
00089    if (path_wide == (WCHAR *) NULL)
00090      return((FILE *) NULL);
00091    count=MultiByteToWideChar(CP_UTF8,0,path,-1,path_wide,count);
00092    count=MultiByteToWideChar(CP_UTF8,0,mode,-1,NULL,0);
00093    mode_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*mode_wide));
00094    if (mode_wide == (WCHAR *) NULL)
00095      {
00096        path_wide=(WCHAR *) RelinquishWizardMemory(path_wide);
00097        return((FILE *) NULL);
00098      }
00099    count=MultiByteToWideChar(CP_UTF8,0,mode,-1,mode_wide,count);
00100    file=_wfopen(path_wide,mode_wide);
00101    mode_wide=(WCHAR *) RelinquishWizardMemory(mode_wide);
00102    path_wide=(WCHAR *) RelinquishWizardMemory(path_wide);
00103    return(file);
00104 #endif
00105 }
00106 
00107 #if defined(WIZARDSTOOLKIT_WINDOWS_SUPPORT) && !defined(__CYGWIN__) && !defined(__MINGW32__)
00108 typedef int
00109   mode_t;
00110 #endif
00111 
00112 static inline int open_utf8(const char *path,int flags,mode_t mode)
00113 {
00114 #if !defined(WIZARDSTOOLKIT_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
00115   return(open(path,flags,mode));
00116 #else
00117    int
00118      count,
00119      status;
00120 
00121    WCHAR
00122      *path_wide;
00123 
00124    path_wide=(WCHAR *) NULL;
00125    count=MultiByteToWideChar(CP_UTF8,0,path,-1,NULL,0);
00126    path_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*path_wide));
00127    if (path_wide == (WCHAR *) NULL)
00128      return(-1);
00129    count=MultiByteToWideChar(CP_UTF8,0,path,-1,path_wide,count);
00130    status=_wopen(path_wide,flags,mode);
00131    path_wide=(WCHAR *) RelinquishWizardMemory(path_wide);
00132    return(status);
00133 #endif
00134 }
00135 
00136 static inline FILE *popen_utf8(const char *command,const char *type)
00137 {
00138 #if !defined(WIZARDSTOOLKIT_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
00139   return(popen(command,type));
00140 #else
00141    FILE
00142      *file;
00143 
00144    int
00145      count;
00146 
00147    WCHAR
00148      *type_wide,
00149      *command_wide;
00150 
00151    command_wide=(WCHAR *) NULL;
00152    count=MultiByteToWideChar(CP_UTF8,0,command,-1,NULL,0);
00153    command_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*command_wide));
00154    if (command_wide == (WCHAR *) NULL)
00155      return((FILE *) NULL);
00156    count=MultiByteToWideChar(CP_UTF8,0,command,-1,command_wide,count);
00157    count=MultiByteToWideChar(CP_UTF8,0,type,-1,NULL,0);
00158    type_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*type_wide));
00159    if (type_wide == (WCHAR *) NULL)
00160      {
00161        command_wide=(WCHAR *) RelinquishWizardMemory(command_wide);
00162        return((FILE *) NULL);
00163      }
00164    count=MultiByteToWideChar(CP_UTF8,0,type,-1,type_wide,count);
00165    file=_wpopen(command_wide,type_wide);
00166    type_wide=(WCHAR *) RelinquishWizardMemory(type_wide);
00167    command_wide=(WCHAR *) RelinquishWizardMemory(command_wide);
00168    return(file);
00169 #endif
00170 }
00171 
00172 static inline int remove_utf8(const char *path)
00173 {
00174 #if !defined(WIZARDSTOOLKIT_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
00175   return(unlink(path));
00176 #else
00177    int
00178      count,
00179      status;
00180 
00181    WCHAR
00182      *path_wide;
00183 
00184    path_wide=(WCHAR *) NULL;
00185    count=MultiByteToWideChar(CP_UTF8,0,path,-1,NULL,0);
00186    path_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*path_wide));
00187    if (path_wide == (WCHAR *) NULL)
00188      return(-1);
00189    count=MultiByteToWideChar(CP_UTF8,0,path,-1,path_wide,count);
00190    status=_wremove(path_wide);
00191    path_wide=(WCHAR *) RelinquishWizardMemory(path_wide);
00192    return(status);
00193 #endif
00194 }
00195 
00196 static inline int rename_utf8(const char *source,const char *destination)
00197 {
00198 #if !defined(WIZARDSTOOLKIT_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
00199   return(rename(source,destination));
00200 #else
00201    int
00202      count,
00203      status;
00204 
00205    WCHAR
00206      *destination_wide,
00207      *source_wide;
00208 
00209    source_wide=(WCHAR *) NULL;
00210    count=MultiByteToWideChar(CP_UTF8,0,source,-1,NULL,0);
00211    source_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*source_wide));
00212    if (source_wide == (WCHAR *) NULL)
00213      return(-1);
00214    count=MultiByteToWideChar(CP_UTF8,0,source,-1,source_wide,count);
00215    count=MultiByteToWideChar(CP_UTF8,0,destination,-1,NULL,0);
00216    destination_wide=(WCHAR *) AcquireQuantumMemory(count,
00217      sizeof(*destination_wide));
00218    if (destination_wide == (WCHAR *) NULL)
00219      {
00220        source_wide=(WCHAR *) RelinquishWizardMemory(source_wide);
00221        return(-1);
00222      }
00223    count=MultiByteToWideChar(CP_UTF8,0,destination,-1,destination_wide,count);
00224    status=_wrename(source_wide,destination_wide);
00225    destination_wide=(WCHAR *) RelinquishWizardMemory(destination_wide);
00226    source_wide=(WCHAR *) RelinquishWizardMemory(source_wide);
00227    return(status);
00228 #endif
00229 }
00230 
00231 static inline int stat_utf8(const char *path,struct stat *attributes)
00232 {
00233 #if !defined(WIZARDSTOOLKIT_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
00234   return(stat(path,attributes));
00235 #else
00236    int
00237      count,
00238      status;
00239 
00240    WCHAR
00241      *path_wide;
00242 
00243    path_wide=(WCHAR *) NULL;
00244    count=MultiByteToWideChar(CP_UTF8,0,path,-1,NULL,0);
00245    path_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*path_wide));
00246    if (path_wide == (WCHAR *) NULL)
00247      return(-1);
00248    count=MultiByteToWideChar(CP_UTF8,0,path,-1,path_wide,count);
00249    status=_wstat64(path_wide,attributes);
00250    path_wide=(WCHAR *) RelinquishWizardMemory(path_wide);
00251    return(status);
00252 #endif
00253 }
00254 
00255 #if defined(__cplusplus) || defined(c_plusplus)
00256 }
00257 #endif
00258 
00259 #endif