LCOV - code coverage report
Current view: top level - libpkg - xstring.h (source / functions) Hit Total Coverage
Test: rapport Lines: 23 25 92.0 %
Date: 2021-12-10 16:22:55 Functions: 4 4 100.0 %
Branches: 6 8 75.0 %

           Branch data     Line data    Source code
       1                 :            : #ifndef __XSTRING_H_
       2                 :            : #define __XSTRING_H_
       3                 :            : 
       4                 :            : #include <stdio.h>
       5                 :            : #include <stdlib.h>
       6                 :            : #include <string.h>
       7                 :            : 
       8                 :            : struct xstring {
       9                 :            :         char* buf;
      10                 :            :         size_t size;
      11                 :            :         FILE* fp;
      12                 :            : };
      13                 :            : 
      14                 :            : typedef struct xstring xstring;
      15                 :            : 
      16                 :            : static inline xstring *
      17                 :      32176 : xstring_new(void)
      18                 :            : {
      19                 :            :         xstring *str;
      20                 :            : 
      21                 :      32176 :         str = calloc(1, sizeof(*str));
      22         [ +  + ]:      32176 :         if (str == NULL)
      23                 :          0 :                 abort();
      24                 :      32176 :         str->fp = open_memstream(&str->buf, &str->size);
      25         [ -  + ]:      32176 :         if (str->fp == NULL)
      26                 :          0 :                 abort();
      27                 :            : 
      28                 :      32176 :         return (str);
      29                 :            : }
      30                 :            : 
      31                 :            : static inline void
      32                 :      35299 : xstring_reset(xstring *str)
      33                 :            : {
      34         [ +  - ]:      35299 :         if (str->buf)
      35                 :      35299 :                 memset(str->buf, 0, str->size);
      36                 :      35299 :         rewind(str->fp);
      37                 :            : 
      38                 :      35299 : }
      39                 :            : 
      40                 :            : static inline void
      41                 :     120491 : xstring_free(xstring *str)
      42                 :            : {
      43         [ +  + ]:     120491 :         if (str == NULL)
      44                 :      91231 :                 return;
      45                 :      29260 :         fclose(str->fp);
      46                 :      29260 :         free(str->buf);
      47                 :      29260 :         free(str);
      48                 :     120491 : }
      49                 :            : 
      50                 :            : #define xstring_renew(s)      \
      51                 :            : do {                          \
      52                 :            :    if (s) {                   \
      53                 :            :      xstring_reset(s);        \
      54                 :            :    } else {                   \
      55                 :            :      s = xstring_new();       \
      56                 :            :    }                          \
      57                 :            : } while(0)
      58                 :            : 
      59                 :            : static inline char *
      60                 :        107 : xstring_get(xstring *str)
      61                 :            : {
      62                 :        107 :         fclose(str->fp);
      63                 :        107 :         char *ret = str->buf;
      64                 :        107 :         free(str);
      65                 :        107 :         return (ret);
      66                 :            : }
      67                 :            : 
      68                 :            : #endif

Generated by: LCOV version 1.15