diff --git a/legacy/eina/ChangeLog b/legacy/eina/ChangeLog index 7d261b3324..6b5d7d0bc8 100644 --- a/legacy/eina/ChangeLog +++ b/legacy/eina/ChangeLog @@ -159,3 +159,9 @@ 2011-12-07 Mike Blumenkrantz (discomfitor/zmike) * eina_log*level_check() functions now return the correct value + +2011-12-08 Tom Hacohen + + * Binbuf + Strbuf + Ustrbuf: Added eina_*buf_manage_new_length. + Same as eina_(u)strbuf_manage_new except that it accepts a length + parameter. diff --git a/legacy/eina/src/include/eina_binbuf.h b/legacy/eina/src/include/eina_binbuf.h index 92f788e354..7c3524bf20 100644 --- a/legacy/eina/src/include/eina_binbuf.h +++ b/legacy/eina/src/include/eina_binbuf.h @@ -48,6 +48,24 @@ typedef struct _Eina_Strbuf Eina_Binbuf; */ EAPI Eina_Binbuf *eina_binbuf_new(void) EINA_MALLOC EINA_WARN_UNUSED_RESULT; +/** + * @brief Create a new string buffer using the passed string. The passed + * string is used directly as the buffer, it's somehow the opposite function of + * @ref eina_binbuf_string_steal . The passed string must be malloced. + * + * @param str the string to manage + * @param length the length of the string. + * @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_binbuf_free(). + * + * @see eina_binbuf_manage_new() + * @since 1.2.0 + */ +EAPI Eina_Binbuf *eina_binbuf_manage_new_length(unsigned char *str, size_t length) EINA_MALLOC EINA_WARN_UNUSED_RESULT; + /** * @brief Free a string buffer. * diff --git a/legacy/eina/src/include/eina_strbuf.h b/legacy/eina/src/include/eina_strbuf.h index 7043575cd8..34c200fb77 100644 --- a/legacy/eina/src/include/eina_strbuf.h +++ b/legacy/eina/src/include/eina_strbuf.h @@ -98,6 +98,24 @@ EAPI Eina_Strbuf *eina_strbuf_new(void) EINA_MALLOC EINA_WARN_UNUSED_RESULT; */ EAPI Eina_Strbuf *eina_strbuf_manage_new(char *str) EINA_MALLOC EINA_WARN_UNUSED_RESULT; +/** + * @brief Create a new string buffer using the passed string. The passed + * string is used directly as the buffer, it's somehow the opposite function of + * @ref eina_strbuf_string_steal . The passed string must be malloced. + * + * @param str the string to manage + * @param length the length of the string. + * @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_free(). + * + * @see eina_strbuf_manage_new() + * @since 1.2.0 + */ +EAPI Eina_Strbuf *eina_strbuf_manage_new_length(char *str, size_t length) EINA_MALLOC EINA_WARN_UNUSED_RESULT; + /** * @brief Free a string buffer. * diff --git a/legacy/eina/src/include/eina_ustrbuf.h b/legacy/eina/src/include/eina_ustrbuf.h index f68cb7b219..9710c4273a 100644 --- a/legacy/eina/src/include/eina_ustrbuf.h +++ b/legacy/eina/src/include/eina_ustrbuf.h @@ -65,7 +65,25 @@ EAPI Eina_UStrbuf *eina_ustrbuf_new(void) EINA_MALLOC EINA_WARN_UNUSED_RESULT; * @see eina_ustrbuf_string_get() * @since 1.1.0 */ -EAPI Eina_Strbuf *eina_ustrbuf_manage_new(Eina_Unicode *str) EINA_MALLOC EINA_WARN_UNUSED_RESULT; +EAPI Eina_UStrbuf *eina_ustrbuf_manage_new(Eina_Unicode *str) EINA_MALLOC EINA_WARN_UNUSED_RESULT; + +/** + * @brief Create a new string buffer using the passed string. The passed + * string is used directly as the buffer, it's somehow the opposite function of + * @ref eina_ustrbuf_string_steal . The passed string must be malloced. + * + * @param str the string to manage + * @param length the length of the string. + * @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_ustrbuf_free(). + * + * @see eina_ustrbuf_manage_new() + * @since 1.2.0 + */ +EAPI Eina_UStrbuf *eina_ustrbuf_manage_new_length(Eina_Unicode *str, size_t length) EINA_MALLOC EINA_WARN_UNUSED_RESULT; /** * @brief Free a string buffer. diff --git a/legacy/eina/src/lib/eina_binbuf_template_c.x b/legacy/eina/src/lib/eina_binbuf_template_c.x index 613a715601..7e0c539f9a 100644 --- a/legacy/eina/src/lib/eina_binbuf_template_c.x +++ b/legacy/eina/src/lib/eina_binbuf_template_c.x @@ -65,6 +65,15 @@ _FUNC_EXPAND(new)(void) return buf; } +EAPI _STRBUF_STRUCT_NAME * +_FUNC_EXPAND(manage_new_length)(_STRBUF_DATA_TYPE *str, size_t length) +{ + _STRBUF_STRUCT_NAME *buf = + eina_strbuf_common_manage_new(_STRBUF_CSIZE, (void *) str, length); + EINA_MAGIC_SET(buf, _STRBUF_MAGIC); + return buf; +} + EAPI void _FUNC_EXPAND(free)(_STRBUF_STRUCT_NAME *buf) { diff --git a/legacy/eina/src/tests/eina_test_binbuf.c b/legacy/eina/src/tests/eina_test_binbuf.c index 713e078c54..90332c161a 100644 --- a/legacy/eina/src/tests/eina_test_binbuf.c +++ b/legacy/eina/src/tests/eina_test_binbuf.c @@ -98,6 +98,30 @@ START_TEST(binbuf_remove) } END_TEST +START_TEST(binbuf_manage_simple) +{ + Eina_Binbuf *buf; + const unsigned char cbuf[] = "12\0 456 78\0 abcthis is some more random junk here!"; + size_t size = sizeof(cbuf) - 1; /* We don't care about the real NULL */ + + eina_init(); + + buf = eina_binbuf_manage_new_length(cbuf, size); + fail_if(!buf); + + fail_if(memcmp(eina_binbuf_string_get(buf), cbuf, size)); + fail_if(size != eina_binbuf_length_get(buf)); + eina_binbuf_append_length(buf, cbuf, size); + fail_if(memcmp(eina_binbuf_string_get(buf), cbuf, size)); + fail_if(memcmp(eina_binbuf_string_get(buf) + size, cbuf, size)); + fail_if(2 * size != eina_binbuf_length_get(buf)); + + eina_binbuf_free(buf); + + eina_shutdown(); +} +END_TEST + START_TEST(binbuf_insert) { #if 0