Mercurial > libedl
view src/datatypes.c @ 12:0cc2555db371
*: make our string functions not stupid
strncat() made everything slow!
new addition internally: an EDL_header structure to make things
not stupid. if we were using C++ it wouldn't really be necessary,
but alas :)
author | Paper <paper@paper.us.eu.org> |
---|---|
date | Wed, 27 Mar 2024 13:38:30 -0400 |
parents | 0c98b46eaf73 |
children | 2d7c810a1ac2 |
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) { if (offset > length) return 0; { char* ptr_return; *int_return = strtol(&input[offset], &ptr_return, 10); if (!ptr_return) 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) { if (offset > length) return 0; { char* ptr_return; *double_return = strtod(&input[offset], &ptr_return); if (!ptr_return) 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 most definitely break on good filesystems */ 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); } }