Mercurial > hg > dmlib
comparison dmres.c @ 730:3d813c81f33c
More work on resources API.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Sun, 21 Apr 2013 19:29:00 +0300 |
parents | 867fc7f205a8 |
children | 0e164aaa7409 |
comparison
equal
deleted
inserted
replaced
729:e09b7fbedf8c | 730:3d813c81f33c |
---|---|
576 dm_mem_fputc, | 576 dm_mem_fputc, |
577 dm_mem_fread, | 577 dm_mem_fread, |
578 dm_mem_fwrite, | 578 dm_mem_fwrite, |
579 | 579 |
580 NULL, | 580 NULL, |
581 NULL, | 581 dmResourceFree, |
582 NULL | 582 NULL |
583 }; | 583 }; |
584 | 584 |
585 | 585 |
586 /* FS file handling functions. These functions call the actual | 586 /* FS file handling functions. These functions call the actual |
587 * functions depending on where the file is located. | 587 * functions depending on where the file is located. |
588 */ | 588 */ |
589 static void dmf_reset(DMResource *handle) | 589 static void dmResourceReset(DMResource *handle) |
590 { | 590 { |
591 if (handle != NULL && handle->fops != NULL) | 591 if (handle != NULL && handle->fops != NULL) |
592 dmfseek(handle, 0, SEEK_SET); | 592 dmfseek(handle, 0, SEEK_SET); |
593 } | 593 } |
594 | 594 |
595 | 595 |
596 static int dmResourcePreload(DMResource *handle) | 596 static int dmResourcePreload(DMResource *handle) |
597 { | 597 { |
598 int ret = DMERR_INIT_FAIL; | 598 int ret = DMERR_INIT_FAIL; |
599 | |
600 // Initialize file ops | |
601 dmf_init_fops(handle); | |
602 | 599 |
603 // Check if we want to preload raw data? | 600 // Check if we want to preload raw data? |
604 if (handle->lib->flags & DRF_PRELOAD_RAW) | 601 if (handle->lib->flags & DRF_PRELOAD_RAW) |
605 { | 602 { |
606 if (handle->flags & DMF_LOADED_RAW) | 603 if (handle->flags & DMF_LOADED_RAW) |
611 ret = handle->fops->preload(handle); | 608 ret = handle->fops->preload(handle); |
612 if (ret == DMERR_OK) | 609 if (ret == DMERR_OK) |
613 handle->flags |= DMF_LOADED_RAW | DMF_PERSIST; | 610 handle->flags |= DMF_LOADED_RAW | DMF_PERSIST; |
614 } | 611 } |
615 | 612 |
616 dmf_reset(handle); | 613 dmResourceReset(handle); |
617 } | 614 } |
618 | 615 |
619 // Check if resource data is to be preloaded | 616 // Check if resource data is to be preloaded |
620 if (handle->lib->flags & DRF_PRELOAD_RES) | 617 if (handle->lib->flags & DRF_PRELOAD_RES) |
621 { | 618 { |
633 | 630 |
634 if (ret == DMERR_OK) | 631 if (ret == DMERR_OK) |
635 handle->flags |= DMF_LOADED_RES; | 632 handle->flags |= DMF_LOADED_RES; |
636 } | 633 } |
637 | 634 |
638 dmf_reset(handle); | 635 dmResourceReset(handle); |
639 } | 636 } |
640 | 637 |
641 return ret; | 638 return ret; |
642 } | 639 } |
643 | 640 |
644 | 641 |
645 int dmf_open(DMResourceLib *lib, const char *filename, DMResource **handle) | 642 int dmf_open(DMResourceLib *lib, const char *filename, DMResource **phandle) |
646 { | 643 { |
644 DMResource *handle; | |
647 int res; | 645 int res; |
648 | 646 |
649 // Check master directory for resource | 647 // Check master directory for resource |
650 if ((*handle = dmResourceFind(lib, filename)) == NULL) | 648 if ((*phandle = handle = dmResourceFind(lib, filename)) == NULL) |
651 { | 649 { |
652 #ifdef DM_USE_STDIO | 650 #ifdef DM_USE_STDIO |
653 if (lib->flags & DRF_USE_STDIO) | 651 if (lib->flags & DRF_USE_STDIO) |
654 { | 652 { |
655 // Hmm.. does not exist? Fall back to a stdio file | 653 // Hmm.. does not exist? Fall back to a stdio file |
656 *handle = dmResourceNew(lib, filename, 0); | 654 *phandle = handle = dmResourceNew(lib, filename, 0); |
657 if (*handle == NULL) | 655 if (handle == NULL) |
658 return NULL; | 656 return DMERR_MALLOC; |
659 | 657 |
660 (*handle)->fops = &dfStdioFileOps; | 658 handle->fops = &dfStdioFileOps; |
661 } | 659 } |
662 else | 660 else |
663 return NULL; | 661 return DMERR_INIT_FAIL; |
664 #else | 662 #else |
665 // Stdio not enabled, fail | 663 // Stdio not enabled, fail |
666 return NULL; | 664 return DMERR_INIT_FAIL; |
667 #endif | 665 #endif |
668 } | 666 } |
669 | 667 |
670 // Check if the data is preloaded | 668 // Check if the data is preloaded |
671 if ((res = handle->fops->fopen(handle)) == DMERR_OK) | 669 if ((res = handle->fops->fopen(handle)) == DMERR_OK) |
670 { | |
672 dmResourceRef(handle); | 671 dmResourceRef(handle); |
672 if (handle->flags & DMF_TEMPORARY) | |
673 { | |
674 handle->flags &= ~DMF_TEMPORARY; | |
675 dmResourceInsert(lib, handle); | |
676 } | |
677 } | |
678 else | |
679 if (handle->flags & DMF_TEMPORARY) | |
680 { | |
681 dmResourceFree(handle); | |
682 *phandle = handle = NULL; | |
683 } | |
673 | 684 |
674 return res; | 685 return res; |
675 } | 686 } |
676 | 687 |
677 | 688 |
678 DMResource * dmf_create_memio(DMResourceLib *lib, const char *filename, Uint8 *buf, const size_t size) | 689 int dmf_create_memio(DMResourceLib *lib, const char *filename, |
690 Uint8 *buf, const size_t size, DMResource **phandle) | |
679 { | 691 { |
680 DMResource *handle; | 692 DMResource *handle; |
681 | 693 |
682 // Check master directory for resource | 694 // Check master directory for resource |
683 if ((handle = dmResourceFind(lib, filename)) == NULL) | 695 if ((*phandle = handle = dmResourceFind(lib, filename)) == NULL) |
684 { | 696 { |
685 handle = dmResourceNew(lib, filename, size); | 697 if ((*phandle = handle = dmResourceNew(lib, filename, size)) == NULL) |
686 if (handle == NULL) | 698 return DMERR_MALLOC; |
687 return NULL; | |
688 | 699 |
689 handle->flags = DMF_LOADED_RAW; | 700 handle->flags = DMF_LOADED_RAW; |
690 handle->fops = &dfMemIOFileOps; | 701 handle->fops = &dfMemIOFileOps; |
691 handle->rawData = buf; | 702 handle->rawData = buf; |
692 dmResourceInsert(lib, handle); | 703 dmResourceInsert(lib, handle); |
693 } | 704 } |
694 | 705 |
695 // Increase refcount | 706 // Increase refcount |
696 dmResourceRef(handle); | 707 dmResourceRef(handle); |
697 | 708 dmResourceReset(handle); |
698 dmf_reset(handle); | 709 return DMERR_OK; |
699 return handle; | |
700 } | 710 } |
701 | 711 |
702 | 712 |
703 #ifdef DM_USE_STDIO | 713 #ifdef DM_USE_STDIO |
704 DMResource * dmf_create_stdio(const char *filename, const char *mode) | 714 int dmf_create_stdio(const char *filename, const char *mode, DMResource **phandle) |
705 { | 715 { |
706 DMResource *handle = dmResourceNew(NULL, filename, 0); | 716 DMResource *handle; |
707 if (handle == NULL) | 717 if ((*phandle = handle = dmResourceNew(NULL, filename, 0)) == NULL) |
708 return NULL; | 718 return DMERR_MALLOC; |
709 | 719 |
710 handle->fops = &dfStdioFileOps; | 720 handle->fops = &dfStdioFileOps; |
711 handle->fh = fopen(filename, mode); | 721 handle->fh = fopen(filename, mode); |
712 handle->error = dmGetErrno(); | 722 handle->error = dmGetErrno(); |
713 | 723 |
714 if (handle->fh != NULL) | 724 if (handle->fh == NULL) |
715 { | |
716 dmResourceRef(handle); | |
717 return handle; | |
718 } | |
719 else | |
720 { | 725 { |
721 dmResourceFree(handle); | 726 dmResourceFree(handle); |
722 return NULL; | 727 return handle->error; |
723 } | 728 } |
724 } | 729 |
725 | 730 dmResourceRef(handle); |
726 | 731 return DMERR_OK; |
727 DMResource * dmf_create_stdio_stream(FILE *fh) | 732 } |
728 { | 733 |
729 DMResource *handle = dmResourceNew(NULL, "", 0); | 734 |
730 if (handle == NULL) | 735 int dmf_create_stdio_stream(FILE *fh, DMResource **phandle) |
731 return NULL; | 736 { |
737 DMResource *handle; | |
738 if ((*phandle = handle = dmResourceNew(NULL, "", 0)) == NULL) | |
739 return DMERR_MALLOC; | |
732 | 740 |
733 handle->fops = &dfStdioFHOps; | 741 handle->fops = &dfStdioFHOps; |
734 handle->fh = fh; | 742 handle->fh = fh; |
735 dmResourceRef(handle); | 743 dmResourceRef(handle); |
736 return handle; | 744 return DMERR_OK; |
737 } | 745 } |
738 #endif | 746 #endif |
739 | 747 |
740 | 748 |
741 void dmf_close(DMResource * f) | 749 void dmf_close(DMResource * f) |