ncbytes.h 1.9 KB

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