| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#include <assert.h> |
| 21 |
#include <errno.h> |
| 22 |
#include <inttypes.h> |
| 23 |
#include <string.h> |
| 24 |
|
| 25 |
#define __USE_LARGEFILE 1 // For declaration of ftello, etc. |
| 26 |
#define __USE_POSIX 1 // For declaration of fileno. |
| 27 |
#define _POSIX_SOURCE 1 // For both the above in Emscripten. |
| 28 |
#include <stdio.h> |
| 29 |
#include <stdlib.h> |
| 30 |
#include <sys/types.h> |
| 31 |
#define __USE_MISC 1 // For declaration of S_IF... |
| 32 |
#include <sys/stat.h> |
| 33 |
|
| 34 |
#include "ktx.h" |
| 35 |
#include "ktxint.h" |
| 36 |
#include "filestream.h" |
| 37 |
|
| 38 |
|
| 39 |
#if defined(_MSC_VER) |
| 40 |
#if defined(_WIN64) |
| 41 |
#define ftello _ftelli64 |
| 42 |
#define fseeko _fseeki64 |
| 43 |
#else |
| 44 |
#define ftello ftell |
| 45 |
#define fseeko fseek |
| 46 |
#endif |
| 47 |
#define fileno _fileno |
| 48 |
#define fstat _fstat |
| 49 |
#define stat _stat |
| 50 |
#define S_IFIFO _S_IFIFO |
| 51 |
#define S_IFSOCK 0xC000 |
| 52 |
typedef unsigned short mode_t; |
| 53 |
#endif |
| 54 |
|
| 55 |
#if defined(__MINGW32__) |
| 56 |
#define S_IFSOCK 0xC000 |
| 57 |
#endif |
| 58 |
|
| 59 |
#define KTX_FILE_STREAM_MAX (1 << (sizeof(ktx_off_t) - 1) - 1) |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
static |
| 78 |
KTX_error_code ktxFileStream_read(ktxStream* str, void* dst, const ktx_size_t count) |
| 79 |
{ |
| 80 |
ktx_size_t nread; |
| 81 |
|
| 82 |
if (!str || !dst) |
| 83 |
return KTX_INVALID_VALUE; |
| 84 |
|
| 85 |
assert(str->type == eStreamTypeFile); |
| 86 |
|
| 87 |
if ((nread = fread(dst, 1, count, str->data.file)) != count) { |
| 88 |
if (feof(str->data.file)) { |
| 89 |
return KTX_FILE_UNEXPECTED_EOF; |
| 90 |
} else { |
| 91 |
return KTX_FILE_READ_ERROR; |
| 92 |
} |
| 93 |
} |
| 94 |
str->readpos += count; |
| 95 |
|
| 96 |
return KTX_SUCCESS; |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
static |
| 119 |
KTX_error_code ktxFileStream_skip(ktxStream* str, const ktx_size_t count) |
| 120 |
{ |
| 121 |
if (!str) |
| 122 |
return KTX_INVALID_VALUE; |
| 123 |
|
| 124 |
assert(str->type == eStreamTypeFile); |
| 125 |
|
| 126 |
for (ktx_uint32_t i = 0; i < count; i++) { |
| 127 |
int ret = getc(str->data.file); |
| 128 |
if (ret == EOF) { |
| 129 |
if (feof(str->data.file)) { |
| 130 |
return KTX_FILE_UNEXPECTED_EOF; |
| 131 |
} else { |
| 132 |
return KTX_FILE_READ_ERROR; |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
str->readpos += count; |
| 137 |
|
| 138 |
return KTX_SUCCESS; |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
static |
| 162 |
KTX_error_code ktxFileStream_write(ktxStream* str, const void *src, |
| 163 |
const ktx_size_t size, |
| 164 |
const ktx_size_t count) |
| 165 |
{ |
| 166 |
if (!str || !src) |
| 167 |
return KTX_INVALID_VALUE; |
| 168 |
|
| 169 |
assert(str->type == eStreamTypeFile); |
| 170 |
|
| 171 |
if (fwrite(src, size, count, str->data.file) != count) { |
| 172 |
if (errno == EFBIG || errno == EOVERFLOW) |
| 173 |
return KTX_FILE_OVERFLOW; |
| 174 |
else |
| 175 |
return KTX_FILE_WRITE_ERROR; |
| 176 |
} |
| 177 |
|
| 178 |
return KTX_SUCCESS; |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
static |
| 196 |
KTX_error_code ktxFileStream_getpos(ktxStream* str, ktx_off_t* pos) |
| 197 |
{ |
| 198 |
ktx_off_t ftellval; |
| 199 |
|
| 200 |
if (!str || !pos) |
| 201 |
return KTX_INVALID_VALUE; |
| 202 |
|
| 203 |
assert(str->type == eStreamTypeFile); |
| 204 |
|
| 205 |
if (str->data.file == stdin) { |
| 206 |
*pos = str->readpos; |
| 207 |
} else { |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
ftellval = (ktx_off_t)ftello(str->data.file); |
| 212 |
if (ftellval < 0) { |
| 213 |
switch (errno) { |
| 214 |
case ESPIPE: return KTX_FILE_ISPIPE; |
| 215 |
case EOVERFLOW: return KTX_FILE_OVERFLOW; |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
*pos = ftellval; |
| 220 |
} |
| 221 |
|
| 222 |
return KTX_SUCCESS; |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
static |
| 247 |
KTX_error_code ktxFileStream_setpos(ktxStream* str, ktx_off_t pos) |
| 248 |
{ |
| 249 |
ktx_size_t fileSize; |
| 250 |
KTX_error_code result; |
| 251 |
|
| 252 |
if (!str) |
| 253 |
return KTX_INVALID_VALUE; |
| 254 |
|
| 255 |
assert(str->type == eStreamTypeFile); |
| 256 |
|
| 257 |
if (str->data.file == stdin) { |
| 258 |
if (pos > str->readpos) |
| 259 |
return str->skip(str, pos - str->readpos); |
| 260 |
else |
| 261 |
return KTX_FILE_ISPIPE; |
| 262 |
} |
| 263 |
|
| 264 |
result = str->getsize(str, &fileSize); |
| 265 |
|
| 266 |
if (result != KTX_SUCCESS) { |
| 267 |
|
| 268 |
return result; |
| 269 |
} |
| 270 |
|
| 271 |
if (pos > (ktx_off_t)fileSize) |
| 272 |
return KTX_INVALID_OPERATION; |
| 273 |
|
| 274 |
if (fseeko(str->data.file, pos, SEEK_SET) < 0) |
| 275 |
return KTX_FILE_SEEK_ERROR; |
| 276 |
else |
| 277 |
return KTX_SUCCESS; |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
static |
| 300 |
KTX_error_code ktxFileStream_getsize(ktxStream* str, ktx_size_t* size) |
| 301 |
{ |
| 302 |
struct stat statbuf; |
| 303 |
int statret; |
| 304 |
|
| 305 |
if (!str || !size) |
| 306 |
return KTX_INVALID_VALUE; |
| 307 |
|
| 308 |
assert(str->type == eStreamTypeFile); |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
#if (defined(_MSC_VER) && _MSC_VER < 1900) || defined(__MINGW64__) && !defined(_UCRT) |
| 314 |
|
| 315 |
|
| 316 |
if (str->data.file->_flag & _IOWRT) |
| 317 |
(void)fflush(str->data.file); |
| 318 |
#else |
| 319 |
(void)fflush(str->data.file); |
| 320 |
#endif |
| 321 |
statret = fstat(fileno(str->data.file), &statbuf); |
| 322 |
if (statret < 0) { |
| 323 |
switch (errno) { |
| 324 |
case EOVERFLOW: return KTX_FILE_OVERFLOW; |
| 325 |
case EIO: |
| 326 |
default: |
| 327 |
return KTX_FILE_READ_ERROR; |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
mode_t ftype = statbuf.st_mode & S_IFMT; |
| 332 |
if (ftype == S_IFIFO || ftype == S_IFSOCK) |
| 333 |
return KTX_FILE_ISPIPE; |
| 334 |
|
| 335 |
if (statbuf.st_mode & S_IFCHR) |
| 336 |
return KTX_INVALID_OPERATION; |
| 337 |
|
| 338 |
*size = (ktx_size_t)statbuf.st_size; |
| 339 |
|
| 340 |
return KTX_SUCCESS; |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
KTX_error_code ktxFileStream_construct(ktxStream* str, FILE* file, |
| 357 |
ktx_bool_t closeFileOnDestruct) |
| 358 |
{ |
| 359 |
if (!str || !file) |
| 360 |
return KTX_INVALID_VALUE; |
| 361 |
|
| 362 |
str->data.file = file; |
| 363 |
str->readpos = 0; |
| 364 |
str->type = eStreamTypeFile; |
| 365 |
str->read = ktxFileStream_read; |
| 366 |
str->skip = ktxFileStream_skip; |
| 367 |
str->write = ktxFileStream_write; |
| 368 |
str->getpos = ktxFileStream_getpos; |
| 369 |
str->setpos = ktxFileStream_setpos; |
| 370 |
str->getsize = ktxFileStream_getsize; |
| 371 |
str->destruct = ktxFileStream_destruct; |
| 372 |
str->closeOnDestruct = closeFileOnDestruct; |
| 373 |
|
| 374 |
return KTX_SUCCESS; |
| 375 |
} |
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
void |
| 388 |
ktxFileStream_destruct(ktxStream* str) |
| 389 |
{ |
| 390 |
assert(str && str->type == eStreamTypeFile); |
| 391 |
|
| 392 |
if (str->closeOnDestruct) |
| 393 |
fclose(str->data.file); |
| 394 |
str->data.file = 0; |
| 395 |
} |