integrated improvements from 1.3.4 (NumberFormatter::append(), DateTimeFormatter::append()

This commit is contained in:
Guenter Obiltschnig 2009-02-21 10:59:58 +00:00
parent 60e1433f51
commit fa1658b23a
23 changed files with 1173 additions and 598 deletions

View File

@ -1,7 +1,7 @@
//
// ArchiveStrategy.h
//
// $Id: //poco/svn/Foundation/include/Poco/ArchiveStrategy.h#2 $
// $Id: //poco/Main/Foundation/include/Poco/ArchiveStrategy.h#5 $
//
// Library: Foundation
// Package: Logging
@ -9,7 +9,7 @@
//
// Definition of the ArchiveStrategy class and subclasses.
//
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
// Copyright (c) 2004-2008, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// Permission is hereby granted, free of charge, to any person or organization
@ -120,7 +120,7 @@ public:
delete pFile;
std::string archPath = path;
archPath.append(".");
archPath.append(DateTimeFormatter::format(DT().timestamp(), "%Y%m%d%H%M%S%i"));
DateTimeFormatter::append(archPath, DT().timestamp(), "%Y%m%d%H%M%S%i");
if (exists(archPath)) archiveByNumber(archPath);
else moveFile(path, archPath);
@ -129,7 +129,6 @@ public:
}
private:
void archiveByNumber(const std::string& basePath)
/// A monotonic increasing number is appended to the
/// log file name. The most recent archived file
@ -141,7 +140,7 @@ private:
{
path = basePath;
path.append(".");
path.append(NumberFormatter::format(++n));
NumberFormatter::append(path, ++n);
}
while (exists(path));
@ -151,16 +150,15 @@ private:
if (n > 0)
{
oldPath.append(".");
oldPath.append(NumberFormatter::format(n - 1));
NumberFormatter::append(oldPath, n - 1);
}
std::string newPath = basePath;
newPath.append(".");
newPath.append(NumberFormatter::format(n));
NumberFormatter::append(newPath, n);
moveFile(oldPath, newPath);
--n;
}
}
};

View File

@ -1,7 +1,7 @@
//
// DateTimeFormatter.h
//
// $Id: //poco/svn/Foundation/include/Poco/DateTimeFormatter.h#2 $
// $Id: //poco/Main/Foundation/include/Poco/DateTimeFormatter.h#3 $
//
// Library: Foundation
// Package: DateTime
@ -41,13 +41,13 @@
#include "Poco/Foundation.h"
#include "Poco/DateTime.h"
#include "Poco/LocalDateTime.h"
namespace Poco {
class DateTime;
class LocalDateTime;
class Timestamp;
class Timespan;
@ -55,6 +55,12 @@ class Timespan;
class Foundation_API DateTimeFormatter
/// This class converts dates and times into strings, supporting a
/// variety of standard and custom formats.
///
/// There are two kind of static member functions:
/// * format* functions return a std::string containing
/// the formatted value.
/// * append* functions append the formatted value to
/// an existing string.
{
public:
enum
@ -119,6 +125,26 @@ public:
/// * %c - centisecond (0 .. 9)
/// * %% - percent sign
static void append(std::string& str, const Timestamp& timestamp, const std::string& fmt, int timeZoneDifferential = UTC);
/// Formats the given timestamp according to the given format and appends it to str.
///
/// See format() for documentation of the formatting string.
static void append(std::string& str, const DateTime& dateTime, const std::string& fmt, int timeZoneDifferential = UTC);
/// Formats the given date and time according to the given format and appends it to str.
///
/// See format() for documentation of the formatting string.
static void append(std::string& str, const LocalDateTime& dateTime, const std::string& fmt);
/// Formats the given local date and time according to the given format and appends it to str.
///
/// See format() for documentation of the formatting string.
static void append(std::string& str, const Timespan& timespan, const std::string& fmt = "%dd %H:%M:%S.%i");
/// Formats the given timespan according to the given format and appends it to str.
///
/// See format() for documentation of the formatting string.
static std::string tzdISO(int timeZoneDifferential);
/// Formats the given timezone differential in ISO format.
/// If timeZoneDifferential is UTC, "Z" is returned,
@ -128,9 +154,80 @@ public:
/// Formats the given timezone differential in RFC format.
/// If timeZoneDifferential is UTC, "GMT" is returned,
/// otherwise ++HHMM (or -HHMM) is returned.
static void tzdISO(std::string& str, int timeZoneDifferential);
/// Formats the given timezone differential in ISO format
/// and appends it to the given string.
/// If timeZoneDifferential is UTC, "Z" is returned,
/// otherwise, +HH.MM (or -HH.MM) is returned.
static void tzdRFC(std::string& str, int timeZoneDifferential);
/// Formats the given timezone differential in RFC format
/// and appends it to the given string.
/// If timeZoneDifferential is UTC, "GMT" is returned,
/// otherwise ++HHMM (or -HHMM) is returned.
};
//
// inlines
//
inline std::string DateTimeFormatter::format(const Timestamp& timestamp, const std::string& fmt, int timeZoneDifferential)
{
DateTime dateTime(timestamp);
return format(dateTime, fmt, timeZoneDifferential);
}
inline std::string DateTimeFormatter::format(const DateTime& dateTime, const std::string& fmt, int timeZoneDifferential)
{
std::string result;
result.reserve(64);
append(result, dateTime, fmt, timeZoneDifferential);
return result;
}
inline std::string DateTimeFormatter::format(const LocalDateTime& dateTime, const std::string& fmt)
{
return format(dateTime._dateTime, fmt, dateTime._tzd);
}
inline std::string DateTimeFormatter::format(const Timespan& timespan, const std::string& fmt)
{
std::string result;
result.reserve(32);
append(result, timespan, fmt);
return result;
}
inline void DateTimeFormatter::append(std::string& str, const Timestamp& timestamp, const std::string& fmt, int timeZoneDifferential)
{
DateTime dateTime(timestamp);
append(str, dateTime, fmt, timeZoneDifferential);
}
inline std::string DateTimeFormatter::tzdISO(int timeZoneDifferential)
{
std::string result;
result.reserve(8);
tzdISO(result, timeZoneDifferential);
return result;
}
inline std::string DateTimeFormatter::tzdRFC(int timeZoneDifferential)
{
std::string result;
result.reserve(8);
tzdRFC(result, timeZoneDifferential);
return result;
}
} // namespace Poco

View File

