istereo

annotate libs/imago2/ftype_module.c @ 26:862a3329a8f0

wohooo, added a shitload of code from zlib/libpng/libjpeg. When the good lord was raining shared libraries the iphone held a fucking umbrella...
author John Tsiombikas <nuclear@mutantstargoat.com>
date Thu, 08 Sep 2011 06:28:38 +0300
parents
children
rev   line source
nuclear@26 1 /*
nuclear@26 2 libimago - a multi-format image file input/output library.
nuclear@26 3 Copyright (C) 2010 John Tsiombikas <nuclear@member.fsf.org>
nuclear@26 4
nuclear@26 5 This program is free software: you can redistribute it and/or modify
nuclear@26 6 it under the terms of the GNU Lesser General Public License as published
nuclear@26 7 by the Free Software Foundation, either version 3 of the License, or
nuclear@26 8 (at your option) any later version.
nuclear@26 9
nuclear@26 10 This program is distributed in the hope that it will be useful,
nuclear@26 11 but WITHOUT ANY WARRANTY; without even the implied warranty of
nuclear@26 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
nuclear@26 13 GNU Lesser General Public License for more details.
nuclear@26 14
nuclear@26 15 You should have received a copy of the GNU Lesser General Public License
nuclear@26 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
nuclear@26 17 */
nuclear@26 18
nuclear@26 19 #include <stdlib.h>
nuclear@26 20 #include <string.h>
nuclear@26 21 #include "ftype_module.h"
nuclear@26 22
nuclear@26 23 static struct list_node {
nuclear@26 24 struct ftype_module *module;
nuclear@26 25 struct list_node *next;
nuclear@26 26 } *modules;
nuclear@26 27
nuclear@26 28 /* defined in modules.c which is generated by configure */
nuclear@26 29 void img_modules_init();
nuclear@26 30
nuclear@26 31 static int done_init;
nuclear@26 32
nuclear@26 33 int img_register_module(struct ftype_module *mod)
nuclear@26 34 {
nuclear@26 35 struct list_node *node;
nuclear@26 36
nuclear@26 37 if(!(node = malloc(sizeof *node))) {
nuclear@26 38 return -1;
nuclear@26 39 }
nuclear@26 40
nuclear@26 41 node->module = mod;
nuclear@26 42 node->next = modules;
nuclear@26 43 modules = node;
nuclear@26 44 return 0;
nuclear@26 45 }
nuclear@26 46
nuclear@26 47 struct ftype_module *img_find_format_module(struct img_io *io)
nuclear@26 48 {
nuclear@26 49 struct list_node *node;
nuclear@26 50
nuclear@26 51 if(!done_init) {
nuclear@26 52 img_modules_init();
nuclear@26 53 done_init = 1;
nuclear@26 54 }
nuclear@26 55
nuclear@26 56 node = modules;
nuclear@26 57 while(node) {
nuclear@26 58 if(node->module->check(io) != -1) {
nuclear@26 59 return node->module;
nuclear@26 60 }
nuclear@26 61 node = node->next;
nuclear@26 62 }
nuclear@26 63 return 0;
nuclear@26 64 }
nuclear@26 65
nuclear@26 66 struct ftype_module *img_guess_format(const char *fname)
nuclear@26 67 {
nuclear@26 68 struct list_node *node;
nuclear@26 69 char *suffix;
nuclear@26 70 int suffix_len;
nuclear@26 71
nuclear@26 72 if(!done_init) {
nuclear@26 73 img_modules_init();
nuclear@26 74 done_init = 1;
nuclear@26 75 }
nuclear@26 76
nuclear@26 77 if(!(suffix = strrchr(fname, '.'))) {
nuclear@26 78 return 0; /* no suffix, can't guess ... */
nuclear@26 79 }
nuclear@26 80 suffix_len = strlen(suffix);
nuclear@26 81
nuclear@26 82 node = modules;
nuclear@26 83 while(node) {
nuclear@26 84 char *suflist = node->module->suffix;
nuclear@26 85 char *start, *end;
nuclear@26 86
nuclear@26 87 while(*suflist) {
nuclear@26 88 if(!(start = strstr(suflist, suffix))) {
nuclear@26 89 break;
nuclear@26 90 }
nuclear@26 91 end = start + suffix_len;
nuclear@26 92
nuclear@26 93 if(*end == ':' || *end == 0) {
nuclear@26 94 return node->module; /* found it */
nuclear@26 95 }
nuclear@26 96 suflist = end;
nuclear@26 97 }
nuclear@26 98
nuclear@26 99 node = node->next;
nuclear@26 100 }
nuclear@26 101 return 0;
nuclear@26 102 }
nuclear@26 103
nuclear@26 104 struct ftype_module *img_get_module(int idx)
nuclear@26 105 {
nuclear@26 106 struct list_node *node;
nuclear@26 107
nuclear@26 108 if(!done_init) {
nuclear@26 109 img_modules_init();
nuclear@26 110 done_init = 1;
nuclear@26 111 }
nuclear@26 112
nuclear@26 113 node = modules;
nuclear@26 114 while(node && idx--) {
nuclear@26 115 node = node->next;
nuclear@26 116 }
nuclear@26 117 return node->module;
nuclear@26 118 }