diff --git a/include/CPstrings.h b/include/CPstrings.h index e14b0268..1a0ef1c1 100644 --- a/include/CPstrings.h +++ b/include/CPstrings.h @@ -6,11 +6,13 @@ #include #include +#if !defined(NO_CPPFORMAT) #ifndef FMT_HEADER_ONLY #define FMT_HEADER_ONLY #endif #include "fmt/format.h" // For addition of the string formatting functions and macros from cppformat #undef FMT_HEADER_ONLY +#endif #if !defined(__powerpc__) /// Copy string to wstring @@ -43,11 +45,16 @@ return lhs == rhs; } +#if defined(NO_CPPFORMAT) + // Missing string formatting function, this old guy is needed for ancient gcc compilers on PowerPC for VxWorks + std::string format(const char* fmt, ...) +#else // Missing std::string formatting function - provided by the cppformat library inline std::string format(const char *format, fmt::ArgList args) { return fmt::sprintf(format, args); } FMT_VARIADIC(std::string, format, const char *) +#endif // Missing string split - like in Python std::vector strsplit(const std::string &s, char del); diff --git a/src/CPstrings.cpp b/src/CPstrings.cpp index 9b0898ad..18f4129c 100644 --- a/src/CPstrings.cpp +++ b/src/CPstrings.cpp @@ -25,4 +25,22 @@ std::vector strsplit(const std::string &s, char del) i1 = i2+1; } return v; -} \ No newline at end of file +} + +#if defined(NO_CPPFORMAT) +std::string format(const char* fmt, ...) +{ + const int size = 512; + struct deleter{ static void delarray(char* p) { delete[] p; } }; // to use delete[] + shared_ptr buffer(new char[size], deleter::delarray); // I'd prefer unique_ptr, but it's only available since c++11 + va_list vl; + va_start(vl,fmt); + int nsize = vsnprintf(buffer.get(),size,fmt,vl); + if(size<=nsize){//fail delete buffer and try again + buffer.reset(new char[++nsize], deleter::delarray);//+1 for /0 + nsize = vsnprintf(buffer.get(),nsize,fmt,vl); + } + va_end(vl); + return buffer.get(); +} +#endif