@ -1,7 +1,7 @@
//
// NumberFormatter.h
//
// $Id: //poco/svn/Foundation/include/Poco/NumberFormatter.h#2 $
// $Id: //poco/Main/Foundation/include/Poco/NumberFormatter.h#4 $
//
// Library: Foundation
// Package: Core
@ -9,7 +9,7 @@
//
// Definition of the NumberFormatter class.
//
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
// Copyright (c) 2004-2008, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// Permission is hereby granted, free of charge, to any person or organization
@ -49,6 +49,15 @@ namespace Poco {
class Foundation_API NumberFormatter
/// The NumberFormatter class provides static methods
/// for formatting numeric values into strings.
///
/// There are two kind of static member functions:
/// * format* functions return a std::string containing
/// the formatted value.
/// * append* functions append the formatted value to
/// an existing string.
///
/// Internally, std::sprintf() is used to do the actual
/// formatting.
{
public:
static std::string format(int value);
@ -206,9 +215,454 @@ public:
/// Formats a pointer in an eight (32-bit architectures) or
/// sixteen (64-bit architectures) characters wide
/// field in hexadecimal notation.
static void append(std::string& str, int value);
/// Formats an integer value in decimal notation.
static void append(std::string& str, int value, int width);
/// Formats an integer value in decimal notation,
/// right justified in a field having at least
/// the specified width.
static void append0(std::string& str, int value, int width);
/// Formats an integer value in decimal notation,
/// right justified and zero-padded in a field
/// having at least the specified width.
static void appendHex(std::string& str, int value);
/// Formats an int value in hexadecimal notation.
/// The value is treated as unsigned.
static void appendHex(std::string& str, int value, int width);
/// Formats a int value in hexadecimal notation,
/// right justified and zero-padded in
/// a field having at least the specified width.
/// The value is treated as unsigned.
static void append(std::string& str, unsigned value);
/// Formats an unsigned int value in decimal notation.
static void append(std::string& str, unsigned value, int width);
/// Formats an unsigned long int in decimal notation,
/// right justified in a field having at least the
/// specified width.
static void append0(std::string& str, unsigned int value, int width);
/// Formats an unsigned int value in decimal notation,
/// right justified and zero-padded in a field having at
/// least the specified width.
static void appendHex(std::string& str, unsigned value);
/// Formats an unsigned int value in hexadecimal notation.
static void appendHex(std::string& str, unsigned value, int width);
/// Formats a int value in hexadecimal notation,
/// right justified and zero-padded in
/// a field having at least the specified width.
static void append(std::string& str, long value);
/// Formats a long value in decimal notation.
static void append(std::string& str, long value, int width);
/// Formats a long value in decimal notation,
/// right justified in a field having at least the
/// specified width.
static void append0(std::string& str, long value, int width);
/// Formats a long value in decimal notation,
/// right justified and zero-padded in a field
/// having at least the specified width.
static void appendHex(std::string& str, long value);
/// Formats an unsigned long value in hexadecimal notation.
/// The value is treated as unsigned.
static void appendHex(std::string& str, long value, int width);
/// Formats an unsigned long value in hexadecimal notation,
/// right justified and zero-padded in a field having at least the
/// specified width.
/// The value is treated as unsigned.
static void append(std::string& str, unsigned long value);
/// Formats an unsigned long value in decimal notation.
static void append(std::string& str, unsigned long value, int width);
/// Formats an unsigned long value in decimal notation,
/// right justified in a field having at least the specified
/// width.
static void append0(std::string& str, unsigned long value, int width);
/// Formats an unsigned long value in decimal notation,
/// right justified and zero-padded
/// in a field having at least the specified width.
static void appendHex(std::string& str, unsigned long value);
/// Formats an unsigned long value in hexadecimal notation.
static void appendHex(std::string& str, unsigned long value, int width);
/// Formats an unsigned long value in hexadecimal notation,
/// right justified and zero-padded in a field having at least the
/// specified width.
#if defined(POCO_HAVE_INT64) && !defined(POCO_LONG_IS_64_BIT)
static void append(std::string& str, Int64 value);
/// Formats a 64-bit integer value in decimal notation.
static void append(std::string& str, Int64 value, int width);
/// Formats a 64-bit integer value in decimal notation,
/// right justified in a field having at least the specified width.
static void append0(std::string& str, Int64 value, int width);
/// Formats a 64-bit integer value in decimal notation,
/// right justified and zero-padded in a field having at least
/// the specified width.
static void appendHex(std::string& str, Int64 value);
/// Formats a 64-bit integer value in hexadecimal notation.
/// The value is treated as unsigned.
static void appendHex(std::string& str, Int64 value, int width);
/// Formats a 64-bit integer value in hexadecimal notation,
/// right justified and zero-padded in a field having at least
/// the specified width.
/// The value is treated as unsigned.
static void append(std::string& str, UInt64 value);
/// Formats an unsigned 64-bit integer value in decimal notation.
static void append(std::string& str, UInt64 value, int width);
/// Formats an unsigned 64-bit integer value in decimal notation,
/// right justified in a field having at least the specified width.
static void append0(std::string& str, UInt64 value, int width);
/// Formats an unsigned 64-bit integer value in decimal notation,
/// right justified and zero-padded in a field having at least the
/// specified width.
static void appendHex(std::string& str, UInt64 value);
/// Formats a 64-bit integer value in hexadecimal notation.
static void appendHex(std::string& str, UInt64 value, int width);
/// Formats a 64-bit integer value in hexadecimal notation,
/// right justified and zero-padded in a field having at least
/// the specified width.
#endif // defined(POCO_HAVE_INT64) && !defined(POCO_LONG_IS_64_BIT)
static void append(std::string& str, float value);
/// Formats a float value in decimal floating-point notation,
/// according to std::printf's %g format with a precision of 8 fractional digits.
static void append(std::string& str, double value);
/// Formats a double value in decimal floating-point notation,
/// according to std::printf's %g format with a precision of 16 fractional digits.
static void append(std::string& str, double value, int precision);
/// Formats a double value in decimal floating-point notation,
/// according to std::printf's %f format with the given precision.
static void append(std::string& str, double value, int width, int precision);
/// Formats a double value in decimal floating-point notation,
/// right justified in a field of the specified width,
/// with the number of fractional digits given in precision.
static void append(std::string& str, const void* ptr);
/// Formats a pointer in an eight (32-bit architectures) or
/// sixteen (64-bit architectures) characters wide
/// field in hexadecimal notation.
};
//
// inlines
//
inline std::string NumberFormatter::format(int value)
{
std::string result;
append(result, value);
return result;
}
inline std::string NumberFormatter::format(int value, int width)
{
std::string result;
append(result, value, width);
return result;
}
inline std::string NumberFormatter::format0(int value, int width)
{
std::string result;
append0(result, value, width);
return result;
}
inline std::string NumberFormatter::formatHex(int value)
{
std::string result;
appendHex(result, value);
return result;
}
inline std::string NumberFormatter::formatHex(int value, int width)
{
std::string result;
appendHex(result, value, width);
return result;
}
inline std::string NumberFormatter::format(unsigned value)
{
std::string result;
append(result, value);
return result;
}
inline std::string NumberFormatter::format(unsigned value, int width)
{
std::string result;
append(result, value, width);
return result;
}
inline std::string NumberFormatter::format0(unsigned int value, int width)
{
std::string result;
append0(result, value, width);
return result;
}
inline std::string NumberFormatter::formatHex(unsigned value)
{
std::string result;
appendHex(result, value);
return result;
}
inline std::string NumberFormatter::formatHex(unsigned value, int width)
{
std::string result;
appendHex(result, value, width);
return result;
}
inline std::string NumberFormatter::format(long value)
{
std::string result;
append(result, value);
return result;
}
inline std::string NumberFormatter::format(long value, int width)
{
std::string result;
append(result, value, width);
return result;
}
inline std::string NumberFormatter::format0(long value, int width)
{
std::string result;
append0(result, value, width);
return result;
}
inline std::string NumberFormatter::formatHex(long value)
{
std::string result;
appendHex(result, value);
return result;
}
inline std::string NumberFormatter::formatHex(long value, int width)
{
std::string result;
appendHex(result, value, width);
return result;
}
inline std::string NumberFormatter::format(unsigned long value)
{
std::string result;
append(result, value);
return result;
}
inline std::string NumberFormatter::format(unsigned long value, int width)
{
std::string result;
append(result, value, width);
return result;
}
inline std::string NumberFormatter::format0(unsigned long value, int width)
{
std::string result;
append0(result, value, width);
return result;
}
inline std::string NumberFormatter::formatHex(unsigned long value)
{
std::string result;
appendHex(result, value);
return result;
}
inline std::string NumberFormatter::formatHex(unsigned long value, int width)
{
std::string result;
appendHex(result, value, width);
return result;
}
#if defined(POCO_HAVE_INT64) && !defined(POCO_LONG_IS_64_BIT)
inline std::string NumberFormatter::format(Int64 value)
{
std::string result;
append(result, value);
return result;
}
inline std::string NumberFormatter::format(Int64 value, int width)
{
std::string result;
append(result, value, width);
return result;
}
inline std::string NumberFormatter::format0(Int64 value, int width)
{
std::string result;
append0(result, value, width);
return result;
}
inline std::string NumberFormatter::formatHex(Int64 value)
{
std::string result;
appendHex(result, value);
return result;
}
inline std::string NumberFormatter::formatHex(Int64 value, int width)
{
std::string result;
appendHex(result, value, width);
return result;
}
inline std::string NumberFormatter::format(UInt64 value)
{
std::string result;
append(result, value);
return result;
}
inline std::string NumberFormatter::format(UInt64 value, int width)
{
std::string result;
append(result, value, width);
return result;
}
inline std::string NumberFormatter::format0(UInt64 value, int width)
{
std::string result;
append0(result, value, width);
return result;
}
inline std::string NumberFormatter::formatHex(UInt64 value)
{
std::string result;
appendHex(result, value);
return result;
}
inline std::string NumberFormatter::formatHex(UInt64 value, int width)
{
std::string result;
appendHex(result, value, width);
return result;
}
#endif // defined(POCO_HAVE_INT64) && !defined(POCO_LONG_IS_64_BIT)
inline std::string NumberFormatter::format(float value)
{
std::string result;
append(result, value);
return result;
}
inline std::string NumberFormatter::format(double value)
{
std::string result;
append(result, value);
return result;
}
inline std::string NumberFormatter::format(double value, int precision)
{
std::string result;
append(result, value, precision);
return result;
}
inline std::string NumberFormatter::format(double value, int width, int precision)
{
std::string result;
append(result, value, width, precision);
return result;
}
inline std::string NumberFormatter::format(const void* ptr)
{
std::string result;
append(result, ptr);
return result;
}
} // namespace Poco

View File

@ -1,7 +1,7 @@
//
// ArchiveStrategy.cpp
//
// $Id: //poco/svn/Foundation/src/ArchiveStrategy.cpp#2 $
// $Id: //poco/Main/Foundation/src/ArchiveStrategy.cpp#9 $
//
// Library: Foundation
// Package: Logging
@ -186,7 +186,7 @@ LogFile* ArchiveByNumberStrategy::archive(LogFile* pFile)
{
path = basePath;
path.append(".");
path.append(NumberFormatter::format(++n));
NumberFormatter::append(path, ++n);
}
while (exists(path));
@ -196,11 +196,11 @@ LogFile* ArchiveByNumberStrategy::archive(LogFile* pFile)
if (n > 0)
{
oldPath.append(".");
oldPath.append(NumberFormatter::format(n - 1));
NumberFormatter::append(oldPath, n - 1);
}
std::string newPath = basePath;
newPath.append(".");
newPath.append(NumberFormatter::format(n));
NumberFormatter::append(newPath, n);
moveFile(oldPath, newPath);
--n;
}

View File

