annotate src/datatypes.c @ 10:d6f8f73a0c50 v2.1

util: make strnchr an actual strnchr
author Paper <paper@paper.us.eu.org>
date Fri, 22 Mar 2024 20:51:40 -0400
parents 0c98b46eaf73
children 0cc2555db371
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"
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
3
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
4 #include <stdlib.h>
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
5 #include <string.h>
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
6 #include <stdio.h>
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
7
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
8 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
9 size_t s = 0;
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
10
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
11 s += strchr(&input[offset + s], ';') - &input[offset];
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
12 if (s + offset > length)
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
13 return s;
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
14
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
15 s += strspn(&input[offset + s], ";\t ");
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
16 if (s + offset > length)
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
17 return s;
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
18
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
19 return s;
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
20 }
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 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
23 if (offset > length)
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
24 return 0;
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
25
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 char* ptr_return;
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
28 *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
29
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
30 if (!ptr_return)
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
31 return 0;
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
32 }
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 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
35 }
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 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
38 if (offset > length)
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
39 return 0;
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
40
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 char* ptr_return;
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
43 *double_return = strtod(&input[offset], &ptr_return);
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
44
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
45 if (!ptr_return)
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
46 return 0;
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
47 }
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 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
50 }
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 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
53 if (offset > length)
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
54 return 0;
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
55
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
56 if (!strncmp(&input[offset], "TRUE", 4))
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
57 *bool_return = true;
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
58 else if (!strncmp(&input[offset], "FALSE", 5))
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
59 *bool_return = false;
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
60
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
61 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
62 }
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 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
65 if (offset > length)
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
66 return 0;
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
67
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
68 if (!strncmp(&input[offset], "VIDEO", 5))
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
69 *media_return = MEDIATYPE_VIDEO;
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
70 else if (!strncmp(&input[offset], "AUDIO", 5))
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
71 *media_return = MEDIATYPE_AUDIO;
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
72 else
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
73 *media_return = MEDIATYPE_UNKNOWN;
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
74
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
75 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
76 }
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 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
79 size_t start, s_length;
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
80
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
81 /* will most definitely break on good filesystems */
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
82 if (offset > length)
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
83 return 0;
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
84
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
85 start = strchr(&input[offset], '\"') - &input[offset] + 1;
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
86 if (start + offset > length)
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
87 return 0;
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
88
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
89 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
90 if (start + s_length + offset > length)
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
91 return 0;
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
92
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
93 *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
94 if (!*string_return)
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
95 return 0;
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
96
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
97 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
98 (*string_return)[s_length] = '\0';
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
99
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
100 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
101 }
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 char* EDL_internal_integer_to_string(int value) {
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
104 char out[256] = {0}; /* this ought to be enough. */
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
105 snprintf(out, 256, "%d", value);
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
106 out[255] = '\0';
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
107 return EDL_internal_strdup(out);
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
108 }
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
109
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
110 char* EDL_internal_double_to_string(double value) {
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
111 char out[256] = {0};
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
112 snprintf(out, 256, "%.6f", value);
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
113 out[255] = '\0';
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
114 return EDL_internal_strdup(out);
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
115 }
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
116
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
117 char* EDL_internal_bool_to_string(bool value) {
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
118 return EDL_internal_strdup(value ? "TRUE" : "FALSE");
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
119 }
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
120
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
121 char* EDL_internal_media_type_to_string(EDL_media_type_t value) {
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
122 switch (value) {
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
123 case MEDIATYPE_AUDIO:
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
124 return EDL_internal_strdup("AUDIO");
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
125 case MEDIATYPE_VIDEO:
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
126 case MEDIATYPE_UNKNOWN:
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
127 default:
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
128 return EDL_internal_strdup("VIDEO");
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
129 }
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
130 }