source: XIOS/dev/dev_olga/extern/src_netcdf4/ocbytes.c @ 1620

Last change on this file since 1620 was 409, checked in by ymipsl, 11 years ago

Add improved nectdf internal library src

YM

  • Property svn:eol-style set to native
File size: 4.1 KB
Line 
1/* Copyright 2009, UCAR/Unidata and OPeNDAP, Inc.
2   See the COPYRIGHT file for more information. */
3
4#include "config.h"
5
6#include <stdlib.h>
7#include <stdio.h>
8#include <string.h>
9
10#include "ocbytes.h"
11
12#ifndef TRUE
13#define TRUE 1
14#endif
15#ifndef FALSE
16#define FALSE 0
17#endif
18
19#define DEFAULTALLOC 1024
20#define ALLOCINCR 1024
21
22static int ocbytesdebug = 1;
23
24static long
25ocbytesfail(void)
26{
27    fflush(stdout);
28    fprintf(stderr,"bytebuffer failure\n");
29    fflush(stderr);
30    if(ocbytesdebug) abort();
31    return FALSE;
32}
33
34OCbytes*
35ocbytesnew(void)
36{
37  OCbytes* bb = (OCbytes*)malloc(sizeof(OCbytes));
38  if(bb == NULL) return (OCbytes*)ocbytesfail();
39  bb->alloc=0;
40  bb->length=0;
41  bb->content=NULL;
42  bb->nonextendible = 0;
43  return bb;
44}
45
46int
47ocbytessetalloc(OCbytes* bb, unsigned int sz)
48{
49  char* newcontent;
50  if(bb == NULL) return ocbytesfail();
51  if(sz <= 0) {sz = (bb->alloc?2*bb->alloc:DEFAULTALLOC);}
52  if(bb->alloc >= sz) return TRUE;
53  if(bb->nonextendible) return ocbytesfail();
54  newcontent=(char*)calloc(sz,sizeof(char));
55  if(newcontent == NULL) return FALSE;
56  if(bb->alloc > 0 && bb->length > 0 && bb->content != NULL) {
57    memcpy((void*)newcontent,(void*)bb->content,sizeof(char)*bb->length);
58  }
59  if(bb->content != NULL) free(bb->content);
60  bb->content=newcontent;
61  bb->alloc=sz;
62  return TRUE;
63}
64
65void
66ocbytesfree(OCbytes* bb)
67{
68  if(bb == NULL) return;
69  if(!bb->nonextendible && bb->content != NULL) free(bb->content);
70  free(bb);
71}
72
73int
74ocbytessetlength(OCbytes* bb, unsigned int sz)
75{
76  if(bb == NULL) return ocbytesfail();
77  if(sz > bb->alloc) {if(!ocbytessetalloc(bb,sz)) return ocbytesfail();}
78  bb->length = sz;
79  return TRUE;
80}
81
82int
83ocbytesfill(OCbytes* bb, char fill)
84{
85  unsigned int i;
86  if(bb == NULL) return ocbytesfail();
87  for(i=0;i<bb->length;i++) bb->content[i] = fill;
88  return TRUE;
89}
90
91int
92ocbytesget(OCbytes* bb, unsigned int index)
93{
94  if(bb == NULL) return -1;
95  if(index >= bb->length) return -1;
96  return bb->content[index];
97}
98
99int
100ocbytesset(OCbytes* bb, unsigned int index, char elem)
101{
102  if(bb == NULL) return ocbytesfail();
103  if(index >= bb->length) return ocbytesfail();
104  bb->content[index] = elem;
105  return TRUE;
106}
107
108int
109ocbytesappend(OCbytes* bb, char elem)
110{
111  if(bb == NULL) return ocbytesfail();
112  /* We need space for the char + null */
113  while(bb->length+1 >= bb->alloc) {
114        if(!ocbytessetalloc(bb,0)) return ocbytesfail();
115  }
116  bb->content[bb->length] = elem;
117  bb->length++;
118  bb->content[bb->length] = '\0';
119  return TRUE;
120}
121
122/* This assumes s is a null terminated string*/
123int
124ocbytescat(OCbytes* bb, char* s)
125{
126    ocbytesappendn(bb,(void*)s,strlen(s)+1); /* include trailing null*/
127    /* back up over the trailing null*/
128    if(bb->length == 0) return ocbytesfail();
129    bb->length--;
130    return 1;
131}
132
133int
134ocbytesappendn(OCbytes* bb, void* elem, unsigned int n)
135{
136  if(bb == NULL || elem == NULL) return ocbytesfail();
137  if(n == 0) {n = strlen((char*)elem);}
138  while(!ocbytesavail(bb,n+1)) {
139    if(!ocbytessetalloc(bb,0)) return ocbytesfail();
140  }
141  memcpy((void*)&bb->content[bb->length],(void*)elem,n);
142  bb->length += n;
143  bb->content[bb->length] = '\0';
144  return TRUE;
145}
146
147int
148ocbytesprepend(OCbytes* bb, char elem)
149{
150  int i; /* do not make unsigned */
151  if(bb == NULL) return ocbytesfail();
152  if(bb->length >= bb->alloc) if(!ocbytessetalloc(bb,0)) return ocbytesfail();
153  /* could we trust memcpy? instead */
154  for(i=bb->alloc;i>=1;i--) {bb->content[i]=bb->content[i-1];}
155  bb->content[0] = elem;
156  bb->length++;
157  return TRUE;
158}
159
160char*
161ocbytesdup(OCbytes* bb)
162{
163    char* result = (char*)malloc(bb->length+1);
164    memcpy((void*)result,(const void*)bb->content,bb->length);
165    result[bb->length] = '\0'; /* just in case it is a string*/
166    return result;
167}
168
169char*
170ocbytesextract(OCbytes* bb)
171{
172    char* result = bb->content;
173    bb->alloc = 0;
174    bb->length = 0;
175    bb->content = NULL;
176    return result;
177}
178
179int
180ocbytessetcontents(OCbytes* bb, char* contents, unsigned int alloc)
181{
182    if(bb == NULL) return ocbytesfail();
183    ocbytesclear(bb);
184    if(!bb->nonextendible && bb->content != NULL) free(bb->content);
185    bb->content = contents;
186    bb->length = 0;
187    bb->alloc = alloc;
188    bb->nonextendible = 1;
189    return 1;
190}
Note: See TracBrowser for help on using the repository browser.