Use statically-allocated thread private data
[projects/chimara/chimara.git] / libchimara / resource.c
1 #include "resource.h"
2 #include "stream.h"
3
4 extern GPrivate glk_data_key;
5
6 /**
7  * giblorb_set_resource_map:
8  * @file: The file stream to read the resource map from
9  *
10  * This function tells the library that the file is indeed the Blorby source
11  * of all resource goodness. Whenever your program calls an image or sound
12  * function, such as glk_image_draw(), the library will search this file for
13  * the resource you request. 
14  *
15  * Do <emphasis>not</emphasis> close the stream after calling this function. 
16  * The library is responsible for closing the stream at shutdown time.
17  *
18  * Returns: a Blorb error code.
19  */
20 giblorb_err_t
21 giblorb_set_resource_map(strid_t file)
22 {
23         ChimaraGlkPrivate *glk_data = g_private_get(&glk_data_key);
24         giblorb_map_t *newmap; /* create map allocates memory */
25         giblorb_err_t error = giblorb_create_map(file, &newmap);
26
27         if(error != giblorb_err_None) {
28                 g_free(newmap);
29                 return error;
30         }
31
32         /* Check if there was already an existing resource map */
33         if(glk_data->resource_map != NULL) {
34                 WARNING("Overwriting existing resource map.\n");
35                 giblorb_destroy_map(glk_data->resource_map);
36                 glk_stream_close(glk_data->resource_file, NULL);
37         }
38
39         glk_data->resource_map = newmap;
40         glk_data->resource_file = file;
41
42         return giblorb_err_None;
43 }
44
45 /**
46  * giblorb_get_resource_map:
47  * 
48  * This function returns the current resource map being used. Returns %NULL
49  * if giblorb_set_resource_map() has not been called yet.
50  *
51  * Returns: a resource map, or %NULL.
52  */
53 giblorb_map_t*
54 giblorb_get_resource_map()
55 {
56         ChimaraGlkPrivate *glk_data = g_private_get(&glk_data_key);
57
58         if(glk_data->resource_map == NULL) {
59                 WARNING("Resource map not set yet.\n");
60         }
61
62         return glk_data->resource_map;
63 }
64
65 /* giblorb_chunkdesc_t: Describes one chunk of the Blorb file. */
66 typedef struct giblorb_chunkdesc_struct {
67     glui32 type;
68     glui32 len;
69     glui32 startpos; /* start of chunk header */
70     glui32 datpos; /* start of data (either startpos or startpos+8) */
71     
72     void *ptr; /* pointer to malloc'd data, if loaded */
73     int auxdatnum; /* entry in the auxsound/auxpict array; -1 if none.
74         This only applies to chunks that represent resources;  */
75     
76 } giblorb_chunkdesc_t;
77
78 /* giblorb_resdesc_t: Describes one resource in the Blorb file. */
79 typedef struct giblorb_resdesc_struct {
80     glui32 usage;
81     glui32 resnum;
82     glui32 chunknum;
83 } giblorb_resdesc_t;
84
85 /* giblorb_map_t: Holds the complete description of an open Blorb file. */
86 struct giblorb_map_struct {
87     glui32 inited; /* holds giblorb_Inited_Magic if the map structure is 
88         valid */
89     strid_t file;
90     
91     int numchunks;
92     giblorb_chunkdesc_t *chunks; /* list of chunk descriptors */
93     
94     int numresources;
95     giblorb_resdesc_t *resources; /* list of resource descriptors */
96     giblorb_resdesc_t **ressorted; /* list of pointers to descriptors 
97         in map->resources -- sorted by usage and resource number. */
98 };
99
100 void
101 giblorb_print_contents(giblorb_map_t *map)
102 {
103         int i;
104         for(i=0; i<map->numresources; i++) {
105                 giblorb_resdesc_t *resource = map->ressorted[i];
106                 printf("Resource #%d, chunknum: %d\n", resource->resnum, resource->chunknum);
107         }       
108
109         printf("\n-------\n");
110
111         for(i=0; i<map->numresources; i++) {
112                 giblorb_chunkdesc_t chunk = map->chunks[i];
113                 printf("Chunk #%d, type: %d\n", i, chunk.type);
114         }       
115 }
116
117 const char *
118 giblorb_get_error_message(giblorb_err_t err)
119 {
120         switch(err)
121         {
122                 case giblorb_err_None:
123                         return "not BLORB's fault";
124                 case giblorb_err_CompileTime:
125                         return "BLORB compile time error";
126                 case giblorb_err_Alloc:
127                         return "memory allocation failed";
128                 case giblorb_err_Read:
129                         return "error during reading of file";
130                 case giblorb_err_NotAMap:
131                         return "invalid resource map supplied";
132                 case giblorb_err_Format:
133                         return "invalid format of resource";
134                 case giblorb_err_NotFound:
135                         return "resource not found";
136                 default:
137                         return "Unknown error code";
138         }
139 }