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);
|
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);
|
Ecore_Strbuf * ecore_strbuf_new(void);
|
||||||
void ecore_strbuf_free(Ecore_Strbuf *buf);
|
void ecore_strbuf_free(Ecore_Strbuf *buf);
|
||||||
void ecore_strbuf_append(Ecore_Strbuf *buf, const char *str);
|
void ecore_strbuf_append(Ecore_Strbuf *buf, const char *str);
|
||||||
void ecore_strbuf_append_char(Ecore_Strbuf *buf, char c);
|
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);
|
const char * ecore_strbuf_string_get(Ecore_Strbuf *buf);
|
||||||
|
size_t ecore_strbuf_length_get(Ecore_Strbuf *buf);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,9 +10,9 @@
|
||||||
struct _ecore_strbuf
|
struct _ecore_strbuf
|
||||||
{
|
{
|
||||||
char *buf;
|
char *buf;
|
||||||
int len;
|
size_t len;
|
||||||
int size;
|
size_t size;
|
||||||
int step;
|
size_t step;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -56,12 +56,12 @@ ecore_strbuf_free(Ecore_Strbuf *buf)
|
||||||
void
|
void
|
||||||
ecore_strbuf_append(Ecore_Strbuf *buf, const char *str)
|
ecore_strbuf_append(Ecore_Strbuf *buf, const char *str)
|
||||||
{
|
{
|
||||||
|
size_t l;
|
||||||
|
size_t off = 0;
|
||||||
|
|
||||||
CHECK_PARAM_POINTER("buf", buf);
|
CHECK_PARAM_POINTER("buf", buf);
|
||||||
CHECK_PARAM_POINTER("str", str);
|
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)
|
||||||
|
@ -81,6 +81,52 @@ ecore_strbuf_append(Ecore_Strbuf *buf, const char *str)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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.
|
* Append a character to a string buffer, reallocating as necessary.
|
||||||
* @param buf the Ecore_Strbuf to append to
|
* @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);
|
CHECK_PARAM_POINTER_RETURN("buf", buf, NULL);
|
||||||
return buf->buf;
|
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