@ -1,7 +1,7 @@
//
// DateTimeFormatter.cpp
//
// $Id: //poco/svn/Foundation/src/DateTimeFormatter.cpp#2 $
// $Id: //poco/Main/Foundation/src/DateTimeFormatter.cpp#13 $
//
// Library: Foundation
// Package: DateTime
@ -36,8 +36,6 @@
#include "Poco/DateTimeFormatter.h"
#include "Poco/DateTimeFormat.h"
#include "Poco/DateTime.h"
#include "Poco/LocalDateTime.h"
#include "Poco/Timestamp.h"
#include "Poco/NumberFormatter.h"
@ -45,17 +43,8 @@
namespace Poco {
std::string DateTimeFormatter::format(const Timestamp& timestamp, const std::string& fmt, int timeZoneDifferential)
void DateTimeFormatter::append(std::string& str, const DateTime& dateTime, const std::string& fmt, int timeZoneDifferential)
{
DateTime dateTime(timestamp);
return format(dateTime, fmt, timeZoneDifferential);
}
std::string DateTimeFormatter::format(const DateTime& dateTime, const std::string& fmt, int timeZoneDifferential)
{
std::string result;
result.reserve(64);
std::string::const_iterator it = fmt.begin();
std::string::const_iterator end = fmt.end();
while (it != end)
@ -66,49 +55,40 @@ std::string DateTimeFormatter::format(const DateTime& dateTime, const std::strin
{
switch (*it)
{
case 'w': result.append(DateTimeFormat::WEEKDAY_NAMES[dateTime.dayOfWeek()], 0, 3); break;
case 'W': result.append(DateTimeFormat::WEEKDAY_NAMES[dateTime.dayOfWeek()]); break;
case 'b': result.append(DateTimeFormat::MONTH_NAMES[dateTime.month() - 1], 0, 3); break;
case 'B': result.append(DateTimeFormat::MONTH_NAMES[dateTime.month() - 1]); break;
case 'd': result.append(NumberFormatter::format0(dateTime.day(), 2)); break;
case 'e': result.append(NumberFormatter::format(dateTime.day())); break;
case 'f': result.append(NumberFormatter::format(dateTime.day(), 2)); break;
case 'm': result.append(NumberFormatter::format0(dateTime.month(), 2)); break;
case 'n': result.append(NumberFormatter::format(dateTime.month())); break;
case 'o': result.append(NumberFormatter::format(dateTime.month(), 2)); break;
case 'y': result.append(NumberFormatter::format0(dateTime.year() % 100, 2)); break;
case 'Y': result.append(NumberFormatter::format0(dateTime.year(), 4)); break;
case 'H': result.append(NumberFormatter::format0(dateTime.hour(), 2)); break;
case 'h': result.append(NumberFormatter::format0(dateTime.hourAMPM(), 2)); break;
case 'a': result.append(dateTime.isAM() ? "am" : "pm"); break;
case 'A': result.append(dateTime.isAM() ? "AM" : "PM"); break;
case 'M': result.append(NumberFormatter::format0(dateTime.minute(), 2)); break;
case 'S': result.append(NumberFormatter::format0(dateTime.second(), 2)); break;
case 'i': result.append(NumberFormatter::format0(dateTime.millisecond(), 3)); break;
case 'c': result.append(NumberFormatter::format(dateTime.millisecond()/100)); break;
case 'z': result.append(tzdISO(timeZoneDifferential)); break;
case 'Z': result.append(tzdRFC(timeZoneDifferential)); break;
default: result += *it;
case 'w': str.append(DateTimeFormat::WEEKDAY_NAMES[dateTime.dayOfWeek()], 0, 3); break;
case 'W': str.append(DateTimeFormat::WEEKDAY_NAMES[dateTime.dayOfWeek()]); break;
case 'b': str.append(DateTimeFormat::MONTH_NAMES[dateTime.month() - 1], 0, 3); break;
case 'B': str.append(DateTimeFormat::MONTH_NAMES[dateTime.month() - 1]); break;
case 'd': NumberFormatter::append0(str, dateTime.day(), 2); break;
case 'e': NumberFormatter::append(str, dateTime.day()); break;
case 'f': NumberFormatter::append(str, dateTime.day(), 2); break;
case 'm': NumberFormatter::append0(str, dateTime.month(), 2); break;
case 'n': NumberFormatter::append(str, dateTime.month()); break;
case 'o': NumberFormatter::append(str, dateTime.month(), 2); break;
case 'y': NumberFormatter::append0(str, dateTime.year() % 100, 2); break;
case 'Y': NumberFormatter::append0(str, dateTime.year(), 4); break;
case 'H': NumberFormatter::append0(str, dateTime.hour(), 2); break;
case 'h': NumberFormatter::append0(str, dateTime.hourAMPM(), 2); break;
case 'a': str.append(dateTime.isAM() ? "am" : "pm"); break;
case 'A': str.append(dateTime.isAM() ? "AM" : "PM"); break;
case 'M': NumberFormatter::append0(str, dateTime.minute(), 2); break;
case 'S': NumberFormatter::append0(str, dateTime.second(), 2); break;
case 'i': NumberFormatter::append0(str, dateTime.millisecond(), 3); break;
case 'c': NumberFormatter::append(str, dateTime.millisecond()/100); break;
case 'z': tzdISO(str, timeZoneDifferential); break;
case 'Z': tzdRFC(str, timeZoneDifferential); break;
default: str += *it;
}
++it;
}
}
else result += *it++;
else str += *it++;
}
return result;
}
std::string DateTimeFormatter::format(const LocalDateTime& dateTime, const std::string& fmt)
void DateTimeFormatter::append(std::string& str, const Timespan& timespan, const std::string& fmt)
{
return format(dateTime._dateTime, fmt, dateTime._tzd);
}
std::string DateTimeFormatter::format(const Timespan& timespan, const std::string& fmt)
{
std::string result;
result.reserve(32);
std::string::const_iterator it = fmt.begin();
std::string::const_iterator end = fmt.end();
while (it != end)
@ -119,73 +99,66 @@ std::string DateTimeFormatter::format(const Timespan& timespan, const std::strin
{
switch (*it)
{
case 'd': result.append(NumberFormatter::format(timespan.days())); break;
case 'H': result.append(NumberFormatter::format0(timespan.hours(), 2)); break;
case 'h': result.append(NumberFormatter::format(timespan.totalHours())); break;
case 'M': result.append(NumberFormatter::format0(timespan.minutes(), 2)); break;
case 'm': result.append(NumberFormatter::format(timespan.totalMinutes())); break;
case 'S': result.append(NumberFormatter::format0(timespan.seconds(), 2)); break;
case 's': result.append(NumberFormatter::format(timespan.totalSeconds())); break;
case 'i': result.append(NumberFormatter::format0(timespan.milliseconds(), 3)); break;
case 'c': result.append(NumberFormatter::format(timespan.milliseconds()/100)); break;
default: result += *it;
case 'd': NumberFormatter::append(str, timespan.days()); break;
case 'H': NumberFormatter::append0(str, timespan.hours(), 2); break;
case 'h': NumberFormatter::append(str, timespan.totalHours()); break;
case 'M': NumberFormatter::append0(str, timespan.minutes(), 2); break;
case 'm': NumberFormatter::append(str, timespan.totalMinutes()); break;
case 'S': NumberFormatter::append0(str, timespan.seconds(), 2); break;
case 's': NumberFormatter::append(str, timespan.totalSeconds()); break;
case 'i': NumberFormatter::append0(str, timespan.milliseconds(), 3); break;
case 'c': NumberFormatter::append(str, timespan.milliseconds()/100); break;
default: str += *it;
}
++it;
}
}
else result += *it++;
else str += *it++;
}
return result;
}
std::string DateTimeFormatter::tzdISO(int timeZoneDifferential)
void DateTimeFormatter::tzdISO(std::string& str, int timeZoneDifferential)
{
std::string tzd;
tzd.reserve(8);
if (timeZoneDifferential != UTC)
{
if (timeZoneDifferential >= 0)
{
tzd += '+';
tzd += NumberFormatter::format0(timeZoneDifferential/3600, 2);
tzd += ':';
tzd += NumberFormatter::format0((timeZoneDifferential%3600)/60, 2);
str += '+';
NumberFormatter::append0(str, timeZoneDifferential/3600, 2);
str += ':';
NumberFormatter::append0(str, (timeZoneDifferential%3600)/60, 2);
}
else
{
tzd += '-';
tzd += NumberFormatter::format0(-timeZoneDifferential/3600, 2);
tzd += ':';
tzd += NumberFormatter::format0((-timeZoneDifferential%3600)/60, 2);
str += '-';
NumberFormatter::append0(str, -timeZoneDifferential/3600, 2);
str += ':';
NumberFormatter::append0(str, (-timeZoneDifferential%3600)/60, 2);
}
}
else tzd = "Z";
return tzd;
else str += 'Z';
}
std::string DateTimeFormatter::tzdRFC(int timeZoneDifferential)
void DateTimeFormatter::tzdRFC(std::string& str, int timeZoneDifferential)
{
std::string tzd;
tzd.reserve(8);
if (timeZoneDifferential != UTC)
{
if (timeZoneDifferential >= 0)
{
tzd += '+';
tzd += NumberFormatter::format0(timeZoneDifferential/3600, 2);
tzd += NumberFormatter::format0((timeZoneDifferential%3600)/60, 2);
str += '+';
NumberFormatter::append0(str, timeZoneDifferential/3600, 2);
NumberFormatter::append0(str, (timeZoneDifferential%3600)/60, 2);
}
else
{
tzd += '-';
tzd += NumberFormatter::format0(-timeZoneDifferential/3600, 2);
tzd += NumberFormatter::format0((-timeZoneDifferential%3600)/60, 2);
str += '-';
NumberFormatter::append0(str, -timeZoneDifferential/3600, 2);
NumberFormatter::append0(str, (-timeZoneDifferential%3600)/60, 2);
}
}
else tzd = "GMT";
return tzd;
else str += "GMT";
}

View File

