Commit b5be2015 authored by Martin Marinov's avatar Martin Marinov
Browse files

Merge remote-tracking branch 'ReForm/master' into merge-from-ReForm

parents c788a82d 3b1cbcab
......@@ -4,6 +4,7 @@
#include "DebFile.hh"
#include "DebCallStack.hh"
#include "DebDefault.hh"
#include "Base/Utils/Environment.hh"
#include <string>
#include <fstream>
......@@ -16,7 +17,6 @@
#include <map>
#include <sstream>
#include <cstring>
#include <time.h>
#if !defined(WIN32) && !defined(_WIN32)
#define sprintf_s snprintf
......@@ -276,32 +276,7 @@ public:
}
// Append current asctime to given string
bool add_time(std::string& str)
{
time_t rawtime;
time(&rawtime);
struct tm timeinfo;
#ifdef WIN32
int err = localtime_s(&timeinfo, &rawtime);
if (err == 0)
{
char buffer[256];
err = asctime_s(buffer, sizeof(buffer), &timeinfo);
if (err == 0)
{
str.append(buffer);
return true;
}
}
#else//WIN32
//TODO: Implement a secure version of this code for Linux, OSX
//timeinfo = *localtime(&rawtime);
//char* buffer = asctime(&timeinfo);
str.append("TODO: add_time()");
#endif//WIN32
return false;
}
void add_time(std::string& str) { str += System::Environment::time(); }
#if 1
bool hover(std::string& _str, const std::string& _hover, const bool _open)
......
......@@ -45,7 +45,7 @@ protected:
#define BASE_THROW_ERROR_if(COND, INDEX) { if (COND) THROW_ERROR(INDEX); }
#define BASE_THROW_ERROR_TODO(MSG) { THROW_ERROR_TODO_MODULE(Base, MSG); }
#define BASE_THROW_ERROR_TODO_if(COND, MSG) { if (COND) THROW_ERROR_TODO(MSG); }
#define BASE_THROW_ERROR_TODO_if(COND, MSG) { if (COND) BASE_THROW_ERROR_TODO(MSG); }
#endif//BASE_ERROR_HH_INCLUDED
......
......@@ -2,7 +2,12 @@
#include "Base/Security/Mandatory.hh"
#include "Environment.hh"
#include "Base/Debug/DebOut.hh"
#include <cstdlib>
#include <locale.h>
#include <locale>
#include <time.h>
namespace System {
namespace Environment {
......@@ -26,5 +31,46 @@ bool variable(const char* _vrbl_name, std::string& _vrbl)
return true;
}
//default session locale is "C" (i.e., "." is the decimal point)
LocaleSession::LocaleSession(const char* const _ssn_lcle)
{
lcl_bckp_ = std::locale::global(std::locale(_ssn_lcle));
auto* lcle = ::setlocale(LC_ALL, nullptr);
DEB_error_if(lcle == nullptr,
"std::locale::global() failed to set " << _ssn_lcle);
DEB_error_if(lcle != nullptr && strcmp(lcle, _ssn_lcle) != 0,
"set_locale() was asked to set " << _ssn_lcle <<
", but actually set " << lcle);
}
LocaleSession::~LocaleSession()
{
std::locale::global(lcl_bckp_);
}
std::string time()
{
time_t rawtime;
::time(&rawtime);
struct tm timeinfo;
#ifdef WIN32
int err = ::localtime_s(&timeinfo, &rawtime);
if (err == 0)
{
char buffer[256];
err = asctime_s(buffer, sizeof(buffer), &timeinfo);
if (err == 0)
return std::string(buffer);
}
#else//WIN32
//TODO: Implement a secure version of this code for Linux, OSX
//timeinfo = *localtime(&rawtime);
//char* buffer = asctime(&timeinfo);
str.append("TODO: add_time()");
#endif//WIN32
return std::string();
}
}//namespace Environment
}//namespace System
......@@ -4,6 +4,7 @@
#define BASE_ENVIRONMENT_HH_INCLUDED
#include <string>
#include <locale>
namespace System {
namespace Environment {
......@@ -33,6 +34,24 @@ inline std::string variable(const char* _vrbl_name, const char* _vrbl_dflt)
return vrbl;
}
/*!
Define a "scoped" custom locale session.
*/
class LocaleSession
{
public:
//default session locale is "C" (i.e., "." is the decimal point)
LocaleSession(const char* const _ssn_lcle = "C");
~LocaleSession();
private:
std::locale lcl_bckp_;
};
//! Return the system time in a string
std::string time();
}//namespace Environment
}//namespace System
......
......@@ -49,76 +49,72 @@ public:
virtual IOutputStream& print(const double) = 0;
virtual IOutputStream& print(const char* const) = 0;
virtual IOutputStream& print(const Command&) = 0;
/*! Print an array of ElementT */
template <typename ElementT>
inline IOutputStream& print(const size_t _nmbr, const ElementT* _elems)
{
*this << "[ ";
for (size_t i = 0; i < _nmbr; ++i)
*this << _elems[i] << " ";
*this << "]";
return *this;
}
};
BASEDLLEXPORT IOutputStream& operator<<(IOutputStream& _ds, const int _i);
BASEDLLEXPORT IOutputStream& operator<<(IOutputStream& _os, const int _i);
BASEDLLEXPORT IOutputStream& operator<<(IOutputStream& _ds, const double _d);
BASEDLLEXPORT IOutputStream& operator<<(IOutputStream& _os, const double _d);
BASEDLLEXPORT IOutputStream& operator<<(IOutputStream& _ds, const char* const _s);
BASEDLLEXPORT IOutputStream& operator<<(IOutputStream& _os, const char* const _s);
BASEDLLEXPORT IOutputStream& operator<<(IOutputStream& _ds, const char _c);
BASEDLLEXPORT IOutputStream& operator<<(IOutputStream& _os, const char _c);
BASEDLLEXPORT IOutputStream& operator<<(IOutputStream& _ds, const size_t _i);
BASEDLLEXPORT IOutputStream& operator<<(IOutputStream& _os, const size_t _i);
BASEDLLEXPORT IOutputStream& operator<<(IOutputStream& _ds, const unsigned int _i);
BASEDLLEXPORT IOutputStream& operator<<(IOutputStream& _os, const unsigned int _i);
BASEDLLEXPORT IOutputStream& operator<<(IOutputStream& _ds, const float _f);
BASEDLLEXPORT IOutputStream& operator<<(IOutputStream& _os, const float _f);
BASEDLLEXPORT IOutputStream& operator<<(IOutputStream& _ds, const std::string& _s);
BASEDLLEXPORT IOutputStream& operator<<(IOutputStream& _os, const std::string& _s);
BASEDLLEXPORT IOutputStream& operator<<(IOutputStream& _ds, const Command& _co);
BASEDLLEXPORT IOutputStream& operator<<(IOutputStream& _os, const Command& _co);
BASEDLLEXPORT IOutputStream& operator<<(IOutputStream& _ds, const Command::Type _cmd_type);
BASEDLLEXPORT IOutputStream& operator<<(IOutputStream& _os, const Command::Type _cmd_type);
BASEDLLEXPORT IOutputStream& operator<<(IOutputStream& _ds,
BASEDLLEXPORT IOutputStream& operator<<(IOutputStream& _os,
const boost::filesystem::path& _path);
// IStream operator for std::vector<>
template< typename ElementT>
IOutputStream& operator<<(IOutputStream& _ds, const std::vector<ElementT>& _vec)
template <typename ElementT>
IOutputStream& operator<<(IOutputStream& _os, const std::vector<ElementT>& _vec)
{
_ds << "[ ";
for (typename std::vector<ElementT>::const_iterator el_it = _vec.begin(); el_it != _vec.end(); ++el_it)
{
const ElementT el = *el_it;
_ds << el << " ";
}
_ds << "]";
return _ds;
return _os.print(_vec.size(), &_vec[0]);
}
#if (_MSC_VER >= 1900 || __cplusplus > 199711L || defined(__GXX_EXPERIMENTAL_CXX0X__))
// IStream operator for std::array<>
template< typename ElementT, size_t _el_nmbr>
IOutputStream& operator<<(IOutputStream& _ds,
const std::array<ElementT, _el_nmbr>& _vec)
template <typename ElementT, size_t _nmbr>
IOutputStream& operator<<(IOutputStream& _os,
const std::array<ElementT, _nmbr>& _vec)
{
_ds << "[ ";
for (const auto el : _vec)
_ds << el << " ";
_ds << "]";
return _ds;
return _os.print(_nmbr, &_vec[0]);
}
#endif
// IStream operator for fixed size arrays
template <typename ElementT, size_t _el_nmbr>
IOutputStream& operator<<(IOutputStream& _ds, const ElementT(&_arr)[_el_nmbr])
template <typename ElementT, size_t _nmbr>
IOutputStream& operator<<(IOutputStream& _os, const ElementT(&_vec)[_nmbr])
{
_ds << "[ ";
for (size_t i = 0; i < _el_nmbr; ++i)
_ds << _arr[i] << " ";
_ds << "]";
return _ds;
return _os.print(_nmbr, &_vec[0]);
}
// IStream std::pair<>
template <typename T0, typename T1>
IOutputStream& operator<<(IOutputStream& _ds, const std::pair<T0, T1>& _pair)
IOutputStream& operator<<(IOutputStream& _os, const std::pair<T0, T1>& _pair)
{
_ds << "(" << _pair.first << ", " << _pair.second << ")";
return _ds;
_os << "(" << _pair.first << ", " << _pair.second << ")";
return _os;
}
template <class StreamT>
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment