From a681ebd1daf4d7268d181e0295b95558e7d4ff95 Mon Sep 17 00:00:00 2001 From: Dan Smith Date: Tue, 12 Sep 2023 14:08:14 -0400 Subject: [PATCH] latest from CODA-OSS --- .../modules/c++/str/include/str/Format.h | 112 +++++++++++++----- .../modules/c++/str/source/Format.cpp | 2 +- .../modules/c++/str/tests/VersionTest.cpp | 8 +- .../modules/c++/sys/source/OSUnix.cpp | 6 +- .../modules/c++/sys/source/OSWin32.cpp | 7 +- 5 files changed, 92 insertions(+), 43 deletions(-) diff --git a/externals/coda-oss/modules/c++/str/include/str/Format.h b/externals/coda-oss/modules/c++/str/include/str/Format.h index 3ecb9e81a..44808100f 100644 --- a/externals/coda-oss/modules/c++/str/include/str/Format.h +++ b/externals/coda-oss/modules/c++/str/include/str/Format.h @@ -26,8 +26,8 @@ #define CODA_OSS_str_Format_h_INCLUDED_ #include -#include -#include +#include +#include #include @@ -35,88 +35,132 @@ namespace str { - CODA_OSS_API std::string Format(const char* format, ...); +namespace details +{ +CODA_OSS_API std::string Format_(const char* format, ...); + +inline auto Format(const char* format) +{ + return Format_(format); +} + +template +inline auto Format(const char* format, const T1& t1) +{ + return Format_(format, t1); +} +template +inline auto Format(const char* format, const T1& t1, const T2& t2) +{ + return Format_(format, t1, t2); +} +template +inline auto Format(const char* format, const T1& t1, const T2& t2, const T3& t3) +{ + return Format_(format, t1, t2, t3); +} +template +inline auto Format(const char* format, const T1& t1, const T2& t2, const T3& t3, const T4& t4) +{ + return Format_(format, t1, t2, t3, t4); +} +} } /*! * \param format The format * \param ... Any printf like thing */ -//CODA_OSS_API std::string format(const char* format, ...); inline auto FmtX(const char* format) { - return str::Format(format); + return str::details::Format(format); } inline auto FmtX(const char* format, const char* pStr) { - return str::Format(format, pStr); + return str::details::Format(format, pStr); } inline auto FmtX(const char* format, const std::string& s) { - return str::Format(format, s.c_str()); + return FmtX(format, s.c_str()); } inline auto FmtX(const char* format, int i) { - return str::Format(format, i); + return str::details::Format(format, i); } inline auto FmtX(const char* format, uint32_t i) { - return str::Format(format, i); + return str::details::Format(format, i); } inline auto FmtX(const char* format, ptrdiff_t l) { - return str::Format(format, l); + return str::details::Format(format, l); } inline auto FmtX(const char* format, size_t ul) { - return str::Format(format, ul); + return str::details::Format(format, ul); } inline auto FmtX(const char* format, float f) { - return str::Format(format, f); + return str::details::Format(format, f); } inline auto FmtX(const char* format, double d) { - return str::Format(format, d); + return str::details::Format(format, d); +} +inline auto FmtX(const char* format, const void* p) +{ + return str::details::Format(format, p); } inline auto FmtX(const char* format, char ch, const char* pStr) { - return str::Format(format, ch, pStr); + return str::details::Format(format, ch, pStr); } inline auto FmtX(const char* format, char ch, const std::string& s) { - return str::Format(format, ch, s.c_str()); + return FmtX(format, ch, s.c_str()); } inline auto FmtX(const char* format, const std::string& s1, const std::string& s2) { - return str::Format(format, s1.c_str(), s2.c_str()); + return str::details::Format(format, s1.c_str(), s2.c_str()); } inline auto FmtX(const char* format, const std::string& s, size_t ul) { - return str::Format(format, s.c_str(), ul); + return str::details::Format(format, s.c_str(), ul); } inline auto FmtX(const char* format, char ch1, char ch2) { - return str::Format(format, ch1, ch2); + return str::details::Format(format, ch1, ch2); +} +inline auto FmtX(const char* format, int i1, int i2) +{ + return str::details::Format(format, i1, i2); } inline auto FmtX(const char* format, long l1, long l2) { - return str::Format(format, l1, l2); + return str::details::Format(format, l1, l2); +} +inline auto FmtX(const char* format, uint32_t ui1, uint32_t ui2) +{ + return str::details::Format(format, ui1, ui2); } inline auto FmtX(const char* format, size_t ul1, size_t ul2) { - return str::Format(format, ul1, ul2); + return str::details::Format(format, ul1, ul2); +} +inline auto FmtX(const char* format, size_t ul1, int i2) +{ + return str::details::Format(format, ul1, i2); } inline auto FmtX(const char* format, double d1, double d2) { - return str::Format(format, d1, d2); + return str::details::Format(format, d1, d2); } inline auto FmtX(const char* format, int i, const char* pStr) { - return str::Format(format, i, pStr); + return str::details::Format(format, i, pStr); } inline auto FmtX(const char* fmt, int i, const std::string& s) { @@ -125,44 +169,48 @@ inline auto FmtX(const char* fmt, int i, const std::string& s) inline auto FmtX(const char* format, char ch1, char ch2, const std::string& s) { - return str::Format(format, ch1, ch2, s.c_str()); + return str::details::Format(format, ch1, ch2, s.c_str()); } inline auto FmtX(const char* format, int i1, int i2, unsigned long ul) { - return str::Format(format, i1, i2, ul); + return str::details::Format(format, i1, i2, ul); } inline auto FmtX(const char* format, int i1, int i2, int i3) { - return str::Format(format, i1, i2, i3); + return str::details::Format(format, i1, i2, i3); } inline auto FmtX(const char* format, float f1, float f2, float f3) { - return str::Format(format, f1, f2, f3); + return str::details::Format(format, f1, f2, f3); } inline std::string FmtX(const char* format, const std::string& s1, int i2, int i3) { - return str::Format(format, s1.c_str(), i2, i3); + return str::details::Format(format, s1.c_str(), i2, i3); } inline auto FmtX(const char* format, const std::string& s1, const std::string& s2, uint32_t ui) { - return str::Format(format, s1.c_str(), s2.c_str(), ui); + return str::details::Format(format, s1.c_str(), s2.c_str(), ui); } inline auto FmtX(const char* format, const std::string& s1, const std::string& s2, const std::string& s3) { - return str::Format(format, s1.c_str(), s2.c_str(), s3.c_str()); + return str::details::Format(format, s1.c_str(), s2.c_str(), s3.c_str()); } inline auto FmtX(const char* format, int i1, int i2, int i3, int i4) { - return str::Format(format, i1, i2, i3, i4); + return str::details::Format(format, i1, i2, i3, i4); +} +inline auto FmtX(const char* format, uint32_t ui1, uint32_t ui2, uint32_t ui3, uint32_t ui4) +{ + return str::details::Format(format, ui1, ui2, ui3, ui4); } inline auto FmtX(const char* format, const char* pStr1, const std::string& s2, const char* pStr3, const std::string& s4) { - return str::Format(format, pStr1, s2.c_str(), pStr3, s4.c_str()); + return str::details::Format(format, pStr1, s2.c_str(), pStr3, s4.c_str()); } inline auto FmtX(const char* format, const std::string& s1, int i2, const std::string& s3, int i4) { - return str::Format(format, s1.c_str(), i2, s3.c_str(), i4); + return str::details::Format(format, s1.c_str(), i2, s3.c_str(), i4); } #endif // CODA_OSS_str_Format_h_INCLUDED_ diff --git a/externals/coda-oss/modules/c++/str/source/Format.cpp b/externals/coda-oss/modules/c++/str/source/Format.cpp index 8fc1205c7..fdcfc8544 100644 --- a/externals/coda-oss/modules/c++/str/source/Format.cpp +++ b/externals/coda-oss/modules/c++/str/source/Format.cpp @@ -51,7 +51,7 @@ inline void va_end_(va_list& args) CODA_OSS_disable_warning_pop } -std::string str::Format(const char* format, ...) +std::string str::details::Format_(const char* format, ...) { va_list args; va_start(args, format); diff --git a/externals/coda-oss/modules/c++/str/tests/VersionTest.cpp b/externals/coda-oss/modules/c++/str/tests/VersionTest.cpp index 6b2c00b6d..aa1d9625d 100644 --- a/externals/coda-oss/modules/c++/str/tests/VersionTest.cpp +++ b/externals/coda-oss/modules/c++/str/tests/VersionTest.cpp @@ -23,17 +23,15 @@ #include #include -#define FORMAT_FUNC str::Format - int main() { - std::cout << FORMAT_FUNC("Your version of str is %d.%d.%d\n", + std::cout << FmtX("Your version of str is %d.%d.%d\n", STR_MAJOR_VERSION, STR_MINOR_VERSION, STR_MICRO_VERSION); - std::cout << "Specialization for string test..." << std::endl; + std::cout << "Specialization for string test...\n"; std::string ok("This test passes"); std::cout << str::toType(ok) << std::endl; - std::cout << "Testing the trim function..." << std::endl; + std::cout << "Testing the trim function...\n"; std::string s = " test "; std::cout << "'" << s << "', length: " << s.length() << std::endl; str::trim(s); diff --git a/externals/coda-oss/modules/c++/sys/source/OSUnix.cpp b/externals/coda-oss/modules/c++/sys/source/OSUnix.cpp index e561186f5..5d5685ba9 100644 --- a/externals/coda-oss/modules/c++/sys/source/OSUnix.cpp +++ b/externals/coda-oss/modules/c++/sys/source/OSUnix.cpp @@ -143,8 +143,10 @@ std::string sys::OSUnix::getPlatformName() const if (uname(&name) == -1) throw sys::SystemException("Uname failed"); - return str::Format("%s (%s): %s [build: %s]", name.sysname, name.machine, - name.release, name.version); + std::string retval = name.sysname; + retval += FmtX(" (%s): %s", name.machine, name.release); + retval += FmtX(" [build: %s]", name.version); + return retval; } std::string sys::OSUnix::getNodeName() const diff --git a/externals/coda-oss/modules/c++/sys/source/OSWin32.cpp b/externals/coda-oss/modules/c++/sys/source/OSWin32.cpp index e50dee5ec..62ac0cda3 100644 --- a/externals/coda-oss/modules/c++/sys/source/OSWin32.cpp +++ b/externals/coda-oss/modules/c++/sys/source/OSWin32.cpp @@ -58,9 +58,10 @@ std::string sys::OSWin32::getPlatformName() const { platform = "Unknown Windows OS"; } - return str::Format("%s: %d.%d [build: %d], %s", platform.c_str(), - info.dwMajorVersion, info.dwMinorVersion, info.dwBuildNumber, - info.szCSDVersion); + auto retval = platform + ": "; + retval += FmtX("%d.%d [build: %d], ", info.dwMajorVersion, info.dwMinorVersion, info.dwBuildNumber); + retval += info.szCSDVersion; + return retval; } std::string sys::OSWin32::getNodeName() const