annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1 #include "datatypes.h"
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2 #include "util.h"
12
0cc2555db371 *: make our string functions not stupid
Paper <paper@paper.us.eu.org>
parents: 8
diff changeset
3 #include "str.h"
8
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
4
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
5 #include <stdlib.h>
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
6 #include <string.h>
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
7 #include <stdio.h>
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
8
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
9 static size_t EDL_internal_append_offset(const char* input, size_t offset, size_t length) {
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
10 size_t s = 0;
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
11
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
12 s += strchr(&input[offset + s], ';') - &input[offset];
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
13 if (s + offset > length)
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
14 return s;
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
15
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
16 s += strspn(&input[offset + s], ";\t ");
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
17 if (s + offset > length)
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
18 return s;
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
19
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
20 return s;
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
21 }
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
22
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
23 size_t EDL_internal_parse_int(const char* input, size_t offset, size_t length, int* int_return) {
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
24 if (offset > length)
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
25 return 0;
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
26
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
27 {
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
28 char* ptr_return;
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
29 *int_return = strtol(&input[offset], &ptr_return, 10);
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
30
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
31 if (!ptr_return)
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
32 return 0;
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
33 }
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
34
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
35 return EDL_internal_append_offset(input, offset, length);
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
36 }
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
37
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
38 size_t EDL_internal_parse_double(const char* input, size_t offset, size_t length, double* double_return) {
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
39 if (offset > length)
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
40 return 0;
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
41
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
42 {
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
43 char* ptr_return;
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
44 *double_return = strtod(&input[offset], &ptr_return);
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
45
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
46 if (!ptr_return)
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
47 return 0;
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
48 }
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
49
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
50 return EDL_internal_append_offset(input, offset, length);
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
51 }
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
52
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
53 size_t EDL_internal_parse_bool(const char* input, size_t offset, size_t length, bool* bool_return) {
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
54 if (offset > length)
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
55 return 0;
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
56
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
57 if (!strncmp(&input[offset], "TRUE", 4))
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
58 *bool_return = true;
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
59 else if (!strncmp(&input[offset], "FALSE", 5))
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
60 *bool_return = false;
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
61
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
62 return EDL_internal_append_offset(input, offset, length);
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
63 }
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
64
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
65 size_t EDL_internal_parse_media_type(const char* input, size_t offset, size_t length, EDL_media_type_t* media_return) {
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
66 if (offset > length)
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
67 return 0;
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
68
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
69 if (!strncmp(&input[offset], "VIDEO", 5))
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
70 *media_return = MEDIATYPE_VIDEO;
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
71 else if (!strncmp(&input[offset], "AUDIO", 5))
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
72 *media_return = MEDIATYPE_AUDIO;
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
73 else
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
74 *media_return = MEDIATYPE_UNKNOWN;
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
75
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
76 return EDL_internal_append_offset(input, offset, length);
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
77 }
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
78
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
79 size_t EDL_internal_parse_string(const char* input, size_t offset, size_t length, char** string_return) {
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
80 size_t start, s_length;
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
81
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
82 /* will most definitely break on good filesystems */
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
83 if (offset > length)
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
84 return 0;
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
85
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
86 start = strchr(&input[offset], '\"') - &input[offset] + 1;
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
87 if (start + offset > length)
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
88 return 0;
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
89
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
90 s_length = strchr(&input[offset + start], '\"') - &input[offset] - start;
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
91 if (start + s_length + offset > length)
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
92 return 0;
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
93
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
94 *string_return = malloc((s_length + 1) * sizeof(char));
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
95 if (!*string_return)
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
96 return 0;
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
97
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
98 memcpy(*string_return, &input[offset + start], s_length);
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
99 (*string_return)[s_length] = '\0';
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
100
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
101 return EDL_internal_append_offset(input, offset, length);
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
102 }
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
103
12
0cc2555db371 *: make our string functions not stupid
Paper <paper@paper.us.eu.org>
parents: 8
diff changeset
104 int EDL_internal_append_integer_to_string(EDL_internal_string* str, int value) {
0cc2555db371 *: make our string functions not stupid
Paper <paper@paper.us.eu.org>
parents: 8
diff changeset
105 char out[32] = {0}; /* this ought to be enough. */
0cc2555db371 *: make our string functions not stupid
Paper <paper@paper.us.eu.org>
parents: 8
diff changeset
106 int c = snprintf(out, 32, "%d", value);
0cc2555db371 *: make our string functions not stupid
Paper <paper@paper.us.eu.org>
parents: 8
diff changeset
107 return EDL_internal_string_append(str, out, (c < 32) ? c : 32);
0cc2555db371 *: make our string functions not stupid
Paper <paper@paper.us.eu.org>
parents: 8
diff changeset
108 }
0cc2555db371 *: make our string functions not stupid
Paper <paper@paper.us.eu.org>
parents: 8
diff changeset
109
0cc2555db371 *: make our string functions not stupid
Paper <paper@paper.us.eu.org>
parents: 8
diff changeset
110 int EDL_internal_append_double_to_string(EDL_internal_string* str, double value) {
0cc2555db371 *: make our string functions not stupid
Paper <paper@paper.us.eu.org>
parents: 8
diff changeset
111 char out[128] = {0};
0cc2555db371 *: make our string functions not stupid
Paper <paper@paper.us.eu.org>
parents: 8
diff changeset
112 int c = snprintf(out, 128, "%.6f", value);
0cc2555db371 *: make our string functions not stupid
Paper <paper@paper.us.eu.org>
parents: 8
diff changeset
113 return EDL_internal_string_append(str, out, (c < 128) ? c : 128);
8
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
114 }
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
115
12
0cc2555db371 *: make our string functions not stupid
Paper <paper@paper.us.eu.org>
parents: 8
diff changeset
116 int EDL_internal_append_bool_to_string(EDL_internal_string* str, bool value) {
0cc2555db371 *: make our string functions not stupid
Paper <paper@paper.us.eu.org>
parents: 8
diff changeset
117 if (value) {
0cc2555db371 *: make our string functions not stupid
Paper <paper@paper.us.eu.org>
parents: 8
diff changeset
118 EDL_internal_string_append(str, "TRUE", 4);
0cc2555db371 *: make our string functions not stupid
Paper <paper@paper.us.eu.org>
parents: 8
diff changeset
119 } else {
0cc2555db371 *: make our string functions not stupid
Paper <paper@paper.us.eu.org>
parents: 8
diff changeset
120 EDL_internal_string_append(str, "FALSE", 5);
0cc2555db371 *: make our string functions not stupid
Paper <paper@paper.us.eu.org>
parents: 8
diff changeset
121 }
8
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
122 }
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
123
12
0cc2555db371 *: make our string functions not stupid
Paper <paper@paper.us.eu.org>
parents: 8
diff changeset
124 int EDL_internal_append_media_type_to_string(EDL_internal_string* str, EDL_media_type_t value) {
8
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
125 switch (value) {
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
126 case MEDIATYPE_AUDIO:
12
0cc2555db371 *: make our string functions not stupid
Paper <paper@paper.us.eu.org>
parents: 8
diff changeset
127 return EDL_internal_string_append(str, "AUDIO", 5);
8
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
128 case MEDIATYPE_VIDEO:
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
129 case MEDIATYPE_UNKNOWN:
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
130 default:
12
0cc2555db371 *: make our string functions not stupid
Paper <paper@paper.us.eu.org>
parents: 8
diff changeset
131 return EDL_internal_string_append(str, "VIDEO", 5);
8
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
132 }
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
133 }