vrshoot

annotate libs/imago/ftype_module.c @ 2:334d17aed7de

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