Use statically-allocated thread private data
[projects/chimara/chimara.git] / libchimara / resource.c
index ad5dbf115d1c4a7fc6a3ecb436ba9b61e7c697c5..49ef35280a369daf60aeecdf6279c81fd3cf5f58 100644 (file)
@@ -1,6 +1,7 @@
 #include "resource.h"
+#include "stream.h"
 
-extern ChimaraGlkPrivate *glk_data;
+extern GPrivate glk_data_key;
 
 /**
  * giblorb_set_resource_map:
@@ -19,6 +20,7 @@ extern ChimaraGlkPrivate *glk_data;
 giblorb_err_t
 giblorb_set_resource_map(strid_t file)
 {
+       ChimaraGlkPrivate *glk_data = g_private_get(&glk_data_key);
        giblorb_map_t *newmap; /* create map allocates memory */
        giblorb_err_t error = giblorb_create_map(file, &newmap);
 
@@ -36,6 +38,7 @@ giblorb_set_resource_map(strid_t file)
 
        glk_data->resource_map = newmap;
        glk_data->resource_file = file;
+
        return giblorb_err_None;
 }
 
@@ -50,9 +53,87 @@ giblorb_set_resource_map(strid_t file)
 giblorb_map_t*
 giblorb_get_resource_map()
 {
+       ChimaraGlkPrivate *glk_data = g_private_get(&glk_data_key);
+
        if(glk_data->resource_map == NULL) {
                WARNING("Resource map not set yet.\n");
        }
 
        return glk_data->resource_map;
 }
+
+/* giblorb_chunkdesc_t: Describes one chunk of the Blorb file. */
+typedef struct giblorb_chunkdesc_struct {
+    glui32 type;
+    glui32 len;
+    glui32 startpos; /* start of chunk header */
+    glui32 datpos; /* start of data (either startpos or startpos+8) */
+    
+    void *ptr; /* pointer to malloc'd data, if loaded */
+    int auxdatnum; /* entry in the auxsound/auxpict array; -1 if none.
+        This only applies to chunks that represent resources;  */
+    
+} giblorb_chunkdesc_t;
+
+/* giblorb_resdesc_t: Describes one resource in the Blorb file. */
+typedef struct giblorb_resdesc_struct {
+    glui32 usage;
+    glui32 resnum;
+    glui32 chunknum;
+} giblorb_resdesc_t;
+
+/* giblorb_map_t: Holds the complete description of an open Blorb file. */
+struct giblorb_map_struct {
+    glui32 inited; /* holds giblorb_Inited_Magic if the map structure is 
+        valid */
+    strid_t file;
+    
+    int numchunks;
+    giblorb_chunkdesc_t *chunks; /* list of chunk descriptors */
+    
+    int numresources;
+    giblorb_resdesc_t *resources; /* list of resource descriptors */
+    giblorb_resdesc_t **ressorted; /* list of pointers to descriptors 
+        in map->resources -- sorted by usage and resource number. */
+};
+
+void
+giblorb_print_contents(giblorb_map_t *map)
+{
+       int i;
+       for(i=0; i<map->numresources; i++) {
+               giblorb_resdesc_t *resource = map->ressorted[i];
+               printf("Resource #%d, chunknum: %d\n", resource->resnum, resource->chunknum);
+       }       
+
+       printf("\n-------\n");
+
+       for(i=0; i<map->numresources; i++) {
+               giblorb_chunkdesc_t chunk = map->chunks[i];
+               printf("Chunk #%d, type: %d\n", i, chunk.type);
+       }       
+}
+
+const char *
+giblorb_get_error_message(giblorb_err_t err)
+{
+       switch(err)
+       {
+               case giblorb_err_None:
+                       return "not BLORB's fault";
+               case giblorb_err_CompileTime:
+                       return "BLORB compile time error";
+               case giblorb_err_Alloc:
+                       return "memory allocation failed";
+               case giblorb_err_Read:
+                       return "error during reading of file";
+               case giblorb_err_NotAMap:
+                       return "invalid resource map supplied";
+               case giblorb_err_Format:
+                       return "invalid format of resource";
+               case giblorb_err_NotFound:
+                       return "resource not found";
+               default:
+                       return "Unknown error code";
+       }
+}