[0532954] | 1 | /* dynamic string handling */ |
---|
| 2 | /* Copyright (c) Olly Betts 1999, 2001, 2012, 2014, 2024 |
---|
[9286525] | 3 | * |
---|
[89231c4] | 4 | * This program is free software; you can redistribute it and/or modify |
---|
| 5 | * it under the terms of the GNU General Public License as published by |
---|
| 6 | * the Free Software Foundation; either version 2 of the License, or |
---|
| 7 | * (at your option) any later version. |
---|
[846746e] | 8 | * |
---|
| 9 | * This program is distributed in the hope that it will be useful, |
---|
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
[89231c4] | 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 12 | * GNU General Public License for more details. |
---|
[846746e] | 13 | * |
---|
[89231c4] | 14 | * You should have received a copy of the GNU General Public License |
---|
| 15 | * along with this program; if not, write to the Free Software |
---|
[ecbc6c18] | 16 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
[846746e] | 17 | */ |
---|
[30d80d2] | 18 | |
---|
[0532954] | 19 | #ifndef SURVEX_INCLUDED_STR_H |
---|
| 20 | #define SURVEX_INCLUDED_STR_H |
---|
| 21 | |
---|
[30d80d2] | 22 | #include "osalloc.h" |
---|
[0532954] | 23 | #include <string.h> |
---|
| 24 | |
---|
| 25 | typedef struct { |
---|
| 26 | char *s; |
---|
| 27 | int len; |
---|
| 28 | int capacity; /* One less than the allocated size! */ |
---|
| 29 | } string; |
---|
| 30 | |
---|
| 31 | // string foo = S_INIT; |
---|
| 32 | #define S_INIT { NULL, 0, 0 } |
---|
| 33 | |
---|
| 34 | void s_expand_(string *pstr, int addition); |
---|
| 35 | |
---|
| 36 | /* Append a block of text with given length. */ |
---|
| 37 | void s_catlen(string *pstr, const char *s, int s_len); |
---|
| 38 | |
---|
[87d7f7c] | 39 | void s_catn(string *pstr, int n, char c); |
---|
| 40 | |
---|
| 41 | /* Append another string. */ |
---|
| 42 | inline void s_cats(string *pstr, const string *s) { |
---|
| 43 | s_catlen(pstr, s->s, s->len); |
---|
| 44 | } |
---|
| 45 | |
---|
[0532954] | 46 | /* Append a C string. */ |
---|
| 47 | inline void s_cat(string *pstr, const char *s) { |
---|
| 48 | s_catlen(pstr, s, strlen(s)); |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | /* Append a character */ |
---|
| 52 | inline void s_catchar(string *pstr, char c) { |
---|
| 53 | if (pstr->capacity == pstr->len) s_expand_(pstr, 1); |
---|
| 54 | pstr->s[pstr->len++] = c; |
---|
| 55 | pstr->s[pstr->len] = '\0'; |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | /* Truncate string to zero length (and ensure it isn't NULL). */ |
---|
| 59 | inline void s_clear(string *pstr) { |
---|
| 60 | if (pstr->s == NULL) s_expand_(pstr, 0); |
---|
| 61 | pstr->len = 0; |
---|
| 62 | pstr->s[0] = '\0'; |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | /* Truncate string */ |
---|
| 66 | inline void s_truncate(string *pstr, int new_len) { |
---|
| 67 | if (new_len < pstr->len) { |
---|
| 68 | pstr->len = new_len; |
---|
| 69 | pstr->s[new_len] = '\0'; |
---|
| 70 | } |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | /* Release allocated memory. */ |
---|
| 74 | inline void s_free(string *pstr) { |
---|
| 75 | osfree(pstr->s); |
---|
| 76 | pstr->s = NULL; |
---|
| 77 | pstr->len = 0; |
---|
| 78 | pstr->capacity = 0; |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | /* Steal the C string. */ |
---|
| 82 | inline char *s_steal(string *pstr) { |
---|
| 83 | char *s = pstr->s; |
---|
| 84 | pstr->s = NULL; |
---|
| 85 | pstr->len = 0; |
---|
| 86 | pstr->capacity = 0; |
---|
| 87 | return s; |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | /* Donate a malloc-ed C string. */ |
---|
| 91 | inline void s_donate(string *pstr, char *s) { |
---|
| 92 | osfree(pstr->s); |
---|
| 93 | pstr->s = s; |
---|
| 94 | pstr->capacity = pstr->len = strlen(s); |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | inline int s_len(const string *pstr) { return pstr->len; } |
---|
[30d80d2] | 98 | |
---|
[0532954] | 99 | inline bool s_empty(const string *pstr) { return pstr->len == 0; } |
---|
[99d5754] | 100 | |
---|
[0532954] | 101 | inline const char *s_str(string *pstr) { return pstr->s; } |
---|
[bd2e33a] | 102 | |
---|
[0532954] | 103 | inline bool s_eqlen(const string *pstr, const char *s, int s_len) { |
---|
| 104 | return pstr->len == s_len && memcmp(pstr->s, s, s_len) == 0; |
---|
| 105 | } |
---|
[99d5754] | 106 | |
---|
[0532954] | 107 | inline bool s_eq(const string *pstr, const char *s) { |
---|
| 108 | return strcmp(pstr->s, s) == 0; |
---|
| 109 | } |
---|
[ab7cded] | 110 | |
---|
[0532954] | 111 | #define S_EQ(PSTR, LITERAL) s_eqlen((PSTR), LITERAL, sizeof(LITERAL "") - 1) |
---|
[99d5754] | 112 | |
---|
[0532954] | 113 | #endif // SURVEX_INCLUDED_STR_H |
---|