| 1 | /* append a string */ |
|---|
| 2 | /* Copyright (c) Olly Betts 1999, 2014, 2024 |
|---|
| 3 | * |
|---|
| 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. |
|---|
| 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 |
|---|
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 12 | * GNU General Public License for more details. |
|---|
| 13 | * |
|---|
| 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 |
|---|
| 16 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
|---|
| 17 | */ |
|---|
| 18 | |
|---|
| 19 | #include <config.h> |
|---|
| 20 | |
|---|
| 21 | #include "str.h" |
|---|
| 22 | |
|---|
| 23 | #include <string.h> |
|---|
| 24 | |
|---|
| 25 | #include "osalloc.h" |
|---|
| 26 | |
|---|
| 27 | void s_expand_(string *pstr, int addition) { |
|---|
| 28 | int new_size = (pstr->len + addition + 33) & ~7; |
|---|
| 29 | pstr->s = osrealloc(pstr->s, new_size); |
|---|
| 30 | pstr->capacity = new_size - 1; |
|---|
| 31 | } |
|---|
| 32 | |
|---|
| 33 | void |
|---|
| 34 | s_appendlen(string* pstr, const char *s, int s_len) |
|---|
| 35 | { |
|---|
| 36 | if (pstr->capacity - pstr->len < s_len || s_len == 0) |
|---|
| 37 | s_expand_(pstr, s_len); |
|---|
| 38 | memcpy(pstr->s + pstr->len, s, s_len); |
|---|
| 39 | pstr->len += s_len; |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | void |
|---|
| 43 | s_appendn(string *pstr, int n, char c) |
|---|
| 44 | { |
|---|
| 45 | if (pstr->capacity - pstr->len < n || n == 0) |
|---|
| 46 | s_expand_(pstr, n); |
|---|
| 47 | memset(pstr->s + pstr->len, c, n); |
|---|
| 48 | pstr->len += n; |
|---|
| 49 | } |
|---|