File: bytebuffer.c

package info (click to toggle)
netcdf 1%3A4.4.1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 96,828 kB
  • ctags: 15,369
  • sloc: ansic: 163,650; sh: 9,294; yacc: 2,457; makefile: 1,208; lex: 1,161; xml: 173; f90: 7; fortran: 6; awk: 2
file content (279 lines) | stat: -rw-r--r-- 5,672 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/* Copyright 2009, UCAR/Unidata and OPeNDAP, Inc.
   See the COPYRIGHT file for more information. */

#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "bytebuffer.h"
#include "debug.h"

#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif

#define DEFAULTALLOC 1024
#define ALLOCINCR 1024

int bbdebug = 1;

/* For debugging purposes*/
static long
bbFail(void)
{
    fflush(stdout);
    fprintf(stderr,"bytebuffer failure\n");
    fflush(stderr);
    if(bbdebug) exit(1);
    return FALSE;
}

Bytebuffer*
bbNew(void)
{
  Bytebuffer* bb = (Bytebuffer*)emalloc(sizeof(Bytebuffer));
  if(bb == NULL) return (Bytebuffer*)bbFail();
  bb->alloc=0;
  bb->length=0;
  bb->content=NULL;
  bb->nonextendible = 0;
  return bb;
}

int
bbSetalloc(Bytebuffer* bb, const unsigned int sz0)
{
  unsigned int sz = sz0;
  char* newcontent;
  if(bb == NULL) return bbFail();
  if(sz <= 0) {sz = (bb->alloc?2*bb->alloc:DEFAULTALLOC);}
  else if(bb->alloc >= sz) return TRUE;
  else if(bb->nonextendible) return bbFail();
  newcontent=(char*)ecalloc(sz,sizeof(char));
  if(bb->alloc > 0 && bb->length > 0 && bb->content != NULL) {
    memcpy((void*)newcontent,(void*)bb->content,sizeof(char)*bb->length);
  }
  if(bb->content != NULL) efree(bb->content);
  bb->content=newcontent;
  bb->alloc=sz;
  return TRUE;
}

void
bbFree(Bytebuffer* bb)
{
  if(bb == NULL) return;
  if(bb->content != NULL) efree(bb->content);
  efree(bb);
}

int
bbSetlength(Bytebuffer* bb, const unsigned int sz)
{
  if(bb == NULL) return bbFail();
  if(bb->length < sz) {
      if(!bbSetalloc(bb,sz)) return bbFail();
  }
  bb->length = sz;
  return TRUE;
}

int
bbFill(Bytebuffer* bb, const char fill)
{
  unsigned int i;
  if(bb == NULL) return bbFail();
  for(i=0;i<bb->length;i++) bb->content[i] = fill;
  return TRUE;
}

int
bbGet(Bytebuffer* bb, unsigned int index)
{
  if(bb == NULL) return -1;
  if(index >= bb->length) return -1;
  return bb->content[index];
}

int
bbSet(Bytebuffer* bb, unsigned int index, char elem)
{
  if(bb == NULL) return bbFail();
  if(index >= bb->length) return bbFail();
  bb->content[index] = elem;
  return TRUE;
}

int
bbAppend(Bytebuffer* bb, char elem)
{
  if(bb == NULL) return bbFail();
  /* We need space for the char + null */
  while(bb->length+1 >= bb->alloc) {
    if(!bbSetalloc(bb,0))
      return bbFail();
  }
  bb->content[bb->length] = elem;
  bb->length++;
  bb->content[bb->length] = '\0';
  return TRUE;
}

/* This assumes s is a null terminated string*/
int
bbCat(Bytebuffer* bb, const char* s)
{
    bbAppendn(bb,(void*)s,strlen(s)+1); /* include trailing null*/
    /* back up over the trailing null*/
    if(bb->length == 0) return bbFail();
    bb->length--;
    return 1;
}

