summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2015-12-05 16:13:25 +0000
committerDan Goodliffe <dan@randomdan.homeip.net>2015-12-05 16:13:25 +0000
commit29591ce2e7599d549ad050a6364055f2ea2d8c4c (patch)
tree01cf7385d45d54542c4b5594369a1b2afe960ec0
parentImplement and test resource testing and replacement on reuse. (diff)
downloadlibadhocutil-29591ce2e7599d549ad050a6364055f2ea2d8c4c.tar.bz2
libadhocutil-29591ce2e7599d549ad050a6364055f2ea2d8c4c.tar.xz
libadhocutil-29591ce2e7599d549ad050a6364055f2ea2d8c4c.zip
Add wrappers for fopen fprintf when using boost things
-rw-r--r--libadhocutil/fprintbf.cpp34
-rw-r--r--libadhocutil/fprintbf.h29
-rw-r--r--libadhocutil/unittests/Jamfile.jam13
-rw-r--r--libadhocutil/unittests/testFprintbf.cpp60
4 files changed, 136 insertions, 0 deletions
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 <stdio.h>
+#include <system_error>
+
+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 <boost/format.hpp>
+#include <boost/filesystem/path.hpp>
+
+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 <typename ... Params>
+size_t fprintbf(FILE * f, const std::string & fmt, const Params & ... p)
+{
+ return fprintbf(f, *AdHoc::Buffer::getFormat(fmt), p...);
+}
+
+template <typename Param, typename ... Params>
+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
+ : : :
+ <define>BOOST_TEST_DYN_LINK
+ <library>..//adhocutil
+ <library>boost_utf
+ <define>ROOT=\"$(me)\"
+ <library>boost_system
+ <library>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 <boost/test/unit_test.hpp>
+
+#include "fprintbf.h"
+#include "definedDirs.h"
+#include <system_error>
+
+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);
+}
+