Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #418 - Minimal supported library subset #419

Merged
merged 1 commit into from
Nov 10, 2016
Merged
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
4 changes: 2 additions & 2 deletions fmt/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Define the fmt library, its includes and the needed defines.
# format.cc is added to FMT_HEADERS for the header-only configuration.
set(FMT_HEADERS format.h format.cc ostream.h ostream.cc printf.h
# *.cc are added to FMT_HEADERS for the header-only configuration.
set(FMT_HEADERS format.h format.cc ostream.h ostream.cc printf.h printf.cc
string.h time.h)
if (HAVE_OPEN)
set(FMT_HEADERS ${FMT_HEADERS} posix.h)
Expand Down
35 changes: 0 additions & 35 deletions fmt/format.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
*/

#include "format.h"
#include "printf.h"

#include <string.h>

Expand Down Expand Up @@ -106,8 +105,6 @@ inline int fmt_snprintf(char *buffer, size_t size, const char *format, ...) {
# define FMT_SWPRINTF swprintf
#endif // defined(_WIN32) && defined(__MINGW32__) && !defined(__NO_ISOCEXT)

const char RESET_COLOR[] = "\x1b[0m";

typedef void (*FormatFunc)(Writer &, int, StringRef);

// Portable thread-safe version of strerror.
Expand Down Expand Up @@ -486,34 +483,6 @@ FMT_FUNC void report_windows_error(
}
#endif

FMT_FUNC void print(std::FILE *f, CStringRef format_str, ArgList args) {
MemoryWriter w;
w.write(format_str, args);
std::fwrite(w.data(), 1, w.size(), f);
}

FMT_FUNC void print(CStringRef format_str, ArgList args) {
print(stdout, format_str, args);
}

FMT_FUNC void print_colored(Color c, CStringRef format, ArgList args) {
char escape[] = "\x1b[30m";
escape[3] = static_cast<char>('0' + c);
std::fputs(escape, stdout);
print(format, args);
std::fputs(RESET_COLOR, stdout);
}

template <typename Char>
void printf(BasicWriter<Char> &w, BasicCStringRef<Char> format, ArgList args);

FMT_FUNC int fprintf(std::FILE *f, CStringRef format, ArgList args) {
MemoryWriter w;
printf(w, format, args);
std::size_t size = w.size();
return std::fwrite(w.data(), 1, size, f) < size ? -1 : static_cast<int>(size);
}

#ifndef FMT_HEADER_ONLY

template struct internal::BasicData<void>;
Expand All @@ -524,8 +493,6 @@ template void internal::FixedBuffer<char>::grow(std::size_t);

template void internal::ArgMap<char>::init(const ArgList &args);

template void PrintfFormatter<char>::format(CStringRef format);

template int internal::CharTraits<char>::format_float(
char *buffer, std::size_t size, const char *format,
unsigned width, int precision, double value);
Expand All @@ -540,8 +507,6 @@ template void internal::FixedBuffer<wchar_t>::grow(std::size_t);

template void internal::ArgMap<wchar_t>::init(const ArgList &args);

template void PrintfFormatter<wchar_t>::format(WCStringRef format);

template int internal::CharTraits<wchar_t>::format_float(
wchar_t *buffer, std::size_t size, const wchar_t *format,
unsigned width, int precision, double value);
Expand Down
56 changes: 56 additions & 0 deletions fmt/printf.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
Formatting library for C++

Copyright (c) 2012 - 2016, Victor Zverovich
All rights reserved.

For the license information refer to format.h.
*/

#include "format.h"
#include "printf.h"

namespace fmt {

namespace {

const char RESET_COLOR[] = "\x1b[0m";

} // namespace

FMT_FUNC void print(std::FILE *f, CStringRef format_str, ArgList args) {
MemoryWriter w;
w.write(format_str, args);
std::fwrite(w.data(), 1, w.size(), f);
}

FMT_FUNC void print(CStringRef format_str, ArgList args) {
print(stdout, format_str, args);
}

FMT_FUNC void print_colored(Color c, CStringRef format, ArgList args) {
char escape[] = "\x1b[30m";
escape[3] = static_cast<char>('0' + c);
std::fputs(escape, stdout);
print(format, args);
std::fputs(RESET_COLOR, stdout);
}

template <typename Char>
void printf(BasicWriter<Char> &w, BasicCStringRef<Char> format, ArgList args);

FMT_FUNC int fprintf(std::FILE *f, CStringRef format, ArgList args) {
MemoryWriter w;
printf(w, format, args);
std::size_t size = w.size();
return std::fwrite(w.data(), 1, size, f) < size ? -1 : static_cast<int>(size);
}

#ifndef FMT_HEADER_ONLY

template void PrintfFormatter<char>::format(CStringRef format);
template void PrintfFormatter<wchar_t>::format(WCStringRef format);

#endif // FMT_HEADER_ONLY

} // namespace fmt
4 changes: 4 additions & 0 deletions fmt/printf.h
Original file line number Diff line number Diff line change
Expand Up @@ -555,4 +555,8 @@ inline int fprintf(std::ostream &os, CStringRef format_str, ArgList args) {
FMT_VARIADIC(int, fprintf, std::ostream &, CStringRef)
} // namespace fmt

#ifdef FMT_HEADER_ONLY
# include "printf.cc"
#endif

#endif // FMT_PRINTF_H_
2 changes: 1 addition & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ endif ()

if (HAVE_OPEN)
add_fmt_executable(posix-mock-test
posix-mock-test.cc ../fmt/format.cc ${TEST_MAIN_SRC})
posix-mock-test.cc ../fmt/format.cc ../fmt/printf.cc ${TEST_MAIN_SRC})
target_include_directories(posix-mock-test PRIVATE ${PROJECT_SOURCE_DIR})
target_compile_definitions(posix-mock-test PRIVATE FMT_USE_FILE_DESCRIPTORS=1)
target_link_libraries(posix-mock-test gmock)
Expand Down
2 changes: 1 addition & 1 deletion test/add-subdirectory-test/main.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "fmt/format.h"
#include "fmt/printf.h"

int main(int argc, char** argv) {
for(int i = 0; i < argc; ++i)
Expand Down
2 changes: 1 addition & 1 deletion test/find-package-test/main.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "fmt/format.h"
#include "fmt/printf.h"

int main(int argc, char** argv) {
for(int i = 0; i < argc; ++i)
Expand Down
3 changes: 2 additions & 1 deletion test/format-impl-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@
#define FMT_NOEXCEPT
#include "test-assert.h"

// Include format.cc instead of format.h to test implementation-specific stuff.
// Include *.cc instead of *.h to test implementation-specific stuff.
#include "fmt/format.cc"
#include "fmt/printf.cc"

#include <algorithm>
#include <cstring>
Expand Down