/src/netcdf-c/libdispatch/ncbytes.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Copyright 2018, UCAR/Unidata and OPeNDAP, Inc. |
2 | | See the COPYRIGHT file for more information. */ |
3 | | |
4 | | #include <stdlib.h> |
5 | | #include <stdio.h> |
6 | | #include <string.h> |
7 | | |
8 | | #include "ncbytes.h" |
9 | | |
10 | | #ifndef TRUE |
11 | 27 | #define TRUE 1 |
12 | | #endif |
13 | | #ifndef FALSE |
14 | 0 | #define FALSE 0 |
15 | | #endif |
16 | | |
17 | 0 | #define DEFAULTALLOC 1024 |
18 | | #define ALLOCINCR 1024 |
19 | | |
20 | | #define NCBYTESDEBUG 1 |
21 | | |
22 | | static int |
23 | | ncbytesfail(void) |
24 | 0 | { |
25 | 0 | fflush(stdout); |
26 | 0 | fprintf(stderr,"bytebuffer failure\n"); |
27 | 0 | fflush(stderr); |
28 | 0 | #ifdef NCBYTESDEBUG |
29 | 0 | abort(); |
30 | 0 | #endif |
31 | 0 | return FALSE; |
32 | 0 | } |
33 | | |
34 | | NCbytes* |
35 | | ncbytesnew(void) |
36 | 90 | { |
37 | 90 | NCbytes* bb = (NCbytes*)malloc(sizeof(NCbytes)); |
38 | 90 | if(bb == NULL) return (ncbytesfail(),NULL); |
39 | 90 | bb->alloc=0; |
40 | 90 | bb->length=0; |
41 | 90 | bb->content=NULL; |
42 | 90 | bb->nonextendible = 0; |
43 | 90 | return bb; |
44 | 90 | } |
45 | | |
46 | | int |
47 | | ncbytessetalloc(NCbytes* bb, unsigned long sz) |
48 | 12 | { |
49 | 12 | char* newcontent; |
50 | 12 | if(bb == NULL) return ncbytesfail(); |
51 | 12 | if(sz == 0) {sz = (bb->alloc?2*bb->alloc:DEFAULTALLOC);} |
52 | 12 | if(bb->alloc >= sz) return TRUE; |
53 | 9 | if(bb->nonextendible) return ncbytesfail(); |
54 | 9 | newcontent=(char*)calloc(sz,sizeof(char)); |
55 | 9 | if(newcontent == NULL) ncbytesfail(); |
56 | 9 | if(bb->alloc > 0 && bb->length > 0 && bb->content != NULL) { |
57 | 8 | memcpy((void*)newcontent,(void*)bb->content,sizeof(char)*bb->length); |
58 | 8 | } |
59 | 9 | if(bb->content != NULL) free(bb->content); |
60 | 9 | bb->content=newcontent; |
61 | 9 | bb->alloc=sz; |
62 | 9 | return TRUE; |
63 | 9 | } |
64 | | |
65 | | EXTERNL void |
66 | | ncbytesfree(NCbytes* bb) |
67 | 90 | { |
68 | 90 | if(bb == NULL) return; |
69 | 90 | if(!bb->nonextendible && bb->content != NULL) free(bb->content); |
70 | 90 | free(bb); |
71 | 90 | } |
72 | | |
73 | | int |
74 | | ncbytessetlength(NCbytes* bb, unsigned long sz) |
75 | 0 | { |
76 | 0 | if(bb == NULL) return ncbytesfail(); |
77 | 0 | if(bb->length < sz) { |
78 | 0 | if(sz > bb->alloc) {if(!ncbytessetalloc(bb,sz)) return ncbytesfail();} |
79 | 0 | } |
80 | 0 | bb->length = sz; |
81 | 0 | return TRUE; |
82 | 0 | } |
83 | | |
84 | | int |
85 | | ncbytesfill(NCbytes* bb, char fill) |
86 | 0 | { |
87 | 0 | unsigned long i; |
88 | 0 | if(bb == NULL) return ncbytesfail(); |
89 | 0 | for(i=0;i<bb->length;i++) bb->content[i] = fill; |
90 | 0 | return TRUE; |
91 | 0 | } |
92 | | |
93 | | int |
94 | | ncbytesget(NCbytes* bb, unsigned long index) |
95 | 0 | { |
96 | 0 | if(bb == NULL) return -1; |
97 | 0 | if(index >= bb->length) return -1; |
98 | 0 | return bb->content[index]; |
99 | 0 | } |
100 | | |
101 | | int |
102 | | ncbytesset(NCbytes* bb, unsigned long index, char elem) |
103 | 0 | { |
104 | 0 | if(bb == NULL) return ncbytesfail(); |
105 | 0 | if(index >= bb->length) return ncbytesfail(); |
106 | 0 | bb->content[index] = elem; |
107 | 0 | return TRUE; |
108 | 0 | } |
109 | | |
110 | | int |
111 | | ncbytesappend(NCbytes* bb, char elem) |
112 | 3 | { |
113 | 3 | char s[2]; |
114 | 3 | if(bb == NULL) return ncbytesfail(); |
115 | 3 | s[0] = elem; |
116 | 3 | s[1] = '\0'; |
117 | 3 | ncbytesappendn(bb,s,1); |
118 | 3 | return TRUE; |
119 | 3 | } |
120 | | |
121 | | /* This assumes s is a null terminated string*/ |
122 | | int |
123 | | ncbytescat(NCbytes* bb, const char* s) |
124 | 9 | { |
125 | 9 | if(s == NULL) { |
126 | 0 | return 1; |
127 | 0 | } |
128 | 9 | ncbytesappendn(bb,(void*)s,strlen(s)+1); /* include trailing null*/ |
129 | | /* back up over the trailing null*/ |
130 | 9 | if(bb->length == 0) return ncbytesfail(); |
131 | 9 | bb->length--; |
132 | 9 | return 1; |
133 | 9 | } |
134 | | |
135 | | int |
136 | | ncbytesappendn(NCbytes* bb, const void* elem, unsigned long n) |
137 | 12 | { |
138 | 12 | if(bb == NULL || elem == NULL) return ncbytesfail(); |
139 | 12 | if(n == 0) {n = strlen((char*)elem);} |
140 | 12 | ncbytessetalloc(bb,bb->length+n+1); |
141 | 12 | memcpy((void*)&bb->content[bb->length],(void*)elem,n); |
142 | 12 | bb->length += n; |
143 | 12 | bb->content[bb->length] = '\0'; |
144 | 12 | return TRUE; |
145 | 12 | } |
146 | | |
147 | | int |
148 | | ncbytesprepend(NCbytes* bb, char elem) |
149 | 0 | { |
150 | 0 | int i; /* do not make unsigned */ |
151 | 0 | if(bb == NULL) return ncbytesfail(); |
152 | 0 | if(bb->length >= bb->alloc) if(!ncbytessetalloc(bb,0)) return ncbytesfail(); |
153 | | /* could we trust memcpy? instead */ |
154 | 0 | for(i=(int)bb->alloc;i>=1;i--) {bb->content[i]=bb->content[i-1];} |
155 | 0 | bb->content[0] = elem; |
156 | 0 | bb->length++; |
157 | 0 | return TRUE; |
158 | 0 | } |
159 | | |
160 | | char* |
161 | | ncbytesdup(NCbytes* bb) |
162 | 0 | { |
163 | 0 | char* result = (char*)malloc(bb->length+1); |
164 | 0 | memcpy((void*)result,(const void*)bb->content,bb->length); |
165 | 0 | result[bb->length] = '\0'; /* just in case it is a string*/ |
166 | 0 | return result; |
167 | 0 | } |
168 | | |
169 | | char* |
170 | | ncbytesextract(NCbytes* bb) |
171 | 1 | { |
172 | 1 | char* result = bb->content; |
173 | 1 | bb->alloc = 0; |
174 | 1 | bb->length = 0; |
175 | 1 | bb->content = NULL; |
176 | 1 | return result; |
177 | 1 | } |
178 | | |
179 | | int |
180 | | ncbytessetcontents(NCbytes* bb, void* contents, unsigned long alloc) |
181 | 0 | { |
182 | 0 | if(bb == NULL) return ncbytesfail(); |
183 | 0 | ncbytesclear(bb); |
184 | 0 | if(!bb->nonextendible && bb->content != NULL) free(bb->content); |
185 | 0 | bb->content = (char*)contents; |
186 | 0 | bb->length = alloc; |
187 | 0 | bb->alloc = alloc; |
188 | 0 | bb->nonextendible = 1; |
189 | 0 | return 1; |
190 | 0 | } |
191 | | |
192 | | /* Null terminate the byte string without extending its length */ |
193 | | int |
194 | | ncbytesnull(NCbytes* bb) |
195 | 1 | { |
196 | 1 | ncbytesappend(bb,'\0'); |
197 | 1 | bb->length--; |
198 | 1 | return 1; |
199 | 1 | } |
200 | | |
201 | | /* Remove char at position i */ |
202 | | int |
203 | | ncbytesremove(NCbytes* bb, unsigned long pos) |
204 | 0 | { |
205 | 0 | if(bb == NULL) return ncbytesfail(); |
206 | 0 | if(bb->length <= pos) return ncbytesfail(); |
207 | 0 | if(pos < (bb->length - 1)) { |
208 | 0 | int copylen = (bb->length - pos) - 1; |
209 | 0 | memmove(bb->content+pos,bb->content+pos+1,copylen); |
210 | 0 | } |
211 | 0 | bb->length--; |
212 | 0 | return TRUE; |
213 | 0 | } |