| 1 | /****************************************************************************** |
| 2 | * |
| 3 | * Purpose: Implementation of a stdio based IO layer. |
| 4 | * |
| 5 | ****************************************************************************** |
| 6 | * Copyright (c) 2009 |
| 7 | * PCI Geomatics, 90 Allstate Parkway, Markham, Ontario, Canada. |
| 8 | * |
| 9 | * SPDX-License-Identifier: MIT |
| 10 | ****************************************************************************/ |
| 11 | |
| 12 | #include "pcidsk_io.h" |
| 13 | #include "pcidsk_exception.h" |
| 14 | #include <cstdio> |
| 15 | #include <cstring> |
| 16 | #include <string> |
| 17 | #include <cerrno> |
| 18 | |
| 19 | using namespace PCIDSK; |
| 20 | |
| 21 | class StdioIOInterface final: public IOInterfaces |
| 22 | { |
| 23 | void *Open( const std::string& filename, const std::string& access ) const override; |
| 24 | uint64 Seek( void *io_handle, uint64 offset, int whence ) const override; |
| 25 | uint64 Tell( void *io_handle ) const override; |
| 26 | uint64 Read( void *buffer, uint64 size, uint64 nmemb, void *io_hanle ) const override; |
| 27 | uint64 Write( const void *buffer, uint64 size, uint64 nmemb, void *io_handle ) const override; |
| 28 | int Eof( void *io_handle ) const override; |
| 29 | int Flush( void *io_handle ) const override; |
| 30 | int Close( void *io_handle ) const override; |
| 31 | |
| 32 | const char *LastError() const; |
| 33 | }; |
| 34 | |
| 35 | typedef struct { |
| 36 | FILE *fp; |
| 37 | uint64 offset; |
| 38 | bool last_op_write; |
| 39 | } FileInfo; |
| 40 | |
| 41 | /************************************************************************/ |
| 42 | /* GetDefaultIOInterfaces() */ |
| 43 | /************************************************************************/ |
| 44 | |
| 45 | /** |
| 46 | * Fetch default IO interfaces. |
| 47 | * |
| 48 | * Returns the default IO interfaces implemented in the PCIDSK library. |
| 49 | * These are suitable for use in a PCIDSK::PCIDSKInterfaces object. |
| 50 | * |
| 51 | * @return pointer to internal IO interfaces class. |
| 52 | */ |
| 53 | const IOInterfaces *PCIDSK::GetDefaultIOInterfaces() |
| 54 | { |
| 55 | static StdioIOInterface singleton_stdio_interface; |
| 56 | |
| 57 | return &singleton_stdio_interface; |
| 58 | } |
| 59 | |
| 60 | /************************************************************************/ |
| 61 | /* Open() */ |
| 62 | /************************************************************************/ |
| 63 | |
| 64 | void * |
| 65 | StdioIOInterface::Open( const std::string& filename, const std::string& access ) const |
| 66 | |
| 67 | { |
| 68 | std::string adjusted_access = access; |
| 69 | |
| 70 | adjusted_access += "b"; |
| 71 | |
| 72 | FILE *fp = fopen( filename.c_str(), adjusted_access.c_str() ); |
| 73 | |
| 74 | if( fp == nullptr ) |
| 75 | ThrowPCIDSKException( "Failed to open %s: %s", |
| 76 | filename.c_str(), LastError() ); |
| 77 | |
| 78 | FileInfo *fi = new FileInfo(); |
| 79 | fi->fp = fp; |
| 80 | fi->offset = 0; |
| 81 | fi->last_op_write = false; |
| 82 | |
| 83 | return fi; |
| 84 | } |
| 85 | |
| 86 | /************************************************************************/ |
| 87 | /* Seek() */ |
| 88 | /************************************************************************/ |
| 89 | |
| 90 | uint64 |
| 91 | StdioIOInterface::Seek( void *io_handle, uint64 offset, int whence ) const |
| 92 | |
| 93 | { |
| 94 | FileInfo *fi = static_cast<FileInfo *>(io_handle); |
| 95 | |
| 96 | // seeks that do nothing are still surprisingly expensive with MSVCRT. |
| 97 | // try and short circuit if possible. |
| 98 | if( whence == SEEK_SET && offset == fi->offset ) |
| 99 | return 0; |
| 100 | |
| 101 | uint64 result = fseek( fi->fp, offset, whence ); |
| 102 | |
| 103 | if( result == static_cast<uint64>(-1) ) |
| 104 | ThrowPCIDSKException( "Seek(" PCIDSK_FRMT_UINT64 ",%d): %s", |
| 105 | offset, whence, |
| 106 | LastError() ); |
| 107 | |
| 108 | if( whence == SEEK_SET ) |
| 109 | fi->offset = offset; |
| 110 | else if( whence == SEEK_END ) |
| 111 | fi->offset = ftell( fi->fp ); |
| 112 | else if( whence == SEEK_CUR ) |
| 113 | fi->offset += offset; |
| 114 | |
| 115 | fi->last_op_write = false; |
| 116 | |
| 117 | return result; |
| 118 | } |
| 119 | |
| 120 | /************************************************************************/ |
| 121 | /* Tell() */ |
| 122 | /************************************************************************/ |
| 123 | |
| 124 | uint64 StdioIOInterface::Tell( void *io_handle ) const |
| 125 | |
| 126 | { |
| 127 | FileInfo *fi = static_cast<FileInfo *>(io_handle); |
| 128 | |
| 129 | return ftell( fi->fp ); |
| 130 | } |
| 131 | |
| 132 | /************************************************************************/ |
| 133 | /* Read() */ |
| 134 | /************************************************************************/ |
| 135 | |
| 136 | uint64 StdioIOInterface::Read( void *buffer, uint64 size, uint64 nmemb, |
| 137 | void *io_handle ) const |
| 138 | |
| 139 | { |
| 140 | FileInfo *fi = static_cast<FileInfo *>(io_handle); |
| 141 | |
| 142 | errno = 0; |
| 143 | |
| 144 | /* -------------------------------------------------------------------- */ |
| 145 | /* If a fwrite() is followed by an fread(), the POSIX rules are */ |
| 146 | /* that some of the write may still be buffered and lost. We */ |
| 147 | /* are required to do a seek between to force flushing. So we */ |
| 148 | /* keep careful track of what happened last to know if we */ |
| 149 | /* skipped a flushing seek that we may need to do now. */ |
| 150 | /* -------------------------------------------------------------------- */ |
| 151 | if( fi->last_op_write ) |
| 152 | fseek( fi->fp, fi->offset, SEEK_SET ); |
| 153 | |
| 154 | /* -------------------------------------------------------------------- */ |
| 155 | /* Do the read. */ |
| 156 | /* -------------------------------------------------------------------- */ |
| 157 | uint64 result = fread( buffer, size, nmemb, fi->fp ); |
| 158 | |
| 159 | if( errno != 0 && result == 0 && nmemb != 0 ) |
| 160 | ThrowPCIDSKException( "Read(" PCIDSK_FRMT_UINT64 "): %s", |
| 161 | size * nmemb, |
| 162 | LastError() ); |
| 163 | |
| 164 | fi->offset += size*result; |
| 165 | fi->last_op_write = false; |
| 166 | |
| 167 | return result; |
| 168 | } |
| 169 | |
| 170 | /************************************************************************/ |
| 171 | /* Write() */ |
| 172 | /************************************************************************/ |
| 173 | |
| 174 | uint64 StdioIOInterface::Write( const void *buffer, uint64 size, uint64 nmemb, |
| 175 | void *io_handle ) const |
| 176 | |
| 177 | { |
| 178 | FileInfo *fi = static_cast<FileInfo *>(io_handle); |
| 179 | |
| 180 | errno = 0; |
| 181 | |
| 182 | uint64 result = fwrite( buffer, size, nmemb, fi->fp ); |
| 183 | |
| 184 | if( errno != 0 && result == 0 && nmemb != 0 ) |
| 185 | ThrowPCIDSKException( "Write(" PCIDSK_FRMT_UINT64 "): %s", |
| 186 | size * nmemb, |
| 187 | LastError() ); |
| 188 | |
| 189 | fi->offset += size*result; |
| 190 | fi->last_op_write = true; |
| 191 | |
| 192 | return result; |
| 193 | } |
| 194 | |
| 195 | /************************************************************************/ |
| 196 | /* Eof() */ |
| 197 | /************************************************************************/ |
| 198 | |
| 199 | int StdioIOInterface::Eof( void *io_handle ) const |
| 200 | |
| 201 | { |
| 202 | FileInfo *fi = static_cast<FileInfo *>(io_handle); |
| 203 | return feof( fi->fp ); |
| 204 | } |
| 205 | |
| 206 | /************************************************************************/ |
| 207 | /* Flush() */ |
| 208 | /************************************************************************/ |
| 209 | |
| 210 | int StdioIOInterface::Flush( void *io_handle ) const |
| 211 | |
| 212 | { |
| 213 | FileInfo *fi = static_cast<FileInfo *>(io_handle); |
| 214 | return fflush( fi->fp ); |
| 215 | } |
| 216 | |
| 217 | /************************************************************************/ |
| 218 | /* Close() */ |
| 219 | /************************************************************************/ |
| 220 | |
| 221 | int StdioIOInterface::Close( void *io_handle ) const |
| 222 | |
| 223 | { |
| 224 | FileInfo *fi = static_cast<FileInfo *>(io_handle); |
| 225 | int result = fclose( fi->fp ); |
| 226 | |
| 227 | delete fi; |
| 228 | |
| 229 | return result; |
| 230 | } |
| 231 | |
| 232 | /************************************************************************/ |
| 233 | /* LastError() */ |
| 234 | /* */ |
| 235 | /* Return a string representation of the last error. */ |
| 236 | /************************************************************************/ |
| 237 | |
| 238 | const char *StdioIOInterface::LastError() const |
| 239 | |
| 240 | { |
| 241 | return strerror( errno ); |
| 242 | } |