]> git.cworth.org Git - vogl/blob - src/extlib/loki/src/SafeFormat.cpp
Initial vogl checkin
[vogl] / src / extlib / loki / src / SafeFormat.cpp
1 ////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2005 by Andrei Alexandrescu
3 // Copyright (c) 2006 Peter Kümmel
4 // Permission to use, copy, modify, distribute, and sell this software for any
5 //     purpose is hereby granted without fee, provided that the above copyright
6 //     notice appear in all copies and that both that copyright notice and this
7 //     permission notice appear in supporting documentation.
8 // The author makes no representations about the suitability of this software
9 //     for any purpose. It is provided "as is" without express or implied
10 //     warranty.
11 ////////////////////////////////////////////////////////////////////////////////
12
13 // $Id: SafeFormat.cpp 756 2006-10-17 20:05:42Z syntheticpp $
14
15
16 #include <loki/SafeFormat.h>
17
18
19 namespace Loki
20 {
21
22 // Crude writing method: writes straight to the file, unbuffered
23 // Must be combined with a buffer to work properly (and efficiently)
24
25 void write(std::FILE *f, const char *from, const char *to)
26 {
27         assert(from <= to);
28         ::std::fwrite(from, 1, to - from, f);
29 }
30
31 // Write to a string
32
33 void write(std::string &s, const char *from, const char *to)
34 {
35         assert(from <= to);
36         s.append(from, to);
37 }
38
39 // Write to a stream
40
41 void write(std::ostream &f, const char *from, const char *to)
42 {
43         assert(from <= to);
44         f.write(from, std::streamsize(to - from));
45 }
46
47 ////////////////////////////////////////////////////////////////////////////////
48 // PrintfState class template
49 // Holds the formatting state, and implements operator() to format stuff
50 // Todo: make sure errors are handled properly
51 ////////////////////////////////////////////////////////////////////////////////
52
53
54 PrintfState<std::FILE *, char> Printf(const char *format)
55 {
56         return PrintfState<std::FILE *, char>(stdout, format);
57 }
58
59 PrintfState<std::FILE *, char> Printf(const std::string &format)
60 {
61         return PrintfState<std::FILE *, char>(stdout, format.c_str());
62 }
63
64 PrintfState<std::FILE *, char> FPrintf(std::FILE *f, const char *format)
65 {
66         return PrintfState<std::FILE *, char>(f, format);
67 }
68
69 PrintfState<std::FILE *, char> FPrintf(std::FILE *f, const std::string &format)
70 {
71         return PrintfState<std::FILE *, char>(f, format.c_str());
72 }
73
74 PrintfState<std::ostream &, char> FPrintf(std::ostream &f, const char *format)
75 {
76         return PrintfState<std::ostream &, char>(f, format);
77 }
78
79 PrintfState<std::ostream &, char> FPrintf(std::ostream &f, const std::string &format)
80 {
81         return PrintfState<std::ostream &, char>(f, format.c_str());
82 }
83
84 PrintfState<std::string &, char> SPrintf(std::string &s, const char *format)
85 {
86         return PrintfState<std::string &, char>(s, format);
87 }
88
89 PrintfState<std::string &, char> SPrintf(std::string &s, const std::string &format)
90 {
91         return PrintfState<std::string &, char>(s, format.c_str());
92 }
93
94
95 } // end namespace Loki
96