LCOV - code coverage report
Current view: top level - pulsecore - strbuf.c (source / functions) Hit Total Coverage
Test: lcov.out Lines: 71 78 91.0 %
Date: 2012-07-17 Functions: 9 10 90.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 25 48 52.1 %

           Branch data     Line data    Source code
       1                 :            : /***
       2                 :            :   This file is part of PulseAudio.
       3                 :            : 
       4                 :            :   Copyright 2004-2006 Lennart Poettering
       5                 :            : 
       6                 :            :   PulseAudio is free software; you can redistribute it and/or modify
       7                 :            :   it under the terms of the GNU Lesser General Public License as published
       8                 :            :   by the Free Software Foundation; either version 2.1 of the License,
       9                 :            :   or (at your option) any later version.
      10                 :            : 
      11                 :            :   PulseAudio is distributed in the hope that it will be useful, but
      12                 :            :   WITHOUT ANY WARRANTY; without even the implied warranty of
      13                 :            :   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
      14                 :            :   General Public License for more details.
      15                 :            : 
      16                 :            :   You should have received a copy of the GNU Lesser General Public License
      17                 :            :   along with PulseAudio; if not, write to the Free Software
      18                 :            :   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
      19                 :            :   USA.
      20                 :            : ***/
      21                 :            : 
      22                 :            : #ifdef HAVE_CONFIG_H
      23                 :            : #include <config.h>
      24                 :            : #endif
      25                 :            : 
      26                 :            : #include <sys/types.h>
      27                 :            : #include <stdlib.h>
      28                 :            : #include <string.h>
      29                 :            : #include <stdarg.h>
      30                 :            : #include <stdio.h>
      31                 :            : 
      32                 :            : #include <pulse/xmalloc.h>
      33                 :            : #include <pulsecore/macro.h>
      34                 :            : 
      35                 :            : #include "strbuf.h"
      36                 :            : 
      37                 :            : /* A chunk of the linked list that makes up the string */
      38                 :            : struct chunk {
      39                 :            :     struct chunk *next;
      40                 :            :     size_t length;
      41                 :            : };
      42                 :            : 
      43                 :            : #define CHUNK_TO_TEXT(c) ((char*) (c) + PA_ALIGN(sizeof(struct chunk)))
      44                 :            : 
      45                 :            : struct pa_strbuf {
      46                 :            :     size_t length;
      47                 :            :     struct chunk *head, *tail;
      48                 :            : };
      49                 :            : 
      50                 :          9 : pa_strbuf *pa_strbuf_new(void) {
      51                 :            :     pa_strbuf *sb;
      52                 :            : 
      53                 :          9 :     sb = pa_xnew(pa_strbuf, 1);
      54                 :          9 :     sb->length = 0;
      55                 :          9 :     sb->head = sb->tail = NULL;
      56                 :            : 
      57                 :          9 :     return sb;
      58                 :            : }
      59                 :            : 
      60                 :          9 : void pa_strbuf_free(pa_strbuf *sb) {
      61         [ +  - ]:          9 :     pa_assert(sb);
      62                 :            : 
      63         [ +  + ]:        104 :     while (sb->head) {
      64                 :         95 :         struct chunk *c = sb->head;
      65                 :         95 :         sb->head = sb->head->next;
      66                 :         95 :         pa_xfree(c);
      67                 :            :     }
      68                 :            : 
      69                 :          9 :     pa_xfree(sb);
      70                 :          9 : }
      71                 :            : 
      72                 :            : /* Make a C string from the string buffer. The caller has to free
      73                 :            :  * string with pa_xfree(). */
      74                 :          9 : char *pa_strbuf_tostring(pa_strbuf *sb) {
      75                 :            :     char *t, *e;
      76                 :            :     struct chunk *c;
      77                 :            : 
      78         [ -  + ]:          9 :     pa_assert(sb);
      79                 :            : 
      80                 :          9 :     e = t = pa_xmalloc(sb->length+1);
      81                 :            : 
      82         [ +  + ]:        104 :     for (c = sb->head; c; c = c->next) {
      83         [ -  + ]:         95 :         pa_assert((size_t) (e-t) <= sb->length);
      84                 :         95 :         memcpy(e, CHUNK_TO_TEXT(c), c->length);
      85                 :         95 :         e += c->length;
      86                 :            :     }
      87                 :            : 
      88                 :            :     /* Trailing NUL */
      89                 :          9 :     *e = 0;
      90                 :            : 
      91         [ -  + ]:          9 :     pa_assert(e == t+sb->length);
      92                 :            : 
      93                 :          9 :     return t;
      94                 :            : }
      95                 :            : 
      96                 :            : /* Combination of pa_strbuf_free() and pa_strbuf_tostring() */
      97                 :          9 : char *pa_strbuf_tostring_free(pa_strbuf *sb) {
      98                 :            :     char *t;
      99                 :            : 
     100         [ -  + ]:          9 :     pa_assert(sb);
     101                 :          9 :     t = pa_strbuf_tostring(sb);
     102                 :          9 :     pa_strbuf_free(sb);
     103                 :            : 
     104                 :          9 :     return t;
     105                 :            : }
     106                 :            : 
     107                 :            : /* Append a string to the string buffer */
     108                 :         59 : void pa_strbuf_puts(pa_strbuf *sb, const char *t) {
     109                 :            : 
     110         [ -  + ]:         59 :     pa_assert(sb);
     111         [ -  + ]:         59 :     pa_assert(t);
     112                 :            : 
     113                 :         59 :     pa_strbuf_putsn(sb, t, strlen(t));
     114                 :         59 : }
     115                 :            : 
     116                 :            : /* Append a character to the string buffer */
     117                 :          0 : void pa_strbuf_putc(pa_strbuf *sb, char c) {
     118         [ #  # ]:          0 :     pa_assert(sb);
     119                 :            : 
     120                 :          0 :     pa_strbuf_putsn(sb, &c, 1);
     121                 :          0 : }
     122                 :            : 
     123                 :            : /* Append a new chunk to the linked list */
     124                 :         95 : static void append(pa_strbuf *sb, struct chunk *c) {
     125         [ -  + ]:         95 :     pa_assert(sb);
     126         [ -  + ]:         95 :     pa_assert(c);
     127                 :            : 
     128         [ +  + ]:         95 :     if (sb->tail) {
     129         [ -  + ]:         86 :         pa_assert(sb->head);
     130                 :         86 :         sb->tail->next = c;
     131                 :            :     } else {
     132         [ -  + ]:          9 :         pa_assert(!sb->head);
     133                 :          9 :         sb->head = c;
     134                 :            :     }
     135                 :            : 
     136                 :         95 :     sb->tail = c;
     137                 :         95 :     sb->length += c->length;
     138                 :         95 :     c->next = NULL;
     139                 :         95 : }
     140                 :            : 
     141                 :            : /* Append up to l bytes of a string to the string buffer */
     142                 :         75 : void pa_strbuf_putsn(pa_strbuf *sb, const char *t, size_t l) {
     143                 :            :     struct chunk *c;
     144                 :            : 
     145         [ -  + ]:         75 :     pa_assert(sb);
     146         [ -  + ]:         75 :     pa_assert(t);
     147                 :            : 
     148         [ +  - ]:         75 :     if (!l)
     149                 :         75 :         return;
     150                 :            : 
     151                 :         75 :     c = pa_xmalloc(PA_ALIGN(sizeof(struct chunk)) + l);
     152                 :         75 :     c->length = l;
     153                 :         75 :     memcpy(CHUNK_TO_TEXT(c), t, l);
     154                 :            : 
     155                 :         75 :     append(sb, c);
     156                 :            : }
     157                 :            : 
     158                 :            : /* Append a printf() style formatted string to the string buffer. */
     159                 :            : /* The following is based on an example from the GNU libc documentation */
     160                 :         20 : size_t pa_strbuf_printf(pa_strbuf *sb, const char *format, ...) {
     161                 :         20 :     size_t size = 100;
     162                 :         20 :     struct chunk *c = NULL;
     163                 :            : 
     164         [ -  + ]:         20 :     pa_assert(sb);
     165         [ +  - ]:         20 :     pa_assert(format);
     166                 :            : 
     167                 :            :     for(;;) {
     168                 :            :         va_list ap;
     169                 :            :         int r;
     170                 :            : 
     171                 :         20 :         c = pa_xrealloc(c, PA_ALIGN(sizeof(struct chunk)) + size);
     172                 :            : 
     173                 :         20 :         va_start(ap, format);
     174                 :         40 :         r = vsnprintf(CHUNK_TO_TEXT(c), size, format, ap);
     175                 :         20 :         CHUNK_TO_TEXT(c)[size-1] = 0;
     176                 :         20 :         va_end(ap);
     177                 :            : 
     178 [ +  - ][ +  - ]:         20 :         if (r > -1 && (size_t) r < size) {
     179                 :         20 :             c->length = (size_t) r;
     180                 :         20 :             append(sb, c);
     181                 :         20 :             return (size_t) r;
     182                 :            :         }
     183                 :            : 
     184         [ #  # ]:          0 :         if (r > -1)    /* glibc 2.1 */
     185                 :          0 :             size = (size_t) r+1;
     186                 :            :         else           /* glibc 2.0 */
     187                 :          0 :             size *= 2;
     188                 :            :     }
     189                 :            : }
     190                 :            : 
     191                 :         20 : pa_bool_t pa_strbuf_isempty(pa_strbuf *sb) {
     192         [ -  + ]:         20 :     pa_assert(sb);
     193                 :            : 
     194                 :         20 :     return sb->length <= 0;
     195                 :            : }

Generated by: LCOV version 1.9