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:
parent
70fdd98598
commit
90f1278043
1 changed files with 468 additions and 463 deletions
|
@ -25,39 +25,42 @@
|
|||
#include <cassert>
|
||||
#include <locale>
|
||||
|
||||
// Crude writing method: writes straight to the file, unbuffered
|
||||
// Must be combined with a buffer to work properly (and efficiently)
|
||||
namespace Loki
|
||||
{
|
||||
|
||||
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);
|
||||
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);
|
||||
s.append(from, to);
|
||||
}
|
||||
}
|
||||
|
||||
// Write to a fixed-size buffer
|
||||
// Write to a fixed-size buffer
|
||||
|
||||
template <class Char>
|
||||
void write(std::pair<Char*, std::size_t>& s, const Char* from, const Char* to) {
|
||||
template <class Char>
|
||||
void write(std::pair<Char*, std::size_t>& s, const Char* from, const Char* to) {
|
||||
assert(from <= to);
|
||||
if (from + s.second > to) throw std::overflow_error("");
|
||||
s.first = copy(from, to, s.first);
|
||||
s.second -= to - from;
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// PrintfState class template
|
||||
// Holds the formatting state, and implements operator() to format stuff
|
||||
// Todo: make sure errors are handled properly
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// PrintfState class template
|
||||
// Holds the formatting state, and implements operator() to format stuff
|
||||
// Todo: make sure errors are handled properly
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template <class Device, class Char>
|
||||
struct PrintfState {
|
||||
template <class Device, class Char>
|
||||
struct PrintfState {
|
||||
PrintfState(Device dev, const Char * format)
|
||||
: device_(dev)
|
||||
, format_(format)
|
||||
|
@ -184,7 +187,7 @@ struct PrintfState {
|
|||
return result_;
|
||||
}
|
||||
|
||||
private:
|
||||
private:
|
||||
PrintfState& operator=(const PrintfState&);
|
||||
template <typename T>
|
||||
PrintfState& StoreCountHelper(T *const pi) {
|
||||
|
@ -317,11 +320,11 @@ private:
|
|||
}
|
||||
memcpy(fmtBuf, fmt, (format_ - fmt) * sizeof(Char));
|
||||
fmtBuf[format_ - fmt] = 0;
|
||||
#ifdef _MSC_VER
|
||||
#ifdef _MSC_VER
|
||||
const int stored = _snprintf(resultBuf,
|
||||
#else
|
||||
#else
|
||||
const int stored = snprintf(resultBuf,
|
||||
#endif
|
||||
#endif
|
||||
sizeof(resultBuf) / sizeof(Char), fmtBuf, n);
|
||||
if (stored < 0) {
|
||||
result_ = -1;
|
||||
|
@ -485,30 +488,32 @@ private:
|
|||
size_t prec_;
|
||||
unsigned int flags_;
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
template <class T, class Char>
|
||||
PrintfState<T&, Char> XPrintf(T& device, const Char* format) {
|
||||
template <class T, class Char>
|
||||
PrintfState<T&, Char> XPrintf(T& device, const Char* format) {
|
||||
return PrintfState<T&, Char>(device, format);
|
||||
}
|
||||
}
|
||||
|
||||
template <class Char, std::size_t N>
|
||||
PrintfState<std::pair<Char*, std::size_t>, Char>
|
||||
BufPrintf(Char (&buf)[N], const Char* format) {
|
||||
template <class Char, std::size_t N>
|
||||
PrintfState<std::pair<Char*, std::size_t>, Char>
|
||||
BufPrintf(Char (&buf)[N], const Char* format) {
|
||||
std::pair<Char*, std::size_t> temp(buf, N);
|
||||
return PrintfState<std::pair<Char*, std::size_t>, Char>(temp, format);
|
||||
}
|
||||
}
|
||||
|
||||
}// namespace Loki
|
||||
|
||||
#endif //SAFEFORMAT_H_
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue