Mercurial > libedl
annotate src/util.c @ 9:21aad49ed2c9
Added tag v2.0 for changeset 0c98b46eaf73
| author | Paper <paper@paper.us.eu.org> |
|---|---|
| date | Fri, 22 Mar 2024 20:50:47 -0400 |
| parents | 0c98b46eaf73 |
| children | d6f8f73a0c50 |
| rev | line source |
|---|---|
|
8
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
1 #include "util.h" |
|
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
2 |
|
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
3 #include <stdlib.h> |
|
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
4 #include <string.h> |
|
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
5 |
|
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
6 /* reimplementations of non-portable stdlib functions */ |
|
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
7 |
|
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
8 size_t EDL_internal_strnlen(const char* s, size_t maxlen) { |
|
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
9 size_t len; |
|
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
10 |
|
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
11 for (len = 0; len < maxlen; len++, s++) { |
|
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
12 if (!*s) |
|
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
13 break; |
|
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
14 } |
|
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 return len; |
|
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
17 } |
|
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
18 |
|
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
19 char* EDL_internal_strnchr(const char* haystack, char needle, size_t length) { |
|
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
20 for (; length && *haystack != needle; haystack++, length--); |
|
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 return length ? haystack : NULL; |
|
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
23 } |
|
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
24 |
|
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
25 char* EDL_internal_strdup(const char* s) { |
|
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
26 const size_t len = strlen(s); |
|
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
27 |
|
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
28 char* d = malloc((len + 1) * sizeof(char)); |
|
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
29 memcpy(d, s, len * sizeof(char)); |
|
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
30 d[len] = '\0'; |
|
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
31 |
|
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
32 return d; |
|
0c98b46eaf73
*: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
33 } |
