From 29591ce2e7599d549ad050a6364055f2ea2d8c4c Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 5 Dec 2015 16:13:25 +0000 Subject: Add wrappers for fopen fprintf when using boost things --- libadhocutil/fprintbf.cpp | 34 +++++++++++++++++++ libadhocutil/fprintbf.h | 29 ++++++++++++++++ libadhocutil/unittests/Jamfile.jam | 13 +++++++ libadhocutil/unittests/testFprintbf.cpp | 60 +++++++++++++++++++++++++++++++++ 4 files changed, 136 insertions(+) create mode 100644 libadhocutil/fprintbf.cpp create mode 100644 libadhocutil/fprintbf.h create mode 100644 libadhocutil/unittests/testFprintbf.cpp diff --git a/libadhocutil/fprintbf.cpp b/libadhocutil/fprintbf.cpp new file mode 100644 index 0000000..a80e571 --- /dev/null +++ b/libadhocutil/fprintbf.cpp @@ -0,0 +1,34 @@ +#include "fprintbf.h" +#include +#include + +size_t +fprintss(FILE * f, const std::string & s) +{ + auto r = fwrite(s.c_str(), 1, s.length(), f); + if (r < s.length()) { + throw std::system_error(errno, std::system_category()); + } + return s.length(); +} + +size_t +fprintbf(FILE * f, const boost::format & fmt) +{ + auto r = fwrite(fmt.str().c_str(), 1, fmt.size(), f); + if (r < fmt.size()) { + throw std::system_error(errno, std::system_category()); + } + return fmt.size(); +} + +FILE * +fopen(const boost::filesystem::path & path, const char * mode) +{ + auto f = fopen(path.c_str(), mode); + if (!f) { + throw std::system_error(errno, std::system_category()); + } + return f; +} + diff --git a/libadhocutil/fprintbf.h b/libadhocutil/fprintbf.h new file mode 100644 index 0000000..59d33d8 --- /dev/null +++ b/libadhocutil/fprintbf.h @@ -0,0 +1,29 @@ +#ifndef ADHOCUTIL_FPRINTBF_H +#define ADHOCUTIL_FPRINTBF_H + +#include "visibility.h" +#include "buffer.h" +#include +#include + +DLL_PUBLIC size_t fprintss(FILE *, const std::string &); + +DLL_PUBLIC size_t fprintbf(FILE *, const boost::format &); + +DLL_PUBLIC FILE * fopen(const boost::filesystem::path & path, const char * mode); + +template +size_t fprintbf(FILE * f, const std::string & fmt, const Params & ... p) +{ + return fprintbf(f, *AdHoc::Buffer::getFormat(fmt), p...); +} + +template +size_t fprintbf(FILE * f, boost::format & fmt, const Param & p, const Params & ... ps) +{ + fmt % p; + return fprintbf(f, fmt, ps...); +} + +#endif + diff --git a/libadhocutil/unittests/Jamfile.jam b/libadhocutil/unittests/Jamfile.jam index 582b174..9f45265 100644 --- a/libadhocutil/unittests/Jamfile.jam +++ b/libadhocutil/unittests/Jamfile.jam @@ -197,3 +197,16 @@ run testResourcePool ; +run + testFprintbf.cpp + : : : + BOOST_TEST_DYN_LINK + ..//adhocutil + boost_utf + ROOT=\"$(me)\" + boost_system + boost_filesystem + : + testFprintbf + ; + diff --git a/libadhocutil/unittests/testFprintbf.cpp b/libadhocutil/unittests/testFprintbf.cpp new file mode 100644 index 0000000..1f4e55a --- /dev/null +++ b/libadhocutil/unittests/testFprintbf.cpp @@ -0,0 +1,60 @@ +#define BOOST_TEST_MODULE fprintbf +#include + +#include "fprintbf.h" +#include "definedDirs.h" +#include + +BOOST_AUTO_TEST_CASE( writestring ) +{ + FILE * f = fopen(binDir / "writestring", "w"); + BOOST_REQUIRE(f); + BOOST_REQUIRE_EQUAL(fprintss(f, std::string("some string")), 11); + fclose(f); +} + +BOOST_AUTO_TEST_CASE( writestringerror ) +{ + FILE * f = fopen(binDir / "writestring", "r"); + BOOST_REQUIRE(f); + BOOST_REQUIRE_THROW(fprintss(f, "some string"), std::system_error); + fclose(f); +} + +BOOST_AUTO_TEST_CASE( writeformatNoArgs ) +{ + FILE * f = fopen(binDir / "writeformatNoArgs", "w"); + BOOST_REQUIRE(f); + BOOST_REQUIRE_EQUAL(fprintbf(f, "some string"), 11); + fclose(f); +} + +BOOST_AUTO_TEST_CASE( writeformatOneArg ) +{ + FILE * f = fopen(binDir / "writeformatOneArg", "w"); + BOOST_REQUIRE(f); + BOOST_REQUIRE_EQUAL(fprintbf(f, "some %s", "string"), 11); + fclose(f); +} + +BOOST_AUTO_TEST_CASE( writeformatTwoArgs ) +{ + FILE * f = fopen(binDir / "writeformatTwoArgs", "w"); + BOOST_REQUIRE(f); + BOOST_REQUIRE_EQUAL(fprintbf(f, "s%dme %s", 0, "string"), 11); + fclose(f); +} + +BOOST_AUTO_TEST_CASE( writeformatError ) +{ + FILE * f = fopen(binDir / "writeformatTwoArgs", "r"); + BOOST_REQUIRE(f); + BOOST_REQUIRE_THROW(fprintbf(f, "s%dme %s", 0, "string"), std::system_error); + fclose(f); +} + +BOOST_AUTO_TEST_CASE( fopenerror ) +{ + BOOST_REQUIRE_THROW(fopen(binDir / "missing" / "folder", "r"), std::system_error); +} + -- cgit v1.2.3