printf.cc revision 1d2049b53e3ae38807ac763df2212b29008549f2
1/*
2 Formatting library for C++
3
4 Copyright (c) 2012 - 2016, Victor Zverovich
5 All rights reserved.
6
7 For the license information refer to format.h.
8 */
9
10#include "format.h"
11#include "printf.h"
12
13namespace fmt {
14
15namespace {
16
17const char RESET_COLOR[] = "\x1b[0m";
18
19}  // namespace
20
21FMT_FUNC void print(std::FILE *f, CStringRef format_str, ArgList args) {
22  MemoryWriter w;
23  w.write(format_str, args);
24  std::fwrite(w.data(), 1, w.size(), f);
25}
26
27FMT_FUNC void print(CStringRef format_str, ArgList args) {
28  print(stdout, format_str, args);
29}
30
31FMT_FUNC void print_colored(Color c, CStringRef format, ArgList args) {
32  char escape[] = "\x1b[30m";
33  escape[3] = static_cast<char>('0' + c);
34  std::fputs(escape, stdout);
35  print(format, args);
36  std::fputs(RESET_COLOR, stdout);
37}
38
39template <typename Char>
40void printf(BasicWriter<Char> &w, BasicCStringRef<Char> format, ArgList args);
41
42FMT_FUNC int fprintf(std::FILE *f, CStringRef format, ArgList args) {
43  MemoryWriter w;
44  printf(w, format, args);
45  std::size_t size = w.size();
46  return std::fwrite(w.data(), 1, size, f) < size ? -1 : static_cast<int>(size);
47}
48
49#ifndef FMT_HEADER_ONLY
50
51template void PrintfFormatter<char>::format(CStringRef format);
52template void PrintfFormatter<wchar_t>::format(WCStringRef format);
53
54#endif  // FMT_HEADER_ONLY
55
56}  // namespace fmt
57