Skip to content

Commit e19ed4c

Browse files
committed
CMake: allow to replace native *printf to stb_*printf
1 parent 332ddfc commit e19ed4c

File tree

8 files changed

+80
-4
lines changed

8 files changed

+80
-4
lines changed

CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ cmake_dependent_option(ENABLE_LIBZFS "Enable libzfs" ON "LINUX OR FreeBSD OR Sun
8181
option(ENABLE_SYSTEM_YYJSON "Use system provided (instead of fastfetch embedded) yyjson library" OFF)
8282
option(ENABLE_ASAN "Build fastfetch with ASAN (address sanitizer)" OFF)
8383
option(ENABLE_LTO "Enable link-time optimization in release mode if supported" ON)
84+
option(ENABLE_STBPRINTF "Enable stb_printf" OFF)
8485
option(BUILD_TESTS "Build tests" OFF) # Also create test executables
8586
option(SET_TWEAK "Add tweak to project version" ON) # This is set to off by github actions for release builds
8687
option(IS_MUSL "Build with musl libc" OFF) # Used by Github Actions
@@ -1052,6 +1053,10 @@ elseif(SunOS)
10521053
)
10531054
endif()
10541055

1056+
if(ENABLE_STBPRINTF)
1057+
list(APPEND LIBFASTFETCH_SRC src/util/stb_printf.c)
1058+
endif()
1059+
10551060
if(ENABLE_DIRECTX_HEADERS)
10561061
message(STATUS "Enabling DirectX headers for WSL")
10571062
list(APPEND LIBFASTFETCH_SRC src/detection/gpu/gpu_wsl.cpp)
@@ -1092,6 +1097,10 @@ add_library(libfastfetch OBJECT
10921097
${LIBFASTFETCH_SRC}
10931098
)
10941099

1100+
if(ENABLE_STBPRINTF)
1101+
target_compile_definitions(libfastfetch PUBLIC FF_USE_STBPRINTF)
1102+
endif()
1103+
10951104
if(yyjson_FOUND)
10961105
target_compile_definitions(libfastfetch PUBLIC FF_USE_SYSTEM_YYJSON)
10971106
target_link_libraries(libfastfetch PUBLIC yyjson::yyjson)

