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