@ -1,13 +1,13 @@
//
// NumberFormatter.cpp
//
// $Id: //poco/svn/Foundation/src/NumberFormatter.cpp#2 $
// $Id: //poco/Main/Foundation/src/NumberFormatter.cpp#13 $
//
// Library: Foundation
// Package: Core
// Module: NumberFormatter
//
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
// Copyright (c) 2004-2008, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// Permission is hereby granted, free of charge, to any person or organization
@ -51,325 +51,325 @@
namespace Poco {
std::string NumberFormatter::format(int value)
void NumberFormatter::append(std::string& str, int value)
{
char buffer[64];
std::sprintf(buffer, "%d", value);
return std::string(buffer);
str.append(buffer);
}
std::string NumberFormatter::format(int value, int width)
void NumberFormatter::append(std::string& str, int value, int width)
{
poco_assert (width > 0 && width < 64);
char buffer[64];
std::sprintf(buffer, "%*d", width, value);
return std::string(buffer);
str.append(buffer);
}
std::string NumberFormatter::format0(int value, int width)
void NumberFormatter::append0(std::string& str, int value, int width)
{
poco_assert (width > 0 && width < 64);
char buffer[64];
std::sprintf(buffer, "%0*d", width, value);
return std::string(buffer);
str.append(buffer);
}
std::string NumberFormatter::formatHex(int value)
void NumberFormatter::appendHex(std::string& str, int value)
{
char buffer[64];
std::sprintf(buffer, "%X", value);
return std::string(buffer);
str.append(buffer);
}
std::string NumberFormatter::formatHex(int value, int width)
void NumberFormatter::appendHex(std::string& str, int value, int width)
{
poco_assert (width > 0 && width < 64);
char buffer[64];
std::sprintf(buffer, "%0*X", width, value);
return std::string(buffer);
str.append(buffer);
}
std::string NumberFormatter::format(unsigned value)
void NumberFormatter::append(std::string& str, unsigned value)
{
char buffer[64];
std::sprintf(buffer, "%u", value);
return std::string(buffer);
str.append(buffer);
}
std::string NumberFormatter::format(unsigned value, int width)
void NumberFormatter::append(std::string& str, unsigned value, int width)
{
poco_assert (width > 0 && width < 64);
char buffer[64];
std::sprintf(buffer, "%*u", width, value);
return std::string(buffer);
str.append(buffer);
}
std::string NumberFormatter::format0(unsigned int value, int width)
void NumberFormatter::append0(std::string& str, unsigned int value, int width)
{
poco_assert (width > 0 && width < 64);
char buffer[64];
std::sprintf(buffer, "%0*u", width, value);
return std::string(buffer);
str.append(buffer);
}
std::string NumberFormatter::formatHex(unsigned value)
void NumberFormatter::appendHex(std::string& str, unsigned value)
{
char buffer[64];
std::sprintf(buffer, "%X", value);
return std::string(buffer);
str.append(buffer);
}
std::string NumberFormatter::formatHex(unsigned value, int width)
void NumberFormatter::appendHex(std::string& str, unsigned value, int width)
{
poco_assert (width > 0 && width < 64);
char buffer[64];
std::sprintf(buffer, "%0*X", width, value);
return std::string(buffer);
str.append(buffer);
}
std::string NumberFormatter::format(long value)
void NumberFormatter::append(std::string& str, long value)
{
char buffer[64];
std::sprintf(buffer, "%ld", value);
return std::string(buffer);
str.append(buffer);
}
std::string NumberFormatter::format(long value, int width)
void NumberFormatter::append(std::string& str, long value, int width)
{
poco_assert (width > 0 && width < 64);
char buffer[64];
std::sprintf(buffer, "%*ld", width, value);
return std::string(buffer);
str.append(buffer);
}
std::string NumberFormatter::format0(long value, int width)
void NumberFormatter::append0(std::string& str, long value, int width)
{
poco_assert (width > 0 && width < 64);
char buffer[64];
std::sprintf(buffer, "%0*ld", width, value);
return std::string(buffer);
str.append(buffer);
}
std::string NumberFormatter::formatHex(long value)
void NumberFormatter::appendHex(std::string& str, long value)
{
char buffer[64];
std::sprintf(buffer, "%lX", value);
return std::string(buffer);
str.append(buffer);
}
std::string NumberFormatter::formatHex(long value, int width)
void NumberFormatter::appendHex(std::string& str, long value, int width)
{
poco_assert (width > 0 && width < 64);
char buffer[64];
std::sprintf(buffer, "%0*lX", width, value);
return std::string(buffer);
str.append(buffer);
}
std::string NumberFormatter::format(unsigned long value)
void NumberFormatter::append(std::string& str, unsigned long value)
{
char buffer[64];
std::sprintf(buffer, "%lu", value);
return std::string(buffer);
str.append(buffer);
}
std::string NumberFormatter::format(unsigned long value, int width)
void NumberFormatter::append(std::string& str, unsigned long value, int width)
{
poco_assert (width > 0 && width < 64);
char buffer[64];
std::sprintf(buffer, "%*lu", width, value);
return std::string(buffer);
str.append(buffer);
}
std::string NumberFormatter::format0(unsigned long value, int width)
void NumberFormatter::append0(std::string& str, unsigned long value, int width)
{
poco_assert (width > 0 && width < 64);
char buffer[64];
std::sprintf(buffer, "%0*lu", width, value);
return std::string(buffer);
str.append(buffer);
}
std::string NumberFormatter::formatHex(unsigned long value)
void NumberFormatter::appendHex(std::string& str, unsigned long value)
{
char buffer[64];
std::sprintf(buffer, "%lX", value);
return std::string(buffer);
str.append(buffer);
}
std::string NumberFormatter::formatHex(unsigned long value, int width)
void NumberFormatter::appendHex(std::string& str, unsigned long value, int width)
{
poco_assert (width > 0 && width < 64);
char buffer[64];
std::sprintf(buffer, "%0*lX", width, value);
return std::string(buffer);
str.append(buffer);
}
#if defined(POCO_HAVE_INT64) && !defined(POCO_LONG_IS_64_BIT)
std::string NumberFormatter::format(Int64 value)
void NumberFormatter::append(std::string& str, Int64 value)
{
char buffer[64];
std::sprintf(buffer, "%"I64_FMT"d", value);
return std::string(buffer);
str.append(buffer);
}
std::string NumberFormatter::format(Int64 value, int width)
void NumberFormatter::append(std::string& str, Int64 value, int width)
{
poco_assert (width > 0 && width < 64);
char buffer[64];
std::sprintf(buffer, "%*"I64_FMT"d", width, value);
return std::string(buffer);
str.append(buffer);
}
std::string NumberFormatter::format0(Int64 value, int width)
void NumberFormatter::append0(std::string& str, Int64 value, int width)
{
poco_assert (width > 0 && width < 64);
char buffer[64];
std::sprintf(buffer, "%0*"I64_FMT"d", width, value);
return std::string(buffer);
str.append(buffer);
}
std::string NumberFormatter::formatHex(Int64 value)
void NumberFormatter::appendHex(std::string& str, Int64 value)
{
char buffer[64];
std::sprintf(buffer, "%"I64_FMT"X", value);
return std::string(buffer);
str.append(buffer);
}
std::string NumberFormatter::formatHex(Int64 value, int width)
void NumberFormatter::appendHex(std::string& str, Int64 value, int width)
{
poco_assert (width > 0 && width < 64);
char buffer[64];
std::sprintf(buffer, "%0*"I64_FMT"X", width, value);
return std::string(buffer);
str.append(buffer);
}
std::string NumberFormatter::format(UInt64 value)
void NumberFormatter::append(std::string& str, UInt64 value)
{
char buffer[64];
std::sprintf(buffer, "%"I64_FMT"u", value);
return std::string(buffer);
str.append(buffer);
}
std::string NumberFormatter::format(UInt64 value, int width)
void NumberFormatter::append(std::string& str, UInt64 value, int width)
{
poco_assert (width > 0 && width < 64);
char buffer[64];
std::sprintf(buffer, "%*"I64_FMT"u", width, value);
return std::string(buffer);
str.append(buffer);
}
std::string NumberFormatter::format0(UInt64 value, int width)
void NumberFormatter::append0(std::string& str, UInt64 value, int width)
{
poco_assert (width > 0 && width < 64);
char buffer[64];
std::sprintf(buffer, "%0*"I64_FMT"u", width, value);
return std::string(buffer);
str.append(buffer);
}
std::string NumberFormatter::formatHex(UInt64 value)
void NumberFormatter::appendHex(std::string& str, UInt64 value)
{
char buffer[64];
std::sprintf(buffer, "%"I64_FMT"X", value);
return std::string(buffer);
str.append(buffer);
}
std::string NumberFormatter::formatHex(UInt64 value, int width)
void NumberFormatter::appendHex(std::string& str, UInt64 value, int width)
{
poco_assert (width > 0 && width < 64);
char buffer[64];
std::sprintf(buffer, "%0*"I64_FMT"X", width, value);
return std::string(buffer);
str.append(buffer);
}
#endif // defined(POCO_HAVE_INT64) && !defined(POCO_LONG_IS_64_BIT)
std::string NumberFormatter::format(float value)
void NumberFormatter::append(std::string& str, float value)
{
char buffer[64];
std::sprintf(buffer, "%.*g", 10, (double) value);
return std::string(buffer);
std::sprintf(buffer, "%.*g", 8, (double) value);
str.append(buffer);
}
std::string NumberFormatter::format(double value)
void NumberFormatter::append(std::string& str, double value)
{
char buffer[64];
std::sprintf(buffer, "%.*g", 20, value);
return std::string(buffer);
std::sprintf(buffer, "%.*g", 16, value);
str.append(buffer);
}
std::string NumberFormatter::format(double value, int precision)
void NumberFormatter::append(std::string& str, double value, int precision)
{
poco_assert (precision >= 0 && precision < 32);
char buffer[64];
std::sprintf(buffer, "%.*f", precision, value);
return std::string(buffer);
str.append(buffer);
}
std::string NumberFormatter::format(double value, int width, int precision)
void NumberFormatter::append(std::string& str, double value, int width, int precision)
{
poco_assert (width > 0 && width < 64 && precision >= 0 && precision < width);
char buffer[64];
std::sprintf(buffer, "%*.*f", width, precision, value);
return std::string(buffer);
str.append(buffer);
}
std::string NumberFormatter::format(const void* ptr)
void NumberFormatter::append(std::string& str, const void* ptr)
{
char buffer[24];
#if defined(POCO_PTR_IS_64_BIT)
@ -377,7 +377,7 @@ std::string NumberFormatter::format(const void* ptr)
#else
std::sprintf(buffer, "%08lX", (UIntPtr) ptr);
#endif
return std::string(buffer);
str.append(buffer);
}

View File

@ -1,7 +1,7 @@
//
// RotateStrategy.cpp
//
// $Id: //poco/1.3/Foundation/src/RotateStrategy.cpp#2 $
// $Id: //poco/Main/Foundation/src/RotateStrategy.cpp#8 $
//
// Library: Foundation
// Package: Logging
@ -101,7 +101,7 @@ bool RotateByIntervalStrategy::mustRotate(LogFile* pFile)
{
_lastRotate.update();
std::string tag(ROTATE_TEXT);
tag += DateTimeFormatter::format(_lastRotate, DateTimeFormat::RFC1036_FORMAT);
DateTimeFormatter::append(tag, _lastRotate, DateTimeFormat::RFC1036_FORMAT);
pFile->write(tag);
}
}

