summaryrefslogtreecommitdiffstats
path: root/src/3rdparty/autotrace/input.c
blob: 8ffb719ac3d7c30b00dc19a560667f6b4c608de1 (plain)
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/* input.c: interface for input handlers

   Copyright (C) 1999, 2000, 2001 Bernhard Herzog.
   Copyright (C) 2003 Masatake YAMATO

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public License
   as published by the Free Software Foundation; either version 2.1 of
   the License, or (at your option) any later version.

   This library 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
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
   USA. */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* Def: HAVE_CONFIG_H */

#include "autotrace.h"
#include "private.h"
#include "input.h"
#include "xstd.h"
#include "filename.h"
#include <string.h>
#include <glib.h>

typedef struct _at_input_format_entry at_input_format_entry;
struct _at_input_format_entry {
  at_bitmap_reader reader;
  const gchar *descr;
  GDestroyNotify user_data_destroy_func;
};

static GHashTable *at_input_formats = NULL;
static at_input_format_entry *at_input_format_new(const char *descr, at_input_func reader, gpointer user_data, GDestroyNotify user_data_destroy_func);
static void at_input_format_free(at_input_format_entry * entry);

/*
 * Helper functions
 */
static void input_list_set(gpointer key, gpointer value, gpointer user_data);
static void input_list_strlen(gpointer key, gpointer value, gpointer user_data);
static void input_list_strcat(gpointer key, gpointer value, gpointer user_data);

/**
 * at_input_init:
 * Initialize at_input input plugin sub system.
 *
 * Return value: 1 for success, else for failure
 **/
int at_input_init(void)
{
  if (at_input_formats)
    return 1;

  at_input_formats = g_hash_table_new_full(g_str_hash, (GEqualFunc) g_str_equal, g_free, (GDestroyNotify) at_input_format_free);
  if (!at_input_formats)
    return 0;
  return 1;
}

static at_input_format_entry *at_input_format_new(const gchar * descr, at_input_func reader, gpointer user_data, GDestroyNotify user_data_destroy_func)
{
  at_input_format_entry *entry;
  entry = g_malloc(sizeof(at_input_format_entry));
  if (entry) {
    entry->reader.func = reader;
    entry->reader.data = user_data;
    entry->descr = g_strdup(descr);
    entry->user_data_destroy_func = user_data_destroy_func;
  }
  return entry;
}

static void at_input_format_free(at_input_format_entry * entry)
{
  g_free((gpointer) entry->descr);
  if (entry->user_data_destroy_func)
    entry->user_data_destroy_func(entry->reader.data);
  g_free(entry);

}

int at_input_add_handler(const gchar * suffix, const gchar * description, at_input_func reader)
{
  return at_input_add_handler_full(suffix, description, reader, 0, NULL, NULL);
}

int at_input_add_handler_full(const gchar * suffix, const gchar * description, at_input_func reader, gboolean override, gpointer user_data, GDestroyNotify user_data_destroy_func)
{
  gchar *gsuffix;
  const gchar *gdescription;
  at_input_format_entry *old_entry;
  at_input_format_entry *new_entry;

  g_return_val_if_fail(suffix, 0);
  g_return_val_if_fail(description, 0);
  g_return_val_if_fail(reader, 0);

  gsuffix = g_strdup((gchar *) suffix);
  g_return_val_if_fail(gsuffix, 0);
  gsuffix = g_ascii_strdown(gsuffix, strlen(gsuffix));

  gdescription = (const gchar *)description;

  old_entry = g_hash_table_lookup(at_input_formats, gsuffix);
  if (old_entry && !override) {
    g_free(gsuffix);
    return 1;
  }

  new_entry = at_input_format_new(gdescription, reader, user_data, user_data_destroy_func);
  g_return_val_if_fail(new_entry, 0);

  g_hash_table_replace(at_input_formats, gsuffix, new_entry);
  return 1;
}

at_bitmap_reader *at_input_get_handler(gchar * filename)
{
  char *ext = find_suffix(filename);
  if (ext == NULL)
    ext = "";

  return at_input_get_handler_by_suffix(ext);
}

at_bitmap_reader *at_input_get_handler_by_suffix(gchar * suffix)
{
  at_input_format_entry *format;
  gchar *gsuffix;

  if (!suffix || suffix[0] == '\0')
    return NULL;

  gsuffix = g_strdup(suffix);
  g_return_val_if_fail(gsuffix, NULL);
  gsuffix = g_ascii_strdown(gsuffix, strlen(gsuffix));
  format = g_hash_table_lookup(at_input_formats, gsuffix);
  g_free(gsuffix);

  if (format)
    return &(format->reader);
  else
    return NULL;
}

const char **at_input_list_new(void)
{
  char **list, **tmp;
  gint format_count;
  gint list_count;

  format_count = g_hash_table_size(at_input_formats);
  list_count = 2 * format_count;
  list = g_new(gchar *, list_count + 1);
  list[list_count] = NULL;

  tmp = list;
  g_hash_table_foreach(at_input_formats, input_list_set, &tmp);
  return (const char **)list;
}

void at_input_list_free(const char **list)
{
  free((char **)list);
}

char *at_input_shortlist(void)
{
  gint length = 0, count;
  char *list, *tmp;
  g_hash_table_foreach(at_input_formats, input_list_strlen, &length);
  count = g_hash_table_size(at_input_formats);

  /* 2 for ", " */
  length += (2 * count);
  list = g_malloc(length + 1);
  list[0] = '\0';

  tmp = list;
  g_hash_table_foreach(at_input_formats, input_list_strcat, &tmp);

  /* remove final ", " */
  g_return_val_if_fail(list[length - 2] == ',', NULL);
  list[length - 2] = '\0';
  return list;
}

static void input_list_set(gpointer key, gpointer value, gpointer user_data)
{
  at_input_format_entry *format = value;
  const char ***list_ptr = user_data;
  const char **list = *list_ptr;
  list[0] = key;
  list[1] = format->descr;
  *list_ptr = &(list[2]);
}

static void input_list_strlen(gpointer key, gpointer value, gpointer user_data)
{
  gint *length;
  g_return_if_fail(key);
  g_return_if_fail(user_data);

  length = user_data;
  *length += strlen(key);
}

static void input_list_strcat(gpointer key, gpointer value, gpointer user_data)
{
  gchar **list_ptr;
  gchar *list;
  list_ptr = user_data;
  list = *list_ptr;
  strcat(list, key);
  strcat(list, ", ");

  /* 2 for ", " */
  *list_ptr = list + strlen(key) + 2;
}