1 /* Copyright 2009, UCAR/Unidata and OPeNDAP, Inc.
2    See the COPYRIGHT file for more information. */
3 
4 #ifndef NCBYTES_H
5 #define NCBYTES_H 1
6 
7 #include "ncexternl.h"
8 
9 typedef struct NCbytes {
10   int nonextendible; /* 1 => fail if an attempt is made to extend this buffer*/
11   unsigned long alloc;
12   unsigned long length;
13   char* content;
14 } NCbytes;
15 
16 #if defined(_CPLUSPLUS_) || defined(__CPLUSPLUS__) || defined(__CPLUSPLUS)
17 extern "C" {
18 #endif
19 
20 EXTERNL NCbytes* ncbytesnew(void);
21 EXTERNL void ncbytesfree(NCbytes*);
22 EXTERNL int ncbytessetalloc(NCbytes*,unsigned long);
23 EXTERNL int ncbytessetlength(NCbytes*,unsigned long);
24 EXTERNL int ncbytesfill(NCbytes*, char fill);
25 
26 /* Produce a duplicate of the contents*/
27 EXTERNL char* ncbytesdup(NCbytes*);
28 /* Extract the contents and leave buffer empty */
29 EXTERNL char* ncbytesextract(NCbytes*);
30 
31 /* Return the ith byte; -1 if no such index */
32 EXTERNL int ncbytesget(NCbytes*,unsigned long);
33 /* Set the ith byte */
34 EXTERNL int ncbytesset(NCbytes*,unsigned long,char);
35 
36 /* Append one byte */
37 EXTERNL int ncbytesappend(NCbytes*,char); /* Add at Tail */
38 /* Append n bytes */
39 EXTERNL int ncbytesappendn(NCbytes*,const void*,unsigned long); /* Add at Tail */
40 
41 /* Null terminate the byte string without extending its length (for debugging) */
42 EXTERNL int ncbytesnull(NCbytes*);
43 
44 /* Remove char at position i */
45 EXTERNL int ncbytesremove(NCbytes*,unsigned long);
46 
47 /* Concatenate a null-terminated string to the end of the buffer */
48 EXTERNL int ncbytescat(NCbytes*,const char*);
49 
50 /* Set the contents of the buffer; mark the buffer as non-extendible */
51 EXTERNL int ncbytessetcontents(NCbytes*, char*, unsigned long);
52 
53 /* Following are always "in-lined"*/
54 #define ncbyteslength(bb) ((bb)!=NULL?(bb)->length:0)
55 #define ncbytesalloc(bb) ((bb)!=NULL?(bb)->alloc:0)
56 #define ncbytescontents(bb) (((bb)!=NULL && (bb)->content!=NULL)?(bb)->content:(char*)"")
57 #define ncbytesextend(bb,len) ncbytessetalloc((bb),(len)+(bb->alloc))
58 #define ncbytesclear(bb) ((bb)!=NULL?(bb)->length=0:0)
59 #define ncbytesavail(bb,n) ((bb)!=NULL?((bb)->alloc - (bb)->length) >= (n):0)
60 
61 #if defined(_CPLUSPLUS_) || defined(__CPLUSPLUS__) || defined(__CPLUSPLUS)
62 }
63 #endif
64 
65 #endif /*NCBYTES_H*/
66