20221212
[devspec.git] / devspec.en_US / project / recutils / src / rec-field.c
blob55c2e8e48674c33c84df6462e066d287b087bd8a
1 /* -*- mode: C -*-
3 * File: rec-field.c
4 * Date: Fri Feb 27 20:40:26 2009
6 * GNU recutils - Fields
8 */
10 /* Copyright (C) 2009-2019 Jose E. Marchesi */
12 /* This program is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation, either version 3 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #include <config.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <stdio.h>
32 #include <rec.h>
34 /* Field Data Structure.
36 * A field is an association between a label and a value.
39 struct rec_field_s
41 /* The name and the value of a field are UTF-8 encoded strings.
42 Thus, we use NULL-terminated strings to store them. */
44 char *name;
45 char *value;
47 /* Localization. */
49 char *source;
50 size_t location;
51 char *location_str;
52 size_t char_location;
53 char *char_location_str;
55 /* Field marks. */
57 int mark;
60 /* Static functions defined below. */
62 static void rec_field_init (rec_field_t field);
65 * Public functions.
68 const char *
69 rec_field_name (rec_field_t field)
71 return field->name;
74 bool
75 rec_field_set_name (rec_field_t field, const char *name)
77 free (field->name);
78 field->name = strdup (name);
79 return (field->name != NULL);
82 const char *
83 rec_field_value (rec_field_t field)
85 return field->value;
88 bool
89 rec_field_set_value (rec_field_t field,
90 const char *value)
92 free (field->value);
93 field->value = strdup (value);
94 return (field->value != NULL);
97 rec_field_t
98 rec_field_new (const char *name,
99 const char *value)
101 rec_field_t field;
103 field = malloc (sizeof (struct rec_field_s));
105 if (field != NULL)
107 rec_field_init (field);
109 if (!rec_field_set_name (field, name))
111 /* Out of memory. */
112 rec_field_destroy (field);
113 return NULL;
116 if (!rec_field_set_value (field, value))
118 /* Out of memory. */
119 rec_field_destroy (field);
120 return NULL;
124 return field;
127 rec_field_t
128 rec_field_dup (rec_field_t field)
130 rec_field_t new_field;
132 new_field = rec_field_new (rec_field_name (field),
133 rec_field_value (field));
134 if (new_field)
136 new_field->location = field->location;
137 new_field->char_location = field->char_location;
138 new_field->mark = field->mark;
140 if (field->source)
142 new_field->source = strdup (field->source);
143 if (!new_field->source)
145 /* Out of memory. */
146 rec_field_destroy (new_field);
147 return NULL;
151 if (field->location_str)
153 new_field->location_str = strdup (field->location_str);
154 if (!new_field->location_str)
156 /* Out of memory. */
157 rec_field_destroy (new_field);
158 return NULL;
162 if (field->char_location_str)
164 new_field->char_location_str = strdup (field->char_location_str);
165 if (!new_field->char_location_str)
167 /* Out of memory. */
168 rec_field_destroy (new_field);
169 return NULL;
174 return new_field;
177 bool
178 rec_field_equal_p (rec_field_t field1,
179 rec_field_t field2)
181 return (strcmp (field1->name, field2->name) == 0);
184 void
185 rec_field_destroy (rec_field_t field)
187 if (field)
189 free (field->name);
190 free (field->value);
191 free (field->source);
192 free (field->location_str);
193 free (field->char_location_str);
194 free (field);
198 rec_comment_t
199 rec_field_to_comment (rec_field_t field)
201 rec_comment_t res;
202 char *comment_str;
204 comment_str = rec_write_field_str (field,
205 REC_WRITER_NORMAL);
206 if (!comment_str)
208 return NULL;
211 /* If the last character of the comment string is a newline, remove
212 it. */
214 if (comment_str[strlen (comment_str) - 1] == '\n')
216 comment_str[strlen (comment_str) - 1] = '\0';
219 res = rec_comment_new (comment_str);
220 free (comment_str);
222 return res;
225 const char *
226 rec_field_source (rec_field_t field)
228 return field->source;
231 bool
232 rec_field_set_source (rec_field_t field,
233 const char *source)
235 free (field->source);
236 field->source = strdup (source);
237 return (field->source != NULL);
240 size_t
241 rec_field_location (rec_field_t field)
243 return field->location;
246 bool
247 rec_field_set_location (rec_field_t field,
248 size_t location)
250 field->location = location;
251 free (field->location_str);
252 return (asprintf (&(field->location_str), "%zu", field->location)
253 != -1);
256 const char *
257 rec_field_location_str (rec_field_t field)
259 char *res;
261 if (field->location_str)
263 res = field->location_str;
265 else
267 res = "";
270 return res;
273 size_t
274 rec_field_char_location (rec_field_t field)
276 return field->char_location;
279 bool
280 rec_field_set_char_location (rec_field_t field,
281 size_t location)
283 field->char_location = location;
284 free (field->char_location_str);
285 return (asprintf (&(field->char_location_str), "%zu", field->char_location)
286 != -1);
289 const char *
290 rec_field_char_location_str (rec_field_t field)
292 char *res;
294 if (field->char_location_str)
296 res = field->char_location_str;
298 else
300 res = "";
303 return res;
306 void
307 rec_field_set_mark (rec_field_t field, int mark)
309 field->mark = mark;
313 rec_field_mark (rec_field_t field)
315 return field->mark;
319 * Private functions.
322 static void
323 rec_field_init (rec_field_t field)
325 /* Initialize the field structure so it can be safely passed to
326 rec_field_destroy even if its contents are not completely
327 initialized with real values. */
329 memset (field, 0 /* NULL */, sizeof (struct rec_field_s));
332 /* End of rec-field.c */