1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
|
/******************************************************************************
*
* Purpose: Implementation of a stdio based IO layer.
*
******************************************************************************
* Copyright (c) 2009
* PCI Geomatics, 50 West Wilmot Street, Richmond Hill, Ont, Canada
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
****************************************************************************/
#include "pcidsk_io.h"
#include "pcidsk_exception.h"
#include <cstdio>
#include <cstring>
#include <string>
#include <cerrno>
using namespace PCIDSK;
class StdioIOInterface : public IOInterfaces
{
virtual void *Open( std::string filename, std::string access ) const;
virtual uint64 Seek( void *io_handle, uint64 offset, int whence ) const;
virtual uint64 Tell( void *io_handle ) const;
virtual uint64 Read( void *buffer, uint64 size, uint64 nmemb, void *io_hanle ) const;
virtual uint64 Write( const void *buffer, uint64 size, uint64 nmemb, void *io_handle ) const;
virtual int Eof( void *io_handle ) const;
virtual int Flush( void *io_handle ) const;
virtual int Close( void *io_handle ) const;
const char *LastError() const;
};
typedef struct {
FILE *fp;
uint64 offset;
bool last_op_write;
} FileInfo;
/************************************************************************/
/* GetDefaultIOInterfaces() */
/************************************************************************/
/**
* Fetch default IO interfaces.
*
* Returns the default IO interfaces implemented in the PCIDSK library.
* These are suitable for use in a PCIDSK::PCIDSKInterfaces object.
*
* @return pointer to internal IO interfaces class.
*/
const IOInterfaces *PCIDSK::GetDefaultIOInterfaces()
{
static StdioIOInterface singleton_stdio_interface;
return &singleton_stdio_interface;
}
/************************************************************************/
/* Open() */
/************************************************************************/
void *
StdioIOInterface::Open( std::string filename, std::string access ) const
{
std::string adjusted_access = access;
adjusted_access += "b";
FILE *fp = fopen( filename.c_str(), adjusted_access.c_str() );
if( fp == NULL )
ThrowPCIDSKException( "Failed to open %s: %s",
filename.c_str(), LastError() );
FileInfo *fi = new FileInfo();
fi->fp = fp;
fi->offset = 0;
fi->last_op_write = false;
return fi;
}
/************************************************************************/
/* Seek() */
/************************************************************************/
uint64
StdioIOInterface::Seek( void *io_handle, uint64 offset, int whence ) const
{
FileInfo *fi = (FileInfo *) io_handle;
// seeks that do nothing are still surprisingly expensive with MSVCRT.
// try and short circuit if possible.
if( whence == SEEK_SET && offset == fi->offset )
return 0;
uint64 result = fseek( fi->fp, offset, whence );
if( result == (uint64) -1 )
ThrowPCIDSKException( "Seek(%d,%d): %s",
(int) offset, whence,
LastError() );
if( whence == SEEK_SET )
fi->offset = offset;
else if( whence == SEEK_END )
fi->offset = ftell( fi->fp );
else if( whence == SEEK_CUR )
fi->offset += offset;
fi->last_op_write = false;
return result;
}
/************************************************************************/
/* Tell() */
/************************************************************************/
uint64 StdioIOInterface::Tell( void *io_handle ) const
{
FileInfo *fi = (FileInfo *) io_handle;
return ftell( fi->fp );
}
/************************************************************************/
/* Read() */
/************************************************************************/
uint64 StdioIOInterface::Read( void *buffer, uint64 size, uint64 nmemb,
void *io_handle ) const
{
FileInfo *fi = (FileInfo *) io_handle;
errno = 0;
/* -------------------------------------------------------------------- */
/* If a fwrite() is followed by an fread(), the POSIX rules are */
/* that some of the write may still be buffered and lost. We */
/* are required to do a seek between to force flushing. So we */
/* keep careful track of what happened last to know if we */
/* skipped a flushing seek that we may need to do now. */
/* -------------------------------------------------------------------- */
if( fi->last_op_write )
fseek( fi->fp, fi->offset, SEEK_SET );
/* -------------------------------------------------------------------- */
/* Do the read. */
/* -------------------------------------------------------------------- */
uint64 result = fread( buffer, size, nmemb, fi->fp );
if( errno != 0 && result == 0 && nmemb != 0 )
ThrowPCIDSKException( "Read(%d): %s",
(int) size * nmemb,
LastError() );
fi->offset += size*result;
fi->last_op_write = false;
return result;
}
/************************************************************************/
/* Write() */
/************************************************************************/
uint64 StdioIOInterface::Write( const void *buffer, uint64 size, uint64 nmemb,
void *io_handle ) const
{
FileInfo *fi = (FileInfo *) io_handle;
errno = 0;
uint64 result = fwrite( buffer, size, nmemb, fi->fp );
if( errno != 0 && result == 0 && nmemb != 0 )
ThrowPCIDSKException( "Write(%d): %s",
(int) size * nmemb,
LastError() );
fi->offset += size*result;
fi->last_op_write = true;
return result;
}
/************************************************************************/
/* Eof() */
/************************************************************************/
int StdioIOInterface::Eof( void *io_handle ) const
{
FileInfo *fi = (FileInfo *) io_handle;
return feof( fi->fp );
}
/************************************************************************/
/* Flush() */
/************************************************************************/
int StdioIOInterface::Flush( void *io_handle ) const
{
FileInfo *fi = (FileInfo *) io_handle;
return fflush( fi->fp );
}
/************************************************************************/
/* Close() */
/************************************************************************/
int StdioIOInterface::Close( void *io_handle ) const
{
FileInfo *fi = (FileInfo *) io_handle;
int result = fclose( fi->fp );
delete fi;
return result;
}
/************************************************************************/
/* LastError() */
/* */
/* Return a string representation of the last error. */
/************************************************************************/
const char *StdioIOInterface::LastError() const
{
return strerror( errno );
}
|