src/3rdparty/stb/stb_sprintf.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,7 @@ STBSP__PUBLICDEF int STB_SPRINTF_DECORATE(vsprintfcb)(STBSP_SPRINTFCB *callback,
591591
s = (char *)"null";
592592
// get the length, limited to desired precision
593593
// always limit to ~0u chars since our counts are 32b
594-
l = stbsp__strlen_limited(s, (pr >= 0) ? pr : ~0u);
594+
l = stbsp__strlen_limited(s, (pr >= 0) ? (unsigned) pr : ~0u);
595595
lead[0] = 0;
596596
tail[0] = 0;
597597
pr = 0;
@@ -695,7 +695,7 @@ STBSP__PUBLICDEF int STB_SPRINTF_DECORATE(vsprintfcb)(STBSP_SPRINTFCB *callback,
695695
n = (dp >= 1000) ? 6 : ((dp >= 100) ? 5 : ((dp >= 10) ? 4 : 3));
696696
tail[0] = (char)n;
697697
for (;;) {
698-
tail[n] = '0' + dp % 10;
698+
tail[n] = (char)('0' + dp % 10);
699699
if (n <= 3)
700700
break;
701701
--n;
@@ -793,7 +793,7 @@ STBSP__PUBLICDEF int STB_SPRINTF_DECORATE(vsprintfcb)(STBSP_SPRINTFCB *callback,
793793
#endif
794794
tail[0] = (char)n;
795795
for (;;) {
796-
tail[n] = '0' + dp % 10;
796+
tail[n] = (char)('0' + dp % 10);
797797
if (n <= 3)
798798
break;
799799
--n;
@@ -1431,7 +1431,7 @@ STBSP__PUBLICDEF int STB_SPRINTF_DECORATE( vsnprintf )( char * buf, int count, c
14311431
{
14321432
stbsp__context c;
14331433

1434-
if ( (count == 0) && !buf )
1434+
if ( count == 0 )
14351435
{
14361436
c.length = 0;
14371437

src/common/init.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,9 @@ void ffListFeatures(void)
266266
#if FF_HAVE_EMBEDDED_PCIIDS
267267
"Embedded pciids\n"
268268
#endif
269+
#if FF_USE_STBPRINTF
270+
"stb_printf\n"
271+
#endif
269272
""
270273
, stdout);
271274
}

src/common/time.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#include <sysinfoapi.h>
1010
#endif
1111

12+
#include "util/stb_printf.h"
13+
1214
static inline double ffTimeGetTick(void) //In msec
1315
{
1416
#ifdef _WIN32

src/util/FFstrbuf.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,15 @@ void ffStrbufInitVF(FFstrbuf* strbuf, const char* format, va_list arguments)
2020
{
2121
assert(format != NULL);
2222

23+
#ifndef FF_USE_STBPRINTF
2324
int len = vasprintf(&strbuf->chars, format, arguments);
2425
assert(len >= 0);
26+
#else
27+
int len = vsnprintf(NULL, 0, format, arguments);
28+
assert(len >= 0);
29+
strbuf->chars = (char*) malloc(sizeof(char) * (len + 1));
30+
vsnprintf(strbuf->chars, len + 1, format, arguments);
31+
#endif
2532

2633
strbuf->allocated = (uint32_t)(len + 1);
2734
strbuf->length = (uint32_t)len;

src/util/FFstrbuf.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
#define FASTFETCH_STRBUF_DEFAULT_ALLOC 32
2020

21+
#include "stb_printf.h"
22+
2123
typedef struct FFstrbuf
2224
{
2325
uint32_t allocated;

src/util/stb_printf.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#define STB_SPRINTF_IMPLEMENTATION 1
2+
#pragma GCC diagnostic ignored "-Wsign-conversion"
3+
#include "3rdparty/stb/stb_sprintf.h"

src/util/stb_printf.h

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#pragma once
2+
3+
#ifdef FF_USE_STBPRINTF
4+
5+
#include <stdio.h>
6+
#include <stdint.h>
7+
#include "3rdparty/stb/stb_sprintf.h"
8+
9+
static inline char *STB_SPRINTF_DECORATE(printf_callback)(const char* __restrict buf, void* __restrict user, int len)
10+
{
11+
fwrite(buf, 1, (uint32_t) len, (FILE *)user);
12+
return (char *) buf;
13+
}
14+
15+
static inline int STB_SPRINTF_DECORATE(vfprintf)(FILE* file, char const* __restrict fmt, va_list va)
16+
{
17+
char tmp[STB_SPRINTF_MIN];
18+
return STB_SPRINTF_DECORATE(vsprintfcb)(STB_SPRINTF_DECORATE(printf_callback), file, tmp, fmt, va);
19+
}
20+
#define vfprintf(...) STB_SPRINTF_DECORATE(vfprintf)(__VA_ARGS__)
21+
22+
static inline int STBSP__ATTRIBUTE_FORMAT(1,2) STB_SPRINTF_DECORATE(printf)(char const* __restrict fmt, ...)
23+
{
24+
va_list va;
25+
va_start(va, fmt);
26+
int result = STB_SPRINTF_DECORATE(vfprintf)(stdout, fmt, va);
27+
va_end(va);
28+
return result;
29+
}
30+
#define printf(...) STB_SPRINTF_DECORATE(printf)(__VA_ARGS__)
31+
32+
static inline int STBSP__ATTRIBUTE_FORMAT(2,3) STB_SPRINTF_DECORATE(fprintf)(FILE* __restrict file, char const* __restrict fmt, ...)
33+
{
34+
va_list va;
35+
va_start(va, fmt);
36+
int result = STB_SPRINTF_DECORATE(vfprintf)(file, fmt, va);
37+
va_end(va);
38+
return result;
39+
}
40+
#define fprintf(...) STB_SPRINTF_DECORATE(fprintf)(__VA_ARGS__)
41+
42+
#define vsprintf(...) STB_SPRINTF_DECORATE(vsprintf)(__VA_ARGS__)
43+
44+
#define vsnprintf(...) STB_SPRINTF_DECORATE(vsnprintf)(__VA_ARGS__)
45+
46+
#define sprintf(...) STB_SPRINTF_DECORATE(sprintf)(__VA_ARGS__)
47+
48+
#define snprintf(...) STB_SPRINTF_DECORATE(snprintf)(__VA_ARGS__)
49+
50+
#endif

0 commit comments

Comments
 (0)