Main Page | Modules | Data Structures | Directories | File List | Data Fields | Globals

emb.c

00001 /*
00002  * Copyright (c) 2005, 2006 by KoanLogic s.r.l. <http://www.koanlogic.com>
00003  * All rights reserved.
00004  *
00005  * This file is part of KLone, and as such it is subject to the license stated
00006  * in the LICENSE file which you have received as part of this distribution.
00007  *
00008  * $Id: emb.c,v 1.17 2007/05/26 16:18:21 tat Exp $
00009  */
00010 
00011 #include <klone/emb.h>
00012 #include <klone/io.h>
00013 #include <klone/codecs.h>
00014 #include <u/libu.h>
00015 #include <u/toolbox/hmap.h>
00016 
00017 /* these are klone-site autogen functions */
00018 void register_pages(void);
00019 void unregister_pages(void);
00020 
00021 static u_hmap_t *embmap = NULL;     /* hashmap of embedded resources */
00022 static int init = 0;
00023 
00024 int emb_init(void)
00025 {
00026     if(init++ == 0)
00027     {
00028         dbg_err_if (u_hmap_new(NULL, &embmap));
00029 
00030         /* call autogen external function (cannot be called more then once!) */
00031         dbg("registering embedded resources");
00032         register_pages();
00033     }
00034 
00035     return 0;
00036  
00037 err:
00038     return ~0;
00039 }
00040 
00041 int emb_term(void)
00042 {
00043     dbg_err_if(init == 0);
00044 
00045     unregister_pages();
00046 
00047     if (embmap) {
00048         u_hmap_free(embmap);
00049         embmap = NULL;
00050     }
00051 
00052     return 0;
00053 err:
00054     return ~0;
00055 }
00056 
00057 int emb_register(embres_t *res)
00058 {
00059     u_hmap_o_t *obj = NULL;
00060 
00061     dbg_err_if(init == 0 || res == NULL);
00062 
00063     if(res->type == ET_FILE) 
00064         dbg("registering %s (%s)", res->filename, 
00065                 ((embfile_t*)res)->comp ? "compressed" : "uncompressed");
00066     else 
00067         dbg("registering %s", res->filename);
00068 
00069     obj = u_hmap_o_new((void *) res->filename, res); 
00070     dbg_err_if (obj == NULL);
00071 
00072     dbg_err_if (u_hmap_put(embmap, obj, NULL)); 
00073     
00074     return 0;
00075 err:
00076     return ~0;
00077 }
00078 
00079 int emb_unregister(embres_t *res)
00080 {
00081     dbg_err_if(init == 0 || res == NULL);
00082 
00083     dbg_err_if (u_hmap_del(embmap, (void *) res->filename, NULL));
00084 
00085     return 0;
00086 err:
00087     return ~0;
00088 }
00089 
00090 int emb_lookup(const char *filename, embres_t **pr)
00091 {
00092     embres_t *res;
00093     u_hmap_o_t *obj = NULL;
00094 
00095     dbg_err_if (init == 0);
00096     dbg_err_if (filename == NULL || !strlen(filename));
00097     dbg_err_if (pr == NULL);
00098 
00099     nop_err_if (u_hmap_get(embmap, (void *) filename, &obj));
00100 
00101     *pr = obj->val;
00102 
00103     return 0;
00104 
00105 err:
00106     /* not found */
00107     return ~0;
00108 }
00109 
00110 int emb_open(const char *file, io_t **pio)
00111 {
00112     embfile_t *e = NULL;
00113     codec_t *gzip = NULL;
00114     io_t *io;
00115 
00116     dbg_return_if (pio == NULL, ~0);
00117     dbg_return_if (file == NULL, ~0);
00118     
00119     dbg_err_if(emb_lookup(file, (embres_t**)&e) || e->res.type != ET_FILE);
00120 
00121     dbg_err_if(io_mem_create((char*)e->data, e->size, 0, &io));
00122 
00123 #ifdef HAVE_LIBZ
00124     if(e->comp)
00125     {
00126         dbg_err_if(codec_gzip_create(GZIP_UNCOMPRESS, &gzip));
00127         dbg_err_if(io_codec_add_tail(io, (codec_t*)gzip));
00128         gzip = NULL;
00129     }
00130 #endif
00131 
00132     *pio = io;
00133 
00134     return 0;
00135 err:
00136     if(gzip)
00137         codec_free(gzip);
00138     return ~0;
00139 }
00140