view pws-record.c @ 11:96a507a110c8

Add support for format 0x030E
author Guido Berhoerster <guido+libpws@berhoerster.name>
date Fri, 02 Aug 2019 14:47:43 +0200
parents 956e97003bad
children
line wrap: on
line source

/*
 * Copyright (C) 2015 Guido Berhoerster <guido+libpws@berhoerster.name>
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include "compat.h"

#include <stdlib.h>

#include "pws-internal.h"

void
pws3_record_destroy(struct pws3_record *record)
{
	size_t		i;

	if (record == NULL) {
		return;
	}

	for (i = 0x01; i <= 0xff; i++) {
		pws3_field_destroy(record->fields[i]);
	}
	pws_free(record, sizeof (struct pws3_record));
}

struct pws3_record *
pws3_record_create(void)
{
	struct pws3_field *uuid_field = NULL;
	unsigned char	uuid[PWS3_UUID_SIZE];
	struct pws3_record *record = NULL;
	size_t		i;

	/*
	 * always add an UUID field which is mandatory and needed for inserting
	 * a record into a database
	 */
	if (pws_generate_uuid(uuid) != 0) {
		goto err;
	}
	uuid_field = pws3_field_create(0, PWS3_RECORD_FIELD_UUID);
	if (uuid_field == NULL) {
		goto err;
	}
	pws3_field_set_uuid(uuid_field, uuid);

	record = pws_alloc(sizeof (struct pws3_record));
	if (record == NULL) {
		goto err;
	}

	for (i = 0x00; i <= 0xff; i++) {
		record->fields[i] = NULL;
	}

	pws3_record_set_field(record, uuid_field);

	return (record);
err:
	pws3_field_destroy(uuid_field);
	pws_free(record, (record != NULL) ? sizeof (struct pws3_record) : 0);

	return (NULL);
}

void
pws3_record_set_field(struct pws3_record *record, struct pws3_field *field)
{
	uint8_t	field_type;

	PWS_ASSERT(!pws3_field_is_header(field));
	PWS_ASSERT((pws3_field_get_data_type(field) != PWS_DATA_TYPE_TEXT) ||
	    (field->value.text != NULL));
	PWS_ASSERT((pws3_field_get_data_type(field) != PWS_DATA_TYPE_BYTES) ||
	    (field->value.bytes != NULL));

	field_type = pws3_field_get_type(field);
	pws3_field_destroy(pws3_record_remove_field(record, field_type));
	record->fields[field_type] = field;
}

struct pws3_field *
pws3_record_get_field(struct pws3_record *record, uint8_t field_type)
{
	return (record->fields[field_type]);
}

struct pws3_field *
pws3_record_remove_field(struct pws3_record *record, uint8_t field_type)
{
	struct pws3_field *field;

	field = pws3_record_get_field(record, field_type);
	record->fields[field_type] = NULL;

	return (field);
}