| 1 | /* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB |
| 2 | 2016 MariaDB Corporation AB |
| 3 | |
| 4 | This library is free software; you can redistribute it and/or |
| 5 | modify it under the terms of the GNU Library General Public |
| 6 | License as published by the Free Software Foundation; either |
| 7 | version 2 of the License, or (at your option) any later version. |
| 8 | |
| 9 | This library 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 GNU |
| 12 | Library General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU Library General Public |
| 15 | License along with this library; if not, write to the Free |
| 16 | Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 17 | MA 02111-1301, USA */ |
| 18 | |
| 19 | /* |
| 20 | Code for handling strings which can grow dynamically. |
| 21 | Copyright Monty Program KB. |
| 22 | By monty. |
| 23 | */ |
| 24 | |
| 25 | #include <ma_global.h> |
| 26 | #include <ma_sys.h> |
| 27 | #include <ma_string.h> |
| 28 | |
| 29 | my_bool ma_init_dynamic_string(DYNAMIC_STRING *str, const char *init_str, |
| 30 | size_t init_alloc, size_t alloc_increment) |
| 31 | { |
| 32 | uint length; |
| 33 | |
| 34 | if (!alloc_increment) |
| 35 | alloc_increment=128; |
| 36 | length=1; |
| 37 | if (init_str && (length= (uint) strlen(init_str)+1) < init_alloc) |
| 38 | init_alloc=((length+alloc_increment-1)/alloc_increment)*alloc_increment; |
| 39 | if (!init_alloc) |
| 40 | init_alloc=alloc_increment; |
| 41 | |
| 42 | if (!(str->str=(char*) malloc(init_alloc))) |
| 43 | return(TRUE); |
| 44 | str->length=length-1; |
| 45 | if (init_str) |
| 46 | memcpy(str->str,init_str,length); |
| 47 | str->max_length=init_alloc; |
| 48 | str->alloc_increment=alloc_increment; |
| 49 | return(FALSE); |
| 50 | } |
| 51 | |
| 52 | my_bool ma_dynstr_set(DYNAMIC_STRING *str, const char *init_str) |
| 53 | { |
| 54 | uint length; |
| 55 | |
| 56 | if (init_str && (length= (uint) strlen(init_str)+1) > str->max_length) |
| 57 | { |
| 58 | str->max_length=((length+str->alloc_increment-1)/str->alloc_increment)* |
| 59 | str->alloc_increment; |
| 60 | if (!str->max_length) |
| 61 | str->max_length=str->alloc_increment; |
| 62 | if (!(str->str=(char*) realloc(str->str,str->max_length))) |
| 63 | return(TRUE); |
| 64 | } |
| 65 | if (init_str) |
| 66 | { |
| 67 | str->length=length-1; |
| 68 | memcpy(str->str,init_str,length); |
| 69 | } |
| 70 | else |
| 71 | str->length=0; |
| 72 | return(FALSE); |
| 73 | } |
| 74 | |
| 75 | |
| 76 | my_bool ma_dynstr_realloc(DYNAMIC_STRING *str, size_t additional_size) |
| 77 | { |
| 78 | if (!additional_size) return(FALSE); |
| 79 | if (str->length + additional_size > str->max_length) |
| 80 | { |
| 81 | str->max_length=((str->length + additional_size+str->alloc_increment-1)/ |
| 82 | str->alloc_increment)*str->alloc_increment; |
| 83 | if (!(str->str=(char*) realloc(str->str,str->max_length))) |
| 84 | return(TRUE); |
| 85 | } |
| 86 | return(FALSE); |
| 87 | } |
| 88 | |
| 89 | |
| 90 | my_bool ma_dynstr_append(DYNAMIC_STRING *str, const char *append) |
| 91 | { |
| 92 | return ma_dynstr_append_mem(str,append,strlen(append)); |
| 93 | } |
| 94 | |
| 95 | |
| 96 | my_bool ma_dynstr_append_mem(DYNAMIC_STRING *str, const char *append, |
| 97 | size_t length) |
| 98 | { |
| 99 | char *new_ptr; |
| 100 | if (str->length+length >= str->max_length) |
| 101 | { |
| 102 | size_t new_length=(str->length+length+str->alloc_increment)/ |
| 103 | str->alloc_increment; |
| 104 | new_length*=str->alloc_increment; |
| 105 | if (!(new_ptr=(char*) realloc(str->str,new_length))) |
| 106 | return TRUE; |
| 107 | str->str=new_ptr; |
| 108 | str->max_length=new_length; |
| 109 | } |
| 110 | memcpy(str->str + str->length,append,length); |
| 111 | str->length+=length; |
| 112 | str->str[str->length]=0; /* Safety for C programs */ |
| 113 | return FALSE; |
| 114 | } |
| 115 | |
| 116 | |
| 117 | void ma_dynstr_free(DYNAMIC_STRING *str) |
| 118 | { |
| 119 | if (str->str) |
| 120 | { |
| 121 | free(str->str); |
| 122 | str->str=0; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | char *ma_strmake(register char *dst, register const char *src, size_t length) |
| 127 | { |
| 128 | while (length--) |
| 129 | if (! (*dst++ = *src++)) |
| 130 | return dst-1; |
| 131 | *dst=0; |
| 132 | return dst; |
| 133 | } |
| 134 | |