Mercurial > libedl
annotate src/str.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:
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 |
14 | 7 * as painful to read */ |
4
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
8 |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
9 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
|
10 if (!str) |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
11 return 0; |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
12 |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
13 str->size = 0; |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
14 str->capacity = 0; |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
15 str->data = NULL; |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
16 return 1; |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
17 } |
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 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
|
20 if (new_capacity == str->capacity) |
8
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
5
diff
changeset
|
21 return 1; /* nothing to do */ |
4
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
22 |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
23 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
|
24 if (!str->data) |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
25 return 0; |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
26 |
12
0cc2555db371
*: make our string functions not stupid
Paper <paper@paper.us.eu.org>
parents:
8
diff
changeset
|
27 if (new_capacity > str->capacity) |
0cc2555db371
*: make our string functions not stupid
Paper <paper@paper.us.eu.org>
parents:
8
diff
changeset
|
28 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
|
29 |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
30 str->capacity = new_capacity; |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
31 |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
32 return 1; |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
33 } |
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 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
|
36 if (str->capacity == 0) |
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
5
diff
changeset
|
37 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
|
38 return 0; |
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
5
diff
changeset
|
39 |
12
0cc2555db371
*: make our string functions not stupid
Paper <paper@paper.us.eu.org>
parents:
8
diff
changeset
|
40 /* increase capacity if needed */ |
8
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 |
12
0cc2555db371
*: make our string functions not stupid
Paper <paper@paper.us.eu.org>
parents:
8
diff
changeset
|
50 memcpy(&str->data[str->size], data, length); |
4
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 } |