view src/datatypes.c @ 14:2d7c810a1ac2 default tip

*: misc. cleanup
author Paper <paper@paper.us.eu.org>
date Fri, 03 May 2024 22:40:28 -0400
parents 0cc2555db371
children
line wrap: on
line source

#include "datatypes.h"
#include "util.h"
#include "str.h"

#include <stdlib.h>
#include <string.h>
#include <stdio.h>

static size_t EDL_internal_append_offset(const char* input, size_t offset, size_t length) {
	size_t s = 0;

	s += strchr(&input[offset + s], ';') - &input[offset];
	if (s + offset > length)
		return s;

	s += strspn(&input[offset + s], ";\t ");
	if (s + offset > length)
		return s;

	return s;
}

size_t EDL_internal_parse_int(const char* input, size_t offset, size_t length, int* int_return) {
	char* ptr_return = input;

	if (offset > length)
		return 0;

	*int_return = strtol(&input[offset], &ptr_return, 10);

	if (ptr_return == input)
		return 0;

	return EDL_internal_append_offset(input, offset, length);
}

size_t EDL_internal_parse_double(const char* input, size_t offset, size_t length, double* double_return) {
	char* ptr_return = input;

	if (offset > length)
		return 0;

	*double_return = strtod(&input[offset], &ptr_return);

	if (ptr_return == input)
		return 0;

	return EDL_internal_append_offset(input, offset, length);
}

size_t EDL_internal_parse_bool(const char* input, size_t offset, size_t length, bool* bool_return) {
	if (offset > length)
		return 0;

	if (!strncmp(&input[offset], "TRUE", 4))
		*bool_return = true;
	else if (!strncmp(&input[offset], "FALSE", 5))
		*bool_return = false;

	return EDL_internal_append_offset(input, offset, length);
}

size_t EDL_internal_parse_media_type(const char* input, size_t offset, size_t length, EDL_media_type_t* media_return) {
	if (offset > length)
		return 0;

	if (!strncmp(&input[offset], "VIDEO", 5))
		*media_return = MEDIATYPE_VIDEO;
	else if (!strncmp(&input[offset], "AUDIO", 5))
		*media_return = MEDIATYPE_AUDIO;
	else
		*media_return = MEDIATYPE_UNKNOWN;

	return EDL_internal_append_offset(input, offset, length);
}

size_t EDL_internal_parse_string(const char* input, size_t offset, size_t length, char** string_return) {
	size_t start, s_length;

	/* will definitely break on any good filesystem */
	if (offset > length)
		return 0;

	start = strchr(&input[offset], '\"') - &input[offset] + 1;
	if (start + offset > length)
		return 0;

	s_length = strchr(&input[offset + start], '\"') - &input[offset] - start;
	if (start + s_length + offset > length)
		return 0;

	*string_return = malloc((s_length + 1) * sizeof(char));
	if (!*string_return)
		return 0;

	memcpy(*string_return, &input[offset + start], s_length);
	(*string_return)[s_length] = '\0';

	return EDL_internal_append_offset(input, offset, length);
}

int EDL_internal_append_integer_to_string(EDL_internal_string* str, int value) {
	char out[32] = {0}; /* this ought to be enough. */
	int c = snprintf(out, 32, "%d", value);
	return EDL_internal_string_append(str, out, (c < 32) ? c : 32);
}

int EDL_internal_append_double_to_string(EDL_internal_string* str, double value) {
	char out[128] = {0};
	int c = snprintf(out, 128, "%.6f", value);
	return EDL_internal_string_append(str, out, (c < 128) ? c : 128);
}

int EDL_internal_append_bool_to_string(EDL_internal_string* str, bool value) {
	if (value) {
		EDL_internal_string_append(str, "TRUE", 4);
	} else {
		EDL_internal_string_append(str, "FALSE", 5);
	}
}

int EDL_internal_append_media_type_to_string(EDL_internal_string* str, EDL_media_type_t value) {
	switch (value) {
		case MEDIATYPE_AUDIO:
			return EDL_internal_string_append(str, "AUDIO", 5);
		case MEDIATYPE_VIDEO:
		case MEDIATYPE_UNKNOWN:
		default:
			return EDL_internal_string_append(str, "VIDEO", 5);
	}
}