projects/libpws

view pws-record.c @ 13:2bb1bbac1d0a

Added tag version-1.0.0 for changeset 1926dfc9feb0
author Guido Berhoerster <guido+libpws@berhoerster.name>
date Sun Aug 04 21:37:56 2019 +0200 (2019-08-04)
parents d541e748cfd8
children
line source
1 /*
2 * Copyright (C) 2015 Guido Berhoerster <guido+libpws@berhoerster.name>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included
13 * in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
24 #include "compat.h"
26 #include <stdlib.h>
28 #include "pws-internal.h"
30 void
31 pws3_record_destroy(struct pws3_record *record)
32 {
33 size_t i;
35 if (record == NULL) {
36 return;
37 }
39 for (i = 0x01; i <= 0xff; i++) {
40 pws3_field_destroy(record->fields[i]);
41 }
42 pws_free(record, sizeof (struct pws3_record));
43 }
45 struct pws3_record *
46 pws3_record_create(void)
47 {
48 struct pws3_field *uuid_field = NULL;
49 unsigned char uuid[PWS3_UUID_SIZE];
50 struct pws3_record *record = NULL;
51 size_t i;
53 /*
54 * always add an UUID field which is mandatory and needed for inserting
55 * a record into a database
56 */
57 if (pws_generate_uuid(uuid) != 0) {
58 goto err;
59 }
60 uuid_field = pws3_field_create(0, PWS3_RECORD_FIELD_UUID);
61 if (uuid_field == NULL) {
62 goto err;
63 }
64 pws3_field_set_uuid(uuid_field, uuid);
66 record = pws_alloc(sizeof (struct pws3_record));
67 if (record == NULL) {
68 goto err;
69 }
71 for (i = 0x00; i <= 0xff; i++) {
72 record->fields[i] = NULL;
73 }
75 pws3_record_set_field(record, uuid_field);
77 return (record);
78 err:
79 pws3_field_destroy(uuid_field);
80 pws_free(record, (record != NULL) ? sizeof (struct pws3_record) : 0);
82 return (NULL);
83 }
85 void
86 pws3_record_set_field(struct pws3_record *record, struct pws3_field *field)
87 {
88 uint8_t field_type;
90 PWS_ASSERT(!pws3_field_is_header(field));
91 PWS_ASSERT((pws3_field_get_data_type(field) != PWS_DATA_TYPE_TEXT) ||
92 (field->value.text != NULL));
93 PWS_ASSERT((pws3_field_get_data_type(field) != PWS_DATA_TYPE_BYTES) ||
94 (field->value.bytes != NULL));
96 field_type = pws3_field_get_type(field);
97 pws3_field_destroy(pws3_record_remove_field(record, field_type));
98 record->fields[field_type] = field;
99 }
101 struct pws3_field *
102 pws3_record_get_field(struct pws3_record *record, uint8_t field_type)
103 {
104 return (record->fields[field_type]);
105 }
107 struct pws3_field *
108 pws3_record_remove_field(struct pws3_record *record, uint8_t field_type)
109 {
110 struct pws3_field *field;
112 field = pws3_record_get_field(record, field_type);
113 record->fields[field_type] = NULL;
115 return (field);
116 }