]> code.delx.au - pulseaudio/blobdiff - src/pulsecore/strbuf.c
core-util: ensure that we chmod only the dir we ourselves created
[pulseaudio] / src / pulsecore / strbuf.c
index ca88c59ff0e6ddb85f2c2fa5a5b105e1134f5721..4fc82ded2ee5ff02c6e87ef65cd0fac2e9f247fa 100644 (file)
@@ -1,5 +1,3 @@
-/* $Id$ */
-
 /***
   This file is part of PulseAudio.
 
 /***
   This file is part of PulseAudio.
 
@@ -7,7 +5,7 @@
 
   PulseAudio is free software; you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License as published
 
   PulseAudio is free software; you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License as published
-  by the Free Software Foundation; either version 2 of the License,
+  by the Free Software Foundation; either version 2.1 of the License,
   or (at your option) any later version.
 
   PulseAudio is distributed in the hope that it will be useful, but
   or (at your option) any later version.
 
   PulseAudio is distributed in the hope that it will be useful, but
@@ -50,17 +48,18 @@ struct pa_strbuf {
 };
 
 pa_strbuf *pa_strbuf_new(void) {
 };
 
 pa_strbuf *pa_strbuf_new(void) {
-    
-    pa_strbuf *sb = pa_xnew(pa_strbuf, 1);
+    pa_strbuf *sb;
+
+    sb = pa_xnew(pa_strbuf, 1);
     sb->length = 0;
     sb->head = sb->tail = NULL;
     sb->length = 0;
     sb->head = sb->tail = NULL;
-    
+
     return sb;
 }
 
 void pa_strbuf_free(pa_strbuf *sb) {
     pa_assert(sb);
     return sb;
 }
 
 void pa_strbuf_free(pa_strbuf *sb) {
     pa_assert(sb);
-    
+
     while (sb->head) {
         struct chunk *c = sb->head;
         sb->head = sb->head->next;
     while (sb->head) {
         struct chunk *c = sb->head;
         sb->head = sb->head->next;
@@ -75,13 +74,13 @@ void pa_strbuf_free(pa_strbuf *sb) {
 char *pa_strbuf_tostring(pa_strbuf *sb) {
     char *t, *e;
     struct chunk *c;
 char *pa_strbuf_tostring(pa_strbuf *sb) {
     char *t, *e;
     struct chunk *c;
-    
+
     pa_assert(sb);
 
     pa_assert(sb);
 
-    e = t = pa_xnew(char, sb->length+1);
+    e = t = pa_xmalloc(sb->length+1);
 
     for (c = sb->head; c; c = c->next) {
 
     for (c = sb->head; c; c = c->next) {
-        assert((size_t) (e-t) <= sb->length);
+        pa_assert((size_t) (e-t) <= sb->length);
         memcpy(e, CHUNK_TO_TEXT(c), c->length);
         e += c->length;
     }
         memcpy(e, CHUNK_TO_TEXT(c), c->length);
         e += c->length;
     }
@@ -89,7 +88,7 @@ char *pa_strbuf_tostring(pa_strbuf *sb) {
     /* Trailing NUL */
     *e = 0;
 
     /* Trailing NUL */
     *e = 0;
 
-    assert(e == t+sb->length);
+    pa_assert(e == t+sb->length);
 
     return t;
 }
 
     return t;
 }
@@ -97,23 +96,30 @@ char *pa_strbuf_tostring(pa_strbuf *sb) {
 /* Combination of pa_strbuf_free() and pa_strbuf_tostring() */
 char *pa_strbuf_tostring_free(pa_strbuf *sb) {
     char *t;
 /* Combination of pa_strbuf_free() and pa_strbuf_tostring() */
 char *pa_strbuf_tostring_free(pa_strbuf *sb) {
     char *t;
-    
+
     pa_assert(sb);
     t = pa_strbuf_tostring(sb);
     pa_strbuf_free(sb);
     pa_assert(sb);
     t = pa_strbuf_tostring(sb);
     pa_strbuf_free(sb);
-    
+
     return t;
 }
 
 /* Append a string to the string buffer */
 void pa_strbuf_puts(pa_strbuf *sb, const char *t) {
     return t;
 }
 
 /* Append a string to the string buffer */
 void pa_strbuf_puts(pa_strbuf *sb, const char *t) {
-    
+
     pa_assert(sb);
     pa_assert(t);
     pa_assert(sb);
     pa_assert(t);
-    
+
     pa_strbuf_putsn(sb, t, strlen(t));
 }
 
     pa_strbuf_putsn(sb, t, strlen(t));
 }
 
+/* Append a character to the string buffer */
+void pa_strbuf_putc(pa_strbuf *sb, char c) {
+    pa_assert(sb);
+
+    pa_strbuf_putsn(sb, &c, 1);
+}
+
 /* Append a new chunk to the linked list */
 static void append(pa_strbuf *sb, struct chunk *c) {
     pa_assert(sb);
 /* Append a new chunk to the linked list */
 static void append(pa_strbuf *sb, struct chunk *c) {
     pa_assert(sb);
@@ -135,7 +141,7 @@ static void append(pa_strbuf *sb, struct chunk *c) {
 /* Append up to l bytes of a string to the string buffer */
 void pa_strbuf_putsn(pa_strbuf *sb, const char *t, size_t l) {
     struct chunk *c;
 /* Append up to l bytes of a string to the string buffer */
 void pa_strbuf_putsn(pa_strbuf *sb, const char *t, size_t l) {
     struct chunk *c;
-    
+
     pa_assert(sb);
     pa_assert(t);
 
     pa_assert(sb);
     pa_assert(t);
 
@@ -151,8 +157,8 @@ void pa_strbuf_putsn(pa_strbuf *sb, const char *t, size_t l) {
 
 /* Append a printf() style formatted string to the string buffer. */
 /* The following is based on an example from the GNU libc documentation */
 
 /* Append a printf() style formatted string to the string buffer. */
 /* The following is based on an example from the GNU libc documentation */
-int pa_strbuf_printf(pa_strbuf *sb, const char *format, ...) {
-    int size = 100;
+size_t pa_strbuf_printf(pa_strbuf *sb, const char *format, ...) {
+    size_t size = 100;
     struct chunk *c = NULL;
 
     pa_assert(sb);
     struct chunk *c = NULL;
 
     pa_assert(sb);
@@ -169,15 +175,21 @@ int pa_strbuf_printf(pa_strbuf *sb, const char *format, ...) {
         CHUNK_TO_TEXT(c)[size-1] = 0;
         va_end(ap);
 
         CHUNK_TO_TEXT(c)[size-1] = 0;
         va_end(ap);
 
-        if (r > -1 && r < size) {
-            c->length = r;
+        if (r > -1 && (size_t) r < size) {
+            c->length = (size_t) r;
             append(sb, c);
             append(sb, c);
-            return r;
+            return (size_t) r;
         }
 
         if (r > -1)    /* glibc 2.1 */
         }
 
         if (r > -1)    /* glibc 2.1 */
-            size = r+1;
+            size = (size_t) r+1;
         else           /* glibc 2.0 */
             size *= 2;
     }
 }
         else           /* glibc 2.0 */
             size *= 2;
     }
 }
+
+pa_bool_t pa_strbuf_isempty(pa_strbuf *sb) {
+    pa_assert(sb);
+
+    return sb->length <= 0;
+}