comparison src/datatypes.c @ 8:0c98b46eaf73 v2.0

*: update API to 2.0, big changes all APIs now use pointers to an EDL object. it is up to the user to make sure that the pointer is valid. additionally, many things have been separated into new files to make it easier to digest
author Paper <paper@paper.us.eu.org>
date Sun, 03 Mar 2024 17:56:58 -0500
parents
children 0cc2555db371
comparison
equal deleted inserted replaced
7:fee08fa622e1 8:0c98b46eaf73
1 #include "datatypes.h"
2 #include "util.h"
3
4 #include <stdlib.h>
5 #include <string.h>
6 #include <stdio.h>
7
8 static size_t EDL_internal_append_offset(const char* input, size_t offset, size_t length) {
9 size_t s = 0;
10
11 s += strchr(&input[offset + s], ';') - &input[offset];
12 if (s + offset > length)
13 return s;
14
15 s += strspn(&input[offset + s], ";\t ");
16 if (s + offset > length)
17 return s;
18
19 return s;
20 }
21
22 size_t EDL_internal_parse_int(const char* input, size_t offset, size_t length, int* int_return) {
23 if (offset > length)
24 return 0;
25
26 {
27 char* ptr_return;
28 *int_return = strtol(&input[offset], &ptr_return, 10);
29
30 if (!ptr_return)
31 return 0;
32 }
33
34 return EDL_internal_append_offset(input, offset, length);
35 }
36
37 size_t EDL_internal_parse_double(const char* input, size_t offset, size_t length, double* double_return) {
38 if (offset > length)
39 return 0;
40
41 {
42 char* ptr_return;
43 *double_return = strtod(&input[offset], &ptr_return);
44
45 if (!ptr_return)
46 return 0;
47 }
48
49 return EDL_internal_append_offset(input, offset, length);
50 }
51
52 size_t EDL_internal_parse_bool(const char* input, size_t offset, size_t length, bool* bool_return) {
53 if (offset > length)
54 return 0;
55
56 if (!strncmp(&input[offset], "TRUE", 4))
57 *bool_return = true;
58 else if (!strncmp(&input[offset], "FALSE", 5))
59 *bool_return = false;
60
61 return EDL_internal_append_offset(input, offset, length);
62 }
63
64 size_t EDL_internal_parse_media_type(const char* input, size_t offset, size_t length, EDL_media_type_t* media_return) {
65 if (offset > length)
66 return 0;
67
68 if (!strncmp(&input[offset], "VIDEO", 5))
69 *media_return = MEDIATYPE_VIDEO;
70 else if (!strncmp(&input[offset], "AUDIO", 5))
71 *media_return = MEDIATYPE_AUDIO;
72 else
73 *media_return = MEDIATYPE_UNKNOWN;
74
75 return EDL_internal_append_offset(input, offset, length);
76 }
77
78 size_t EDL_internal_parse_string(const char* input, size_t offset, size_t length, char** string_return) {
79 size_t start, s_length;
80
81 /* will most definitely break on good filesystems */
82 if (offset > length)
83 return 0;
84
85 start = strchr(&input[offset], '\"') - &input[offset] + 1;
86 if (start + offset > length)
87 return 0;
88
89 s_length = strchr(&input[offset + start], '\"') - &input[offset] - start;
90 if (start + s_length + offset > length)
91 return 0;
92
93 *string_return = malloc((s_length + 1) * sizeof(char));
94 if (!*string_return)
95 return 0;
96
97 memcpy(*string_return, &input[offset + start], s_length);
98 (*string_return)[s_length] = '\0';
99
100 return EDL_internal_append_offset(input, offset, length);
101 }
102
103 char* EDL_internal_integer_to_string(int value) {
104 char out[256] = {0}; /* this ought to be enough. */
105 snprintf(out, 256, "%d", value);
106 out[255] = '\0';
107 return EDL_internal_strdup(out);
108 }
109
110 char* EDL_internal_double_to_string(double value) {
111 char out[256] = {0};
112 snprintf(out, 256, "%.6f", value);
113 out[255] = '\0';
114 return EDL_internal_strdup(out);
115 }
116
117 char* EDL_internal_bool_to_string(bool value) {
118 return EDL_internal_strdup(value ? "TRUE" : "FALSE");
119 }
120
121 char* EDL_internal_media_type_to_string(EDL_media_type_t value) {
122 switch (value) {
123 case MEDIATYPE_AUDIO:
124 return EDL_internal_strdup("AUDIO");
125 case MEDIATYPE_VIDEO:
126 case MEDIATYPE_UNKNOWN:
127 default:
128 return EDL_internal_strdup("VIDEO");
129 }
130 }