Mercurial > libedl
annotate 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 |
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) { |
14 | 24 char* ptr_return = input; |
25 | |
8
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
26 if (offset > length) |
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
27 return 0; |
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
28 |
14 | 29 *int_return = strtol(&input[offset], &ptr_return, 10); |
8
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
30 |
14 | 31 if (ptr_return == input) |
32 return 0; | |
8
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) { |
14 | 38 char* ptr_return = input; |
39 | |
8
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
40 if (offset > length) |
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
41 return 0; |
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
42 |
14 | 43 *double_return = strtod(&input[offset], &ptr_return); |
8
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
44 |
14 | 45 if (ptr_return == input) |
46 return 0; | |
8
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 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
|
49 } |
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 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
|
52 if (offset > length) |
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
53 return 0; |
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
54 |
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
55 if (!strncmp(&input[offset], "TRUE", 4)) |
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
56 *bool_return = true; |
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
57 else if (!strncmp(&input[offset], "FALSE", 5)) |
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
58 *bool_return = false; |
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
59 |
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
60 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
|
61 } |
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 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
|
64 if (offset > length) |
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
65 return 0; |
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
66 |
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
67 if (!strncmp(&input[offset], "VIDEO", 5)) |
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
68 *media_return = MEDIATYPE_VIDEO; |
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
69 else if (!strncmp(&input[offset], "AUDIO", 5)) |
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
70 *media_return = MEDIATYPE_AUDIO; |
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
71 else |
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
72 *media_return = MEDIATYPE_UNKNOWN; |
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
73 |
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
74 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
|
75 } |
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 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
|
78 size_t start, s_length; |
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
79 |
14 | 80 /* will definitely break on any good filesystem */ |
8
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
81 if (offset > length) |
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
82 return 0; |
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
83 |
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
84 start = strchr(&input[offset], '\"') - &input[offset] + 1; |
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
85 if (start + offset > length) |
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
86 return 0; |
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
87 |
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
88 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
|
89 if (start + s_length + offset > length) |
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
90 return 0; |
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
91 |
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
92 *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
|
93 if (!*string_return) |
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
94 return 0; |
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
95 |
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
96 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
|
97 (*string_return)[s_length] = '\0'; |
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
98 |
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
99 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
|
100 } |
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
101 |
12
0cc2555db371
*: make our string functions not stupid
Paper <paper@paper.us.eu.org>
parents:
8
diff
changeset
|
102 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
|
103 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
|
104 int c = snprintf(out, 32, "%d", value); |
0cc2555db371
*: make our string functions not stupid
Paper <paper@paper.us.eu.org>
parents:
8
diff
changeset
|
105 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
|
106 } |
0cc2555db371
*: make our string functions not stupid
Paper <paper@paper.us.eu.org>
parents:
8
diff
changeset
|
107 |
0cc2555db371
*: make our string functions not stupid
Paper <paper@paper.us.eu.org>
parents:
8
diff
changeset
|
108 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
|
109 char out[128] = {0}; |
0cc2555db371
*: make our string functions not stupid
Paper <paper@paper.us.eu.org>
parents:
8
diff
changeset
|
110 int c = snprintf(out, 128, "%.6f", value); |
0cc2555db371
*: make our string functions not stupid
Paper <paper@paper.us.eu.org>
parents:
8
diff
changeset
|
111 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
|
112 } |
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
113 |
12
0cc2555db371
*: make our string functions not stupid
Paper <paper@paper.us.eu.org>
parents:
8
diff
changeset
|
114 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
|
115 if (value) { |
0cc2555db371
*: make our string functions not stupid
Paper <paper@paper.us.eu.org>
parents:
8
diff
changeset
|
116 EDL_internal_string_append(str, "TRUE", 4); |
0cc2555db371
*: make our string functions not stupid
Paper <paper@paper.us.eu.org>
parents:
8
diff
changeset
|
117 } else { |
0cc2555db371
*: make our string functions not stupid
Paper <paper@paper.us.eu.org>
parents:
8
diff
changeset
|
118 EDL_internal_string_append(str, "FALSE", 5); |
0cc2555db371
*: make our string functions not stupid
Paper <paper@paper.us.eu.org>
parents:
8
diff
changeset
|
119 } |
8
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 |
12
0cc2555db371
*: make our string functions not stupid
Paper <paper@paper.us.eu.org>
parents:
8
diff
changeset
|
122 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
|
123 switch (value) { |
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
124 case MEDIATYPE_AUDIO: |
12
0cc2555db371
*: make our string functions not stupid
Paper <paper@paper.us.eu.org>
parents:
8
diff
changeset
|
125 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
|
126 case MEDIATYPE_VIDEO: |
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
127 case MEDIATYPE_UNKNOWN: |
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
128 default: |
12
0cc2555db371
*: make our string functions not stupid
Paper <paper@paper.us.eu.org>
parents:
8
diff
changeset
|
129 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
|
130 } |
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
131 } |