From 96d85c5f40b944639a2c9eed74e0d7e332db5c81 Mon Sep 17 00:00:00 2001 From: Geoffrey Merck Date: Sun, 26 Dec 2021 17:46:16 +0100 Subject: [PATCH] Move string_format to header (as it should be) --- APRSWriter.cpp | 58 ++++++++++++++++++++++++------------------------- APRSWriter.h | 1 + StringUtils.cpp | 33 ---------------------------- StringUtils.h | 15 +++++++++++-- 4 files changed, 43 insertions(+), 64 deletions(-) delete mode 100644 StringUtils.cpp diff --git a/APRSWriter.cpp b/APRSWriter.cpp index 1a28a52..5cffc44 100644 --- a/APRSWriter.cpp +++ b/APRSWriter.cpp @@ -274,7 +274,7 @@ void CAPRSWriter::writeData(const std::string& callsign, const CAMBEData& data) if (n != std::string::npos) body = body.substr(0, n); - std::string output = string_format(wxT("%s,qAR,%s-%s:%s"), header.c_str(), entry->getCallsign().c_str(), entry->getBand().c_str(), body.c_str()); + std::string output = CStringUtils::string_format(wxT("%s,qAR,%s-%s:%s"), header.c_str(), entry->getCallsign().c_str(), entry->getBand().c_str(), body.c_str()); char ascii[500U]; ::memset(ascii, 0x00, 500U); @@ -353,12 +353,12 @@ void CAPRSWriter::sendIdFramesFixed() std::string desc; if (entry->getBand().length() > 1U) { if (entry->getFrequency() != 0.0) - desc = string_format("Data %.5lfMHz", entry->getFrequency()); + desc = CStringUtils::string_format("Data %.5lfMHz", entry->getFrequency()); else desc = "Data"; } else { if (entry->getFrequency() != 0.0) - desc = string_format(wxT("Voice %.5lfMHz %c%.4lfMHz"), + desc = CStringUtils::string_format(wxT("Voice %.5lfMHz %c%.4lfMHz"), entry->getFrequency(), entry->getOffset() < 0.0 ? '-' : '+', ::fabs(entry->getOffset())); @@ -389,32 +389,32 @@ void CAPRSWriter::sendIdFramesFixed() std::string lat; if (latitude >= 1000.0F) - lat = string_format(wxT("%.2lf"), latitude); + lat = CStringUtils::string_format(wxT("%.2lf"), latitude); else if (latitude >= 100.0F) - lat = string_format(wxT("0%.2lf"), latitude); + lat = CStringUtils::string_format(wxT("0%.2lf"), latitude); else if (latitude >= 10.0F) - lat = string_format(wxT("00%.2lf"), latitude); + lat = CStringUtils::string_format(wxT("00%.2lf"), latitude); else - lat = string_format(wxT("000%.2lf"), latitude); + lat = CStringUtils::string_format(wxT("000%.2lf"), latitude); std::string lon; if (longitude >= 10000.0F) - lon = string_format(wxT("%.2lf"), longitude); + lon = CStringUtils::string_format(wxT("%.2lf"), longitude); else if (longitude >= 1000.0F) - lon = string_format(wxT("0%.2lf"), longitude); + lon = CStringUtils::string_format(wxT("0%.2lf"), longitude); else if (longitude >= 100.0F) - lon = string_format(wxT("00%.2lf"), longitude); + lon = CStringUtils::string_format(wxT("00%.2lf"), longitude); else if (longitude >= 10.0F) - lon = string_format(wxT("000%.2lf"), longitude); + lon = CStringUtils::string_format(wxT("000%.2lf"), longitude); else - lon = string_format(wxT("0000%.2lf"), longitude); + lon = CStringUtils::string_format(wxT("0000%.2lf"), longitude); // Convert commas to periods in the latitude and longitude boost::replace_all(lat, wxT(","), wxT(".")); boost::replace_all(lon, ",", wxT(".")); std::string output; - output = string_format(wxT("%s-S>APDG01,TCPIP*,qAC,%s-GS:;%-7s%-2s*%02d%02d%02dz%s%cD%s%caRNG%04.0lf/A=%06.0lf %s %s"), + output = CStringUtils::string_format(wxT("%s-S>APDG01,TCPIP*,qAC,%s-GS:;%-7s%-2s*%02d%02d%02dz%s%cD%s%caRNG%04.0lf/A=%06.0lf %s %s"), m_gateway.c_str(), m_gateway.c_str(), entry->getCallsign().c_str(), entry->getBand().c_str(), tm->tm_mday, tm->tm_hour, tm->tm_min, lat.c_str(), (entry->getLatitude() < 0.0F) ? 'S' : 'N', @@ -429,7 +429,7 @@ void CAPRSWriter::sendIdFramesFixed() m_thread->write(ascii); if (entry->getBand().length() == 1U) { - output = string_format(wxT("%s-%s>APDG02,TCPIP*,qAC,%s-%sS:!%s%cD%s%c&RNG%04.0lf/A=%06.0lf %s %s"), + output = CStringUtils::string_format(wxT("%s-%s>APDG02,TCPIP*,qAC,%s-%sS:!%s%cD%s%c&RNG%04.0lf/A=%06.0lf %s %s"), entry->getCallsign().c_str(), entry->getBand().c_str(), entry->getCallsign().c_str(), entry->getBand().c_str(), lat.c_str(), (entry->getLatitude() < 0.0F) ? 'S' : 'N', lon.c_str(), (entry->getLongitude() < 0.0F) ? 'W' : 'E', @@ -485,12 +485,12 @@ void CAPRSWriter::sendIdFramesMobile() std::string desc; if (entry->getBand().length() > 1U) { if (entry->getFrequency() != 0.0) - desc = string_format(wxT("Data %.5lfMHz"), entry->getFrequency()); + desc = CStringUtils::string_format(wxT("Data %.5lfMHz"), entry->getFrequency()); else desc = wxT("Data"); } else { if (entry->getFrequency() != 0.0) - desc = string_format(wxT("Voice %.5lfMHz %c%.4lfMHz"), + desc = CStringUtils::string_format(wxT("Voice %.5lfMHz %c%.4lfMHz"), entry->getFrequency(), entry->getOffset() < 0.0 ? '-' : '+', ::fabs(entry->getOffset())); @@ -521,32 +521,32 @@ void CAPRSWriter::sendIdFramesMobile() std::string lat; if (latitude >= 1000.0F) - lat = string_format(wxT("%.2lf"), latitude); + lat = CStringUtils::string_format(wxT("%.2lf"), latitude); else if (latitude >= 100.0F) - lat = string_format(wxT("0%.2lf"), latitude); + lat = CStringUtils::string_format(wxT("0%.2lf"), latitude); else if (latitude >= 10.0F) - lat = string_format(wxT("00%.2lf"), latitude); + lat = CStringUtils::string_format(wxT("00%.2lf"), latitude); else - lat = string_format(wxT("000%.2lf"), latitude); + lat = CStringUtils::string_format(wxT("000%.2lf"), latitude); std::string lon; if (longitude >= 10000.0F) - lon = string_format(wxT("%.2lf"), longitude); + lon = CStringUtils::string_format(wxT("%.2lf"), longitude); else if (longitude >= 1000.0F) - lon = string_format(wxT("0%.2lf"), longitude); + lon = CStringUtils::string_format(wxT("0%.2lf"), longitude); else if (longitude >= 100.0F) - lon = string_format(wxT("00%.2lf"), longitude); + lon = CStringUtils::string_format(wxT("00%.2lf"), longitude); else if (longitude >= 10.0F) - lon = string_format(wxT("000%.2lf"), longitude); + lon = CStringUtils::string_format(wxT("000%.2lf"), longitude); else - lon = string_format(wxT("0000%.2lf"), longitude); + lon = CStringUtils::string_format(wxT("0000%.2lf"), longitude); // Convert commas to periods in the latitude and longitude boost::replace_all(lat, ",", "."); boost::replace_all(lon, ",", "."); std::string output1; - output1 = string_format(wxT("%s-S>APDG01,TCPIP*,qAC,%s-GS:;%-7s%-2s*%02d%02d%02dz%s%cD%s%ca/A=%06.0lf"), + output1 = CStringUtils::string_format(wxT("%s-S>APDG01,TCPIP*,qAC,%s-GS:;%-7s%-2s*%02d%02d%02dz%s%cD%s%ca/A=%06.0lf"), m_gateway.c_str(), m_gateway.c_str(), entry->getCallsign().c_str(), entry->getBand().c_str(), tm->tm_mday, tm->tm_hour, tm->tm_min, lat.c_str(), (rawLatitude < 0.0) ? 'S' : 'N', @@ -558,11 +558,11 @@ void CAPRSWriter::sendIdFramesMobile() double rawBearing = ::atof(pBearing); double rawVelocity = ::atof(pVelocity); - output2 = string_format(wxT("%03.0lf/%03.0lf"), rawBearing, rawVelocity * 0.539957F); + output2 = CStringUtils::string_format(wxT("%03.0lf/%03.0lf"), rawBearing, rawVelocity * 0.539957F); } std::string output3; - output3 = string_format(wxT("RNG%04.0lf %s %s"), entry->getRange() * 0.6214, band.c_str(), desc.c_str()); + output3 = CStringUtils::string_format(wxT("RNG%04.0lf %s %s"), entry->getRange() * 0.6214, band.c_str(), desc.c_str()); char ascii[300U]; ::memset(ascii, 0x00, 300U); @@ -577,7 +577,7 @@ void CAPRSWriter::sendIdFramesMobile() m_thread->write(ascii); if (entry->getBand().length() == 1U) { - output1 = string_format(wxT("%s-%s>APDG02,TCPIP*,qAC,%s-%sS:!%s%cD%s%c&/A=%06.0lf"), + output1 = CStringUtils::string_format(wxT("%s-%s>APDG02,TCPIP*,qAC,%s-%sS:!%s%cD%s%c&/A=%06.0lf"), entry->getCallsign().c_str(), entry->getBand().c_str(), entry->getCallsign().c_str(), entry->getBand().c_str(), lat.c_str(), (rawLatitude < 0.0) ? 'S' : 'N', lon.c_str(), (rawLongitude < 0.0) ? 'W' : 'E', diff --git a/APRSWriter.h b/APRSWriter.h index 6deb4f3..54c672c 100644 --- a/APRSWriter.h +++ b/APRSWriter.h @@ -21,6 +21,7 @@ #define APRSWriter_H #include +#include #include "APRSWriterThread.h" #include "UDPReaderWriter.h" diff --git a/StringUtils.cpp b/StringUtils.cpp deleted file mode 100644 index 89eb94d..0000000 --- a/StringUtils.cpp +++ /dev/null @@ -1,33 +0,0 @@ - -/* - * Copyright (C) 2009-2011,2013 by Jonathan Naylor G4KLX - * Copyright (c) 2017 by Thomas A. Early N7TAE - * Copyright (c) 2021 by Geoffrey Merck F4FXL / KC3FRA - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - */ - -#include "StringUtils.h" - -template -std::string string_format( const std::string& format, Args ... args ) -{ - int size_s = std::snprintf( nullptr, 0, format.c_str(), args ... ) + 1; // Extra space for '\0' - if( size_s <= 0 ){ throw std::runtime_error( "Error during formatting." ); } - auto size = static_cast( size_s ); - auto buf = std::make_unique( size ); - std::snprintf( buf.get(), size, format.c_str(), args ... ); - return std::string( buf.get(), buf.get() + size - 1 ); // We don't want the '\0' inside -} \ No newline at end of file diff --git a/StringUtils.h b/StringUtils.h index bde9d7f..552153c 100644 --- a/StringUtils.h +++ b/StringUtils.h @@ -27,5 +27,16 @@ #define wxT(x) std::string(x) #define string_right(s,l) (s.substr(s.length() - l, l)) -template -std::string string_format( const std::string& format, Args ... args ); \ No newline at end of file +class CStringUtils { +public: + template + static std::string string_format( const std::string& format, Args ... args ) + { + int size_s = std::snprintf( nullptr, 0, format.c_str(), args ... ) + 1; // Extra space for '\0' + if( size_s <= 0 ){ throw std::runtime_error( "Error during formatting." ); } + auto size = static_cast( size_s ); + auto buf = std::make_unique( size ); + std::snprintf( buf.get(), size, format.c_str(), args ... ); + return std::string( buf.get(), buf.get() + size - 1 ); // We don't want the '\0' inside + } +};