Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
safe-printf.h
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
2//
3// Safer replacement for sprintf()
4//
5// When invoked with a char buffer of size known to compiler, it will call
6// snprintf passing correct buffer size without programmer specifying one explicitly.
7//
8// When buffer size is only known at runtime, one should use snprintf instead.
9
10#ifndef INKSCAPE_SAFE_PRINTF_H
11#define INKSCAPE_SAFE_PRINTF_H
12
13#include <cstdarg>
14#include <cstddef>
15#include <glib/gmacros.h>
16
17template<size_t N>
18int G_GNUC_PRINTF(2, 3) safeprintf(char (&buf)[N], const char* fmt, ...) {
19 va_list args;
20 va_start(args, fmt);
21 auto len = vsnprintf(buf, N, fmt, args);
22 va_end(args);
23 return len;
24}
25
26#endif
int buf
va_end(args)
int G_GNUC_PRINTF(2, 3) safeprintf(char(&buf)[N]
auto len
Definition safe-printf.h:21
int const char * fmt
Definition safe-printf.h:18
int const char va_start(args, fmt)
size_t N