Coverage Report

Created: 2025-07-07 10:01

/work/workdir/UnpackedTarball/rasqal/src/snprintf.c
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: c; c-basic-offset: 2 -*-
2
 *
3
 * snprintf.c - Rasqal formatted numbers utilities
4
 *
5
 * Copyright (C) 2011, David Beckett http://www.dajobe.org/
6
 * 
7
 * This package is Free Software and part of Redland http://librdf.org/
8
 * 
9
 * It is licensed under the following three licenses as alternatives:
10
 *   1. GNU Lesser General Public License (LGPL) V2.1 or any newer version
11
 *   2. GNU General Public License (GPL) V2 or any newer version
12
 *   3. Apache License, V2.0 or any newer version
13
 * 
14
 * You may not use this file except in compliance with at least one of
15
 * the above three licenses.
16
 * 
17
 * See LICENSE.html or LICENSE.txt at the top of this package for the
18
 * complete terms and further detail along with the license texts for
19
 * the licenses in COPYING.LIB, COPYING and LICENSE-2.0.txt respectively.
20
 * 
21
 * 
22
 */
23
24
#ifdef HAVE_CONFIG_H
25
#include <rasqal_config.h>
26
#endif
27
28
#ifdef WIN32
29
#include <win32_rasqal_config.h>
30
#endif
31
32
#include <stdio.h>
33
#include <string.h>
34
#ifdef HAVE_STDLIB_H
35
#include <stdlib.h>
36
#endif
37
#include <stdarg.h>
38
39
#include "rasqal.h"
40
#include "rasqal_internal.h"
41
42
43
static const char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz";
44
45
/**
46
 * rasqal_format_integer:
47
 * @buffer: buffer (or NULL)
48
 * @bufsize: size of above (or 0)
49
 * @integer: integer value to format
50
 *
51
 * INTERNAL - Format an integer as a decimal into a buffer or
52
 * calculate the size needed.
53
 *
54
 * Works Like the C99 snprintf() but just for integers.
55
 *
56
 * If @buffer is NULL or the @bufsize is too small, the number of
57
 * bytes needed (excluding NUL) is returned and no formatting is done.
58
 *
59
 * Return value: number of bytes needed or written (excluding NUL) or 0 on failure
60
 */
61
size_t
62
rasqal_format_integer(char* buffer, size_t bufsize, int integer,
63
                      int width, char padding)
64
0
{
65
0
  size_t len = 1;
66
0
  char *p;
67
0
  unsigned int value;
68
0
  unsigned int base = 10;
69
70
0
  if(integer < 0) {
71
0
    value = RASQAL_GOOD_CAST(unsigned int, -integer);
72
0
    len++;
73
0
    width++;
74
0
  } else
75
0
    value = RASQAL_GOOD_CAST(unsigned int, integer);
76
0
  while(value /= base)
77
0
    len++;
78
79
0
  if(width > 0) {
80
0
    size_t width_l = RASQAL_GOOD_CAST(size_t, width);
81
0
    if(width_l > len)
82
0
      len = width_l;
83
0
  }
84
85
0
  if(!buffer || bufsize < (len + 1)) /* +1 for NUL */
86
0
    return len;
87
88
0
  if(!padding)
89
0
    padding = ' ';
90
91
0
  if(integer < 0)
92
0
    value = RASQAL_GOOD_CAST(unsigned int, -integer);
93
0
  else
94
0
    value = RASQAL_GOOD_CAST(unsigned int, integer);
95
96
0
  p = &buffer[len];
97
0
  *p-- = '\0';
98
0
  while(value  > 0 && p >= buffer) {
99
0
    *p-- = digits[value % base];
100
0
    value /= base;
101
0
  }
102
0
  while(p >= buffer)
103
0
    *p-- = padding;
104
0
  if(integer < 0)
105
0
    *buffer = '-';
106
107
0
  return len;
108
0
}