Subversion Repositories QNX 8.QNX8 IFS tool

Rev

Rev 16 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. #ifndef BUFFER_H
  2. #define BUFFER_H
  3.  
  4.  
  5. #ifdef __cplusplus
  6. extern "C" {
  7. #endif
  8.  
  9.  
  10. // standard C includes
  11. #include <stdint.h>
  12. #include <stddef.h>
  13.  
  14.  
  15. // resizeable buffer structure type definition
  16. typedef struct buffer_s
  17. {
  18.    uint8_t *bytes; // buffer data, mallocated
  19.    size_t size; // buffer size in bytes (i.e byte length of data)
  20. } buffer_t;
  21.  
  22.  
  23. // function prototypes
  24. #define Buffer_Initialize(buffer) (memset ((buffer), 0, sizeof (buffer_t)) != NULL)
  25. #define Buffer_InitWithSize(buffer,size) (Buffer_Initialize ((buffer)) && Buffer_Append ((buffer), NULL, (size)))
  26. #define Buffer_InitWithCString(buffer,data_string) (Buffer_Initialize ((buffer)) && Buffer_Append ((buffer), (data_string), strlen ((data_string))))
  27. #define Buffer_InitWithFormattedCString(buffer,fmt_string,...) (Buffer_Initialize ((buffer)) && Buffer_AppendFormattedCString ((buffer), (fmt_string), __VA_ARGS__))
  28. #define Buffer_InitWithByteArray(buffer,data_array) (Buffer_Initialize ((buffer)) && Buffer_Append ((buffer), (data_array), sizeof ((data_array)) - 1)) // WARNING: DO NOT USE WITH TERNARY OPERATORS!
  29. #define Buffer_InitWithData(buffer,data,data_size) (Buffer_Initialize ((buffer)) && Buffer_Append ((buffer), (data), (data_size)))
  30. #define Buffer_InitWithBuffer(buffer,data_buffer) (Buffer_Initialize ((buffer)) && Buffer_Append ((buffer), (data_buffer)->bytes, (data_buffer)->size))
  31. void Buffer_Forget (buffer_t *buffer);
  32. int Buffer_Reset (buffer_t *buffer);
  33. #define Buffer_ResetWithData(buffer,data,data_size) (Buffer_Reset ((buffer)) && Buffer_Append ((buffer), (data), (data_size)))
  34. #define Buffer_ResetWithCString(buffer,data_string) (Buffer_Reset ((buffer)) && Buffer_Append ((buffer), (data_string), strlen ((data_string))))
  35. #define Buffer_ResetWithFormattedCString(buffer,fmt_string,...) (Buffer_Reset ((buffer)) && Buffer_AppendFormattedCString ((buffer), (fmt_string), __VA_ARGS__))
  36. #define Buffer_ResetWithByteArray(buffer,data_array) (Buffer_Reset ((buffer)) && Buffer_Append ((buffer), (data_array), sizeof ((data_array)) - 1)) // WARNING: DO NOT USE WITH TERNARY OPERATORS!
  37. #define Buffer_ResetWithBuffer(buffer,data_buffer) (Buffer_Reset ((buffer)) && Buffer_Append ((buffer), (data_buffer)->bytes, (data_buffer)->size))
  38. int Buffer_Append (buffer_t *buffer, const void *data, const size_t data_size);
  39. #define Buffer_AppendCString(buffer,data_string) Buffer_Append ((buffer), (data_string), strlen ((data_string)))
  40. #define Buffer_AppendByteArray(buffer,data_array) Buffer_Append ((buffer), (data_array), sizeof ((data_array)) - 1) // WARNING: DO NOT USE WITH TERNARY OPERATORS!
  41. #define Buffer_AppendBuffer(buffer,data_buffer) Buffer_Append ((buffer), (data_buffer)->bytes, (data_buffer)->size)
  42. int Buffer_AppendFormattedCString (buffer_t *buffer, const char *fmt_string, ...);
  43. int Buffer_Prepend (buffer_t *buffer, const void *data, const size_t data_size);
  44. #define Buffer_PrependCString(buffer,data_string) Buffer_Prepend ((buffer), (data_string), strlen ((data_string)))
  45. #define Buffer_PrependByteArray(buffer,data_array) Buffer_Prepend ((buffer), (data_array), sizeof ((data_array)) - 1) // WARNING: DO NOT USE WITH TERNARY OPERATORS!
  46. #define Buffer_PrependBuffer(buffer,data_buffer) Buffer_Prepend ((buffer), (data_buffer)->bytes, (data_buffer)->size)
  47. int Buffer_PrependFormattedCString (buffer_t *buffer, const char *fmt_string, ...);
  48. int Buffer_InsertAt (buffer_t *buffer, const size_t insert_index, const void *data, const size_t data_size);
  49. static inline int Buffer_InsertInt8At  (buffer_t *buffer, const size_t insert_index, const uint8_t  int8_data)  { return (Buffer_InsertAt (buffer, insert_index, (const uint8_t *) &int8_data,  1)); }
  50. static inline int Buffer_InsertInt16At (buffer_t *buffer, const size_t insert_index, const uint16_t int16_data) { return (Buffer_InsertAt (buffer, insert_index, (const uint8_t *) &int16_data, 2)); }
  51. static inline int Buffer_InsertInt32At (buffer_t *buffer, const size_t insert_index, const uint32_t int32_data) { return (Buffer_InsertAt (buffer, insert_index, (const uint8_t *) &int32_data, 4)); }
  52. static inline int Buffer_InsertInt64At (buffer_t *buffer, const size_t insert_index, const uint64_t int64_data) { return (Buffer_InsertAt (buffer, insert_index, (const uint8_t *) &int64_data, 8)); }
  53. #define Buffer_InsertCStringAt(buffer,insert_index,data_string) Buffer_InsertAt ((buffer), (insert_index), (data_string), strlen ((data_string)))
  54. #define Buffer_InsertByteArrayAt(buffer,insert_index,data_array) Buffer_InsertAt ((buffer), (insert_index), (data_array), sizeof ((data_array)) - 1) // WARNING: DO NOT USE WITH TERNARY OPERATORS!
  55. #define Buffer_InsertBufferAt(buffer,insert_index,data_buffer) Buffer_InsertAt ((buffer), (insert_index), (data_buffer)->bytes, (data_buffer)->size)
  56. int Buffer_WriteAt (buffer_t *buffer, const size_t write_index, const void *data, const size_t data_size);
  57. static inline int Buffer_WriteInt8At  (buffer_t *buffer, const size_t write_index, const uint8_t  int8_data)  { return (Buffer_WriteAt (buffer, write_index, (const uint8_t *) &int8_data,  1)); }
  58. static inline int Buffer_WriteInt16At (buffer_t *buffer, const size_t write_index, const uint16_t int16_data) { return (Buffer_WriteAt (buffer, write_index, (const uint8_t *) &int16_data, 2)); }
  59. static inline int Buffer_WriteInt32At (buffer_t *buffer, const size_t write_index, const uint32_t int32_data) { return (Buffer_WriteAt (buffer, write_index, (const uint8_t *) &int32_data, 4)); }
  60. static inline int Buffer_WriteInt64At (buffer_t *buffer, const size_t write_index, const uint64_t int64_data) { return (Buffer_WriteAt (buffer, write_index, (const uint8_t *) &int64_data, 8)); }
  61. #define Buffer_WriteCStringAt(buffer,write_index,data_string) Buffer_WriteAt ((buffer), (write_index), (data_string), strlen ((data_string)))
  62. #define Buffer_WriteByteArrayAt(buffer,write_index,data_array) Buffer_WriteAt ((buffer), (write_index), (data_array), sizeof ((data_array)) - 1) // WARNING: DO NOT USE WITH TERNARY OPERATORS!
  63. #define Buffer_WriteBufferAt(buffer,write_index,data_buffer) Buffer_WriteAt ((buffer), (write_index), (data_buffer)->bytes, (data_buffer)->size)
  64. #define Buffer_PadWithZeroesTo(buffer,offset) Buffer_WriteAt ((buffer), (offset), NULL, 0)
  65. int Buffer_SubsetFromTo (buffer_t *buffer, const size_t from_index, const size_t to_index);
  66. #define Buffer_SubsetWithLength(buffer,from_index,length) Buffer_SubsetFromTo ((buffer), (from_index), ((length) > 0 ? (from_index) : (buffer)->size) + (length)) // length > 0 => character index; length == 0 => end of string; length < 0 => end of string minus length
  67. #define Buffer_Subset(buffer,from_index) Buffer_SubsetFromTo ((buffer), (from_index), (buffer)->size - (from_index))
  68. int Buffer_ReadFromFile (buffer_t *buffer, const char *file_pathname);
  69. int Buffer_WriteToFile (buffer_t *buffer, const char *file_pathname);
  70. //#define Buffer_Find(buffer,needle,needle_length) Buffer_FindFirst (buffer, needle, needle_length) // FIXME: DEPRECATED, REMOVE
  71. //#define Buffer_FindCString(buffer,needle_string) Buffer_FindFirst ((buffer), (needle_string), strlen (needle_string)) // FIXME: DEPRECATED, REMOVE
  72. //#define Buffer_FindByteArray(buffer,needle_array) Buffer_FindFirst ((buffer), (needle_array), sizeof (needle_array) - 1) // FIXME: DEPRECATED, REMOVE
  73. //#define Buffer_FindBuffer(buffer,needle_buffer) Buffer_FindFirst ((buffer), (needle_buffer)->bytes, (needle_buffer)->size) // FIXME: DEPRECATED, REMOVE
  74. uint8_t *Buffer_FindFirst (const buffer_t *buffer, const void *needle, const size_t needle_length);
  75. uint8_t *Buffer_FindFirstCI (const buffer_t *buffer, const char *needle);
  76. #define Buffer_FindFirstCString(buffer,needle_string) Buffer_FindFirst ((buffer), (needle_string), strlen ((needle_string)))
  77. #define Buffer_FindFirstByteArray(buffer,needle_array) Buffer_FindFirst ((buffer), (needle_array), sizeof ((needle_array)) - 1) // WARNING: DO NOT USE WITH TERNARY OPERATORS!
  78. #define Buffer_FindFirstBuffer(buffer,needle_buffer) Buffer_FindFirst ((buffer), (needle_buffer)->bytes, (needle_buffer)->size)
  79. uint8_t *Buffer_FindLast (const buffer_t *buffer, const void *needle, const size_t needle_length);
  80. uint8_t *Buffer_FindLastCI (const buffer_t *buffer, const char *needle);
  81. #define Buffer_FindLastCString(buffer,needle_string) Buffer_FindLast ((buffer), (needle_string), strlen ((needle_string)))
  82. #define Buffer_FindLastByteArray(buffer,needle_array) Buffer_FindLast ((buffer), (needle_array), sizeof ((needle_array)) - 1) // WARNING: DO NOT USE WITH TERNARY OPERATORS!
  83. #define Buffer_FindLastBuffer(buffer,needle_buffer) Buffer_FindLast ((buffer), (needle_buffer)->bytes, (needle_buffer)->size)
  84. size_t Buffer_Replace (buffer_t *buffer, const void *needle, const size_t needle_length, const void *replacement, const size_t replacement_length, const int howmany_with_zero_for_all_and_minus_for_backwards);
  85. #define Buffer_ReplaceCStringWithCString(buffer,needle_string,replacement_string,howmany_with_zero_for_all_and_minus_for_backwards) Buffer_Replace ((buffer), (needle_string), strlen ((needle_string)), (replacement_string), strlen ((replacement_string)), (howmany_with_zero_for_all_and_minus_for_backwards))
  86. #define Buffer_ReplaceCStringWithByteArray(buffer,needle_string,replacement_array,howmany_with_zero_for_all_and_minus_for_backwards) Buffer_Replace ((buffer), (needle_string), strlen ((needle_string)), (replacement_array), sizeof ((replacement_array)) - 1, (howmany_with_zero_for_all_and_minus_for_backwards)) // WARNING: DO NOT USE WITH TERNARY OPERATORS!
  87. #define Buffer_ReplaceCStringWithBuffer(buffer,needle_string,replacement_buffer,howmany_with_zero_for_all_and_minus_for_backwards) Buffer_Replace ((buffer), (needle_string), strlen ((needle_string)), (replacement_buffer)->bytes, (replacement_buffer)->size, (howmany_with_zero_for_all_and_minus_for_backwards))
  88. #define Buffer_ReplaceByteArrayWithCString(buffer,needle_array,replacement_string,howmany_with_zero_for_all_and_minus_for_backwards) Buffer_Replace ((buffer), (needle_array), sizeof ((needle_array)) - 1, (replacement_string), strlen ((replacement_string)), (howmany_with_zero_for_all_and_minus_for_backwards)) // WARNING: DO NOT USE WITH TERNARY OPERATORS!
  89. #define Buffer_ReplaceByteArrayWithByteArray(buffer,needle_array,replacement_array,howmany_with_zero_for_all_and_minus_for_backwards) Buffer_Replace ((buffer), (needle_array), sizeof ((needle_array)) - 1, (replacement_array), sizeof ((replacement_array)) - 1, (howmany_with_zero_for_all_and_minus_for_backwards)) // WARNING: DO NOT USE WITH TERNARY OPERATORS!
  90. #define Buffer_ReplaceByteArrayWithBuffer(buffer,needle_array,replacement_buffer,howmany_with_zero_for_all_and_minus_for_backwards) Buffer_Replace ((buffer), (needle_array), sizeof ((needle_array)) - 1, (replacement_buffer)->bytes, (replacement_buffer)->size, (howmany_with_zero_for_all_and_minus_for_backwards)) // WARNING: DO NOT USE WITH TERNARY OPERATORS!
  91. #define Buffer_ReplaceBufferWithCString(buffer,needle_buffer,replacement_string,howmany_with_zero_for_all_and_minus_for_backwards) Buffer_Replace ((buffer), (needle_buffer)->bytes, ((needle_buffer))->size, (replacement_string), strlen ((replacement_string)), (howmany_with_zero_for_all_and_minus_for_backwards))
  92. #define Buffer_ReplaceBufferWithByteArray(buffer,needle_buffer,replacement_array,howmany_with_zero_for_all_and_minus_for_backwards) Buffer_Replace ((buffer), (needle_buffer)->bytes, ((needle_buffer))->size, (replacement_array), sizeof ((replacement_array)) - 1, (howmany_with_zero_for_all_and_minus_for_backwards)) // WARNING: DO NOT USE WITH TERNARY OPERATORS!
  93. #define Buffer_ReplaceBufferWithBuffer(buffer,needle_buffer,replacement_buffer,howmany_with_zero_for_all_and_minus_for_backwards) Buffer_Replace ((buffer), (needle_buffer)->bytes, ((needle_buffer))->size, (replacement_buffer)->bytes, (replacement_buffer)->size, (howmany_with_zero_for_all_and_minus_for_backwards))
  94. int Buffer_Compare (buffer_t *buffer, const void *data, const size_t data_size);
  95. #define Buffer_CompareWithCString(buffer,comparable_string) Buffer_Compare ((buffer), (comparable_string), strlen ((comparable_string)))
  96. #define Buffer_CompareWithByteArray(buffer,comparable_array) Buffer_Compare ((buffer), (comparable_array), sizeof ((comparable_array)) - 1) // WARNING: DO NOT USE WITH TERNARY OPERATORS!
  97. #define Buffer_CompareWithBuffer(buffer,comparable_buffer) Buffer_Compare ((buffer), (comparable_buffer)->bytes, (comparable_buffer)->size)
  98. #define Buffer_BeginsWith(buffer,data,data_size) (((buffer)->size >= (data_size)) && (memcmp ((buffer)->bytes, (data), (data_size)) == 0))
  99. #define Buffer_BeginsWithCString(buffer,data_string) (((buffer)->size >= strlen ((data_string))) && (memcmp ((buffer)->bytes, (data_string), strlen ((data_string))) == 0))
  100. #define Buffer_BeginsWithByteArray(buffer,data_array) (((buffer)->size >= sizeof ((data_array)) - 1) && (memcmp ((buffer)->bytes, (data_array), sizeof ((data_array)) - 1) == 0)) // WARNING: DO NOT USE WITH TERNARY OPERATORS!
  101. #define Buffer_EndsWith(buffer,data,data_size) (((buffer)->size >= (data_size)) && (memcmp (&(buffer)->bytes[(buffer)->size - (data_size)], (data), (data_size)) == 0))
  102. #define Buffer_EndsWithCString(buffer,data_string) (((buffer)->size >= strlen ((data_string))) && (memcmp (&(buffer)->bytes[(buffer)->size - strlen ((data_string))], (data_string), strlen ((data_string))) == 0))
  103. #define Buffer_EndsWithByteArray(buffer,data_array) (((buffer)->size >= sizeof ((data_array)) - 1) && (memcmp (&(buffer)->bytes[(buffer)->size - (sizeof ((data_array)) - 1)], (data_array), sizeof ((data_array)) - 1) == 0)) // WARNING: DO NOT USE WITH TERNARY OPERATORS!
  104. size_t Buffer_OffsetOf (buffer_t *buffer, const void *something);
  105. int Buffer_TrimLeftUntil (buffer_t *buffer, const uint8_t *trimmable_bytes, const size_t trimmable_count, const int howmany);
  106. #define Buffer_TrimLeft(buffer,trimmable_data,trimmable_size) Buffer_TrimLeftUntil (buffer, trimmable_data, trimmable_size, 0)
  107. #define Buffer_TrimLeftCString(buffer,trimmable_string) Buffer_TrimLeft ((buffer), (trimmable_string), strlen ((trimmable_string)))
  108. #define Buffer_TrimLeftCString(buffer,trimmable_string) Buffer_TrimLeft ((buffer), (trimmable_string), strlen ((trimmable_string)))
  109. #define Buffer_TrimLeftByteArray(buffer,trimmable_array) Buffer_TrimLeft ((buffer), (trimmable_array), sizeof ((trimmable_array)) - 1) // WARNING: DO NOT USE WITH TERNARY OPERATORS!
  110. int Buffer_TrimRightUntil (buffer_t *buffer, const uint8_t *trimmable_bytes, const size_t trimmable_count, const int howmany);
  111. #define Buffer_TrimRight(buffer,trimmable_data,trimmable_size) Buffer_TrimRightUntil ((buffer), (trimmable_data), (trimmable_size), 0)
  112. #define Buffer_TrimRightCString(buffer,trimmable_string) Buffer_TrimRight ((buffer), (trimmable_string), strlen ((trimmable_string)))
  113. #define Buffer_TrimRightByteArray(buffer,trimmable_array) Buffer_TrimRight ((buffer), (trimmable_array), sizeof ((trimmable_array)) - 1) // WARNING: DO NOT USE WITH TERNARY OPERATORS!
  114. #define Buffer_Trim(buffer,trimmable_bytes,trimmable_count) (Buffer_TrimRight ((buffer), (trimmable_bytes), (trimmable_count)) & Buffer_TrimLeft ((buffer), (trimmable_bytes), (trimmable_count)))
  115. #define Buffer_TrimCString(buffer,trimmable_string) (Buffer_TrimRight ((buffer), (trimmable_string), strlen ((trimmable_string))) & Buffer_TrimLeft ((buffer), (trimmable_string), strlen ((trimmable_string))))
  116. #define Buffer_TrimByteArray(buffer,trimmable_array) (Buffer_TrimRight ((buffer), (trimmable_array), sizeof ((trimmable_array)) - 1) & Buffer_TrimLeft ((buffer), (trimmable_array), sizeof ((trimmable_array)) - 1)) // WARNING: DO NOT USE WITH TERNARY OPERATORS!
  117. int Buffer_GetNthParts (buffer_t *buffer, const size_t wanted_part_index, const int howmany, const void *separator, const size_t separator_len, const int should_parse_as_text, buffer_t *preinitialized_outbuf);
  118. #define Buffer_GetNthLine(buffer,line_index,preinitialized_outbuf) Buffer_GetNthParts ((buffer), (line_index), 1, "\n", 1, 1, (preinitialized_outbuf))
  119.  
  120.  
  121. #ifdef __cplusplus
  122. }
  123. #endif
  124.  
  125.  
  126. #endif // BUFFER_H
  127.