4 * Date: Fri Feb 27 20:40:26 2009
6 * GNU recutils - Fields
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/>.
34 /* Field Data Structure.
36 * A field is an association between a label and a value.
41 /* The name and the value of a field are UTF-8 encoded strings.
42 Thus, we use NULL-terminated strings to store them. */
53 char *char_location_str
;
60 /* Static functions defined below. */
62 static void rec_field_init (rec_field_t field
);
69 rec_field_name (rec_field_t field
)
75 rec_field_set_name (rec_field_t field
, const char *name
)
78 field
->name
= strdup (name
);
79 return (field
->name
!= NULL
);
83 rec_field_value (rec_field_t field
)
89 rec_field_set_value (rec_field_t field
,
93 field
->value
= strdup (value
);
94 return (field
->value
!= NULL
);
98 rec_field_new (const char *name
,
103 field
= malloc (sizeof (struct rec_field_s
));
107 rec_field_init (field
);
109 if (!rec_field_set_name (field
, name
))
112 rec_field_destroy (field
);
116 if (!rec_field_set_value (field
, value
))
119 rec_field_destroy (field
);
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
));
136 new_field
->location
= field
->location
;
137 new_field
->char_location
= field
->char_location
;
138 new_field
->mark
= field
->mark
;
142 new_field
->source
= strdup (field
->source
);
143 if (!new_field
->source
)
146 rec_field_destroy (new_field
);
151 if (field
->location_str
)
153 new_field
->location_str
= strdup (field
->location_str
);
154 if (!new_field
->location_str
)
157 rec_field_destroy (new_field
);
162 if (field
->char_location_str
)
164 new_field
->char_location_str
= strdup (field
->char_location_str
);
165 if (!new_field
->char_location_str
)
168 rec_field_destroy (new_field
);
178 rec_field_equal_p (rec_field_t field1
,
181 return (strcmp (field1
->name
, field2
->name
) == 0);
185 rec_field_destroy (rec_field_t field
)
191 free (field
->source
);
192 free (field
->location_str
);
193 free (field
->char_location_str
);
199 rec_field_to_comment (rec_field_t field
)
204 comment_str
= rec_write_field_str (field
,
211 /* If the last character of the comment string is a newline, remove
214 if (comment_str
[strlen (comment_str
) - 1] == '\n')
216 comment_str
[strlen (comment_str
) - 1] = '\0';
219 res
= rec_comment_new (comment_str
);
226 rec_field_source (rec_field_t field
)
228 return field
->source
;
232 rec_field_set_source (rec_field_t field
,
235 free (field
->source
);
236 field
->source
= strdup (source
);
237 return (field
->source
!= NULL
);
241 rec_field_location (rec_field_t field
)
243 return field
->location
;
247 rec_field_set_location (rec_field_t field
,
250 field
->location
= location
;
251 free (field
->location_str
);
252 return (asprintf (&(field
->location_str
), "%zu", field
->location
)
257 rec_field_location_str (rec_field_t field
)
261 if (field
->location_str
)
263 res
= field
->location_str
;
274 rec_field_char_location (rec_field_t field
)
276 return field
->char_location
;
280 rec_field_set_char_location (rec_field_t field
,
283 field
->char_location
= location
;
284 free (field
->char_location_str
);
285 return (asprintf (&(field
->char_location_str
), "%zu", field
->char_location
)
290 rec_field_char_location_str (rec_field_t field
)
294 if (field
->char_location_str
)
296 res
= field
->char_location_str
;
307 rec_field_set_mark (rec_field_t field
, int mark
)
313 rec_field_mark (rec_field_t field
)
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 */