/
lt-region.c
252 lines (219 loc) · 5.45 KB
/
lt-region.c
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
* lt-region.c
* Copyright (C) 2011-2012 Akira TAGOH
*
* Authors:
* Akira TAGOH <akira@tagoh.org>
*
* 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 3 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "lt-mem.h"
#include "lt-region.h"
#include "lt-region-private.h"
/**
* SECTION:lt-region
* @Short_Description: A container class for Region subtag
* @Title: Container - Region
*
* This container class provides a data access to Region subtag entry.
*/
struct _lt_region_t {
lt_mem_t parent;
gchar *tag;
gchar *description;
gchar *preferred_tag;
};
/*< private >*/
/*< protected >*/
lt_region_t *
lt_region_create(void)
{
lt_region_t *retval = lt_mem_alloc_object(sizeof (lt_region_t));
return retval;
}
void
lt_region_set_name(lt_region_t *region,
const gchar *description)
{
g_return_if_fail (region != NULL);
g_return_if_fail (description != NULL);
if (region->description)
lt_mem_remove_ref(®ion->parent, region->description);
region->description = g_strdup(description);
lt_mem_add_ref(®ion->parent, region->description,
(lt_destroy_func_t)g_free);
}
void
lt_region_set_tag(lt_region_t *region,
const gchar *subtag)
{
g_return_if_fail (region != NULL);
g_return_if_fail (subtag != NULL);
if (region->tag)
lt_mem_remove_ref(®ion->parent, region->tag);
region->tag = g_strdup(subtag);
lt_mem_add_ref(®ion->parent, region->tag,
(lt_destroy_func_t)g_free);
}
void
lt_region_set_preferred_tag(lt_region_t *region,
const gchar *subtag)
{
g_return_if_fail (region != NULL);
g_return_if_fail (subtag != NULL);
if (region->preferred_tag)
lt_mem_remove_ref(®ion->parent, region->preferred_tag);
region->preferred_tag = g_strdup(subtag);
lt_mem_add_ref(®ion->parent, region->preferred_tag,
(lt_destroy_func_t)g_free);
}
/*< public >*/
/**
* lt_region_ref:
* @region: a #lt_region_t.
*
* Increases the reference count of @region.
*
* Returns: (transfer none): the same @region object.
*/
lt_region_t *
lt_region_ref(lt_region_t *region)
{
g_return_val_if_fail (region != NULL, NULL);
return lt_mem_ref(®ion->parent);
}
/**
* lt_region_unref:
* @region: a #lt_region_t.
*
* Decreases the reference count of @region. when its reference count
* drops to 0, the object is finalized (i.e. its memory is freed).
*/
void
lt_region_unref(lt_region_t *region)
{
if (region)
lt_mem_unref(®ion->parent);
}
/**
* lt_region_get_name:
* @region: a #lt_region_t.
*
* Obtains the description of the subtag.
*
* Returns: a description string.
*/
const gchar *
lt_region_get_name(const lt_region_t *region)
{
g_return_val_if_fail (region != NULL, NULL);
return region->description;
}
/**
* lt_region_get_better_tag:
* @region: a #lt_region_t.
*
* Obtains the better tag for use. this is a convenient function to get
* the preferred-value if available.
*
* Returns: a tag string.
*/
const gchar *
lt_region_get_better_tag(const lt_region_t *region)
{
const gchar *retval = lt_region_get_preferred_tag(region);
if (!retval)
retval = lt_region_get_tag(region);
return retval;
}
/**
* lt_region_get_tag:
* @region: a #lt_region_t.
*
* Obtains the tag name.
*
* Returns: a tag string.
*/
const gchar *
lt_region_get_tag(const lt_region_t *region)
{
g_return_val_if_fail (region != NULL, NULL);
return region->tag;
}
/**
* lt_region_get_preferred_tag:
* @region: a #lt_region_t.
*
* Obtains the preferred-value. this is available only when the tag is
* marked as deprecated.
*
* Returns: a preferred-value for the tag or %NULL.
*/
const gchar *
lt_region_get_preferred_tag(const lt_region_t *region)
{
g_return_val_if_fail (region != NULL, NULL);
return region->preferred_tag;
}
/**
* lt_region_dump:
* @region: a #lt_region_t.
*
* Dumps the container information to the standard output.
*/
void
lt_region_dump(const lt_region_t *region)
{
GString *string = g_string_new(NULL);
const gchar *preferred = lt_region_get_preferred_tag(region);
if (preferred) {
if (string->len == 0)
g_string_append(string, " (");
g_string_append_printf(string, "preferred-value: %s", preferred);
}
if (string->len > 0)
g_string_append(string, ")");
g_print("Region: %s [%s]%s\n",
lt_region_get_tag(region),
lt_region_get_name(region),
string->str);
g_string_free(string, TRUE);
}
/**
* lt_region_compare:
* @v1: a #lt_region_t.
* @v2: a #lt_region_t.
*
* Compare if @v1 and @v2 is the same object or not.
*
* Returns: %TRUE if it's the same, otherwise %FALSE.
*/
gboolean
lt_region_compare(const lt_region_t *v1,
const lt_region_t *v2)
{
const gchar *s1, *s2;
if (v1 == v2)
return TRUE;
s1 = v1 ? lt_region_get_tag(v1) : NULL;
s2 = v2 ? lt_region_get_tag(v2) : NULL;
if (g_strcmp0(s1, "*") == 0 ||
g_strcmp0(s2, "*") == 0)
return TRUE;
return g_strcmp0(s1, s2) == 0;
}