Mercurial > libedl
annotate src/str.c @ 4:c2408abb258a
*: add dumping to string, rename EDL_file to EDL
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Mon, 25 Dec 2023 16:24:16 -0500 |
parents | |
children | a6ab6d9c0dac |
rev | line source |
---|---|
4
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
1 #include <stdlib.h> |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
2 #include <string.h> |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
3 #include "str.h" |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
4 |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
5 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
|
6 if (!str) |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
7 return 0; |
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 str->size = 0; |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
10 str->capacity = 0; |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
11 str->data = NULL; |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
12 return 1; |
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 |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
15 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
|
16 if (new_capacity == str->capacity) |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
17 return 1; // nothing to do |
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 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
|
20 if (!str->data) |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
21 return 0; |
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 if (new_capacity > str->capacity) |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
24 memset(&str->data[str->capacity], 0, (new_capacity - str->capacity) * sizeof(char)); |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
25 |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
26 str->capacity = new_capacity; |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
27 |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
28 return 1; |
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 |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
31 int EDL_internal_string_append(EDL_internal_string* str, const char* data, const size_t length) { |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
32 /* this sucks, but I'm too lazy to write anything smarter. */ |
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 size_t capacity = 1; |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
35 while (capacity < (str->size + length + 1)) |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
36 capacity *= 2; |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
37 |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
38 if (capacity < str->capacity || !EDL_internal_string_allocate(str, capacity)) |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
39 return 0; |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
40 } |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
41 |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
42 strncat(str->data, data, length); |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
43 str->size += length; |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
44 |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
45 return 1; |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
46 } |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
47 |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
48 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
|
49 if (str->data) |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
50 free(str->data); |
c2408abb258a
*: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
51 } |