Skip to content

Commit

Permalink
New function 'GetStringPrintf' can avoid sprintf/snprintf
Browse files Browse the repository at this point in the history
  • Loading branch information
anaegel committed May 17, 2024
1 parent 3ba17eb commit 2fde2e6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
19 changes: 19 additions & 0 deletions ugbase/common/util/string_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@
#include <algorithm>
#include <sstream>
#include <cctype>
#include <cstdio>
#include <string>
#include <cassert>

#include "hash_function.h"
#include "common/ug_config.h"
#include "stringify.h"
Expand Down Expand Up @@ -440,6 +444,21 @@ inline const char *OnOffString(bool b)
return b ? "ON" : "OFF";
}


template< typename... Args >
inline std::string GetStringPrintf( const char* format, Args... args )
{
int length = std::snprintf( nullptr, 0, format, args... );
assert( length >= 0 );

char* buf = new char[length + 1];
std::snprintf( buf, length + 1, format, args... );

std::string str( buf );
delete[] buf;
return str;
}

} // end namespace ug

#endif /*__H__COMMON_STRING_UTIL__*/
Original file line number Diff line number Diff line change
Expand Up @@ -236,13 +236,14 @@ bool NestedIterationSolver<TDomain,TAlgebra>::apply(vector_type& u)
// Create tmp vectors
SmartPtr<vector_type> spD = u.clone_without_values();

char ext[50];
std::string ext; //char ext[50];
int loopCnt = 0;
m_lastNumSteps = 0;
{
// write start defect for debug
snprintf(ext, sizeof(ext), "_call%03d", m_dgbCall);
//snprintf(ext, sizeof(ext), "_call%03d", m_dgbCall);
std::string name("NESTED_ITER_StartSolution");
ext = GetStringPrintf("_call%03d", m_dgbCall);
name.append(ext);
write_debug(u, name.c_str());
}
Expand All @@ -266,7 +267,8 @@ bool NestedIterationSolver<TDomain,TAlgebra>::apply(vector_type& u)
m_spAss->adjust_solution(u, surfGridLevel);
NESTED_ITER_PROFILE_END();

snprintf(ext, sizeof(ext),"_call%03d_iter%03d", m_dgbCall, loopCnt);
//snprintf(ext, sizeof(ext),"_call%03d_iter%03d", m_dgbCall, loopCnt);
ext = GetStringPrintf("_call%03d_iter%03d", m_dgbCall, loopCnt);
{
// write initial data for debug
std::string name0("NESTED_ITER_InitialSolution"); name0.append(ext);
Expand Down

0 comments on commit 2fde2e6

Please sign in to comment.