File: module.c

package info (click to toggle)
gimageview 0.2.27-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny, sarge
  • size: 7,880 kB
  • ctags: 8,193
  • sloc: ansic: 80,389; sh: 8,625; perl: 1,167; makefile: 914; yacc: 318
file content (127 lines) | stat: -rw-r--r-- 2,645 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
 * module.c
 * (C)Copyright 2000 by Hiroshi Takekawa
 * This file is part of Enfle.
 *
 * Last Modified: Mon Feb 18 01:39:10 2002.
 * $Id: module.c,v 1.1 2003/08/01 13:41:06 makeinu Exp $
 *
 * Enfle is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * Enfle is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 */

#define REQUIRE_STRING_H
#include "compat.h"
#include "common.h"

#include "module.h"
#include "misc.h"

typedef struct _module_list ModuleList;
struct _module_list {
  char *filename;
  HMODULE module;
  ModuleList *prev;
  ModuleList *next;
};

ModuleList *mod_list = NULL;

static ModuleList *
module_find(const char *filename)
{
  ModuleList *p = mod_list;

  if (!mod_list)
    return NULL;

  if (filename == NULL) {
    while (p && p->filename) {
      p = p->prev;
    }
  } else {
    char *trimmed = misc_trim_ext(p->filename, "dll");

    do {
      if (strcasecmp(p->filename, filename) == 0 ||
	  strcasecmp(trimmed, filename) == 0) {
	free(trimmed);
	return p;
      }
      p = p->prev;
    } while (p);
    free(trimmed);
  }

  return p;
}

int
module_register(const char *filename, HMODULE module)
{
  if (mod_list == NULL) {
    mod_list = malloc(sizeof(ModuleList));
    mod_list->next = mod_list->prev = NULL;
  } else {
    if (module_find(filename))
      return 1;

    mod_list->next = malloc(sizeof(ModuleList));
    mod_list->next->prev = mod_list;
    mod_list->next->next = NULL;
    mod_list = mod_list->next;
  }

  mod_list->filename = filename ? strdup(filename) : NULL;
  mod_list->module = module;

  return 1;
}

static int
module_delete(ModuleList *p)
{
  if (p) {
    if (p->prev)
      p->prev->next = p->next;
    if (p->next)
      p->next->prev = p->prev;
    if (mod_list == p)
      mod_list = mod_list->prev;
    if (p->module)
      free(p->module);
    free(p);
    return 1;
  }

  return 0;
}

int
module_deregister(const char *filename)
{
  ModuleList *p;

  p = module_find(filename);
  return module_delete(p);
}

HMODULE
module_lookup(const char *filename)
{
  ModuleList *p;

  if ((p = module_find(filename)) == NULL)
    return NULL;
  return p->module;
}