Mercurial > hg > dmlib
comparison src/dmres.c @ 2445:fa089a430121
Cleanup, rename DMResource::error to DMResource::status.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 09 Mar 2020 22:37:00 +0200 |
parents | fd31b5d1ed5e |
children | 8193f62a541d |
comparison
equal
deleted
inserted
replaced
2444:fd31b5d1ed5e | 2445:fa089a430121 |
---|---|
180 return DMERR_MALLOC; | 180 return DMERR_MALLOC; |
181 | 181 |
182 handle->fh = fopen(rfilename, "rb"); | 182 handle->fh = fopen(rfilename, "rb"); |
183 dmFree(rfilename); | 183 dmFree(rfilename); |
184 | 184 |
185 handle->error = dmGetErrno(); | 185 handle->status = dmGetErrno(); |
186 return (handle->fh != NULL) ? DMERR_OK : DMERR_FOPEN; | 186 return (handle->fh != NULL) ? DMERR_OK : DMERR_FOPEN; |
187 } | 187 } |
188 | 188 |
189 | 189 |
190 static void dm_stdio_fclose(DMResource *fh) | 190 static void dm_stdio_fclose(DMResource *fh) |
197 } | 197 } |
198 | 198 |
199 | 199 |
200 static int dm_stdio_ferror(DMResource *fh) | 200 static int dm_stdio_ferror(DMResource *fh) |
201 { | 201 { |
202 return fh->error; | 202 return fh->status; |
203 } | 203 } |
204 | 204 |
205 | 205 |
206 static off_t dm_stdio_ftell(DMResource *fh) | 206 static off_t dm_stdio_ftell(DMResource *fh) |
207 { | 207 { |
210 | 210 |
211 | 211 |
212 static int dm_stdio_fseek(DMResource *fh, const off_t pos, const int whence) | 212 static int dm_stdio_fseek(DMResource *fh, const off_t pos, const int whence) |
213 { | 213 { |
214 int ret = fseeko(fh->fh, pos, whence); | 214 int ret = fseeko(fh->fh, pos, whence); |
215 fh->error = dmGetErrno(); | 215 fh->status = dmGetErrno(); |
216 return ret; | 216 return ret; |
217 } | 217 } |
218 | 218 |
219 | 219 |
220 static int dm_stdio_freset(DMResource *fh) | 220 static int dm_stdio_freset(DMResource *fh) |
259 | 259 |
260 | 260 |
261 static int dm_stdio_fgetc(DMResource *fh) | 261 static int dm_stdio_fgetc(DMResource *fh) |
262 { | 262 { |
263 int ret = fgetc(fh->fh); | 263 int ret = fgetc(fh->fh); |
264 fh->error = dmGetErrno(); | 264 fh->status = dmGetErrno(); |
265 return ret; | 265 return ret; |
266 } | 266 } |
267 | 267 |
268 | 268 |
269 static int dm_stdio_fputc(int v, DMResource *fh) | 269 static int dm_stdio_fputc(int v, DMResource *fh) |
270 { | 270 { |
271 int ret = fputc(v, fh->fh); | 271 int ret = fputc(v, fh->fh); |
272 fh->error = dmGetErrno(); | 272 fh->status = dmGetErrno(); |
273 return ret; | 273 return ret; |
274 } | 274 } |
275 | 275 |
276 | 276 |
277 static size_t dm_stdio_fread(void *ptr, size_t size, size_t nmemb, DMResource *fh) | 277 static size_t dm_stdio_fread(void *ptr, size_t size, size_t nmemb, DMResource *fh) |
278 { | 278 { |
279 size_t ret = fread(ptr, size, nmemb, fh->fh); | 279 size_t ret = fread(ptr, size, nmemb, fh->fh); |
280 fh->error = dmGetErrno(); | 280 fh->status = dmGetErrno(); |
281 return ret; | 281 return ret; |
282 } | 282 } |
283 | 283 |
284 | 284 |
285 static size_t dm_stdio_fwrite(const void *ptr, size_t size, size_t nmemb, DMResource *fh) | 285 static size_t dm_stdio_fwrite(const void *ptr, size_t size, size_t nmemb, DMResource *fh) |
286 { | 286 { |
287 size_t ret = fwrite(ptr, size, nmemb, fh->fh); | 287 size_t ret = fwrite(ptr, size, nmemb, fh->fh); |
288 fh->error = dmGetErrno(); | 288 fh->status = dmGetErrno(); |
289 return ret; | 289 return ret; |
290 } | 290 } |
291 | 291 |
292 | 292 |
293 static int dm_stdio_preload(DMResource *handle) | 293 static int dm_stdio_preload(DMResource *handle) |
422 | 422 |
423 #else | 423 #else |
424 | 424 |
425 static int dm_pack_decompress(DMResource *handle, DMPackEntry *node) | 425 static int dm_pack_decompress(DMResource *handle, DMPackEntry *node) |
426 { | 426 { |
427 DMZLibContext ctx; | 427 DMZLibContext zctx; |
428 Uint8 *inBuf = NULL; | 428 Uint8 *inBuf = NULL; |
429 int ret; | 429 int ret; |
430 | 430 |
431 // Allocate buffer for compressed data | 431 // Allocate buffer for compressed data |
432 if ((inBuf = dmMalloc(node->length)) == NULL) | 432 if ((inBuf = dmMalloc(node->length)) == NULL) |
441 ret = DMERR_FREAD; | 441 ret = DMERR_FREAD; |
442 goto out; | 442 goto out; |
443 } | 443 } |
444 | 444 |
445 // Initialize decompression structures | 445 // Initialize decompression structures |
446 if ((ret = dmZLibInitInflate(&ctx)) != DMERR_OK) | 446 if ((ret = dmZLibInitInflate(&zctx)) != DMERR_OK) |
447 goto out; | 447 goto out; |
448 | 448 |
449 ctx.inBuffer = ctx.inBufferStart = inBuf; | 449 zctx.inBuffer = zctx.inBufferStart = inBuf; |
450 ctx.inBufferEnd = inBuf + node->length; | 450 zctx.inBufferEnd = inBuf + node->length; |
451 ctx.outBuffer = ctx.outBufferStart = handle->memData; | 451 zctx.outBuffer = zctx.outBufferStart = handle->memData; |
452 ctx.outBufferEnd = handle->memData + node->size; | 452 zctx.outBufferEnd = handle->memData + node->size; |
453 ctx.expandable = FALSE; | 453 zctx.expandable = FALSE; |
454 | 454 |
455 // Attempt decompression | 455 // Attempt decompression |
456 if ((ret = dmZLibParseHeader(&ctx, TRUE)) != DMERR_OK) | 456 if ((ret = dmZLibParseHeader(&zctx, TRUE)) != DMERR_OK) |
457 goto out; | 457 goto out; |
458 | 458 |
459 if ((ret = dmZLibInflate(&ctx)) != DMERR_OK) | 459 if ((ret = dmZLibInflate(&zctx)) != DMERR_OK) |
460 goto out; | 460 goto out; |
461 | 461 |
462 handle->memData = ctx.outBufferStart; | 462 handle->memData = zctx.outBufferStart; |
463 handle->memSize = ctx.outBuffer - ctx.outBufferStart; | 463 handle->memSize = zctx.outBuffer - zctx.outBufferStart; |
464 | 464 |
465 out: | 465 out: |
466 dmZLibCloseInflate(&ctx); | 466 dmZLibCloseInflate(&zctx); |
467 dmFree(inBuf); | 467 dmFree(inBuf); |
468 return ret; | 468 return ret; |
469 } | 469 } |
470 | 470 |
471 #endif | 471 #endif |
570 size_t grow; | 570 size_t grow; |
571 | 571 |
572 // Check against max size | 572 // Check against max size |
573 if (ctx->maxSize > 0 && newSize > ctx->maxSize) | 573 if (ctx->maxSize > 0 && newSize > ctx->maxSize) |
574 { | 574 { |
575 ctx->error = DMERR_BOUNDS; | 575 ctx->status = DMERR_BOUNDS; |
576 return FALSE; | 576 return FALSE; |
577 } | 577 } |
578 | 578 |
579 // New size is smaller than old | 579 // New size is smaller than old |
580 if (newSize < ctx->memAlloc) | 580 if (newSize < ctx->memAlloc) |
585 if (newSize - ctx->memAlloc > grow) | 585 if (newSize - ctx->memAlloc > grow) |
586 grow += newSize - ctx->memAlloc; | 586 grow += newSize - ctx->memAlloc; |
587 | 587 |
588 if (ctx->maxSize > 0 && ctx->memAlloc + grow >= ctx->maxSize) | 588 if (ctx->maxSize > 0 && ctx->memAlloc + grow >= ctx->maxSize) |
589 { | 589 { |
590 ctx->error = DMERR_BOUNDS; | 590 ctx->status = DMERR_BOUNDS; |
591 return FALSE; | 591 return FALSE; |
592 } | 592 } |
593 | 593 |
594 // Grow the buffer | 594 // Grow the buffer |
595 ctx->memAlloc += grow; | 595 ctx->memAlloc += grow; |
596 if ((ctx->memData = dmRealloc(ctx->memData, ctx->memAlloc)) == NULL) | 596 if ((ctx->memData = dmRealloc(ctx->memData, ctx->memAlloc)) == NULL) |
597 { | 597 { |
598 ctx->error = DMERR_MALLOC; | 598 ctx->status = DMERR_MALLOC; |
599 return FALSE; | 599 return FALSE; |
600 } | 600 } |
601 | 601 |
602 out: | 602 out: |
603 ctx->memSize = newSize; | 603 ctx->memSize = newSize; |
613 } | 613 } |
614 | 614 |
615 | 615 |
616 static int dm_mem_ferror(DMResource *ctx) | 616 static int dm_mem_ferror(DMResource *ctx) |
617 { | 617 { |
618 return ctx->error; | 618 return ctx->status; |
619 } | 619 } |
620 | 620 |
621 | 621 |
622 static int dm_mem_fseek(DMResource *ctx, const off_t offset, const int whence) | 622 static int dm_mem_fseek(DMResource *ctx, const off_t offset, const int whence) |
623 { | 623 { |
907 if ((*phandle = handle = dmResourceNew(NULL, filename, 0, 0)) == NULL) | 907 if ((*phandle = handle = dmResourceNew(NULL, filename, 0, 0)) == NULL) |
908 return DMERR_MALLOC; | 908 return DMERR_MALLOC; |
909 | 909 |
910 handle->fops = &dfStdioFileOps; | 910 handle->fops = &dfStdioFileOps; |
911 handle->fh = fopen(filename, mode); | 911 handle->fh = fopen(filename, mode); |
912 handle->error = dmGetErrno(); | 912 handle->status = dmGetErrno(); |
913 | 913 |
914 if (handle->fh == NULL) | 914 if (handle->fh == NULL) |
915 { | 915 { |
916 int error = handle->error; | 916 int error = handle->status; |
917 dmResourceFree(handle); | 917 dmResourceFree(handle); |
918 return error; | 918 return error; |
919 } | 919 } |
920 | 920 |
921 dmResourceRef(handle); | 921 dmResourceRef(handle); |
1343 { | 1343 { |
1344 int len, res; | 1344 int len, res; |
1345 char *str = dm_strdup_vprintf_len(fmt, ap, &len); | 1345 char *str = dm_strdup_vprintf_len(fmt, ap, &len); |
1346 if (str == NULL) | 1346 if (str == NULL) |
1347 { | 1347 { |
1348 fh->error = DMERR_MALLOC; | 1348 fh->status = DMERR_MALLOC; |
1349 return -1; | 1349 return -1; |
1350 } | 1350 } |
1351 | 1351 |
1352 res = dmfwrite(str, 1, len, fh); | 1352 res = dmfwrite(str, 1, len, fh); |
1353 dmFree(str); | 1353 dmFree(str); |