Skip to content

Commit dc97113

Browse files
committed
Cleanup the C API
1 parent 60d5d0c commit dc97113

File tree

4 files changed

+106
-251
lines changed

4 files changed

+106
-251
lines changed

CMakeLists.txt

Lines changed: 9 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,14 @@ target_include_directories(fmt-header-only
383383
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
384384
$<INSTALL_INTERFACE:${FMT_INC_DIR}>)
385385

386+
add_library(fmt-c STATIC src/fmt-c.cc)
387+
target_compile_features(fmt-c INTERFACE c_std_11)
388+
if (MSVC)
389+
target_compile_options(fmt-c PUBLIC /Zc:preprocessor)
390+
endif ()
391+
target_link_libraries(fmt-c PUBLIC fmt::fmt)
392+
add_library(fmt::fmt-c ALIAS fmt-c)
393+
386394
# Install targets.
387395
if (FMT_INSTALL)
388396
include(CMakePackageConfigHelpers)
@@ -422,7 +430,7 @@ if (FMT_INSTALL)
422430
${project_config}
423431
INSTALL_DESTINATION ${FMT_CMAKE_DIR})
424432

425-
set(INSTALL_TARGETS fmt fmt-header-only)
433+
set(INSTALL_TARGETS fmt fmt-header-only fmt-c)
426434

427435
set(INSTALL_FILE_SET)
428436
if (FMT_USE_CMAKE_MODULES)
@@ -533,51 +541,3 @@ if (FMT_MASTER_PROJECT AND EXISTS ${gitignore})
533541
set(CPACK_RESOURCE_FILE_README ${PROJECT_SOURCE_DIR}/README.md)
534542
include(CPack)
535543
endif ()
536-
537-
# C API Wrapper
538-
add_library(fmt_c STATIC src/fmt-c.cc)
539-
540-
target_compile_features(fmt_c PUBLIC cxx_std_11)
541-
target_link_libraries(fmt_c PUBLIC fmt::fmt)
542-
543-
target_include_directories(fmt_c PUBLIC
544-
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
545-
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
546-
)
547-
548-
set_target_properties(fmt_c PROPERTIES
549-
VERSION ${FMT_VERSION}
550-
SOVERSION ${CPACK_PACKAGE_VERSION_MAJOR}
551-
DEBUG_POSTFIX "${CMAKE_DEBUG_POSTFIX}"
552-
C_VISIBILITY_PRESET default
553-
CXX_VISIBILITY_PRESET hidden
554-
)
555-
556-
add_library(fmt::fmt_c ALIAS fmt_c)
557-
if(FMT_INSTALL)
558-
install(TARGETS fmt_c
559-
EXPORT fmt-targets
560-
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
561-
)
562-
install(FILES include/fmt/fmt-c.h
563-
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/fmt
564-
)
565-
endif()
566-
567-
if(FMT_TEST)
568-
enable_language(C)
569-
message(STATUS "Adding C API test executable")
570-
571-
add_executable(test-c-api test/test_c.c)
572-
target_link_libraries(test-c-api PRIVATE fmt::fmt_c)
573-
set_target_properties(test-c-api PROPERTIES
574-
C_STANDARD 11
575-
C_STANDARD_REQUIRED ON
576-
)
577-
if(MSVC)
578-
target_compile_options(test-c-api PRIVATE /Zc:preprocessor)
579-
endif()
580-
581-
add_test(NAME c-api-test COMMAND test-c-api)
582-
endif()
583-

test/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,3 +242,8 @@ if (FMT_CUDA_TEST)
242242
add_test(NAME cuda-test COMMAND fmt-in-cuda-test)
243243
endif ()
244244
endif ()
245+
246+
enable_language(C)
247+
add_executable(c-test c-test.c)
248+
target_link_libraries(c-test PRIVATE fmt::fmt-c)
249+
add_test(NAME c-test COMMAND c-test)

test/c-test.c

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// Formatting library for C++ - the C API tests
2+
//
3+
// Copyright (c) 2012 - present, Victor Zverovich
4+
// All rights reserved.
5+
//
6+
// For the license information refer to format.h.
7+
8+
#include "fmt/fmt-c.h"
9+
10+
#include <stdio.h>
11+
#include <stdlib.h>
12+
#include <string.h>
13+
14+
#define ASSERT_STR_EQ(actual, expected) \
15+
do { \
16+
if (strcmp(actual, expected) != 0) { \
17+
fprintf(stderr, \
18+
"\nAssertion failed:\n Expected: \"%s\"\n Got: \"%s\"\n", \
19+
expected, actual); \
20+
exit(1); \
21+
} \
22+
} while (0)
23+
24+
#define ASSERT_INT_EQ(actual, expected) \
25+
do { \
26+
if ((actual) != (expected)) { \
27+
fprintf(stderr, "\nAssertion failed:\n Expected: %d\n Got: %d\n", \
28+
expected, actual); \
29+
exit(1); \
30+
} \
31+
} while (0)
32+
33+
static inline int fmt_vformat_cstr(char* buf, size_t size, int result) {
34+
if (result >= 0 && (size_t)result < size)
35+
buf[result] = '\0';
36+
else if (size > 0)
37+
buf[size - 1] = '\0';
38+
return result;
39+
}
40+
41+
#define fmt_format_cstr(buf, size, ...) \
42+
fmt_vformat_cstr(buf, size, fmt_format(buf, size, __VA_ARGS__))
43+
44+
void test_types(void) {
45+
char buf[100];
46+
47+
fmt_format_cstr(buf, sizeof(buf), "{}", 42);
48+
ASSERT_STR_EQ(buf, "42");
49+
50+
fmt_format_cstr(buf, sizeof(buf), "{}", 123u);
51+
ASSERT_STR_EQ(buf, "123");
52+
53+
fmt_format_cstr(buf, sizeof(buf), "{}", (bool)true);
54+
ASSERT_STR_EQ(buf, "true");
55+
56+
fmt_format_cstr(buf, sizeof(buf), "{}", (char)'x');
57+
ASSERT_STR_EQ(buf, "x");
58+
59+
fmt_format_cstr(buf, sizeof(buf), "{}", 1.2f);
60+
ASSERT_STR_EQ(buf, "1.2");
61+
62+
fmt_format_cstr(buf, sizeof(buf), "{}", 3.14159);
63+
ASSERT_STR_EQ(buf, "3.14159");
64+
65+
fmt_format_cstr(buf, sizeof(buf), "{}", 1.2l);
66+
ASSERT_STR_EQ(buf, "1.2");
67+
68+
fmt_format_cstr(buf, sizeof(buf), "{}", "foo");
69+
ASSERT_STR_EQ(buf, "foo");
70+
71+
fmt_format_cstr(buf, sizeof(buf), "{}", (void*)0x12345678);
72+
ASSERT_STR_EQ(buf, "0x12345678");
73+
}
74+
75+
void test_zero_arguments(void) {
76+
char buf[100];
77+
int ret = fmt_format_cstr(buf, sizeof(buf), "No arguments");
78+
ASSERT_STR_EQ(buf, "No arguments");
79+
}
80+
81+
void test_buffer_size_query(void) {
82+
int size = fmt_format(NULL, 0, "Test string: {}", 42);
83+
ASSERT_INT_EQ(size, 15);
84+
}
85+
86+
int main(void) {
87+
printf("Running C API tests\n");
88+
test_types();
89+
test_zero_arguments();
90+
test_buffer_size_query();
91+
printf("C API tests passed\n");
92+
}

test/test_c.c

Lines changed: 0 additions & 202 deletions
This file was deleted.

0 commit comments

Comments
 (0)