View File

@ -1,7 +1,7 @@
//
// URI.cpp
//
// $Id: //poco/svn/Foundation/src/URI.cpp#2 $
// $Id: //poco/Main/Foundation/src/URI.cpp#15 $
//
// Library: Foundation
// Package: URI
@ -293,7 +293,7 @@ std::string URI::getAuthority() const
if (_port && !isWellKnownPort())
{
auth += ':';
auth.append(NumberFormatter::format(_port));
NumberFormatter::append(auth, _port);
}
return auth;
}

View File

@ -1,7 +1,7 @@
//
// HTTPChunkedStream.cpp
//
// $Id: //poco/svn/Net/src/HTTPChunkedStream.cpp#2 $
// $Id: //poco/Main/Net/src/HTTPChunkedStream.cpp#17 $
//
// Library: Net
// Package: HTTP
@ -113,7 +113,8 @@ int HTTPChunkedStreamBuf::readFromDevice(char* buffer, std::streamsize length)
int HTTPChunkedStreamBuf::writeToDevice(const char* buffer, std::streamsize length)
{
_chunkBuffer = NumberFormatter::formatHex(length);
_chunkBuffer.clear();
NumberFormatter::appendHex(_chunkBuffer, length);
_chunkBuffer.append("\r\n", 2);
_chunkBuffer.append(buffer, static_cast<std::string::size_type>(length));
_chunkBuffer.append("\r\n", 2);

View File

@ -1,7 +1,7 @@
//
// HTTPClientSession.cpp
//
// $Id: //poco/svn/Net/src/HTTPClientSession.cpp#2 $
// $Id: //poco/Main/Net/src/HTTPClientSession.cpp#20 $
//
// Library: Net
// Package: HTTPClient
@ -301,7 +301,7 @@ std::string HTTPClientSession::getHostInfo() const
std::string result("http://");
result.append(_host);
result.append(":");
result.append(NumberFormatter::format(_port));
NumberFormatter::append(result, _port);
return result;
}

View File

@ -1,7 +1,7 @@
//
// HTTPCookie.cpp
//
// $Id: //poco/Main/Net/src/HTTPCookie.cpp#8 $
// $Id: //poco/Main/Net/src/HTTPCookie.cpp#9 $
//
// Library: Net
// Package: HTTP
@ -260,7 +260,7 @@ std::string HTTPCookie::toString() const
Timestamp ts;
ts += _maxAge*Timestamp::resolution();
result.append("; expires=");
result.append(DateTimeFormatter::format(ts, DateTimeFormat::HTTP_FORMAT));
DateTimeFormatter::append(result, ts, DateTimeFormat::HTTP_FORMAT);
}
if (_secure)
{
@ -298,7 +298,7 @@ std::string HTTPCookie::toString() const
if (_maxAge >= 0)
{
result.append("; Max-Age=\"");
result.append(NumberFormatter::format(_maxAge));
NumberFormatter::append(result, _maxAge);
result.append("\"");
}
if (_secure)

View File

@ -1,7 +1,7 @@
//
// HTTPRequest.cpp
//
// $Id: //poco/Main/Net/src/HTTPRequest.cpp#13 $
// $Id: //poco/Main/Net/src/HTTPRequest.cpp#14 $
//
// Library: Net
// Package: HTTP
@ -121,7 +121,7 @@ void HTTPRequest::setHost(const std::string& host, Poco::UInt16 port)
if (port != HTTPSession::HTTP_PORT)
{
value.append(":");
value.append(NumberFormatter::format(port));
NumberFormatter::append(value, port);
}
setHost(value);
}

View File

@ -1,7 +1,7 @@
//
// HTTPResponse.cpp
//
// $Id: //poco/svn/Net/src/HTTPResponse.cpp#2 $
// $Id: //poco/Main/Net/src/HTTPResponse.cpp#13 $
//
// Library: Net
// Package: HTTP
@ -212,7 +212,7 @@ void HTTPResponse::getCookies(std::vector<HTTPCookie>& cookies) const
void HTTPResponse::write(std::ostream& ostr) const
{
ostr << getVersion() << " " << NumberFormatter::format((int) _status) << " " << _reason << "\r\n";
ostr << getVersion() << " " << static_cast<int>(_status) << " " << _reason << "\r\n";
HTTPMessage::write(ostr);
ostr << "\r\n";
}

View File

@ -1,7 +1,7 @@
//
// IPAddress.cpp
//
// $Id: //poco/svn/Net/src/IPAddress.cpp#3 $
// $Id: //poco/Main/Net/src/IPAddress.cpp#20 $
//
// Library: Net
// Package: NetCore
@ -117,13 +117,13 @@ public:
const UInt8* bytes = reinterpret_cast<const UInt8*>(&_addr);
std::string result;
result.reserve(16);
result.append(NumberFormatter::format(bytes[0]));
NumberFormatter::append(result, bytes[0]);
result.append(".");
result.append(NumberFormatter::format(bytes[1]));
NumberFormatter::append(result, bytes[1]);
result.append(".");
result.append(NumberFormatter::format(bytes[2]));
NumberFormatter::append(result, bytes[2]);
result.append(".");
result.append(NumberFormatter::format(bytes[3]));
NumberFormatter::append(result, bytes[3]);
return result;
}
@ -295,13 +295,13 @@ public:
else
result.append("::FFFF:");
const UInt8* bytes = reinterpret_cast<const UInt8*>(&_addr);
result.append(NumberFormatter::format(bytes[12]));
NumberFormatter::append(result, bytes[12]);
result.append(".");
result.append(NumberFormatter::format(bytes[13]));
NumberFormatter::append(result, bytes[13]);
result.append(".");
result.append(NumberFormatter::format(bytes[14]));
NumberFormatter::append(result, bytes[14]);
result.append(".");
result.append(NumberFormatter::format(bytes[15]));
NumberFormatter::append(result, bytes[15]);
return result;
}
else
@ -324,7 +324,7 @@ public:
}
}
if (i > 0) result.append(":");
if (i < 8) result.append(NumberFormatter::formatHex(ntohs(words[i++])));
if (i < 8) NumberFormatter::appendHex(result, ntohs(words[i++])));
}
return result;
}

View File

@ -1,7 +1,7 @@
//
// MultipartWriter.cpp
//
// $Id: //poco/svn/Net/src/MultipartWriter.cpp#2 $
// $Id: //poco/Main/Net/src/MultipartWriter.cpp#8 $
//
// Library: Net
// Package: Messages
@ -99,8 +99,8 @@ std::string MultipartWriter::createBoundary()
{
std::string boundary("MIME_boundary_");
Random rnd;
boundary.append(NumberFormatter::formatHex(rnd.next(), 8));
boundary.append(NumberFormatter::formatHex(rnd.next(), 8));
NumberFormatter::appendHex(boundary, rnd.next(), 8);
NumberFormatter::appendHex(boundary, rnd.next(), 8);
return boundary;
}

View File

