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
11 ////////////////////////////////////////////////////////////////////////////////
13 // $Id: SafeFormat.cpp 756 2006-10-17 20:05:42Z syntheticpp $
16 #include <loki/SafeFormat.h>
22 // Crude writing method: writes straight to the file, unbuffered
23 // Must be combined with a buffer to work properly (and efficiently)
25 void write(std::FILE *f, const char *from, const char *to)
28 ::std::fwrite(from, 1, to - from, f);
33 void write(std::string &s, const char *from, const char *to)
41 void write(std::ostream &f, const char *from, const char *to)
44 f.write(from, std::streamsize(to - from));
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 ////////////////////////////////////////////////////////////////////////////////
54 PrintfState<std::FILE *, char> Printf(const char *format)
56 return PrintfState<std::FILE *, char>(stdout, format);
59 PrintfState<std::FILE *, char> Printf(const std::string &format)
61 return PrintfState<std::FILE *, char>(stdout, format.c_str());
64 PrintfState<std::FILE *, char> FPrintf(std::FILE *f, const char *format)
66 return PrintfState<std::FILE *, char>(f, format);
69 PrintfState<std::FILE *, char> FPrintf(std::FILE *f, const std::string &format)
71 return PrintfState<std::FILE *, char>(f, format.c_str());
74 PrintfState<std::ostream &, char> FPrintf(std::ostream &f, const char *format)
76 return PrintfState<std::ostream &, char>(f, format);
79 PrintfState<std::ostream &, char> FPrintf(std::ostream &f, const std::string &format)
81 return PrintfState<std::ostream &, char>(f, format.c_str());
84 PrintfState<std::string &, char> SPrintf(std::string &s, const char *format)
86 return PrintfState<std::string &, char>(s, format);
89 PrintfState<std::string &, char> SPrintf(std::string &s, const std::string &format)
91 return PrintfState<std::string &, char>(s, format.c_str());
95 } // end namespace Loki