Enlightenment CVS committal

Author  : pfritz
Project : e17
Module  : libs/ecore

Dir     : e17/libs/ecore/src/lib/ecore


Modified Files:
        Ecore_Data.h ecore_strbuf.c 


Log Message:
add ecore_strbuf_insert() and ecore_strbuf_length_get()

===================================================================
RCS file: /cvs/e/e17/libs/ecore/src/lib/ecore/Ecore_Data.h,v
retrieving revision 1.34
retrieving revision 1.35
diff -u -3 -r1.34 -r1.35
--- Ecore_Data.h        16 Feb 2007 23:49:55 -0000      1.34
+++ Ecore_Data.h        21 Feb 2007 04:31:50 -0000      1.35
@@ -481,11 +481,13 @@
 
 
 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
 }
===================================================================
RCS file: /cvs/e/e17/libs/ecore/src/lib/ecore/ecore_strbuf.c,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -3 -r1.1 -r1.2
--- ecore_strbuf.c      16 Feb 2007 23:49:55 -0000      1.1
+++ ecore_strbuf.c      21 Feb 2007 04:31:50 -0000      1.2
@@ -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 @@
 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,11 +77,57 @@
 
         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
  * @param c the char to append
@@ -115,3 +161,15 @@
   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;
+}
+



-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
enlightenment-cvs mailing list
enlightenment-cvs@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/enlightenment-cvs

Reply via email to