/** * @brief Steal the contents of a string buffer. * * @param buf The string buffer to steal. * @return The current string in the string buffer. * * This function returns the string contained in @p buf. @p buf is * then initialized and does not own the returned string anymore. The * caller must release the memory of the returned string by calling * free(). * * @see eina_strbuf_common_string_get() */ void *eina_strbuf_common_string_steal(size_t csize, Eina_Strbuf * buf) { void *ret; ret = buf->buf; // TODO: Check return value and do something clever _eina_strbuf_common_init(csize, buf); return ret; }
/** * @internal * @brief Create a new string buffer. * * @param csize the character size * @return Newly allocated string buffer instance. * * This function creates a new string buffer. On error, @c NULL is * returned. To free the resources, use eina_strbuf_common_free(). * * @see eina_strbuf_common_free() * @see eina_strbuf_common_append() * @see eina_strbuf_common_string_get() */ Eina_Strbuf * eina_strbuf_common_new(size_t csize) { Eina_Strbuf *buf; buf = calloc(1, sizeof(Eina_Strbuf)); if (EINA_UNLIKELY(!buf)) return NULL; if (EINA_UNLIKELY(!_eina_strbuf_common_init(csize, buf))) { eina_strbuf_common_free(buf); return NULL; } return buf; }
/** * @internal * @brief Reset a string buffer. * * @param csize the character size * @param buf The string buffer to reset. * * This function reset @p buf: the buffer len is set to 0, and the * string is set to '\\0'. No memory is free'd. */ void eina_strbuf_common_reset(size_t csize, Eina_Strbuf *buf) { /* This is a read only buffer which need change to be made */ if (buf->ro) { _eina_strbuf_common_init(csize, buf); buf->ro = EINA_FALSE; return ; } buf->len = 0; buf->step = EINA_STRBUF_INIT_STEP; memset(buf->buf, 0, csize); }
/** * @brief Create a new string buffer. * * @return Newly allocated string buffer instance. * * This function creates a new string buffer. On error, @c NULL is * returned and Eina error is set to #EINA_ERROR_OUT_OF_MEMORY. To * free the resources, use eina_strbuf_common_free(). * * @see eina_strbuf_common_free() * @see eina_strbuf_common_append() * @see eina_strbuf_common_string_get() */ Eina_Strbuf *eina_strbuf_common_new(size_t csize) { Eina_Strbuf *buf; eina_error_set(0); buf = malloc(sizeof(Eina_Strbuf)); if (EINA_UNLIKELY(!buf)) { eina_error_set(EINA_ERROR_OUT_OF_MEMORY); return NULL; } if (EINA_UNLIKELY(!_eina_strbuf_common_init(csize, buf))) { eina_strbuf_common_free(buf); return NULL; } return buf; }
/** * @internal * @brief Steal the contents of a string buffer. * * @param csize the character size * @param buf The string buffer to steal. * @return The current string in the string buffer. * * This function returns the string contained in @p buf. @p buf is * then initialized and does not own the returned string anymore. The * caller must release the memory of the returned string by calling * free(). * * @see eina_strbuf_common_string_get() */ void * eina_strbuf_common_string_steal(size_t csize, Eina_Strbuf *buf) { void *ret; // If the buffer is ro, the caller would have to do additional // test to detect if it is the same string or not. Let's make // life for everyone easy. if (buf->ro) { char *dest; dest = malloc(buf->size); if (!dest) return 0; memcpy(dest, buf->buf, buf->len); buf->buf = dest; } ret = buf->buf; // TODO: Check return value and do something clever _eina_strbuf_common_init(csize, buf); return ret; }
/** * @brief Remove a slice of the given string buffer. * * @param buf The string buffer to remove a slice. * @param start The initial (inclusive) slice position to start * removing, in bytes. * @param end The final (non-inclusive) slice position to finish * removing, in bytes. * @return #EINA_TRUE on success, #EINA_FALSE on failure. * * This function removes a slice of @p buf, starting at @p start * (inclusive) and ending at @p end (non-inclusive). Both values are * in bytes. It returns #EINA_FALSE on failure, #EINA_TRUE otherwise. */ Eina_Bool eina_strbuf_common_remove(size_t csize, Eina_Strbuf * buf, size_t start, size_t end) { size_t remove_len, tail_len; if (end >= buf->len) end = buf->len; if (end <= start) return EINA_TRUE; remove_len = end - start; if (remove_len == buf->len) { free(buf->buf); return _eina_strbuf_common_init(csize, buf); } tail_len = buf->len - end + 1; /* includes '\0' */ memmove(buf->buf + (start * csize), buf->buf + (end * csize), tail_len * csize); buf->len -= remove_len; return _eina_strbuf_common_resize(csize, buf, buf->len); }
/** * @internal * @brief Remove a slice of the given string buffer. * * @param csize the character size * @param buf The string buffer to remove a slice. * @param start The initial (inclusive) slice position to start * removing, in bytes. * @param end The final (non-inclusive) slice position to finish * removing, in bytes. * @return #EINA_TRUE on success, #EINA_FALSE on failure. * * This function removes a slice of @p buf, starting at @p start * (inclusive) and ending at @p end (non-inclusive). Both values are * in bytes. It returns #EINA_FALSE on failure, #EINA_TRUE otherwise. */ Eina_Bool eina_strbuf_common_remove(size_t csize, Eina_Strbuf *buf, size_t start, size_t end) { size_t remove_len, tail_len; if (end >= buf->len) end = buf->len; if (end <= start) return EINA_TRUE; /* This is a read only buffer which need change to be made */ if (buf->ro) { char *dest; dest = malloc(buf->size); if (!dest) return 0; memcpy(dest, buf->buf, buf->len); buf->buf = dest; } remove_len = end - start; if (remove_len == buf->len) { free(buf->buf); return _eina_strbuf_common_init(csize, buf); } tail_len = buf->len - end + 1; /* includes '\0' */ memmove(((unsigned char *)(buf->buf)) + (start * csize), ((unsigned char *)(buf->buf)) + (end * csize), tail_len * csize); buf->len -= remove_len; return _eina_strbuf_common_resize(csize, buf, buf->len); }
/** * @brief Free the contents of a string buffer but not the buffer. * * @param buf The string buffer to free the string of. * * This function frees the string contained in @p buf without freeing * @p buf. */ void eina_strbuf_common_string_free(size_t csize, Eina_Strbuf * buf) { free(buf->buf); _eina_strbuf_common_init(csize, buf); }