forked from enlightenment/efl
add ecore_strbuf_insert() and ecore_strbuf_length_get()
SVN revision: 28415
This commit is contained in:
parent
47dac6602d
commit
376e84e228
|
@ -480,12 +480,14 @@ extern "C" {
|
|||
EAPI int ecore_tree_set_free_cb(Ecore_Tree * tree, Ecore_Free_Cb free_func);
|
||||
|
||||
|
||||
Ecore_Strbuf * ecore_strbuf_new(void);
|
||||
Ecore_Strbuf * ecore_strbuf_new(void);
|
||||
void ecore_strbuf_free(Ecore_Strbuf *buf);
|
||||
void ecore_strbuf_append(Ecore_Strbuf *buf, const char *str);
|
||||
void ecore_strbuf_append_char(Ecore_Strbuf *buf, char c);
|
||||
void ecore_strbuf_insert(Ecore_Strbuf *buf, const char *str, size_t pos);
|
||||
#define ecore_strbuf_prepend(buf, str) ecore_strbuf_insert(buf, str, 0)
|
||||
const char * ecore_strbuf_string_get(Ecore_Strbuf *buf);
|
||||
size_t ecore_strbuf_length_get(Ecore_Strbuf *buf);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
|
|
@ -10,9 +10,9 @@
|
|||
struct _ecore_strbuf
|
||||
{
|
||||
char *buf;
|
||||
int len;
|
||||
int size;
|
||||
int step;
|
||||
size_t len;
|
||||
size_t size;
|
||||
size_t step;
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -56,15 +56,15 @@ ecore_strbuf_free(Ecore_Strbuf *buf)
|
|||
void
|
||||
ecore_strbuf_append(Ecore_Strbuf *buf, const char *str)
|
||||
{
|
||||
size_t l;
|
||||
size_t off = 0;
|
||||
|
||||
CHECK_PARAM_POINTER("buf", buf);
|
||||
CHECK_PARAM_POINTER("str", str);
|
||||
|
||||
int l;
|
||||
int off = 0;
|
||||
l = ecore_strlcpy(buf->buf + buf->len, str, buf->size - buf->len);
|
||||
|
||||
l = ecore_strlcpy(buf->buf + buf->len, str, buf->size - buf->len);
|
||||
|
||||
while (l > buf->size - buf->len)
|
||||
while (l > buf->size - buf->len)
|
||||
{
|
||||
/* we successfully appended this much */
|
||||
off += buf->size - buf->len - 1;
|
||||
|
@ -77,10 +77,56 @@ ecore_strbuf_append(Ecore_Strbuf *buf, const char *str)
|
|||
|
||||
l = ecore_strlcpy(buf->buf + buf->len, str + off, buf->size - buf->len);
|
||||
}
|
||||
buf->len += l;
|
||||
buf->len += l;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Insert a string to a buffer, reallocating as necessary.
|
||||
* @param buf the Ecore_Strbuf to insert
|
||||
* @param str the string to insert
|
||||
* @param pos the position to insert the string
|
||||
*/
|
||||
void
|
||||
ecore_strbuf_insert(Ecore_Strbuf *buf, const char *str, size_t pos)
|
||||
{
|
||||
size_t len;
|
||||
size_t new_size;
|
||||
|
||||
CHECK_PARAM_POINTER("buf", buf);
|
||||
CHECK_PARAM_POINTER("str", str);
|
||||
|
||||
if (pos >= buf->len)
|
||||
{
|
||||
ecore_strbuf_append(buf, str);
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
* resize the buffer if necessary
|
||||
*/
|
||||
len = strlen(str);
|
||||
new_size = buf->size;
|
||||
while (len + buf->len > new_size)
|
||||
{
|
||||
new_size += buf->step;
|
||||
if (buf->step < ECORE_STRBUF_MAX_STEP)
|
||||
buf->step *= 2;
|
||||
}
|
||||
|
||||
if (new_size != buf->size)
|
||||
{
|
||||
buf->size = new_size;
|
||||
buf->buf = realloc(buf->buf, buf->size);
|
||||
}
|
||||
/* move the existing text */
|
||||
memmove(buf->buf + len + pos, buf->buf + pos, buf->len - pos);
|
||||
/* and now insert the given string */
|
||||
strncpy(buf->buf + pos, str, len);
|
||||
buf->len += len;
|
||||
buf->buf[buf->len] = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Append a character to a string buffer, reallocating as necessary.
|
||||
* @param buf the Ecore_Strbuf to append to
|
||||
|
@ -115,3 +161,15 @@ ecore_strbuf_string_get(Ecore_Strbuf *buf)
|
|||
CHECK_PARAM_POINTER_RETURN("buf", buf, NULL);
|
||||
return buf->buf;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the length of the string buffer content
|
||||
* @param buf the buffer
|
||||
*/
|
||||
size_t
|
||||
ecore_strbuf_length_get(Ecore_Strbuf *buf)
|
||||
{
|
||||
CHECK_PARAM_POINTER_RETURN("buf", buf, 0);
|
||||
return buf->len;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue