aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2017-11-21 14:06:21 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2017-11-21 14:06:21 +0200
commit634d73eb9a84e27cecf0cc39cf371f5d16556514 (patch)
tree53740049ddb9102c720b508647134f8f3beb6d33
parent1af2a51508e989956414f55240923a11a938ec6d (diff)
Move eof() utility to libbutl
-rw-r--r--libbutl/utility.ixx12
-rw-r--r--libbutl/utility.mxx17
2 files changed, 28 insertions, 1 deletions
diff --git a/libbutl/utility.ixx b/libbutl/utility.ixx
index cec61c8..d703211 100644
--- a/libbutl/utility.ixx
+++ b/libbutl/utility.ixx
@@ -4,6 +4,18 @@
namespace butl
{
+ inline bool
+ eof (std::istream& is)
+ {
+ if (!is.fail ())
+ return false;
+
+ if (is.eof ())
+ return true;
+
+ throw std::istream::failure ("");
+ }
+
inline char
ucase (char c)
{
diff --git a/libbutl/utility.mxx b/libbutl/utility.mxx
index 8ef0d08..410dae8 100644
--- a/libbutl/utility.mxx
+++ b/libbutl/utility.mxx
@@ -15,6 +15,7 @@
#ifndef __cpp_lib_modules
#include <string>
#include <iosfwd> // ostream
+#include <istream>
#include <cstddef> // size_t
#include <utility> // move(), forward()
#include <cstring> // strcmp(), strlen()
@@ -37,7 +38,6 @@ import std.io;
#include <libbutl/export.hxx>
-
LIBBUTL_MODEXPORT namespace butl
{
// Throw std::system_error with generic_category or system_category,
@@ -56,6 +56,21 @@ LIBBUTL_MODEXPORT namespace butl
[[noreturn]] LIBBUTL_SYMEXPORT void
throw_system_error (int system_code, int fallback_errno_code = 0);
+ // If an input stream is in a failed state, then return true if this is
+ // because of the eof and throw istream::failure otherwise. If the stream
+ // is not in a failed state, return false. This helper function is normally
+ // used like this:
+ //
+ // is.exceptions (istream::badbit);
+ //
+ // for (string l; !eof (getline (is, l)); )
+ // {
+ // ...
+ // }
+ //
+ bool
+ eof (std::istream&);
+
// Convert ASCII character/string case. If there is no upper/lower case
// counterpart, leave the character unchanged. The POSIX locale (also known
// as C locale) must be the current application locale. Otherwise the