| [9286525] | 1 | /* append a string */ |
|---|
| [bd2e33a] | 2 | /* Copyright (c) Olly Betts 1999, 2014 |
|---|
| [846746e] | 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 | */ |
|---|
| 18 | |
|---|
| [99d5754] | 19 | #include <string.h> |
|---|
| 20 | |
|---|
| 21 | #include "osalloc.h" |
|---|
| [b6ea3c2] | 22 | #include "str.h" |
|---|
| [99d5754] | 23 | |
|---|
| 24 | /* append a string */ |
|---|
| 25 | void |
|---|
| [8c1d295] | 26 | s_cat(char **pstr, int *plen, const char *s) |
|---|
| [99d5754] | 27 | { |
|---|
| [8c1d295] | 28 | s_catlen(pstr, plen, s, strlen(s)); |
|---|
| [bd2e33a] | 29 | } |
|---|
| 30 | |
|---|
| 31 | /* append a string with length */ |
|---|
| 32 | void |
|---|
| [8c1d295] | 33 | s_catlen(char **pstr, int *plen, const char *s, int s_len) |
|---|
| [bd2e33a] | 34 | { |
|---|
| 35 | int new_len = s_len + 1; /* extra 1 for nul */ |
|---|
| [99d5754] | 36 | int len = 0; |
|---|
| 37 | |
|---|
| 38 | if (*pstr) { |
|---|
| 39 | len = strlen(*pstr); |
|---|
| 40 | new_len += len; |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | if (!*pstr || new_len > *plen) { |
|---|
| 44 | *plen = (new_len + 32) & ~3; |
|---|
| 45 | *pstr = osrealloc(*pstr, *plen); |
|---|
| 46 | } |
|---|
| [cb3d1e2] | 47 | |
|---|
| [bd2e33a] | 48 | memcpy(*pstr + len, s, s_len); |
|---|
| 49 | (*pstr + len)[s_len] = '\0'; |
|---|
| [99d5754] | 50 | } |
|---|
| 51 | |
|---|
| 52 | /* append a character */ |
|---|
| 53 | void |
|---|
| 54 | s_catchar(char **pstr, int *plen, char ch) |
|---|
| 55 | { |
|---|
| 56 | int new_len = 2; /* extra 1 for nul */ |
|---|
| 57 | int len = 0; |
|---|
| 58 | |
|---|
| 59 | if (*pstr) { |
|---|
| 60 | len = strlen(*pstr); |
|---|
| 61 | new_len += len; |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | if (!*pstr || new_len > *plen) { |
|---|
| 65 | *plen = (new_len + 32) & ~3; |
|---|
| 66 | *pstr = osrealloc(*pstr, *plen); |
|---|
| 67 | } |
|---|
| [cb3d1e2] | 68 | |
|---|
| [99d5754] | 69 | (*pstr)[len] = ch; |
|---|
| 70 | (*pstr)[len + 1] = '\0'; |
|---|
| 71 | } |
|---|