summaryrefslogtreecommitdiff
path: root/src/main/string-list.c
blob: 337a717619f567cd7744266200d4004df0de9cf8 (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
/*
 * "$Id: string-list.c,v 1.21 2014/01/04 00:31:38 rlk Exp $"
 *
 *   Print plug-in driver utility functions for the GIMP.
 *
 *   Copyright 1997-2000 Michael Sweet (mike@easysw.com) and
 *	Robert Krawitz (rlk@alum.mit.edu)
 *
 *   This program is free software; you can redistribute it and/or modify it
 *   under the terms of the GNU General Public License as published by the Free
 *   Software Foundation; either version 2 of the License, or (at your option)
 *   any later version.
 *
 *   This program 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.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <gutenprint/gutenprint.h>
#include "gutenprint-internal.h"
#include <gutenprint/gutenprint-intl-internal.h>
#include <string.h>

static void
free_list_element(void *item)
{
  stp_param_string_t *string = (stp_param_string_t *) (item);
  stp_free(stpi_cast_safe(string->name));
  stp_free(stpi_cast_safe(string->text));
  stp_free(string);
}

static const char *
namefunc(const void *item)
{
  const stp_param_string_t *string = (const stp_param_string_t *) (item);
  return string->name;
}

static void *
copyfunc(const void *item)
{
  const stp_param_string_t *string = (const stp_param_string_t *) (item);
  stp_param_string_t *new_string = stp_malloc(sizeof(stp_param_string_t));
  new_string->name = stp_strdup(string->name);
  new_string->text = stp_strdup(string->text);
  return new_string;
}

static const char *
long_namefunc(const void *item)
{
  const stp_param_string_t *string = (const stp_param_string_t *) (item);
  return string->text;
}

stp_string_list_t *
stp_string_list_create(void)
{
  stp_list_t *ret = stp_list_create();
  stp_list_set_freefunc(ret, free_list_element);
  stp_list_set_namefunc(ret, namefunc);
  stp_list_set_copyfunc(ret, copyfunc);
  stp_list_set_long_namefunc(ret, long_namefunc);
  return (stp_string_list_t *) ret;
}

void
stp_string_list_destroy(stp_string_list_t *list)
{
  stp_list_destroy((stp_list_t *) list);
}

stp_param_string_t *
stp_string_list_param(const stp_string_list_t *list, size_t element)
{
  stp_list_item_t *answer =
    stp_list_get_item_by_index((const stp_list_t *)list, element);
  if (answer)
    return (stp_param_string_t *) stp_list_item_get_data(answer);
  else
    return NULL;
}

stp_param_string_t *
stp_string_list_find(const stp_string_list_t *list, const char *name)
{
  stp_list_item_t *answer =
    stp_list_get_item_by_name((const stp_list_t *)list, name);
  if (answer)
    return (stp_param_string_t *) stp_list_item_get_data(answer);
  else
    return NULL;
}

size_t
stp_string_list_count(const stp_string_list_t *list)
{
  return stp_list_get_length((const stp_list_t *)list);
}

stp_string_list_t *
stp_string_list_create_copy(const stp_string_list_t *list)
{
  return (stp_string_list_t *) stp_list_copy((const stp_list_t *)list);
}

stp_string_list_t *
stp_string_list_create_from_params(const stp_param_string_t *list,
				   size_t count)
{
  size_t i = 0;
  stp_string_list_t *retval = stp_string_list_create();
  for (i = 0; i < count; i++)
    stp_string_list_add_string(retval, list[i].name, list[i].text);
  return retval;
}

void
stp_string_list_add_string(stp_string_list_t *list,
			   const char *name,
			   const char *text)
{
  stp_param_string_t *new_string = stp_malloc(sizeof(stp_param_string_t));
  do
    {
      const char *xname = name;
      while (*xname)
	{
	  if (!isalnum(*xname) &&
	      *xname != '_' && *xname != '-' && *xname != '+')
	    {
	      stp_erprintf("Gutenprint: bad string %s (%s)\n", name, text);
	      break;
	    }
	  xname++;
	}
    } while(0);
  new_string->name = stp_strdup(name);
  new_string->text = stp_strdup(text);
  stp_list_item_create((stp_list_t *) list, NULL, new_string);
}

void
stp_string_list_add_string_unsafe(stp_string_list_t *list,
				  const char *name,
				  const char *text)
{
  stp_param_string_t *new_string = stp_malloc(sizeof(stp_param_string_t));
  new_string->name = stp_strdup(name);
  new_string->text = stp_strdup(text);
  stp_list_item_create((stp_list_t *) list, NULL, new_string);
}

void
stp_string_list_remove_string(stp_string_list_t *list,
			      const char *name)
{
  stp_list_item_t *item =
    stp_list_get_item_by_name((const stp_list_t *) list, name);
  if (item)
    stp_list_item_destroy((stp_list_t *) list, item);
}

int
stp_string_list_is_present(const stp_string_list_t *list,
			   const char *value)
{
  if (list && value &&
      stp_list_get_item_by_name((const stp_list_t *) list, value))
    return 1;
  else
    return 0;
}