Mercurial > libedl
annotate src/str.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 | a6ab6d9c0dac |
children | 0cc2555db371 |
rev | line source |
---|---|
8
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
5
diff
changeset
|
1 #include "str.h" |
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
5
diff
changeset
|
2 |
4
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
3 #include <stdlib.h> |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
4 #include <string.h> |
8
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
5
diff
changeset
|
5 |
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
5
diff
changeset
|
6 /* rudimentary string functions so the code in edl.c isn't |
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
5
diff
changeset
|
7 * as painful to read |
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
5
diff
changeset
|
8 */ |
4
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
9 |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
10 int EDL_internal_string_init(EDL_internal_string* str) { |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
11 if (!str) |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
12 return 0; |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
13 |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
14 str->size = 0; |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
15 str->capacity = 0; |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
16 str->data = NULL; |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
17 return 1; |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
18 } |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
19 |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
20 int EDL_internal_string_allocate(EDL_internal_string* str, size_t new_capacity) { |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
21 if (new_capacity == str->capacity) |
8
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
5
diff
changeset
|
22 return 1; /* nothing to do */ |
4
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
23 |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
24 str->data = realloc(str->data, new_capacity * sizeof(char)); |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
25 if (!str->data) |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
26 return 0; |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
27 |
8
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
5
diff
changeset
|
28 //if (new_capacity > str->capacity) |
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
5
diff
changeset
|
29 // memset(&str->data[str->capacity], 0, (new_capacity - str->capacity) * sizeof(char)); |
4
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
30 |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
31 str->capacity = new_capacity; |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
32 |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
33 return 1; |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
34 } |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
35 |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
36 int EDL_internal_string_append(EDL_internal_string* str, const char* data, const size_t length) { |
8
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
5
diff
changeset
|
37 if (str->capacity == 0) |
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
5
diff
changeset
|
38 if (!EDL_internal_string_allocate(str, 1)) |
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
5
diff
changeset
|
39 return 0; |
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
5
diff
changeset
|
40 |
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
5
diff
changeset
|
41 if (str->size + length + 1 >= str->capacity) { |
4
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
42 size_t capacity = 1; |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
43 while (capacity < (str->size + length + 1)) |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
44 capacity *= 2; |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
45 |
8
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
5
diff
changeset
|
46 if (!EDL_internal_string_allocate(str, capacity)) |
4
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
47 return 0; |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
48 } |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
49 |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
50 strncat(str->data, data, length); |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
51 str->size += length; |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
52 |
8
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
5
diff
changeset
|
53 return length; |
4
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
54 } |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
55 |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
56 void EDL_internal_string_free(EDL_internal_string* str) { |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
57 if (str->data) |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
58 free(str->data); |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
59 } |