@ -1,355 +1,355 @@
//
// RemoteSyslogChannel.cpp
//
// $Id: //poco/svn/Net/src/RemoteSyslogChannel.cpp#2 $
//
// Library: Net
// Package: Logging
// Module: RemoteSyslogChannel
//
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the
// Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following:
//
// The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer,
// must be included in all copies of the Software, in whole or in part, and
// all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by
// a source language processor.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
#include "Poco/Net/RemoteSyslogChannel.h"
#include "Poco/Message.h"
#include "Poco/DateTimeFormatter.h"
#include "Poco/NumberFormatter.h"
#include "Poco/Net/SocketAddress.h"
#include "Poco/Net/DNS.h"
#include "Poco/LoggingFactory.h"
#include "Poco/Instantiator.h"
#include "Poco/String.h"
namespace Poco {
namespace Net {
const std::string RemoteSyslogChannel::BSD_TIMEFORMAT("%b %f %H:%M:%S");
const std::string RemoteSyslogChannel::SYSLOG_TIMEFORMAT("%Y-%m-%dT%H:%M:%S.%i%z");
const std::string RemoteSyslogChannel::PROP_NAME("name");
const std::string RemoteSyslogChannel::PROP_FACILITY("facility");
const std::string RemoteSyslogChannel::PROP_FORMAT("format");
const std::string RemoteSyslogChannel::PROP_LOGHOST("loghost");
const std::string RemoteSyslogChannel::PROP_HOST("host");
RemoteSyslogChannel::RemoteSyslogChannel():
_logHost("localhost"),
_name("-"),
_facility(SYSLOG_USER),
_bsdFormat(false),
_open(false)
{
}
RemoteSyslogChannel::RemoteSyslogChannel(const std::string& address, const std::string& name, int facility, bool bsdFormat):
_logHost(address),
_name(name),
_facility(facility),
_bsdFormat(bsdFormat),
_open(false)
{
if (_name.empty()) _name = "-";
}
RemoteSyslogChannel::~RemoteSyslogChannel()
{
close();
}
void RemoteSyslogChannel::open()
{
if (_open) return;
SocketAddress sa;
if (_logHost.find(':') != std::string::npos)
sa = SocketAddress(_logHost);
else
sa = SocketAddress(_logHost, SYSLOG_PORT);
_socket.connect(sa);
if (_host.empty())
{
try
{
_host = DNS::thisHost().name();
}
catch (Poco::Exception&)
{
_host = _socket.address().host().toString();
}
}
}
void RemoteSyslogChannel::close()
{
if (_open)
{
_socket.close();
_open = false;
}
}
void RemoteSyslogChannel::log(const Message& msg)
{
if (!_open) open();
std::string m;
m.reserve(1024);
m += '<';
m += Poco::NumberFormatter::format(getPrio(msg) + _facility);
m += '>';
if (_bsdFormat)
{
m += Poco::DateTimeFormatter::format(msg.getTime(), BSD_TIMEFORMAT);
m += ' ';
m += _host;
}
else
{
m += "1 "; // version
m += Poco::DateTimeFormatter::format(msg.getTime(), SYSLOG_TIMEFORMAT);
m += ' ';
m += _host;
m += ' ';
m += _name;
m += ' ';
m += Poco::NumberFormatter::format(msg.getPid());
m += ' ';
m += msg.getSource();
}
m += ' ';
m += msg.getText();
_socket.sendBytes(m.data(), (int) m.size());
}
void RemoteSyslogChannel::setProperty(const std::string& name, const std::string& value)
{
if (name == PROP_NAME)
{
_name = value;
if (_name.empty()) _name = "-";
}
else if (name == PROP_FACILITY)
{
std::string facility;
if (Poco::icompare(value, 4, "LOG_") == 0)
facility = Poco::toUpper(value.substr(4));
else if (Poco::icompare(value, 4, "SYSLOG_") == 0)
facility = Poco::toUpper(value.substr(7));
else
facility = Poco::toUpper(value);
if (facility == "KERN")
_facility = SYSLOG_KERN;
else if (facility == "USER")
_facility = SYSLOG_USER;
else if (facility == "MAIL")
_facility = SYSLOG_MAIL;
else if (facility == "DAEMON")
_facility = SYSLOG_DAEMON;
else if (facility == "AUTH")
_facility = SYSLOG_AUTH;
else if (facility == "AUTHPRIV")
_facility = SYSLOG_AUTHPRIV;
else if (facility == "SYSLOG")
_facility = SYSLOG_SYSLOG;
else if (facility == "LPR")
_facility = SYSLOG_LPR;
else if (facility == "NEWS")
_facility = SYSLOG_NEWS;
else if (facility == "UUCP")
_facility = SYSLOG_UUCP;
else if (facility == "CRON")
_facility = SYSLOG_CRON;
else if (facility == "FTP")
_facility = SYSLOG_FTP;
else if (facility == "NTP")
_facility = SYSLOG_NTP;
else if (facility == "LOGAUDIT")
_facility = SYSLOG_LOGAUDIT;
else if (facility == "LOGALERT")
_facility = SYSLOG_LOGALERT;
else if (facility == "CLOCK")
_facility = SYSLOG_CLOCK;
else if (facility == "LOCAL0")
_facility = SYSLOG_LOCAL0;
else if (facility == "LOCAL1")
_facility = SYSLOG_LOCAL1;
else if (facility == "LOCAL2")
_facility = SYSLOG_LOCAL2;
else if (facility == "LOCAL3")
_facility = SYSLOG_LOCAL3;
else if (facility == "LOCAL4")
_facility = SYSLOG_LOCAL4;
else if (facility == "LOCAL5")
_facility = SYSLOG_LOCAL5;
else if (facility == "LOCAL6")
_facility = SYSLOG_LOCAL6;
else if (facility == "LOCAL7")
_facility = SYSLOG_LOCAL7;
}
else if (name == PROP_LOGHOST)
{
_logHost = value;
}
else if (name == PROP_HOST)
{
_host = value;
}
else if (name == PROP_FORMAT)
{
_bsdFormat = (value == "bsd");
}
else
{
Channel::setProperty(name, value);
}
}
std::string RemoteSyslogChannel::getProperty(const std::string& name) const
{
if (name == PROP_NAME)
{
if (_name != "-")
return _name;
else
return "";
}
else if (name == PROP_FACILITY)
{
if (_facility == SYSLOG_KERN)
return "KERN";
else if (_facility == SYSLOG_USER)
return "USER";
else if (_facility == SYSLOG_MAIL)
return "MAIL";
else if (_facility == SYSLOG_DAEMON)
return "DAEMON";
else if (_facility == SYSLOG_AUTH)
return "AUTH";
else if (_facility == SYSLOG_AUTHPRIV)
return "AUTHPRIV";
else if (_facility == SYSLOG_SYSLOG)
return "SYSLOG";
else if (_facility == SYSLOG_LPR)
return "LPR";
else if (_facility == SYSLOG_NEWS)
return "NEWS";
else if (_facility == SYSLOG_UUCP)
return "UUCP";
else if (_facility == SYSLOG_CRON)
return "CRON";
else if (_facility == SYSLOG_FTP)
return "FTP";
else if (_facility == SYSLOG_NTP)
return "NTP";
else if (_facility == SYSLOG_LOGAUDIT)
return "LOGAUDIT";
else if (_facility == SYSLOG_LOGALERT)
return "LOGALERT";
else if (_facility == SYSLOG_CLOCK)
return "CLOCK";
else if (_facility == SYSLOG_LOCAL0)
return "LOCAL0";
else if (_facility == SYSLOG_LOCAL1)
return "LOCAL1";
else if (_facility == SYSLOG_LOCAL2)
return "LOCAL2";
else if (_facility == SYSLOG_LOCAL3)
return "LOCAL3";
else if (_facility == SYSLOG_LOCAL4)
return "LOCAL4";
else if (_facility == SYSLOG_LOCAL5)
return "LOCAL5";
else if (_facility == SYSLOG_LOCAL6)
return "LOCAL6";
else if (_facility == SYSLOG_LOCAL7)
return "LOCAL7";
else
return "";
}
else if (name == PROP_LOGHOST)
{
return _logHost;
}
else if (name == PROP_HOST)
{
return _host;
}
else if (name == PROP_FORMAT)
{
return _bsdFormat ? "bsd" : "new";
}
else
{
return Channel::getProperty(name);
}
}
int RemoteSyslogChannel::getPrio(const Message& msg)
{
switch (msg.getPriority())
{
case Message::PRIO_TRACE:
case Message::PRIO_DEBUG:
return SYSLOG_DEBUG;
case Message::PRIO_INFORMATION:
return SYSLOG_INFORMATIONAL;
case Message::PRIO_NOTICE:
return SYSLOG_NOTICE;
case Message::PRIO_WARNING:
return SYSLOG_WARNING;
case Message::PRIO_ERROR:
return SYSLOG_ERROR;
case Message::PRIO_CRITICAL:
return SYSLOG_CRITICAL;
case Message::PRIO_FATAL:
return SYSLOG_ALERT;
default:
return 0;
}
}
void RemoteSyslogChannel::registerChannel()
{
Poco::LoggingFactory::defaultFactory().registerChannelClass("RemoteSyslogChannel", new Poco::Instantiator<RemoteSyslogChannel, Poco::Channel>);
}
} } // namespace Poco::Net
//
// RemoteSyslogChannel.cpp
//
// $Id: //poco/Main/Net/src/RemoteSyslogChannel.cpp#4 $
//
// Library: Net
// Package: Logging
// Module: RemoteSyslogChannel
//
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the
// Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following:
//
// The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer,
// must be included in all copies of the Software, in whole or in part, and
// all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by
// a source language processor.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
#include "Poco/Net/RemoteSyslogChannel.h"
#include "Poco/Message.h"
#include "Poco/DateTimeFormatter.h"
#include "Poco/NumberFormatter.h"
#include "Poco/Net/SocketAddress.h"
#include "Poco/Net/DNS.h"
#include "Poco/LoggingFactory.h"
#include "Poco/Instantiator.h"
#include "Poco/String.h"
namespace Poco {
namespace Net {
const std::string RemoteSyslogChannel::BSD_TIMEFORMAT("%b %f %H:%M:%S");
const std::string RemoteSyslogChannel::SYSLOG_TIMEFORMAT("%Y-%m-%dT%H:%M:%S.%i%z");
const std::string RemoteSyslogChannel::PROP_NAME("name");
const std::string RemoteSyslogChannel::PROP_FACILITY("facility");
const std::string RemoteSyslogChannel::PROP_FORMAT("format");
const std::string RemoteSyslogChannel::PROP_LOGHOST("loghost");
const std::string RemoteSyslogChannel::PROP_HOST("host");
RemoteSyslogChannel::RemoteSyslogChannel():
_logHost("localhost"),
_name("-"),
_facility(SYSLOG_USER),
_bsdFormat(false),
_open(false)
{
}
RemoteSyslogChannel::RemoteSyslogChannel(const std::string& address, const std::string& name, int facility, bool bsdFormat):
_logHost(address),
_name(name),
_facility(facility),
_bsdFormat(bsdFormat),
_open(false)
{
if (_name.empty()) _name = "-";
}
RemoteSyslogChannel::~RemoteSyslogChannel()
{
close();
}
void RemoteSyslogChannel::open()
{
if (_open) return;
SocketAddress sa;
if (_logHost.find(':') != std::string::npos)
sa = SocketAddress(_logHost);
else
sa = SocketAddress(_logHost, SYSLOG_PORT);
_socket.connect(sa);
if (_host.empty())
{
try
{
_host = DNS::thisHost().name();
}
catch (Poco::Exception&)
{
_host = _socket.address().host().toString();
}
}
}
void RemoteSyslogChannel::close()
{
if (_open)
{
_socket.close();
_open = false;
}
}
void RemoteSyslogChannel::log(const Message& msg)
{
if (!_open) open();
std::string m;
m.reserve(1024);
m += '<';
Poco::NumberFormatter::append(m, getPrio(msg) + _facility);
m += '>';
if (_bsdFormat)
{
Poco::DateTimeFormatter::append(m, msg.getTime(), BSD_TIMEFORMAT);
m += ' ';
m += _host;
}
else
{
m += "1 "; // version
Poco::DateTimeFormatter::append(m, msg.getTime(), SYSLOG_TIMEFORMAT);
m += ' ';
m += _host;
m += ' ';
m += _name;
m += ' ';
Poco::NumberFormatter::append(m, msg.getPid());
m += ' ';
m += msg.getSource();
}
m += ' ';
m += msg.getText();
_socket.sendBytes(m.data(), (int) m.size());
}
void RemoteSyslogChannel::setProperty(const std::string& name, const std::string& value)
{
if (name == PROP_NAME)
{
_name = value;
if (_name.empty()) _name = "-";
}
else if (name == PROP_FACILITY)
{
std::string facility;
if (Poco::icompare(value, 4, "LOG_") == 0)
facility = Poco::toUpper(value.substr(4));
else if (Poco::icompare(value, 4, "SYSLOG_") == 0)
facility = Poco::toUpper(value.substr(7));
else
facility = Poco::toUpper(value);
if (facility == "KERN")
_facility = SYSLOG_KERN;
else if (facility == "USER")
_facility = SYSLOG_USER;
else if (facility == "MAIL")
_facility = SYSLOG_MAIL;
else if (facility == "DAEMON")
_facility = SYSLOG_DAEMON;
else if (facility == "AUTH")
_facility = SYSLOG_AUTH;
else if (facility == "AUTHPRIV")
_facility = SYSLOG_AUTHPRIV;
else if (facility == "SYSLOG")
_facility = SYSLOG_SYSLOG;
else if (facility == "LPR")
_facility = SYSLOG_LPR;
else if (facility == "NEWS")
_facility = SYSLOG_NEWS;
else if (facility == "UUCP")
_facility = SYSLOG_UUCP;
else if (facility == "CRON")
_facility = SYSLOG_CRON;
else if (facility == "FTP")
_facility = SYSLOG_FTP;
else if (facility == "NTP")
_facility = SYSLOG_NTP;
else if (facility == "LOGAUDIT")
_facility = SYSLOG_LOGAUDIT;
else if (facility == "LOGALERT")
_facility = SYSLOG_LOGALERT;
else if (facility == "CLOCK")
_facility = SYSLOG_CLOCK;
else if (facility == "LOCAL0")
_facility = SYSLOG_LOCAL0;
else if (facility == "LOCAL1")
_facility = SYSLOG_LOCAL1;
else if (facility == "LOCAL2")
_facility = SYSLOG_LOCAL2;
else if (facility == "LOCAL3")
_facility = SYSLOG_LOCAL3;
else if (facility == "LOCAL4")
_facility = SYSLOG_LOCAL4;
else if (facility == "LOCAL5")
_facility = SYSLOG_LOCAL5;
else if (facility == "LOCAL6")
_facility = SYSLOG_LOCAL6;
else if (facility == "LOCAL7")
_facility = SYSLOG_LOCAL7;
}
else if (name == PROP_LOGHOST)
{
_logHost = value;
}
else if (name == PROP_HOST)
{
_host = value;
}
else if (name == PROP_FORMAT)
{
_bsdFormat = (value == "bsd");
}
else
{
Channel::setProperty(name, value);
}
}
std::string RemoteSyslogChannel::getProperty(const std::string& name) const
{
if (name == PROP_NAME)
{
if (_name != "-")
return _name;
else
return "";
}
else if (name == PROP_FACILITY)
{
if (_facility == SYSLOG_KERN)
return "KERN";
else if (_facility == SYSLOG_USER)
return "USER";
else if (_facility == SYSLOG_MAIL)
return "MAIL";
else if (_facility == SYSLOG_DAEMON)
return "DAEMON";
else if (_facility == SYSLOG_AUTH)
return "AUTH";
else if (_facility == SYSLOG_AUTHPRIV)
return "AUTHPRIV";
else if (_facility == SYSLOG_SYSLOG)
return "SYSLOG";
else if (_facility == SYSLOG_LPR)
return "LPR";
else if (_facility == SYSLOG_NEWS)
return "NEWS";
else if (_facility == SYSLOG_UUCP)
return "UUCP";
else if (_facility == SYSLOG_CRON)
return "CRON";
else if (_facility == SYSLOG_FTP)
return "FTP";
else if (_facility == SYSLOG_NTP)
return "NTP";
else if (_facility == SYSLOG_LOGAUDIT)
return "LOGAUDIT";
else if (_facility == SYSLOG_LOGALERT)
return "LOGALERT";
else if (_facility == SYSLOG_CLOCK)
return "CLOCK";
else if (_facility == SYSLOG_LOCAL0)
return "LOCAL0";
else if (_facility == SYSLOG_LOCAL1)
return "LOCAL1";
else if (_facility == SYSLOG_LOCAL2)
return "LOCAL2";
else if (_facility == SYSLOG_LOCAL3)
return "LOCAL3";
else if (_facility == SYSLOG_LOCAL4)
return "LOCAL4";
else if (_facility == SYSLOG_LOCAL5)
return "LOCAL5";
else if (_facility == SYSLOG_LOCAL6)
return "LOCAL6";
else if (_facility == SYSLOG_LOCAL7)
return "LOCAL7";
else
return "";
}
else if (name == PROP_LOGHOST)
{
return _logHost;
}
else if (name == PROP_HOST)
{
return _host;
}
else if (name == PROP_FORMAT)
{
return _bsdFormat ? "bsd" : "new";
}
else
{
return Channel::getProperty(name);
}
}
int RemoteSyslogChannel::getPrio(const Message& msg)
{
switch (msg.getPriority())
{
case Message::PRIO_TRACE:
case Message::PRIO_DEBUG:
return SYSLOG_DEBUG;
case Message::PRIO_INFORMATION:
return SYSLOG_INFORMATIONAL;
case Message::PRIO_NOTICE:
return SYSLOG_NOTICE;
case Message::PRIO_WARNING:
return SYSLOG_WARNING;
case Message::PRIO_ERROR:
return SYSLOG_ERROR;
case Message::PRIO_CRITICAL:
return SYSLOG_CRITICAL;
case Message::PRIO_FATAL:
return SYSLOG_ALERT;
default:
return 0;
}
}
void RemoteSyslogChannel::registerChannel()
{
Poco::LoggingFactory::defaultFactory().registerChannelClass("RemoteSyslogChannel", new Poco::Instantiator<RemoteSyslogChannel, Poco::Channel>);
}
} } // namespace Poco::Net

View File

@ -1,7 +1,7 @@
//
// SocketAddress.cpp
//
// $Id: //poco/svn/Net/src/SocketAddress.cpp#2 $
// $Id: //poco/Main/Net/src/SocketAddress.cpp#14 $
//
// Library: Net
// Package: NetCore
@ -331,7 +331,7 @@ std::string SocketAddress::toString() const
if (host().family() == IPAddress::IPv6)
result.append("]");
result.append(":");
result.append(NumberFormatter::format(port()));
NumberFormatter::append(result, port());
return result;
}

View File

@ -1,7 +1,7 @@
//
// SystemConfiguration.h
//
// $Id: //poco/svn/Util/include/Poco/Util/SystemConfiguration.h#1 $
// $Id: //poco/Main/Util/include/Poco/Util/SystemConfiguration.h#4 $
//
// Library: Util
// Package: Configuration
@ -60,6 +60,8 @@ class Util_API SystemConfiguration: public AbstractConfiguration
/// - system.currentDir: the current working directory
/// - system.homeDir: the user's home directory
/// - system.tempDir: the system's temporary directory
/// - system.dateTime: the current UTC date and time, formatted in ISO 8601 format.
/// - system.pid: the current process ID.
/// - system.env.<NAME>: the environment variable with the given <NAME>.
///
/// An attempt to set a system variable will result in an
@ -88,6 +90,8 @@ private:
static const std::string CURRENTDIR;
static const std::string HOMEDIR;
static const std::string TEMPDIR;
static const std::string DATETIME;
static const std::string PID;
static const std::string ENV;
};

