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

emb.c

Go to the documentation of this file.
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.14 2006/04/22 13:14:46 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 
00016 /* these are klone-site autogen functions */
00017 void register_pages(void);
00018 void unregister_pages(void);
00019 
00020 static struct emblist_s list;   /* list of emb resources     */
00021 static size_t nres;             /* num of emb resources      */
00022 static int init = 0;
00023 
00024 int emb_init(void)
00025 {
00026     if(init++ == 0)
00027     {
00028         LIST_INIT(&list); /* init resource list */
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 
00038 int emb_term(void)
00039 {
00040     dbg_err_if(init == 0);
00041 
00042     unregister_pages();
00043 
00044     return 0;
00045 err:
00046     return ~0;
00047 }
00048 
00049 int emb_register(embres_t *res)
00050 {
00051     dbg_err_if(init == 0 || res == NULL);
00052 
00053     if(res->type == ET_FILE) 
00054         dbg("registering %s (%s)", res->filename, 
00055                 ((embfile_t*)res)->comp ? "compressed" : "uncompressed");
00056     else 
00057         dbg("registering %s", res->filename);
00058 
00059     LIST_INSERT_HEAD(&list, res, np);
00060     nres++;
00061 
00062     return 0;
00063 err:
00064     return ~0;
00065 }
00066 
00067 int emb_unregister(embres_t *res)
00068 {
00069     dbg_err_if(init == 0 || res == NULL);
00070 
00071     LIST_REMOVE(res, np);
00072     nres--;
00073 
00074     return 0;
00075 err:
00076     return ~0;
00077 }
00078 
00079 int emb_lookup(const char *filename, embres_t **pr)
00080 {
00081     embres_t *res;
00082 
00083     dbg_err_if (init == 0);
00084     dbg_err_if (filename == NULL || !strlen(filename));
00085     dbg_err_if (pr == NULL);
00086 
00087     LIST_FOREACH(res, &list, np)
00088     {
00089         if(strcmp(filename, res->filename))
00090             continue;
00091 
00092         /* save found resource pointer */
00093         *pr = res;
00094 
00095         return 0; /* found */
00096     }
00097 
00098 err:
00099     /* not found */
00100     return ~0;
00101 }
00102 
00103 int emb_count(void)
00104 {
00105     dbg_err_if (init == 0);
00106 
00107     return nres;
00108 err:
00109     return -1;
00110 }
00111 
00112 int emb_getn(size_t n, embres_t **pr)
00113 {
00114     embres_t *res = NULL;
00115 
00116     dbg_err_if (init == 0);
00117     dbg_err_if (n >= nres);
00118     dbg_err_if (pr == NULL);
00119 
00120     LIST_FOREACH(res, &list, np)
00121     {
00122         if(n-- == 0)
00123             break;
00124     }
00125 
00126     *pr = res;
00127 
00128     return 0;
00129 err:
00130     return ~0;
00131 }
00132 
00133 int emb_open(const char *file, io_t **pio)
00134 {
00135     embfile_t *e = NULL;
00136     codec_t *gzip = NULL;
00137     io_t *io;
00138 
00139     dbg_return_if (pio == NULL, ~0);
00140     dbg_return_if (file == NULL, ~0);
00141     
00142     dbg_err_if(emb_lookup(file, (embres_t**)&e) || e->res.type != ET_FILE);
00143 
00144     dbg_err_if(io_mem_create((char*)e->data, e->size, 0, &io));
00145 
00146 #ifdef HAVE_LIBZ
00147     if(e->comp)
00148     {
00149         dbg_err_if(codec_gzip_create(GZIP_UNCOMPRESS, &gzip));
00150         dbg_err_if(io_codec_add_tail(io, (codec_t*)gzip));
00151         gzip = NULL;
00152     }
00153 #endif
00154 
00155     *pio = io;
00156 
00157     return 0;
00158 err:
00159     if(gzip)
00160         codec_free(gzip);
00161     return ~0;
00162 }
00163 

←Products
© 2005-2006 - KoanLogic S.r.l. - All rights reserved