move Printf/SPrintf into the Loki namespace

git-svn-id: svn://svn.code.sf.net/p/loki-lib/code/trunk@433 7ec92016-0320-0410-acc4-a06ded1c099a
This commit is contained in:
syntheticpp 2006-01-05 20:41:47 +00:00
parent 70fdd98598
commit 90f1278043

View file

@ -25,39 +25,42 @@
#include <cassert> #include <cassert>
#include <locale> #include <locale>
// Crude writing method: writes straight to the file, unbuffered namespace Loki
// Must be combined with a buffer to work properly (and efficiently) {
void write(std::FILE* f, const char* from, const char* to) { // Crude writing method: writes straight to the file, unbuffered
// Must be combined with a buffer to work properly (and efficiently)
void write(std::FILE* f, const char* from, const char* to) {
assert(from <= to); assert(from <= to);
fwrite(from, 1, to - from, f); fwrite(from, 1, to - from, f);
} }
// Write to a string // Write to a string
void write(std::string& s, const char* from, const char* to) { void write(std::string& s, const char* from, const char* to) {
assert(from <= to); assert(from <= to);
s.append(from, to); s.append(from, to);
} }
// Write to a fixed-size buffer // Write to a fixed-size buffer
template <class Char> template <class Char>
void write(std::pair<Char*, std::size_t>& s, const Char* from, const Char* to) { void write(std::pair<Char*, std::size_t>& s, const Char* from, const Char* to) {
assert(from <= to); assert(from <= to);
if (from + s.second > to) throw std::overflow_error(""); if (from + s.second > to) throw std::overflow_error("");
s.first = copy(from, to, s.first); s.first = copy(from, to, s.first);
s.second -= to - from; s.second -= to - from;
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// PrintfState class template // PrintfState class template
// Holds the formatting state, and implements operator() to format stuff // Holds the formatting state, and implements operator() to format stuff
// Todo: make sure errors are handled properly // Todo: make sure errors are handled properly
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
template <class Device, class Char> template <class Device, class Char>
struct PrintfState { struct PrintfState {
PrintfState(Device dev, const Char * format) PrintfState(Device dev, const Char * format)
: device_(dev) : device_(dev)
, format_(format) , format_(format)
@ -184,7 +187,7 @@ struct PrintfState {
return result_; return result_;
} }
private: private:
PrintfState& operator=(const PrintfState&); PrintfState& operator=(const PrintfState&);
template <typename T> template <typename T>
PrintfState& StoreCountHelper(T *const pi) { PrintfState& StoreCountHelper(T *const pi) {
@ -317,11 +320,11 @@ private:
} }
memcpy(fmtBuf, fmt, (format_ - fmt) * sizeof(Char)); memcpy(fmtBuf, fmt, (format_ - fmt) * sizeof(Char));
fmtBuf[format_ - fmt] = 0; fmtBuf[format_ - fmt] = 0;
#ifdef _MSC_VER #ifdef _MSC_VER
const int stored = _snprintf(resultBuf, const int stored = _snprintf(resultBuf,
#else #else
const int stored = snprintf(resultBuf, const int stored = snprintf(resultBuf,
#endif #endif
sizeof(resultBuf) / sizeof(Char), fmtBuf, n); sizeof(resultBuf) / sizeof(Char), fmtBuf, n);
if (stored < 0) { if (stored < 0) {
result_ = -1; result_ = -1;
@ -485,30 +488,32 @@ private:
size_t prec_; size_t prec_;
unsigned int flags_; unsigned int flags_;
int result_; int result_;
}; };
PrintfState<std::FILE*, char> Printf(const char* format) { PrintfState<std::FILE*, char> Printf(const char* format) {
return PrintfState<std::FILE*, char>(stdout, format); return PrintfState<std::FILE*, char>(stdout, format);
} }
PrintfState<std::FILE*, char> FPrintf(FILE* f, const char* format) { PrintfState<std::FILE*, char> FPrintf(FILE* f, const char* format) {
return PrintfState<std::FILE*, char>(f, format); return PrintfState<std::FILE*, char>(f, format);
} }
PrintfState<std::string&, char> SPrintf(std::string& s, const char* format) { PrintfState<std::string&, char> SPrintf(std::string& s, const char* format) {
return PrintfState<std::string&, char>(s, format); return PrintfState<std::string&, char>(s, format);
} }
template <class T, class Char> template <class T, class Char>
PrintfState<T&, Char> XPrintf(T& device, const Char* format) { PrintfState<T&, Char> XPrintf(T& device, const Char* format) {
return PrintfState<T&, Char>(device, format); return PrintfState<T&, Char>(device, format);
} }
template <class Char, std::size_t N> template <class Char, std::size_t N>
PrintfState<std::pair<Char*, std::size_t>, Char> PrintfState<std::pair<Char*, std::size_t>, Char>
BufPrintf(Char (&buf)[N], const Char* format) { BufPrintf(Char (&buf)[N], const Char* format) {
std::pair<Char*, std::size_t> temp(buf, N); std::pair<Char*, std::size_t> temp(buf, N);
return PrintfState<std::pair<Char*, std::size_t>, Char>(temp, format); return PrintfState<std::pair<Char*, std::size_t>, Char>(temp, format);
} }
}// namespace Loki
#endif //SAFEFORMAT_H_ #endif //SAFEFORMAT_H_