~ubuntu-branches/ubuntu/precise/openwalnut/precise

« back to all changes in this revision

Viewing changes to src/modules/data/ext/libeep/libeep/var_string.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastian Eichelbaum
  • Date: 2011-06-21 10:26:54 UTC
  • Revision ID: james.westby@ubuntu.com-20110621102654-rq0zf436q949biih
Tags: upstream-1.2.5
ImportĀ upstreamĀ versionĀ 1.2.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/********************************************************************************
 
2
 *                                                                              *
 
3
 * this file is part of:                                                        *
 
4
 * libeep, the project for reading and writing avr/cnt eeg and related files    *
 
5
 *                                                                              *
 
6
 ********************************************************************************
 
7
 *                                                                              *
 
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  *
 
11
 *                                                                              *
 
12
 ********************************************************************************
 
13
 *                                                                              *
 
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.                                          *
 
18
 *                                                                              *
 
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.                          *
 
22
 *                                                                              *
 
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/>          *
 
25
 *                                                                              *
 
26
 *******************************************************************************/
 
27
 
 
28
#include <stdlib.h>
 
29
#include <string.h>
 
30
#include <eep/var_string.h>
 
31
 
 
32
/* Create a new instance of simple_string.
 
33
   The string will be empty */
 
34
var_string varstr_construct()
 
35
{
 
36
  var_string s = (var_string) malloc(sizeof(struct var_string_s));
 
37
  if (NULL != s)
 
38
  {
 
39
    s->s = (char*) malloc(256 * sizeof(char));
 
40
    if (NULL == s->s)
 
41
    {
 
42
      free(s);
 
43
      s = NULL;
 
44
    }
 
45
    else
 
46
    {
 
47
      s->num_alloc = 256;
 
48
      s->length = 0;
 
49
      s->s[0] = '\0';
 
50
    }
 
51
  }
 
52
  return s;
 
53
}
 
54
 
 
55
/* Delete a string instance */
 
56
void varstr_destruct(var_string s)
 
57
{
 
58
  if (NULL != s)
 
59
  {
 
60
    if (NULL != s->s)
 
61
      free(s->s);
 
62
    free(s);
 
63
  }
 
64
}
 
65
 
 
66
/* Append a string to the existing string */
 
67
int varstr_append(var_string s, const char* app)
 
68
{
 
69
  long needed = s->num_alloc;
 
70
  long added_len = strlen(app);
 
71
 
 
72
  while (s->length + added_len >= needed)
 
73
    needed*=2;
 
74
 
 
75
  if (needed > s->num_alloc)
 
76
  { /* We have to alloc more space */
 
77
    char* new_s = (char*)realloc(s->s, needed);
 
78
    if (NULL == new_s)
 
79
      return 0; /* Can not enlarge string; the original should
 
80
                still be OK though */
 
81
    s->s = new_s; /* We now have twice as much space */
 
82
    s->num_alloc = needed;
 
83
  }
 
84
  /* Now append the string */
 
85
  strcat(s->s, app);
 
86
  s->length += added_len;
 
87
  return 1;
 
88
}
 
89
 
 
90
/* Set the string to this value */
 
91
 
 
92
int varstr_set(var_string s, const char* newstr)
 
93
{
 
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)
 
98
    new_alloc*=2;
 
99
  if (new_alloc > s->num_alloc)
 
100
  {
 
101
    char *new_s = (char*)realloc(s->s, new_alloc);
 
102
    if (NULL == new_s)
 
103
      return 0;
 
104
    s->s = new_s;
 
105
    s->num_alloc = new_alloc;
 
106
  }
 
107
  strcpy(s->s, newstr);
 
108
  s->length = new_len;
 
109
  return 1;
 
110
}
 
111
 
 
112
/* Get the 'char*' representation of this string */
 
113
 
 
114
const char* varstr_cstr(var_string s)
 
115
{
 
116
  return s ? s->s : NULL;
 
117
}
 
118
 
 
119
/* Returns the current length of the string */
 
120
long varstr_length(var_string s)
 
121
{
 
122
  return s ? s->length : -1;
 
123
}