LCOV - code coverage report
Current view: top level - src - write_utils.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 20 20 100.0 %
Date: 2026-07-14 21:28:07 Functions: 4 4 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 26 26 100.0 %

           Branch data     Line data    Source code
       1                 :            : // Copyright 2019-2026 David Robillard <d@drobilla.net>
       2                 :            : // SPDX-License-Identifier: ISC
       3                 :            : 
       4                 :            : #include "write_utils.h"
       5                 :            : 
       6                 :            : #include "result.h"
       7                 :            : 
       8                 :            : #include <exess/exess.h>
       9                 :            : 
      10                 :            : #include <stdbool.h>
      11                 :            : #include <string.h>
      12                 :            : 
      13                 :            : EXESS_NONBLOCKING size_t
      14                 :      89301 : write_char(const char c, const size_t buf_size, char* const buf, const size_t i)
      15                 :            : {
      16   [ +  +  +  + ]:      89301 :   if (buf && i < buf_size) {
      17                 :      79123 :     buf[i] = c;
      18                 :            :   }
      19                 :            : 
      20                 :      89301 :   return 1;
      21                 :            : }
      22                 :            : 
      23                 :            : EXESS_NONBLOCKING size_t
      24                 :      20421 : write_string(const size_t      len,
      25                 :            :              const char* const str,
      26                 :            :              const size_t      buf_size,
      27                 :            :              char* const       buf,
      28                 :            :              const size_t      i)
      29                 :            : {
      30   [ +  +  +  + ]:      20421 :   if (buf && i + len <= buf_size) {
      31                 :      19304 :     memcpy(buf + i, str, len);
      32                 :            :   }
      33                 :            : 
      34                 :      20421 :   return len;
      35                 :            : }
      36                 :            : 
      37                 :            : EXESS_NONBLOCKING ExessResult
      38                 :     247112 : end_write(const ExessStatus status,
      39                 :            :           const size_t      buf_size,
      40                 :            :           char* const       buf,
      41                 :            :           const size_t      i)
      42                 :            : {
      43                 :     247112 :   const bool overflow =
      44   [ +  +  +  +  :     247112 :     (status == EXESS_NO_SPACE || (!status && i >= buf_size));
                   +  + ]
      45                 :            : 
      46                 :            :   // Truncate on error and null terminate output if at all possible
      47   [ +  +  +  + ]:     247112 :   const size_t end = (status || overflow) ? 0 : i;
      48   [ +  +  +  + ]:     247112 :   if (buf && end < buf_size) {
      49                 :     245202 :     buf[end] = '\0';
      50                 :            :   }
      51                 :            : 
      52         [ +  + ]:     247112 :   return overflow ? RESULT(EXESS_NO_SPACE, i) : RESULT(status, end);
      53                 :            : }
      54                 :            : 
      55                 :            : EXESS_NONBLOCKING ExessResult
      56                 :       1006 : write_special(const size_t      string_length,
      57                 :            :               const char* const string,
      58                 :            :               const size_t      buf_size,
      59                 :            :               char* const       buf)
      60                 :            : {
      61         [ +  + ]:       1006 :   if (string_length >= buf_size) {
      62                 :         11 :     return end_write(EXESS_NO_SPACE, buf_size, buf, string_length);
      63                 :            :   }
      64                 :            : 
      65                 :        995 :   memcpy(buf, string, string_length);
      66                 :        995 :   return end_write(EXESS_SUCCESS, buf_size, buf, string_length);
      67                 :            : }

Generated by: LCOV version 1.16