comparison dmres.c @ 29:e9f562f07cb0

Modularize some more and fix a nasty bug when raw data is not preloaded but resource data needs to be preloaded.
author Matti Hamalainen <ccr@tnsp.org>
date Sat, 29 Sep 2012 07:19:53 +0300
parents 21c14afbf63d
children b2fe4301384d
comparison
equal deleted inserted replaced
28:920fb22cffcf 29:e9f562f07cb0
531 #endif 531 #endif
532 } 532 }
533 } 533 }
534 534
535 535
536 int dmf_preload(DMResource *handle)
537 {
538 int ret = DMERR_INIT_FAIL;
539
540 // Check if we want to preload raw data?
541 if (((handle->flags & DMF_PRELOAD_RAW) ||
542 (dfResFlags & DRF_PRELOAD_ALL)) &&
543 (handle->flags & DMF_LOADED_RAW) == 0 &&
544 handle->fops->preload != NULL)
545 {
546 ret = handle->fops->preload(handle);
547 if (ret == DMERR_OK)
548 {
549 handle->flags |= DMF_LOADED_RAW;
550 }
551 }
552 else
553 {
554 if (handle->fops->fopen != NULL)
555 ret = handle->fops->fopen(handle);
556 else
557 if (handle->fops->preload != NULL)
558 {
559 ret = handle->fops->preload(handle);
560 if (ret == DMERR_OK)
561 {
562 handle->flags |= DMF_LOADED_RAW;
563 }
564 }
565 }
566
567 // Check if resource data is to be preloaded
568 if (((handle->flags & DMF_PRELOAD_RES) || (dfResFlags & DRF_PRELOAD_RES)) &&
569 (handle->flags & DMF_LOADED_RES) == 0 &&
570 handle->rops != NULL &&
571 handle->rops->load != NULL)
572 {
573 ret = handle->rops->load(handle);
574 if (ret == DMERR_OK)
575 {
576 // Okay, mark as loaded
577 handle->flags |= DMF_LOADED_RES;
578
579 // Check if we can purge the raw data now
580 if ((handle->flags & DMF_PERSIST) == 0)
581 dmres_purge_raw_data(handle);
582 }
583 }
584
585 return ret;
586 }
587
588
536 DMResource *dmf_open(const char *filename) 589 DMResource *dmf_open(const char *filename)
537 { 590 {
538 int ret; 591 int ret;
539 DMResource *handle; 592 DMResource *handle;
540 593
553 // Stdio not enabled, fail 606 // Stdio not enabled, fail
554 return NULL; 607 return NULL;
555 #endif 608 #endif
556 } 609 }
557 610
611 // Initialize file ops
558 dmf_init_fops(handle); 612 dmf_init_fops(handle);
559 613
560 // Check if the data is preloaded 614 // Check if the data is preloaded
561 if (handle->flags & DMF_LOADED_RAW) 615 if (handle->flags & DMF_LOADED_RAW)
562 { 616 {
563 dmres_ref(handle); 617 dmres_ref(handle);
564 return handle; 618 return handle;
565 } 619 }
566 620
567 // Check if we want to preload .. 621 // Try preloading
568 ret = DMERR_INIT_FAIL; 622 ret = dmf_preload(handle);
569 if (((handle->flags & DMF_PRELOAD_RAW) || (dfResFlags & DRF_PRELOAD_ALL)) &&
570 (handle->flags & DMF_LOADED_RAW) == 0 &&
571 handle->fops->preload != NULL)
572 ret = handle->fops->preload(handle);
573 else
574 {
575 if (handle->fops->fopen != NULL)
576 ret = handle->fops->fopen(handle);
577 else
578 if (handle->fops->preload != NULL)
579 ret = handle->fops->preload(handle);
580 }
581
582 if (((handle->flags & DMF_PRELOAD_RES) || (dfResFlags & DRF_PRELOAD_RES)) &&
583 (handle->flags & DMF_LOADED_RES) == 0 &&
584 handle->rops != NULL &&
585 handle->rops->load != NULL)
586 ret = handle->rops->load(handle);
587
588 623
589 if (ret == DMERR_OK) 624 if (ret == DMERR_OK)
590 { 625 {
591 dmres_ref(handle); 626 dmres_ref(handle);
592 return handle; 627 return handle;
941 } 976 }
942 } 977 }
943 else 978 else
944 if (dfPreload != NULL) 979 if (dfPreload != NULL)
945 { 980 {
946 // Check if the raw resource wants to be preloaded 981 // Initialize fops and preload
947 if (((dfPreload->flags & DMF_PRELOAD_RAW) || 982 dmf_init_fops(dfPreload);
948 (dfResFlags & DRF_PRELOAD_ALL)) && 983 if ((ret = dmf_preload(dfPreload)) != DMERR_OK)
949 (dfPreload->flags & DMF_LOADED_RAW) == 0) 984 goto error;
950 { 985
951 dmf_init_fops(dfPreload); 986 (*loaded)++;
952 if (dfPreload->fops->preload != NULL)
953 {
954 ret = dfPreload->fops->preload(dfPreload);
955 if (ret == DMERR_OK)
956 {
957 dfPreload->flags |= DMF_LOADED_RAW;
958 (*loaded)++;
959 }
960 else
961 goto error;
962 }
963 }
964
965 // Preload actual resource, if requested
966 if (((dfPreload->flags & DMF_PRELOAD_RES) ||
967 (dfResFlags & DRF_PRELOAD_RES)) &&
968 (dfPreload->flags & DMF_LOADED_RES) == 0 &&
969 dfPreload->rops != NULL &&
970 dfPreload->rops->load != NULL)
971 {
972 dmf_init_fops(dfPreload);
973 ret = dfPreload->rops->load(dfPreload);
974 if (ret != DMERR_OK)
975 goto error;
976
977 dfPreload->flags |= DMF_LOADED_RES;
978 }
979
980 dfPreload = dfPreload->next; 987 dfPreload = dfPreload->next;
981 } 988 }
982 989
983 DMRES_UNLOCK(); 990 DMRES_UNLOCK();
984 return (dfPreload == NULL) ? DMERR_OK : DMERR_PROGRESS; 991 return (dfPreload == NULL) ? DMERR_OK : DMERR_PROGRESS;