GRASS Programmer's Manual  6.4.2(2012)
safileio.c
Go to the documentation of this file.
00001 /******************************************************************************
00002  * $Id: safileio.c,v 1.4 2008/01/16 20:05:14 bram Exp $
00003  *
00004  * Project:  Shapelib
00005  * Purpose:  Default implementation of file io based on stdio.
00006  * Author:   Frank Warmerdam, warmerdam@pobox.com
00007  *
00008  ******************************************************************************
00009  * Copyright (c) 2007, Frank Warmerdam
00010  *
00011  * This software is available under the following "MIT Style" license,
00012  * or at the option of the licensee under the LGPL (see LICENSE.LGPL).  This
00013  * option is discussed in more detail in shapelib.html.
00014  *
00015  * --
00016  * 
00017  * Permission is hereby granted, free of charge, to any person obtaining a
00018  * copy of this software and associated documentation files (the "Software"),
00019  * to deal in the Software without restriction, including without limitation
00020  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
00021  * and/or sell copies of the Software, and to permit persons to whom the
00022  * Software is furnished to do so, subject to the following conditions:
00023  *
00024  * The above copyright notice and this permission notice shall be included
00025  * in all copies or substantial portions of the Software.
00026  *
00027  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00028  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00029  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
00030  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00031  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
00032  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
00033  * DEALINGS IN THE SOFTWARE.
00034  ******************************************************************************
00035  *
00036  * $Log: safileio.c,v $
00037  * Revision 1.4  2008/01/16 20:05:14  bram
00038  * Add file hooks that accept UTF-8 encoded filenames on some platforms.  Use SASetupUtf8Hooks
00039  *  tosetup the hooks and check SHPAPI_UTF8_HOOKS for its availability.  Currently, this
00040  *  is only available on the Windows platform that decodes the UTF-8 filenames to wide
00041  *  character strings and feeds them to _wfopen and _wremove.
00042  *
00043  * Revision 1.3  2007/12/18 18:28:11  bram
00044  * - create hook for client specific atof (bugzilla ticket 1615)
00045  * - check for NULL handle before closing cpCPG file, and close after reading.
00046  *
00047  * Revision 1.2  2007/12/15 20:25:30  bram
00048  * dbfopen.c now reads the Code Page information from the DBF file, and exports
00049  * this information as a string through the DBFGetCodePage function.  This is 
00050  * either the number from the LDID header field ("LDID/<number>") or as the 
00051  * content of an accompanying .CPG file.  When creating a DBF file, the code can
00052  * be set using DBFCreateEx.
00053  *
00054  * Revision 1.1  2007/12/06 06:56:41  fwarmerdam
00055  * new
00056  *
00057  */
00058 
00059 #include "shapefil.h"
00060 
00061 #include <math.h>
00062 #include <limits.h>
00063 #include <assert.h>
00064 #include <stdlib.h>
00065 #include <string.h>
00066 #include <stdio.h>
00067 
00068 SHP_CVSID("$Id: safileio.c,v 1.4 2008/01/16 20:05:14 bram Exp $");
00069 
00070 #ifdef SHPAPI_UTF8_HOOKS
00071 #   ifdef SHPAPI_WINDOWS
00072 #       define WIN32_LEAN_AND_MEAN
00073 #       define NOMINMAX
00074 #       include <windows.h>
00075 #       pragma comment(lib, "kernel32.lib")
00076 #   endif
00077 #endif
00078 
00079 /************************************************************************/
00080 /*                              SADFOpen()                              */
00081 /************************************************************************/
00082 
00083 SAFile SADFOpen( const char *pszFilename, const char *pszAccess )
00084 
00085 {
00086     return (SAFile) fopen( pszFilename, pszAccess );
00087 }
00088 
00089 /************************************************************************/
00090 /*                              SADFRead()                              */
00091 /************************************************************************/
00092 
00093 SAOffset SADFRead( void *p, SAOffset size, SAOffset nmemb, SAFile file )
00094 
00095 {
00096     return (SAOffset) fread( p, (size_t) size, (size_t) nmemb, 
00097                              (FILE *) file );
00098 }
00099 
00100 /************************************************************************/
00101 /*                             SADFWrite()                              */
00102 /************************************************************************/
00103 
00104 SAOffset SADFWrite( void *p, SAOffset size, SAOffset nmemb, SAFile file )
00105 
00106 {
00107     return (SAOffset) fwrite( p, (size_t) size, (size_t) nmemb, 
00108                               (FILE *) file );
00109 }
00110 
00111 /************************************************************************/
00112 /*                              SADFSeek()                              */
00113 /************************************************************************/
00114 
00115 SAOffset SADFSeek( SAFile file, SAOffset offset, int whence )
00116 
00117 {
00118     return (SAOffset) fseek( (FILE *) file, (long) offset, whence );
00119 }
00120 
00121 /************************************************************************/
00122 /*                              SADFTell()                              */
00123 /************************************************************************/
00124 
00125 SAOffset SADFTell( SAFile file )
00126 
00127 {
00128     return (SAOffset) ftell( (FILE *) file );
00129 }
00130 
00131 /************************************************************************/
00132 /*                             SADFFlush()                              */
00133 /************************************************************************/
00134 
00135 int SADFFlush( SAFile file )
00136 
00137 {
00138     return fflush( (FILE *) file );
00139 }
00140 
00141 /************************************************************************/
00142 /*                             SADFClose()                              */
00143 /************************************************************************/
00144 
00145 int SADFClose( SAFile file )
00146 
00147 {
00148     return fclose( (FILE *) file );
00149 }
00150 
00151 /************************************************************************/
00152 /*                             SADFClose()                              */
00153 /************************************************************************/
00154 
00155 int SADRemove( const char *filename )
00156 
00157 {
00158     return remove( filename );
00159 }
00160 
00161 /************************************************************************/
00162 /*                              SADError()                              */
00163 /************************************************************************/
00164 
00165 void SADError( const char *message )
00166 
00167 {
00168     fprintf( stderr, "%s\n", message );
00169 }
00170 
00171 /************************************************************************/
00172 /*                        SASetupDefaultHooks()                         */
00173 /************************************************************************/
00174 
00175 void SASetupDefaultHooks( SAHooks *psHooks )
00176 
00177 {
00178     psHooks->FOpen   = SADFOpen;
00179     psHooks->FRead   = SADFRead;
00180     psHooks->FWrite  = SADFWrite;
00181     psHooks->FSeek   = SADFSeek;
00182     psHooks->FTell   = SADFTell;
00183     psHooks->FFlush  = SADFFlush;
00184     psHooks->FClose  = SADFClose;
00185 
00186     psHooks->Error   = SADError;
00187 }
00188 
00189 
00190 
00191 
00192 #ifdef SHPAPI_WINDOWS
00193 
00194 /************************************************************************/
00195 /*                          Utf8ToWideChar                              */
00196 /************************************************************************/
00197 
00198 const wchar_t* Utf8ToWideChar( const char *pszFilename )
00199 {
00200     int nMulti, nWide;
00201     wchar_t *pwszFileName;
00202     
00203     nMulti = strlen(pszFilename) + 1;
00204     nWide = MultiByteToWideChar( CP_UTF8, 0, pszFilename, nMulti, 0, 0);
00205     if( nWide == 0 )
00206     {
00207         return NULL;
00208     }
00209     pwszFileName = (wchar_t*) malloc(nWide * sizeof(wchar_t));
00210     if ( pwszFileName == NULL )
00211     {
00212         return NULL;
00213     }
00214     if( MultiByteToWideChar( CP_UTF8, 0, pszFilename, nMulti, pwszFileName, nWide ) == 0 )
00215     {
00216         free( pwszFileName );
00217         return NULL;
00218     }
00219     return pwszFileName;
00220 }
00221 
00222 /************************************************************************/
00223 /*                           SAUtf8WFOpen                               */
00224 /************************************************************************/
00225 
00226 SAFile SAUtf8WFOpen( const char *pszFilename, const char *pszAccess )
00227 {
00228     SAFile file = NULL;
00229     const wchar_t *pwszFileName, *pwszAccess;
00230     pwszFileName = Utf8ToWideChar( pszFilename );
00231     pwszAccess = Utf8ToWideChar( pszAccess );
00232     if( pwszFileName != NULL && pwszFileName != NULL)
00233     {
00234         file = (SAFile) _wfopen( pwszFileName, pwszAccess );
00235     }
00236     free ((wchar_t*) pwszFileName);
00237     free ((wchar_t*) pwszAccess);
00238     return file;
00239 }
00240 
00241 /************************************************************************/
00242 /*                             SAUtf8WRemove()                          */
00243 /************************************************************************/
00244 
00245 int SAUtf8WRemove( const char *pszFilename )
00246 {
00247     const wchar_t *pwszFileName = Utf8ToWideChar( pszFilename );
00248     int rc = -1; 
00249     if( pwszFileName != NULL )
00250     {
00251         rc = _wremove( pwszFileName );
00252     }
00253     free ((wchar_t*) pwszFileName);
00254     return rc;
00255 }
00256 
00257 #endif
00258 
00259 #ifdef SHPAPI_UTF8_HOOKS
00260 
00261 /************************************************************************/
00262 /*                          SASetupUtf8Hooks()                          */
00263 /************************************************************************/
00264 
00265 void SASetupUtf8Hooks( SAHooks *psHooks )
00266 {
00267 #ifdef SHPAPI_WINDOWS    
00268     psHooks->FOpen   = SAUtf8WFOpen;
00269     psHooks->Remove  = SAUtf8WRemove;
00270 #else
00271 #   error "no implementations of UTF-8 hooks available for this platform"
00272 #endif
00273     psHooks->FRead   = SADFRead;
00274     psHooks->FWrite  = SADFWrite;
00275     psHooks->FSeek   = SADFSeek;
00276     psHooks->FTell   = SADFTell;
00277     psHooks->FFlush  = SADFFlush;
00278     psHooks->FClose  = SADFClose;
00279 
00280     psHooks->Error   = SADError;
00281     psHooks->Atof    = atof;
00282 }
00283 
00284 #endif
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines