1 #ifndef HELPER_MEMBUF_H
2 #define HELPER_MEMBUF_H
6 /** @page MEMBUF - an auto-growing string buffer
8 * With OpenOCD often, one must write code that sends text to
9 * different places.. the historical command_ctx, or JIM output,
10 * and/or other places.
12 * This is a simple 'string buffer' that auto-grows.
14 * More correctly put, this is a "memory buffer"
15 * it may contain binary data
17 * Note: Internally the buffer always has a 'null terminator'
20 /* contents of this structure are 'opaque' */
24 /** Create a new membuf
25 * By default the memory buffer has "some non-zero-size"
26 * (couple hundred bytes, exact amount is opaque)
28 struct membuf
*membuf_new(void);
30 /** delete (destroy) the mem buffer
31 * @param pBuf - buffer to release
33 void membuf_delete(struct membuf
*pBuf
);
36 /** grow/shrink a membuf by specified amount.
37 * @param pBuf - the buffer
38 * @param amount - the amount to grow or shrink by.
40 * Symantics of 'realloc()' return NULL on failure
42 struct membuf
*membuf_grow(struct membuf
*pBuf
, int amount
);
44 /** how long is this buffer (memlen(), strlen())
45 * @param pBuf - the buffer
47 * @returns: length of current buffer.
49 size_t membuf_len(struct membuf
*pBuf
);
52 /** reset an membuf to zero length.
53 * @param pBuf - buffer to reset
55 * Note this does not 'release' the memory buffer
57 void membuf_reset(struct membuf
*pBuf
);
60 /** sprintf() to the string buffer
61 * @param pBuf - buffer to capture sprintf() data into
62 * @param fmt - printf format
64 * Returns 0 on success
65 * Returns non-zero on failure
67 int membuf_sprintf(struct membuf
*pBuf
, const char *fmt
, ... );
69 /** vsprintf() to the string buffer
70 * @param pBuf - buffer to capture sprintf() data into
71 * @param fmt - printf format
72 * @param ap - va_list for fmt
74 * Returns 0 on success
75 * Returns non-zero on failure
77 int membuf_vsprintf(struct membuf
*pBuf
, const char *fmt
, va_list ap
);
79 /** Tokenize lines using strtok()
80 * @param pBuf - buffer to tokenize
81 * @param delim - delimiter parameter for strtok_r()
83 * Identical to "strtok()" - pass "pBuff = NULL" on second call
85 * NOTE: This call is <b > destructive</b> to the buffer.
87 const char *membuf_strtok(struct membuf
*pBuf
, const char *delim
, void **pSave
);
89 /** Return pointer to the memory in the buffer
90 * @param pBuf - buffer
92 * NOTE: Thou shall not modify this pointer, it is <b > CONST</b>
94 const void *membuf_datapointer(struct membuf
*pBuf
);
97 /** Append data to the buffer
98 * @param pBuf - buffer to append
99 * @param pData - pointer to data to append
100 * @param len - length of data to append
102 * Modified symantics of "memcpy()". On memory allocation failure
103 * returns NULL. On success, returns pointer to orginal membuf.
105 struct membuf
*membuf_append(struct membuf
*pBuf
, const void *pData
, size_t len
);
108 /** Append string to the buffer
109 * @param pBuf - buffer to append
110 * @param str - string to append
112 * Modified symantics of "strcat()". On memory allocation failure
113 * returns NULL. On success, returns pointer to orginal membuf.
115 struct membuf
*membuf_strcat(struct membuf
*pBuf
, const char *s
);