Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions include/fmt/fmt-c.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include <stdbool.h> // bool
#include <stddef.h> // size_t
#include <stdio.h> // FILE

#ifdef __cplusplus
extern "C" {
Expand Down Expand Up @@ -46,6 +47,10 @@ enum { fmt_error = -1, fmt_error_invalid_arg = -2 };

int fmt_vformat(char* buffer, size_t size, const char* fmt, const fmt_arg* args,
size_t num_args);
int fmt_vprint(FILE* stream, const char* fmt, const fmt_arg* args,
size_t num_args);
int fmt_vprintln(FILE* stream, const char* fmt, const fmt_arg* args,
size_t num_args);

#ifdef __cplusplus
}
Expand Down Expand Up @@ -181,11 +186,20 @@ typedef enum {} fmt_signed_char;
(fmt_arg[]) { FMT_MAP(FMT_MAKE_ARG, ##__VA_ARGS__) }
# define FMT_EXPAND(v) v

# define fmt_format(buffer, size, fmt, ...) \
fmt_vformat((buffer), (size), (fmt), \
FMT_EXPAND(FMT_VA_SELECT(FMT_MAKE_NULL, FMT_MAKE_ARGLIST, \
##__VA_ARGS__)(__VA_ARGS__)), \
FMT_NARG(, ##__VA_ARGS__))
# define FMT_FORMAT_ARGS(fmt, ...) \
(fmt), \
FMT_EXPAND(FMT_VA_SELECT(FMT_MAKE_NULL, FMT_MAKE_ARGLIST, \
##__VA_ARGS__)(__VA_ARGS__)), \
FMT_NARG(, ##__VA_ARGS__)

# define fmt_format(buffer, size, fmt, ...) \
fmt_vformat((buffer), (size), FMT_FORMAT_ARGS((fmt), ##__VA_ARGS__))

# define fmt_print(stream, fmt, ...) \
fmt_vprint((stream), FMT_FORMAT_ARGS((fmt), ##__VA_ARGS__))

# define fmt_println(stream, fmt, ...) \
fmt_vprintln((stream), FMT_FORMAT_ARGS((fmt), ##__VA_ARGS__))

#endif // __cplusplus

Expand Down
49 changes: 41 additions & 8 deletions src/fmt-c.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@
#include "fmt/fmt-c.h"

#include <fmt/base.h>
#include <utility>

extern "C" int fmt_vformat(char* buffer, size_t size, const char* fmt,
const fmt_arg* args, size_t num_args) {
constexpr size_t max_args = 16;
if (num_args > max_args) return fmt_error_invalid_arg;
constexpr size_t max_c_format_args = 16;
static int parse_c_format_args(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not parsing, maybe rename to convert?

fmt::basic_format_arg<fmt::format_context>* format_args,
const fmt_arg* args, size_t num_args) {
if (num_args > max_c_format_args) return fmt_error_invalid_arg;

fmt::basic_format_arg<fmt::format_context> format_args[max_args];
for (size_t i = 0; i < num_args; ++i) {
switch (args[i].type) {
case fmt_int: format_args[i] = args[i].value.int_value; break;
Expand All @@ -31,12 +32,44 @@ extern "C" int fmt_vformat(char* buffer, size_t size, const char* fmt,
default: return fmt_error_invalid_arg;
}
}
return 0;
}

template <typename F>
static int fmt_c_wrapper(const char* fmt, const fmt_arg* args, size_t num_args,
F&& f) {
Comment on lines +39 to +40
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is needed.

fmt::basic_format_arg<fmt::format_context> format_args[max_c_format_args];
int error = parse_c_format_args(format_args, args, num_args);
if (error != 0) return error;

FMT_TRY {
auto result = fmt::vformat_to_n(
buffer, size, fmt,
return std::forward<F>(f)(
fmt::format_args(format_args, static_cast<int>(num_args)));
return static_cast<int>(result.size);
}
FMT_CATCH(...) {}
return fmt_error;
}

extern "C" int fmt_vformat(char* buffer, size_t size, const char* fmt,
const fmt_arg* args, size_t num_args) {
return fmt_c_wrapper(fmt, args, num_args, [=](fmt::format_args format_args) {
auto result = fmt::vformat_to_n(buffer, size, fmt, format_args);
return static_cast<int>(result.size);
});
}

extern "C" int fmt_vprint(FILE* stream, const char* fmt, const fmt_arg* args,
size_t num_args) {
return fmt_c_wrapper(fmt, args, num_args, [=](fmt::format_args format_args) {
fmt::vprint(stream, fmt, format_args);
return 0;
});
}

extern "C" int fmt_vprintln(FILE* stream, const char* fmt, const fmt_arg* args,
size_t num_args) {
return fmt_c_wrapper(fmt, args, num_args, [=](fmt::format_args format_args) {
fmt::vprintln(stream, fmt, format_args);
return 0;
});
}
Loading