1
/********************************************************************************
3
* this file is part of: *
4
* libeep, the project for reading and writing avr/cnt eeg and related files *
6
********************************************************************************
8
* LICENSE:Copyright (c) 2003-2009, *
9
* Advanced Neuro Technology (ANT) B.V., Enschede, The Netherlands *
10
* Max-Planck Institute for Human Cognitive & Brain Sciences, Leipzig, Germany *
12
********************************************************************************
14
* This library is free software; you can redistribute it and/or modify *
15
* it under the terms of the GNU Lesser General Public License as published by *
16
* the Free Software Foundation; either version 3 of the License, or *
17
* (at your option) any later version. *
19
* This library is distributed WITHOUT ANY WARRANTY; even the implied warranty *
20
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
21
* GNU Lesser General Public License for more details. *
23
* You should have received a copy of the GNU Lesser General Public License *
24
* along with this program. If not, see <http://www.gnu.org/licenses/> *
26
*******************************************************************************/
30
#include <eep/var_string.h>
32
/* Create a new instance of simple_string.
33
The string will be empty */
34
var_string varstr_construct()
36
var_string s = (var_string) malloc(sizeof(struct var_string_s));
39
s->s = (char*) malloc(256 * sizeof(char));
55
/* Delete a string instance */
56
void varstr_destruct(var_string s)
66
/* Append a string to the existing string */
67
int varstr_append(var_string s, const char* app)
69
long needed = s->num_alloc;
70
long added_len = strlen(app);
72
while (s->length + added_len >= needed)
75
if (needed > s->num_alloc)
76
{ /* We have to alloc more space */
77
char* new_s = (char*)realloc(s->s, needed);
79
return 0; /* Can not enlarge string; the original should
81
s->s = new_s; /* We now have twice as much space */
82
s->num_alloc = needed;
84
/* Now append the string */
86
s->length += added_len;
90
/* Set the string to this value */
92
int varstr_set(var_string s, const char* newstr)
94
long new_len = strlen(newstr);
95
/* round to nearest power of 2 for optimal allocs */
96
long new_alloc = s->num_alloc;
97
while (new_alloc < new_len)
99
if (new_alloc > s->num_alloc)
101
char *new_s = (char*)realloc(s->s, new_alloc);
105
s->num_alloc = new_alloc;
107
strcpy(s->s, newstr);
112
/* Get the 'char*' representation of this string */
114
const char* varstr_cstr(var_string s)
116
return s ? s->s : NULL;
119
/* Returns the current length of the string */
120
long varstr_length(var_string s)
122
return s ? s->length : -1;