SafeFormat.cpp

00001 
00002 // Copyright (c) 2005 by Andrei Alexandrescu
00003 // Copyright (c) 2006 Peter Kümmel
00004 // Permission to use, copy, modify, distribute, and sell this software for any
00005 //     purpose is hereby granted without fee, provided that the above copyright
00006 //     notice appear in all copies and that both that copyright notice and this
00007 //     permission notice appear in supporting documentation.
00008 // The author makes no representations about the suitability of this software 
00009 //     for any purpose. It is provided "as is" without express or implied 
00010 //     warranty.
00012 
00013 // $Id: SafeFormat.cpp 756 2006-10-17 20:05:42Z syntheticpp $
00014 
00015 
00016 #include <loki/SafeFormat.h>
00017 
00018 
00019 namespace Loki
00020 {
00021 
00022     // Crude writing method: writes straight to the file, unbuffered
00023     // Must be combined with a buffer to work properly (and efficiently)
00024 
00025     void write(std::FILE* f, const char* from, const char* to) {
00026         assert(from <= to);
00027         ::std::fwrite(from, 1, to - from, f);
00028     }
00029 
00030     // Write to a string
00031 
00032     void write(std::string& s, const char* from, const char* to) {
00033         assert(from <= to);
00034         s.append(from, to);
00035     }
00036   
00037     // Write to a stream
00038 
00039     void write(std::ostream& f, const char* from, const char* to) {
00040         assert(from <= to);
00041         f.write(from, std::streamsize(to - from));
00042     }
00043 
00045     // PrintfState class template
00046     // Holds the formatting state, and implements operator() to format stuff
00047     // Todo: make sure errors are handled properly
00049 
00050 
00051     PrintfState<std::FILE*, char> Printf(const char* format) {
00052         return PrintfState<std::FILE*, char>(stdout, format);
00053     }
00054 
00055     PrintfState<std::FILE*, char> Printf(const std::string& format) {
00056         return PrintfState<std::FILE*, char>(stdout, format.c_str());
00057     }
00058 
00059     PrintfState<std::FILE*, char> FPrintf(std::FILE* f, const char* format) {
00060         return PrintfState<std::FILE*, char>(f, format);
00061     }
00062 
00063     PrintfState<std::FILE*, char> FPrintf(std::FILE* f, const std::string& format) {
00064         return PrintfState<std::FILE*, char>(f, format.c_str());
00065     }
00066 
00067     PrintfState<std::ostream&, char> FPrintf(std::ostream& f, const char* format) {
00068         return PrintfState<std::ostream&, char>(f, format);
00069     }
00070 
00071     PrintfState<std::ostream&, char> FPrintf(std::ostream& f, const std::string& format) {
00072         return PrintfState<std::ostream&, char>(f, format.c_str());
00073     }
00074 
00075     PrintfState<std::string&, char> SPrintf(std::string& s, const char* format) {
00076         return PrintfState<std::string&, char>(s, format);
00077     }
00078 
00079     PrintfState<std::string&, char> SPrintf(std::string& s, const std::string& format) {
00080         return PrintfState<std::string&, char>(s, format.c_str());
00081     }
00082 
00083 
00084 } // end namespace Loki
00085 

Generated on Sun Feb 25 16:52:26 2007 for Loki by  doxygen 1.5.1-p1