int
bbCatbuf(Bytebuffer* bb, const Bytebuffer* s)
{
    if(bbLength(s) > 0)
	bbAppendn(bb,bbContents(s),bbLength(s));
    bbNull(bb);
    return 1;
}

int
bbAppendn(Bytebuffer* bb, const void* elem, const unsigned int n0)
{
  unsigned int n = n0;
  if(bb == NULL || elem == NULL) return bbFail();
  if(n == 0) {n = strlen((char*)elem);}
  while(!bbNeed(bb,(n+1))) {if(!bbSetalloc(bb,0)) return bbFail();}
  memcpy((void*)&bb->content[bb->length],(void*)elem,n);
  bb->length += n;
  bb->content[bb->length] = '\0';
  return TRUE;
}

int
bbInsert(Bytebuffer* bb, const unsigned int index, const char elem)
{
  char tmp[2];
  tmp[0]=elem;
  return bbInsertn(bb,index,tmp,1);
}

int
bbInsertn(Bytebuffer* bb, const unsigned int index, const char* elem, const unsigned int n)
{
  unsigned int i;
  int j;
  unsigned int newlen = 0;

  if(bb == NULL) return bbFail();

  newlen = bb->length + n;

  if(newlen >= bb->alloc) {
    if(!bbExtend(bb,n)) return bbFail();
  }
  /*
index=0
n=3
len=3
newlen=6
a b c
x y z a b c
-----------
0 1 2 3 4 5

i=0 1 2
j=5 4 3
  2 1 0
*/
  for(j=newlen-1,i=index;i<bb->length;i++) {
    bb->content[j]=bb->content[j-n];
  }
  memcpy((void*)(bb->content+index),(void*)elem,n);
  bb->length += n;
  return TRUE;
}

/*! Pop head off of a byte buffer.
 *
 * @param Bytebuffer bb Pointer to Bytebuffer.
 * @param char* pelem pointer to location for head element.
 *
 * @return Returns TRUE on success.
 */
int bbHeadpop(Bytebuffer* bb, char* pelem)
{
  if(bb == NULL) return bbFail();
  if(bb->length == 0) return bbFail();
  *pelem = bb->content[0];
  memmove((void*)&bb->content[0],
          (void*)&bb->content[1],
          sizeof(char)*(bb->length - 1));
  bb->length--;
  return TRUE;
}

int
bbTailpop(Bytebuffer* bb, char* pelem)
{
  if(bb == NULL) return bbFail();
  if(bb->length == 0) return bbFail();
  *pelem = bb->content[bb->length-1];
  bb->length--;
  return TRUE;
}

int
bbHeadpeek(Bytebuffer* bb, char* pelem)
{
  if(bb == NULL) return bbFail();
  if(bb->length == 0) return bbFail();
  *pelem = bb->content[0];
  return TRUE;
}

int
bbTailpeek(Bytebuffer* bb, char* pelem)
{
  if(bb == NULL) return bbFail();
  if(bb->length == 0) return bbFail();
  *pelem = bb->content[bb->length - 1];
  return TRUE;
}

char*
bbDup(const Bytebuffer* bb)
{
    char* result = (char*)emalloc(bb->length+1);
    memcpy((void*)result,(const void*)bb->content,bb->length);
    result[bb->length] = '\0'; /* just in case it is a string*/
    return result;
}

int
bbSetcontents(Bytebuffer* bb, char* contents, const unsigned int alloc)
{
    if(bb == NULL) return bbFail();
    bbClear(bb);
    if(!bb->nonextendible && bb->content != NULL) efree(bb->content);
    bb->content = contents;
    bb->length = 0;
    bb->alloc = alloc;
    bb->nonextendible = 1;
    return 1;
}

/* Add invisible NULL terminator */
int
bbNull(Bytebuffer* bb)
{
    bbAppend(bb,'\0');
    bb->length--;
    return 1;
}