Move string_format to header (as it should be)

master
Geoffrey Merck 4 years ago
parent 5b5a639529
commit 96d85c5f40

@ -274,7 +274,7 @@ void CAPRSWriter::writeData(const std::string& callsign, const CAMBEData& data)
if (n != std::string::npos) if (n != std::string::npos)
body = body.substr(0, n); 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]; char ascii[500U];
::memset(ascii, 0x00, 500U); ::memset(ascii, 0x00, 500U);
@ -353,12 +353,12 @@ void CAPRSWriter::sendIdFramesFixed()
std::string desc; std::string desc;
if (entry->getBand().length() > 1U) { if (entry->getBand().length() > 1U) {
if (entry->getFrequency() != 0.0) if (entry->getFrequency() != 0.0)
desc = string_format("Data %.5lfMHz", entry->getFrequency()); desc = CStringUtils::string_format("Data %.5lfMHz", entry->getFrequency());
else else
desc = "Data"; desc = "Data";
} else { } else {
if (entry->getFrequency() != 0.0) 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->getFrequency(),
entry->getOffset() < 0.0 ? '-' : '+', entry->getOffset() < 0.0 ? '-' : '+',
::fabs(entry->getOffset())); ::fabs(entry->getOffset()));
@ -389,32 +389,32 @@ void CAPRSWriter::sendIdFramesFixed()
std::string lat; std::string lat;
if (latitude >= 1000.0F) if (latitude >= 1000.0F)
lat = string_format(wxT("%.2lf"), latitude); lat = CStringUtils::string_format(wxT("%.2lf"), latitude);
else if (latitude >= 100.0F) 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) else if (latitude >= 10.0F)
lat = string_format(wxT("00%.2lf"), latitude); lat = CStringUtils::string_format(wxT("00%.2lf"), latitude);
else else
lat = string_format(wxT("000%.2lf"), latitude); lat = CStringUtils::string_format(wxT("000%.2lf"), latitude);
std::string lon; std::string lon;
if (longitude >= 10000.0F) if (longitude >= 10000.0F)
lon = string_format(wxT("%.2lf"), longitude); lon = CStringUtils::string_format(wxT("%.2lf"), longitude);
else if (longitude >= 1000.0F) 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) 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) else if (longitude >= 10.0F)
lon = string_format(wxT("000%.2lf"), longitude); lon = CStringUtils::string_format(wxT("000%.2lf"), longitude);
else 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 // Convert commas to periods in the latitude and longitude
boost::replace_all(lat, wxT(","), wxT(".")); boost::replace_all(lat, wxT(","), wxT("."));
boost::replace_all(lon, ",", wxT(".")); boost::replace_all(lon, ",", wxT("."));
std::string output; 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(), 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, tm->tm_mday, tm->tm_hour, tm->tm_min,
lat.c_str(), (entry->getLatitude() < 0.0F) ? 'S' : 'N', lat.c_str(), (entry->getLatitude() < 0.0F) ? 'S' : 'N',
@ -429,7 +429,7 @@ void CAPRSWriter::sendIdFramesFixed()
m_thread->write(ascii); m_thread->write(ascii);
if (entry->getBand().length() == 1U) { 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(), 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', lat.c_str(), (entry->getLatitude() < 0.0F) ? 'S' : 'N',
lon.c_str(), (entry->getLongitude() < 0.0F) ? 'W' : 'E', lon.c_str(), (entry->getLongitude() < 0.0F) ? 'W' : 'E',
@ -485,12 +485,12 @@ void CAPRSWriter::sendIdFramesMobile()
std::string desc; std::string desc;
if (entry->getBand().length() > 1U) { if (entry->getBand().length() > 1U) {
if (entry->getFrequency() != 0.0) if (entry->getFrequency() != 0.0)
desc = string_format(wxT("Data %.5lfMHz"), entry->getFrequency()); desc = CStringUtils::string_format(wxT("Data %.5lfMHz"), entry->getFrequency());
else else
desc = wxT("Data"); desc = wxT("Data");
} else { } else {
if (entry->getFrequency() != 0.0) 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->getFrequency(),
entry->getOffset() < 0.0 ? '-' : '+', entry->getOffset() < 0.0 ? '-' : '+',
::fabs(entry->getOffset())); ::fabs(entry->getOffset()));
@ -521,32 +521,32 @@ void CAPRSWriter::sendIdFramesMobile()
std::string lat; std::string lat;
if (latitude >= 1000.0F) if (latitude >= 1000.0F)
lat = string_format(wxT("%.2lf"), latitude); lat = CStringUtils::string_format(wxT("%.2lf"), latitude);
else if (latitude >= 100.0F) 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) else if (latitude >= 10.0F)
lat = string_format(wxT("00%.2lf"), latitude); lat = CStringUtils::string_format(wxT("00%.2lf"), latitude);
else else
lat = string_format(wxT("000%.2lf"), latitude); lat = CStringUtils::string_format(wxT("000%.2lf"), latitude);
std::string lon; std::string lon;
if (longitude >= 10000.0F) if (longitude >= 10000.0F)
lon = string_format(wxT("%.2lf"), longitude); lon = CStringUtils::string_format(wxT("%.2lf"), longitude);
else if (longitude >= 1000.0F) 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) 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) else if (longitude >= 10.0F)
lon = string_format(wxT("000%.2lf"), longitude); lon = CStringUtils::string_format(wxT("000%.2lf"), longitude);
else 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 // Convert commas to periods in the latitude and longitude
boost::replace_all(lat, ",", "."); boost::replace_all(lat, ",", ".");
boost::replace_all(lon, ",", "."); boost::replace_all(lon, ",", ".");
std::string output1; 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(), 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, tm->tm_mday, tm->tm_hour, tm->tm_min,
lat.c_str(), (rawLatitude < 0.0) ? 'S' : 'N', lat.c_str(), (rawLatitude < 0.0) ? 'S' : 'N',
@ -558,11 +558,11 @@ void CAPRSWriter::sendIdFramesMobile()
double rawBearing = ::atof(pBearing); double rawBearing = ::atof(pBearing);
double rawVelocity = ::atof(pVelocity); 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; 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]; char ascii[300U];
::memset(ascii, 0x00, 300U); ::memset(ascii, 0x00, 300U);
@ -577,7 +577,7 @@ void CAPRSWriter::sendIdFramesMobile()
m_thread->write(ascii); m_thread->write(ascii);
if (entry->getBand().length() == 1U) { 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(), entry->getCallsign().c_str(), entry->getBand().c_str(), entry->getCallsign().c_str(), entry->getBand().c_str(),
lat.c_str(), (rawLatitude < 0.0) ? 'S' : 'N', lat.c_str(), (rawLatitude < 0.0) ? 'S' : 'N',
lon.c_str(), (rawLongitude < 0.0) ? 'W' : 'E', lon.c_str(), (rawLongitude < 0.0) ? 'W' : 'E',

@ -21,6 +21,7 @@
#define APRSWriter_H #define APRSWriter_H
#include <unordered_map> #include <unordered_map>
#include <string>
#include "APRSWriterThread.h" #include "APRSWriterThread.h"
#include "UDPReaderWriter.h" #include "UDPReaderWriter.h"

@ -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<typename ... Args>
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_t>( size_s );
auto buf = std::make_unique<char[]>( 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
}

@ -27,5 +27,16 @@
#define wxT(x) std::string(x) #define wxT(x) std::string(x)
#define string_right(s,l) (s.substr(s.length() - l, l)) #define string_right(s,l) (s.substr(s.length() - l, l))
template<typename ... Args> class CStringUtils {
std::string string_format( const std::string& format, Args ... args ); public:
template<typename ... Args>
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_t>( size_s );
auto buf = std::make_unique<char[]>( 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
}
};

Loading…
Cancel
Save

Powered by TurnKey Linux.