BRL-CAD
plugin.c
Go to the documentation of this file.
1 /* P L U G I N . C
2  * BRL-CAD
3  *
4  * Copyright (c) 2015 United States Government as represented by
5  * the U.S. Army Research Laboratory.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * version 2.1 as published by the Free Software Foundation.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this file; see the file named COPYING for more
18  * information.
19  */
20 /** @file plugin.c
21  *
22  * Loading and registration of GCV conversion plugins.
23  *
24  */
25 
26 
27 #include "common.h"
28 
29 #include "plugin.h"
30 #include "gcv_private.h"
31 
32 #include <string.h>
33 
34 
35 struct gcv_plugin {
36  struct bu_list l;
37 
39 };
40 
41 
42 static inline struct bu_list *
43 gcv_get_plugin_list(void)
44 {
45  static struct bu_list plugin_list = BU_LIST_INIT_ZERO;
46 
47  if (!BU_LIST_IS_INITIALIZED(&plugin_list))
48  BU_LIST_INIT(&plugin_list);
49 
50  return &plugin_list;
51 }
52 
53 
54 void
55 gcv_plugin_register(const struct gcv_plugin_info *plugin_info)
56 {
57  struct bu_list * const plugin_list = gcv_get_plugin_list();
58 
59  struct gcv_plugin *plugin;
60 
61  BU_GET(plugin, struct gcv_plugin);
62  BU_LIST_PUSH(plugin_list, &plugin->l);
63  plugin->plugin_info = plugin_info;
64 }
65 
66 
67 HIDDEN void
69 {
70  BU_LIST_DEQUEUE(&entry->l);
71  BU_PUT(entry, struct gcv_plugin);
72 }
73 
74 
75 HIDDEN int
76 gcv_extension_match(const char *path, const char *extension)
77 {
78  path = strrchr(path, '.');
79 
80  if (!path++) return 0;
81 
82  while (1) {
83  const char * const next = strchr(extension, ';');
84 
85  if (!next) break;
86 
87  if (bu_strncasecmp(path, extension, next - 1 - extension) == 0)
88  return 1;
89 
90  extension = next + 1;
91  }
92 
93  return bu_strcasecmp(path, extension) == 0;
94 }
95 
96 
97 const struct gcv_converter *
98 gcv_converter_find(const char *path, enum gcv_conversion_type type)
99 {
100  const struct bu_list * const plugin_list = gcv_get_plugin_list();
101 
102  const struct gcv_plugin *entry;
103 
104  for (BU_LIST_FOR(entry, gcv_plugin, plugin_list)) {
105  const struct gcv_converter *converter = entry->plugin_info->converters;
106 
107  if (!converter) continue;
108 
109  while (converter->file_extensions) {
110  if (gcv_extension_match(path, converter->file_extensions)) {
111  if (type == GCV_CONVERSION_READ && converter->reader_fn)
112  return converter;
113  else if (type == GCV_CONVERSION_WRITE && converter->writer_fn)
114  return converter;
115  }
116 
117  ++converter;
118  }
119  }
120 
121  return NULL;
122 }
123 
124 
125 /*
126  * Local Variables:
127  * tab-width: 8
128  * mode: C
129  * indent-tabs-mode: t
130  * c-file-style: "stroustrup"
131  * End:
132  * ex: shiftwidth=4 tabstop=8
133  */
gcv_conversion_type
Definition: gcv_private.h:39
#define BU_LIST_FOR(p, structure, hp)
Definition: list.h:365
Definition: list.h:118
#define BU_LIST_IS_INITIALIZED(_hp)
Definition: list.h:175
#define BU_LIST_INIT_ZERO
Definition: list.h:167
Header file for the BRL-CAD common definitions.
#define HIDDEN
Definition: common.h:86
int(* writer_fn)(const char *path, struct db_i *dbip, const struct gcv_opts *options)
Definition: plugin.h:43
char * strchr(const char *sp, int c)
const struct gcv_converter * gcv_converter_find(const char *path, enum gcv_conversion_type type)
Definition: plugin.c:98
int bu_strncasecmp(const char *string1, const char *string2, size_t n)
Definition: str.c:239
int(* reader_fn)(const char *path, struct rt_wdb *wdbp, const struct gcv_opts *options)
Definition: plugin.h:41
const struct gcv_plugin_info * plugin_info
Definition: plugin.c:38
HIDDEN void gcv_plugin_free(struct gcv_plugin *entry)
Definition: plugin.c:68
#define BU_GET(_ptr, _type)
Definition: malloc.h:201
struct bu_list l
Definition: plugin.c:36
#define BU_PUT(_ptr, _type)
Definition: malloc.h:215
void gcv_plugin_register(const struct gcv_plugin_info *plugin_info)
Definition: plugin.c:55
#define BU_LIST_PUSH(hp, p)
Definition: list.h:246
#define BU_LIST_INIT(_hp)
Definition: list.h:148
const struct gcv_converter * converters
Definition: plugin.h:49
const char * file_extensions
Definition: plugin.h:40
HIDDEN int gcv_extension_match(const char *path, const char *extension)
Definition: plugin.c:76
#define BU_LIST_DEQUEUE(cur)
Definition: list.h:209
int bu_strcasecmp(const char *string1, const char *string2)
Definition: str.c:211