From 3b76d3da369e38141eb40753a70243cb286de5f5 Mon Sep 17 00:00:00 2001
From: Dominik Thoennes <dominik.thoennes@fau.de>
Date: Wed, 15 Jan 2020 22:11:21 +0100
Subject: [PATCH] introduce walberla::format which can be used to format a
 string in a fprintf fashion

---
 src/core/Format.hpp | 46 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 46 insertions(+)
 create mode 100644 src/core/Format.hpp

diff --git a/src/core/Format.hpp b/src/core/Format.hpp
new file mode 100644
index 000000000..078acd143
--- /dev/null
+++ b/src/core/Format.hpp
@@ -0,0 +1,46 @@
+//======================================================================================================================
+//
+//  This file is part of waLBerla. waLBerla 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 3 of
+//  the License, or (at your option) any later version.
+//
+//  waLBerla 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 waLBerla (see COPYING.txt). If not, see <http://www.gnu.org/licenses/>.
+//
+//! \file Format.h
+//! \ingroup core
+//! \author Dominik Thoennes <dominik.thoennes@fau.de>
+//
+//======================================================================================================================
+
+#pragma once
+
+#include "core/Abort.h"
+
+#include <cstdio>
+#include <string>
+
+namespace walberla
+{
+/// format uses the printf syntax to format a given formatString and return a std::string
+///\param formatString the format string
+///\param args all arguments which will be inserted into the string, these CANNOT be std::string but have to be
+/// converted using .c_str()
+template< typename... Args >
+std::string format(const std::string& formatString, Args&&... args)
+{
+   /// this size is arbitrary
+   const size_t maxBufferSize = 4096;
+   char buffer[maxBufferSize];
+   int check = snprintf(buffer, maxBufferSize, formatString.c_str(), args...);
+   if (check <= 0 || check > int(maxBufferSize)) { WALBERLA_ABORT("snprintf failed"); }
+   return std::string(buffer);
+}
+
+} // namespace walberla
\ No newline at end of file
-- 
GitLab