Flow123d  master-f44eb46
ostream.cc
Go to the documentation of this file.
1 /*
2  Formatting library for C++ - std::ostream support
3 
4  Copyright (c) 2012 - 2016, Victor Zverovich
5  All rights reserved.
6 
7  Redistribution and use in source and binary forms, with or without
8  modification, are permitted provided that the following conditions are met:
9 
10  1. Redistributions of source code must retain the above copyright notice, this
11  list of conditions and the following disclaimer.
12  2. Redistributions in binary form must reproduce the above copyright notice,
13  this list of conditions and the following disclaimer in the documentation
14  and/or other materials provided with the distribution.
15 
16  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
20  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include "system/fmt/ostream.h"
29 #include "system/fmt/printf.h"
30 
31 namespace fmt {
32 
33 namespace {
34 // Write the content of w to os.
35 void write(std::ostream &os, Writer &w) {
36  const char *data = w.data();
37  typedef internal::MakeUnsigned<std::streamsize>::Type UnsignedStreamSize;
38  UnsignedStreamSize size = w.size();
39  UnsignedStreamSize max_size =
40  internal::to_unsigned((std::numeric_limits<std::streamsize>::max)());
41  do {
42  UnsignedStreamSize n = size <= max_size ? size : max_size;
43  os.write(data, static_cast<std::streamsize>(n));
44  data += n;
45  size -= n;
46  } while (size != 0);
47 }
48 }
49 
50 FMT_FUNC void print(std::ostream &os, CStringRef format_str, ArgList args) {
51  MemoryWriter w;
52  w.write(format_str, args);
53  write(os, w);
54 }
55 
56 FMT_FUNC int fprintf(std::ostream &os, CStringRef format, ArgList args) {
57  MemoryWriter w;
58  printf(w, format, args);
59  write(os, w);
60  return static_cast<int>(w.size());
61 }
62 } // namespace fmt
std::size_t size() const
Definition: format.h:2427
void write(BasicCStringRef< Char > format, ArgList args)
Definition: format.h:2480
#define FMT_FUNC
Definition: format.h:3799
MakeUnsigned< Int >::Type to_unsigned(Int value)
Definition: format.h:561
Definition: format.cc:82
void printf(BasicWriter< Char > &w, BasicCStringRef< Char > format, ArgList args)
Definition: printf.h:444
FMT_FUNC int fprintf(std::ostream &os, CStringRef format, ArgList args)
Definition: ostream.cc:56
BasicWriter< char > Writer
Definition: format.h:376
std::string format(CStringRef format_str, ArgList args)
Definition: format.h:3141
FMT_API void print(std::FILE *f, CStringRef format_str, ArgList args)
Definition: format.cc:489
void write(const mValue &value, std::ostream &os, unsigned int options=0)