max@0: // Copyright (C) 2008-2010 NICTA (www.nicta.com.au) max@0: // Copyright (C) 2008-2010 Conrad Sanderson max@0: // max@0: // This file is part of the Armadillo C++ library. max@0: // It is provided without any warranty of fitness max@0: // for any purpose. You can redistribute this file max@0: // and/or modify it under the terms of the GNU max@0: // Lesser General Public License (LGPL) as published max@0: // by the Free Software Foundation, either version 3 max@0: // of the License or (at your option) any later version. max@0: // (see http://www.opensource.org/licenses for more info) max@0: max@0: max@0: //! \addtogroup format_wrap max@0: //! @{ max@0: max@0: max@0: //! \namespace arma_boost namespace for functions and classes which partially emulate Boost functionality max@0: namespace arma_boost max@0: { max@0: max@0: #if defined(ARMA_USE_BOOST_FORMAT) max@0: max@0: using boost::format; max@0: using boost::basic_format; max@0: using boost::str; max@0: max@0: #else max@0: max@0: #if defined(ARMA_HAVE_STD_SNPRINTF) max@0: max@0: #define arma_snprintf std::snprintf max@0: max@0: #else max@0: max@0: // better-than-nothing emulation of C99 snprintf(), max@0: // with correct return value and null-terminated output string. max@0: // note that _snprintf() provided by MS is not a good substitute for snprintf() max@0: max@0: inline max@0: int max@0: arma_snprintf(char* out, size_t size, const char* fmt, ...) max@0: { max@0: size_t i; max@0: max@0: for(i=0; i 0) max@0: out[size-1] = char(0); max@0: max@0: return int(i); max@0: } max@0: max@0: #endif max@0: max@0: class format max@0: { max@0: public: max@0: max@0: format(const char* in_fmt) max@0: : A(in_fmt) max@0: { max@0: } max@0: max@0: format(const std::string& in_fmt) max@0: : A(in_fmt) max@0: { max@0: } max@0: max@0: max@0: const std::string A; max@0: max@0: private: max@0: format(); max@0: }; max@0: max@0: max@0: max@0: template max@0: class basic_format max@0: { max@0: public: max@0: max@0: basic_format(const T1& in_A, const T2& in_B) max@0: : A(in_A) max@0: , B(in_B) max@0: { max@0: } max@0: max@0: const T1& A; max@0: const T2& B; max@0: max@0: private: max@0: basic_format(); max@0: }; max@0: max@0: max@0: max@0: template max@0: inline max@0: basic_format< format, T2 > max@0: operator% (const format& X, const T2& arg) max@0: { max@0: return basic_format< format, T2 >(X, arg); max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: basic_format< basic_format, T3 > max@0: operator% (const basic_format& X, const T3& arg) max@0: { max@0: return basic_format< basic_format, T3 >(X, arg); max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: std::string max@0: str(const basic_format< format, T2>& X) max@0: { max@0: char local_buffer[1024]; max@0: char* buffer = local_buffer; max@0: max@0: int buffer_size = 1024; max@0: int required_size = buffer_size; max@0: max@0: bool using_local_buffer = true; max@0: max@0: std::string out; max@0: max@0: do max@0: { max@0: if(using_local_buffer == false) max@0: { max@0: buffer = new char[buffer_size]; max@0: } max@0: max@0: required_size = arma_snprintf(buffer, size_t(buffer_size), X.A.A.c_str(), X.B); max@0: max@0: if(required_size < buffer_size) max@0: { max@0: if(required_size > 0) max@0: { max@0: out = buffer; max@0: } max@0: } max@0: else max@0: { max@0: buffer_size *= 2; max@0: } max@0: max@0: if(using_local_buffer == true) max@0: { max@0: using_local_buffer = false; max@0: } max@0: else max@0: { max@0: delete[] buffer; max@0: } max@0: max@0: } while( (required_size >= buffer_size) ); max@0: max@0: return out; max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: std::string max@0: str(const basic_format< basic_format< format, T2>, T3>& X) max@0: { max@0: char local_buffer[1024]; max@0: char* buffer = local_buffer; max@0: max@0: int buffer_size = 1024; max@0: int required_size = buffer_size; max@0: max@0: bool using_local_buffer = true; max@0: max@0: std::string out; max@0: max@0: do max@0: { max@0: if(using_local_buffer == false) max@0: { max@0: buffer = new char[buffer_size]; max@0: } max@0: max@0: required_size = arma_snprintf(buffer, size_t(buffer_size), X.A.A.A.c_str(), X.A.B, X.B); max@0: max@0: if(required_size < buffer_size) max@0: { max@0: if(required_size > 0) max@0: { max@0: out = buffer; max@0: } max@0: } max@0: else max@0: { max@0: buffer_size *= 2; max@0: } max@0: max@0: if(using_local_buffer == true) max@0: { max@0: using_local_buffer = false; max@0: } max@0: else max@0: { max@0: delete[] buffer; max@0: } max@0: max@0: } while( (required_size >= buffer_size) ); max@0: max@0: return out; max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: std::string max@0: str(const basic_format< basic_format< basic_format< format, T2>, T3>, T4>& X) max@0: { max@0: char local_buffer[1024]; max@0: char* buffer = local_buffer; max@0: max@0: int buffer_size = 1024; max@0: int required_size = buffer_size; max@0: max@0: bool using_local_buffer = true; max@0: max@0: std::string out; max@0: max@0: do max@0: { max@0: if(using_local_buffer == false) max@0: { max@0: buffer = new char[buffer_size]; max@0: } max@0: max@0: required_size = arma_snprintf(buffer, size_t(buffer_size), X.A.A.A.A.c_str(), X.A.A.B, X.A.B, X.B); max@0: max@0: if(required_size < buffer_size) max@0: { max@0: if(required_size > 0) max@0: { max@0: out = buffer; max@0: } max@0: } max@0: else max@0: { max@0: buffer_size *= 2; max@0: } max@0: max@0: if(using_local_buffer == true) max@0: { max@0: using_local_buffer = false; max@0: } max@0: else max@0: { max@0: delete[] buffer; max@0: } max@0: max@0: } while( (required_size >= buffer_size) ); max@0: max@0: return out; max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: std::string max@0: str(const basic_format< basic_format< basic_format< basic_format< format, T2>, T3>, T4>, T5>& X) max@0: { max@0: char local_buffer[1024]; max@0: char* buffer = local_buffer; max@0: max@0: int buffer_size = 1024; max@0: int required_size = buffer_size; max@0: max@0: bool using_local_buffer = true; max@0: max@0: std::string out; max@0: max@0: do max@0: { max@0: if(using_local_buffer == false) max@0: { max@0: buffer = new char[buffer_size]; max@0: } max@0: max@0: required_size = arma_snprintf(buffer, size_t(buffer_size), X.A.A.A.A.A.c_str(), X.A.A.A.B, X.A.A.B, X.A.B, X.B); max@0: max@0: if(required_size < buffer_size) max@0: { max@0: if(required_size > 0) max@0: { max@0: out = buffer; max@0: } max@0: } max@0: else max@0: { max@0: buffer_size *= 2; max@0: } max@0: max@0: if(using_local_buffer == true) max@0: { max@0: using_local_buffer = false; max@0: } max@0: else max@0: { max@0: delete[] buffer; max@0: } max@0: max@0: } while( (required_size >= buffer_size) ); max@0: max@0: return out; max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: std::string max@0: str(const basic_format< basic_format< basic_format< basic_format< basic_format< format, T2>, T3>, T4>, T5>, T6>& X) max@0: { max@0: char local_buffer[1024]; max@0: char* buffer = local_buffer; max@0: max@0: int buffer_size = 1024; max@0: int required_size = buffer_size; max@0: max@0: bool using_local_buffer = true; max@0: max@0: std::string out; max@0: max@0: do max@0: { max@0: if(using_local_buffer == false) max@0: { max@0: buffer = new char[buffer_size]; max@0: } max@0: max@0: required_size = arma_snprintf(buffer, size_t(buffer_size), X.A.A.A.A.A.A.c_str(), X.A.A.A.A.B, X.A.A.A.B, X.A.A.B, X.A.B, X.B); max@0: max@0: if(required_size < buffer_size) max@0: { max@0: if(required_size > 0) max@0: { max@0: out = buffer; max@0: } max@0: } max@0: else max@0: { max@0: buffer_size *= 2; max@0: } max@0: max@0: if(using_local_buffer == true) max@0: { max@0: using_local_buffer = false; max@0: } max@0: else max@0: { max@0: delete[] buffer; max@0: } max@0: max@0: } while( (required_size >= buffer_size) ); max@0: max@0: return out; max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: std::string max@0: str(const basic_format< basic_format< basic_format< basic_format< basic_format< basic_format< format, T2>, T3>, T4>, T5>, T6>, T7>& X) max@0: { max@0: char local_buffer[1024]; max@0: char* buffer = local_buffer; max@0: max@0: int buffer_size = 1024; max@0: int required_size = buffer_size; max@0: max@0: bool using_local_buffer = true; max@0: max@0: std::string out; max@0: max@0: do max@0: { max@0: if(using_local_buffer == false) max@0: { max@0: buffer = new char[buffer_size]; max@0: } max@0: max@0: required_size = arma_snprintf(buffer, size_t(buffer_size), X.A.A.A.A.A.A.A.c_str(), X.A.A.A.A.A.B, X.A.A.A.A.B, X.A.A.A.B, X.A.A.B, X.A.B, X.B); max@0: max@0: if(required_size < buffer_size) max@0: { max@0: if(required_size > 0) max@0: { max@0: out = buffer; max@0: } max@0: } max@0: else max@0: { max@0: buffer_size *= 2; max@0: } max@0: max@0: if(using_local_buffer == true) max@0: { max@0: using_local_buffer = false; max@0: } max@0: else max@0: { max@0: delete[] buffer; max@0: } max@0: max@0: } while( (required_size >= buffer_size) ); max@0: max@0: return out; max@0: } max@0: max@0: max@0: max@0: template max@0: struct format_metaprog max@0: { max@0: static const uword depth = 0; max@0: max@0: inline max@0: static max@0: const std::string& max@0: get_fmt(const T1& X) max@0: { max@0: return X.A; max@0: } max@0: }; max@0: max@0: max@0: max@0: //template<> max@0: template max@0: struct format_metaprog< basic_format > max@0: { max@0: static const uword depth = 1 + format_metaprog::depth; max@0: max@0: inline max@0: static max@0: const std::string& max@0: get_fmt(const T1& X) max@0: { max@0: return format_metaprog::get_fmt(X.A); max@0: } max@0: max@0: }; max@0: max@0: max@0: max@0: template max@0: inline max@0: std::string max@0: str(const basic_format& X) max@0: { max@0: return format_metaprog< basic_format >::get_fmt(X.A); max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: std::ostream& max@0: operator<< (std::ostream& o, const basic_format& X) max@0: { max@0: o << str(X); max@0: return o; max@0: } max@0: max@0: max@0: #endif max@0: max@0: max@0: template struct string_only { }; max@0: template<> struct string_only { typedef std::string result; }; max@0: max@0: template struct char_only { }; max@0: template<> struct char_only { typedef char result; }; max@0: max@0: template max@0: struct basic_format_only { }; max@0: max@0: #if defined(ARMA_USE_BOOST_FORMAT) max@0: template max@0: struct basic_format_only< basic_format > { typedef basic_format result; }; max@0: #else max@0: template max@0: struct basic_format_only< basic_format > { typedef basic_format result; }; max@0: #endif max@0: max@0: max@0: max@0: template max@0: inline max@0: static max@0: const T1& max@0: str_wrapper(const T1& x, const typename string_only::result* junk = 0) max@0: { max@0: arma_ignore(junk); max@0: max@0: return x; max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: static max@0: const T1* max@0: str_wrapper(const T1* x, const typename char_only::result* junk = 0) max@0: { max@0: arma_ignore(junk); max@0: max@0: return x; max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: static max@0: std::string max@0: str_wrapper(const T1& x, const typename basic_format_only::result* junk = 0) max@0: { max@0: arma_ignore(junk); max@0: max@0: return str(x); max@0: } max@0: max@0: } max@0: max@0: //! @}