Skip to content

Commit 432632c

Browse files
committed
replace sprintf by std::stringstream; add another IpoptData::Append_info_string
1 parent 40440a5 commit 432632c

7 files changed

+23
-22
lines changed

ChangeLog.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ More detailed information about incremental changes can be found in the
1818
- New option `mumps_mpi_communicator` to specify the MPI communicator when using
1919
an MPI-enabled build of MUMPS [#790, by Alex Tyler Chapman].
2020
- Updated build system to current autotools versions; initial support for icx/ifx and flang
21-
- Removed use of `vsprintf`.
21+
- Removed use of `vsprintf` and `sprintf`. Added `IpoptData::Append_info_string(std::string,double)`.
2222

2323
### 3.14.16 (2024-04-22)
2424

contrib/sIPOPT/src/SensAlgorithm.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
#include "SensAlgorithm.hpp"
88
#include "SensUtils.hpp"
99
#include "IpSmartPtr.hpp"
10-
1110
#include "IpVector.hpp"
11+
#include "IpUtils.hpp"
1212

1313
namespace Ipopt
1414
{
@@ -243,7 +243,7 @@ SensAlgorithmExitStatus SensAlgorithm::ComputeSensitivityMatrix(void)
243243
}
244244
}
245245

246-
sprintf(buffer, "Column %" IPOPT_INDEX_FORMAT, idx_ipopt[Scol]);
246+
Snprintf(buffer, sizeof(buffer), "Column %" IPOPT_INDEX_FORMAT, idx_ipopt[Scol]);
247247

248248
sens_step_calc_->SetSchurDriver(driver_vec_[0]);
249249

src/Algorithm/Inexact/IpInexactLSAcceptor.cpp

+2-6
Original file line numberDiff line numberDiff line change
@@ -473,15 +473,11 @@ char InexactLSAcceptor::UpdateForNextIteration(
473473
if( last_nu_ != nu_ )
474474
{
475475
info_alpha_primal_char = 'n';
476-
char snu[40];
477-
sprintf(snu, " nu=%8.2e", nu_);
478-
IpData().Append_info_string(snu);
476+
IpData().Append_info_string(" nu", nu_);
479477
}
480478
if( flexible_penalty_function_ && last_nu_low_ != nu_low_ )
481479
{
482-
char snu[40];
483-
sprintf(snu, " nl=%8.2e", nu_low_);
484-
IpData().Append_info_string(snu);
480+
IpData().Append_info_string(" nl", nu_low_);
485481
if( info_alpha_primal_char == 'k' )
486482
{
487483
info_alpha_primal_char = 'l';

src/Algorithm/IpIpoptData.hpp

+13
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
#include "IpRegOptions.hpp"
1414
#include "IpTimingStatistics.hpp"
1515

16+
#include <sstream>
17+
#include <iomanip>
18+
1619
namespace Ipopt
1720
{
1821

@@ -519,6 +522,16 @@ class IPOPTLIB_EXPORT IpoptData: public ReferencedObject
519522
{
520523
info_string_ += add_str;
521524
}
525+
/// @since 3.14.17
526+
void Append_info_string(
527+
const std::string& add_str,
528+
double value
529+
)
530+
{
531+
std::ostringstream sstream;
532+
sstream << add_str << '=' << std::setw(8) << std::setprecision(2) << std::scientific << value;
533+
info_string_ += sstream.str();
534+
}
522535
const std::string& info_string() const
523536
{
524537
return info_string_;

src/Algorithm/IpLoqoMuOracle.cpp

+2-5
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,8 @@ bool LoqoMuOracle::CalculateMu(
5757
" Barrier parameter proposed by LOQO rule is %lf\n", mu);
5858

5959
/*
60-
char ssigma[40];
61-
sprintf(ssigma, " sigma=%8.2e", sigma);
62-
IpData().Append_info_string(ssigma);
63-
sprintf(ssigma, " xi=%8.2e ", IpCq().curr_centrality_measure());
64-
IpData().Append_info_string(ssigma);
60+
IpData().Append_info_string(" sigma", sigma);
61+
IpData().Append_info_string(" xi", IpCq().curr_centrality_measure());
6562
*/
6663

6764
new_mu = Max(Min(mu_max, mu), mu_min);

src/Algorithm/IpPenaltyLSAcceptor.cpp

+1-3
Original file line numberDiff line numberDiff line change
@@ -417,9 +417,7 @@ char PenaltyLSAcceptor::UpdateForNextIteration(
417417
if( last_nu_ != nu_ )
418418
{
419419
info_alpha_primal_char = 'n';
420-
char snu[40];
421-
sprintf(snu, " nu=%8.2e", nu_);
422-
IpData().Append_info_string(snu);
420+
IpData().Append_info_string(" nu", nu_);
423421
}
424422
else
425423
{

src/Algorithm/IpProbingMuOracle.cpp

+2-5
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,8 @@ bool ProbingMuOracle::CalculateMu(
125125
IpData().set_delta_aff(step);
126126
IpData().SetHaveAffineDeltas(true);
127127

128-
char ssigma[40];
129-
sprintf(ssigma, " sigma=%8.2e", sigma);
130-
IpData().Append_info_string(ssigma);
131-
//sprintf(ssigma, " xi=%8.2e ", IpCq().curr_centrality_measure());
132-
//IpData().Append_info_string(ssigma);
128+
IpData().Append_info_string(" sigma", sigma);
129+
//IpData().Append_info_string(" xi", IpCq().curr_centrality_measure());
133130

134131
new_mu = Max(Min(mu, mu_max), mu_min);
135132
return true;

0 commit comments

Comments
 (0)