1 #if !defined( CSTRING_H ) 2 #define CSTRING_H 3 4 /* 5 * An implementation of expandable c strings in heap memory. 6 * Copyright (C) 2018 Michael Goehler 7 * 8 * This file is part of mdp. 9 * 10 * This program is free software: you can redistribute it and/or modify 11 * it under the terms of the GNU General Public License as published by 12 * the Free Software Foundation, either version 3 of the License, or 13 * (at your option) any later version. 14 * 15 * This program is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU General Public License for more details. 19 * 20 * You should have received a copy of the GNU General Public License 21 * along with this program. If not, see <http://www.gnu.org/licenses/>. 22 * 23 * 24 * struct: cstring_t which defines a expandable c string type in heap memory 25 * 26 * function: cstring_init to intialize struct of type cstring_t 27 * function: cstring_t->expand to add one character to the struct 28 * function: cstring_t->expand_arr to add a string to the struct 29 * function: cstring_t->strip to remove a substring 30 * function: cstring_t->reset to clear and reuse the struct 31 * function: cstring_t->delete to free the allocated memory 32 * 33 * Example: 34 * cstring_t *p = cstring_init(); 35 * (p->expand)(p, 'X'); 36 * (p->delete)(p); 37 * 38 */ 39 40 // The amount of memory allocated from heap when string expansion hits the 41 // allocated memory limit 42 #define REALLOC_ADD 10 43 44 typedef struct _cstring_t { 45 wchar_t *value; 46 size_t size; 47 size_t alloc; 48 void (*expand)(struct _cstring_t *self, wchar_t x); 49 void (*expand_arr)(struct _cstring_t *self, wchar_t *x); 50 void (*strip)(struct _cstring_t *self, int pos, int len); 51 void (*reset)(struct _cstring_t *self); 52 void (*delete)(struct _cstring_t *self); 53 } cstring_t; 54 55 cstring_t *cstring_init(); 56 void cstring_expand(cstring_t *self, wchar_t x); 57 void cstring_expand_arr(cstring_t *self, wchar_t *x); 58 void cstring_strip(cstring_t *self, int pos, int len); 59 void cstring_reset(cstring_t *self); 60 void cstring_delete(cstring_t *self); 61 62 #endif // !defined( CSTRING_H ) 63