ocbytes.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /* Copyright 2009, UCAR/Unidata and OPeNDAP, Inc.
  2. See the COPYRIGHT file for more information. */
  3. #ifndef OCBYTES_H
  4. #define OCBYTES_H 1
  5. typedef struct OCbytes {
  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. } OCbytes;
  11. #if defined(_CPLUSPLUS_) || defined(__CPLUSPLUS__) || defined(__CPLUSPLUS)
  12. #define EXTERNC extern "C"
  13. #else
  14. #define EXTERNC extern
  15. #endif
  16. EXTERNC OCbytes* ocbytesnew(void);
  17. EXTERNC void ocbytesfree(OCbytes*);
  18. EXTERNC int ocbytessetalloc(OCbytes*,unsigned int);
  19. EXTERNC int ocbytessetlength(OCbytes*,unsigned int);
  20. EXTERNC int ocbytesfill(OCbytes*, char fill);
  21. /* Produce a duplicate of the contents*/
  22. EXTERNC char* ocbytesdup(OCbytes*);
  23. /* Extract the contents and leave buffer empty */
  24. EXTERNC char* ocbytesextract(OCbytes*);
  25. /* Return the ith byte; -1 if no such index */
  26. EXTERNC int ocbytesget(OCbytes*,unsigned int);
  27. /* Set the ith byte */
  28. EXTERNC int ocbytesset(OCbytes*,unsigned int,char);
  29. /* Append one byte */
  30. EXTERNC int ocbytesappend(OCbytes*,char); /* Add at Tail */
  31. /* Append n bytes */
  32. EXTERNC int ocbytesappendn(OCbytes*,void*,unsigned int); /* Add at Tail */
  33. /* Concatenate a null-terminated string to the end of the buffer */
  34. EXTERNC int ocbytescat(OCbytes*,char*);
  35. /* Set the contents of the buffer; mark the buffer as non-extendible */
  36. EXTERNC int ocbytessetcontents(OCbytes*, char*, unsigned int);
  37. /* Following are always "in-lined"*/
  38. #define ocbyteslength(bb) ((bb)?(bb)->length:0U)
  39. #define ocbytesalloc(bb) ((bb)?(bb)->alloc:0U)
  40. #define ocbytescontents(bb) ((bb && bb->content)?(bb)->content:(char*)"")
  41. #define ocbytesextend(bb,len) ocbytessetalloc((bb),(len)+(bb->alloc))
  42. #define ocbytesclear(bb) ((bb)?(bb)->length=0:0U)
  43. #define ocbytesavail(bb,n) ((bb)?((bb)->alloc - (bb)->length) >= (n):0U)
  44. #endif /*OCBYTES_H*/