View File

@ -1,13 +1,13 @@
//
// PropertyFileConfiguration.cpp
//
// $Id: //poco/svn/Util/src/PropertyFileConfiguration.cpp#2 $
// $Id: //poco/Main/Util/src/PropertyFileConfiguration.cpp#11 $
//
// Library: Util
// Package: Configuration
// Module: PropertyFileConfiguration
//
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
// Copyright (c) 2004-2009, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// Permission is hereby granted, free of charge, to any person or organization
@ -150,34 +150,37 @@ void PropertyFileConfiguration::parseLine(std::istream& istr)
int PropertyFileConfiguration::readChar(std::istream& istr)
{
int c = istr.get();
if (c == '\\')
for (;;)
{
c = istr.get();
switch (c)
int c = istr.get();
if (c == '\\')
{
case 't':
return '\t';
case 'r':
return '\r';
case 'n':
return '\n';
case 'f':
return '\f';
case '\r':
if (istr.peek() == '\n')
istr.get();
return ' ';
case '\n':
return ' ';
default:
return c;
c = istr.get();
switch (c)
{
case 't':
return '\t';
case 'r':
return '\r';
case 'n':
return '\n';
case 'f':
return '\f';
case '\r':
if (istr.peek() == '\n')
istr.get();
continue;
case '\n':
continue;
default:
return c;
}
}
else if (c == '\n' || c == '\r')
return 0;
else
return c;
}
else if (c == '\n' || c == '\r')
return 0;
else
return c;
}

View File

@ -1,7 +1,7 @@
//
// ServerApplication.cpp
//
// $Id: //poco/svn/Util/src/ServerApplication.cpp#2 $
// $Id: //poco/Main/Util/src/ServerApplication.cpp#27 $
//
// Library: Util
// Package: Application
@ -43,11 +43,13 @@
#include "Poco/NamedEvent.h"
#include "Poco/Logger.h"
#if defined(POCO_OS_FAMILY_UNIX)
#include "Poco/TemporaryFile.h"
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <signal.h>
#include <sys/stat.h>
#include <fstream>
#elif defined(POCO_OS_FAMILY_WINDOWS)
#include "Poco/Util/WinService.h"
#include "Poco/UnWindows.h"
@ -221,7 +223,7 @@ void ServerApplication::waitForTerminationRequest()
{
SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE);
std::string evName("POCOTRM");
evName.append(NumberFormatter::formatHex(Process::id(), 8));
NumberFormatter::appendHex(evName, Process::id(), 8);
NamedEvent ev(evName);
ev.wait();
_terminated.set();
@ -465,9 +467,14 @@ void ServerApplication::beDaemon()
setsid();
umask(0);
close(0);
close(1);
close(2);
// attach stdin, stdout, stderr to /dev/null
// instead of just closing them. This avoids
// issues with third party/legacy code writing
// stuff to stdout/stderr.
freopen("/dev/null", "r+", stdin);
freopen("/dev/null", "r+", stdout);
freopen("/dev/null", "r+", stderr);
}
@ -479,6 +486,12 @@ void ServerApplication::defineOptions(OptionSet& options)
Option("daemon", "", "run application as a daemon")
.required(false)
.repeatable(false));
options.addOption(
Option("pidfile", "", "write PID to given file")
.required(false)
.repeatable(false)
.argument("path"));
}
@ -488,6 +501,15 @@ void ServerApplication::handleOption(const std::string& name, const std::string&
{
config().setBool("application.runAsDaemon", true);
}
else if (name == "pidfile")
{
std::ofstream ostr(value.c_str());
if (ostr.good())
ostr << Poco::Process::id() << std::endl;
else
throw Poco::CreateFileException("Cannot write PID to file", value);
Poco::TemporaryFile::registerForDeletion(value);
}
else Application::handleOption(name, value);
}
@ -526,7 +548,7 @@ void ServerApplication::waitForTerminationRequest()
sys$qiow(0, ioChan, IO$_SETMODE | IO$M_CTRLCAST, 0, 0, 0, terminate, 0, 0, 0, 0, 0);
std::string evName("POCOTRM");
evName.append(NumberFormatter::formatHex(Process::id(), 8));
NumberFormatter::appendHex(evName, Process::id(), 8);
NamedEvent ev(evName);
try
{

View File

@ -1,7 +1,7 @@
//
// SystemConfiguration.cpp
//
// $Id: //poco/svn/Util/src/SystemConfiguration.cpp#1 $
// $Id: //poco/Main/Util/src/SystemConfiguration.cpp#7 $
//
// Library: Util
// Package: Configuration
@ -37,6 +37,11 @@
#include "Poco/Util/SystemConfiguration.h"
#include "Poco/Environment.h"
#include "Poco/Path.h"
#include "Poco/DateTime.h"
#include "Poco/DateTimeFormatter.h"
#include "Poco/DateTimeFormat.h"
#include "Poco/NumberFormatter.h"
#include "Poco/Process.h"
#include "Poco/Exception.h"
@ -55,6 +60,8 @@ const std::string SystemConfiguration::NODENAME = "system.nodeName";
const std::string SystemConfiguration::CURRENTDIR = "system.currentDir";
const std::string SystemConfiguration::HOMEDIR = "system.homeDir";
const std::string SystemConfiguration::TEMPDIR = "system.tempDir";
const std::string SystemConfiguration::DATETIME = "system.dateTime";
const std::string SystemConfiguration::PID = "system.pid";
const std::string SystemConfiguration::ENV = "system.env.";
@ -84,6 +91,10 @@ bool SystemConfiguration::getRaw(const std::string& key, std::string& value) con
value = Path::home();
else if (key == TEMPDIR)
value = Path::temp();
else if (key == DATETIME)
value = Poco::DateTimeFormatter::format(Poco::DateTime(), Poco::DateTimeFormat::ISO8601_FORMAT);
else if (key == PID)
value = Poco::NumberFormatter::format(Poco::Process::id());
else if (key.compare(0, ENV.size(), ENV) == 0)
return getEnv(key.substr(ENV.size()), value);
else
@ -113,6 +124,8 @@ void SystemConfiguration::enumerate(const std::string& key, Keys& range) const
range.push_back("currentDir");
range.push_back("homeDir");
range.push_back("tempDir");
range.push_back("dateTime");
range.push_back("pid");
range.push_back("env");
}
}

View File

@ -1,7 +1,7 @@
//
// PropertyFileConfigurationTest.cpp
//
// $Id: //poco/svn/Util/testsuite/src/PropertyFileConfigurationTest.cpp#1 $
// $Id: //poco/Main/Util/testsuite/src/PropertyFileConfigurationTest.cpp#6 $
//
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
@ -66,8 +66,8 @@ void PropertyFileConfigurationTest::testLoad()
"prop3.prop31: value3\n"
"# comment\n"
" prop3.prop32: value 4\n"
"prop3.prop33: value5, value6,\\\n"
"value7, value8,\\\r\n"
"prop3.prop33: value5, value6, \\\n"
"value7, value8, \\\r\n"
"value9\n"
"prop4 = escaped[\\t\\r\\n\\f]\n"
"#prop4 = foo\n"

View File

@ -1,7 +1,7 @@
//
// SystemConfigurationTest.cpp
//
// $Id: //poco/svn/Util/testsuite/src/SystemConfigurationTest.cpp#1 $
// $Id: //poco/Main/Util/testsuite/src/SystemConfigurationTest.cpp#6 $
//
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
@ -38,6 +38,8 @@
#include "Poco/Exception.h"
#include "Poco/Environment.h"
#include "Poco/Path.h"
#include "Poco/Process.h"
#include "Poco/NumberParser.h"
#include <algorithm>
@ -72,6 +74,12 @@ void SystemConfigurationTest::testProperties()
assert (pConf->getString("system.homeDir") == Path::home());
assert (pConf->getString("system.tempDir") == Path::temp());
std::string dateTime = pConf->getString("system.dateTime");
assert (dateTime.size() == 20);
std::string pid = pConf->getString("system.pid");
assert (Poco::NumberParser::parse64(pid) == Poco::Process::id());
#if defined(POCO_OS_FAMILY_WINDOWS)
std::string home = pConf->getString("system.env.HOMEPATH");
#else
@ -91,7 +99,7 @@ void SystemConfigurationTest::testKeys()
assert (std::find(keys.begin(), keys.end(), "system") != keys.end());
pConf->keys("system", keys);
assert (keys.size() == 8);
assert (keys.size() == 10);
assert (std::find(keys.begin(), keys.end(), "osName") != keys.end());
assert (std::find(keys.begin(), keys.end(), "osVersion") != keys.end());
assert (std::find(keys.begin(), keys.end(), "osArchitecture") != keys.end());
@ -99,6 +107,8 @@ void SystemConfigurationTest::testKeys()
assert (std::find(keys.begin(), keys.end(), "currentDir") != keys.end());
assert (std::find(keys.begin(), keys.end(), "homeDir") != keys.end());
assert (std::find(keys.begin(), keys.end(), "tempDir") != keys.end());
assert (std::find(keys.begin(), keys.end(), "dateTime") != keys.end());
assert (std::find(keys.begin(), keys.end(), "pid") != keys.end());
assert (std::find(keys.begin(), keys.end(), "env") != keys.end());
}