aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2017-06-23 14:14:16 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2017-06-23 14:14:16 +0200
commitc36042925c8d52e3899ea310018cb7243d191306 (patch)
tree1878230d84e479078862099e06feec807c670041
parent5d50c0499b30650deafc291a3872a386d08a3200 (diff)
Add support for querying position in fdbuf
-rw-r--r--libbutl/fdstream.cxx11
-rw-r--r--libbutl/fdstream.hxx13
2 files changed, 22 insertions, 2 deletions
diff --git a/libbutl/fdstream.cxx b/libbutl/fdstream.cxx
index 292324a..daa344f 100644
--- a/libbutl/fdstream.cxx
+++ b/libbutl/fdstream.cxx
@@ -145,7 +145,7 @@ namespace butl
setg (buf_, buf_, buf_);
setp (buf_, buf_ + sizeof (buf_) - 1); // Keep space for overflow's char.
-
+ off_ = 0; // @@ Strictly speaking, need to query, can be at end.
fd_ = move (fd);
}
@@ -177,6 +177,8 @@ namespace butl
return -1;
setg (buf_, buf_, buf_ + n);
+ off_ += n;
+
return n;
}
#endif
@@ -223,6 +225,7 @@ namespace butl
throw_ios_failure (errno);
setg (buf_, buf_, buf_ + n);
+ off_ += n;
return n != 0;
}
@@ -295,6 +298,8 @@ namespace butl
if (m == -1)
throw_ios_failure (errno);
+ off_ += m;
+
if (n != static_cast<size_t> (m))
return false;
@@ -352,6 +357,7 @@ namespace butl
throw_ios_failure (errno);
size_t m (static_cast<size_t> (r));
+ off_ += m;
// If the buffered data wasn't fully written then move the unwritten part
// to the beginning of the buffer.
@@ -395,6 +401,7 @@ namespace butl
throw_ios_failure (errno);
size_t m (static_cast<size_t> (r));
+ off_ += m;
// If the buffered data wasn't fully written then move the unwritten part
// to the beginning of the buffer.
@@ -430,6 +437,8 @@ namespace butl
if (r == -1)
throw_ios_failure (errno);
+ off_ += r;
+
return an + r;
#endif
}
diff --git a/libbutl/fdstream.hxx b/libbutl/fdstream.hxx
index 771d242..27530e2 100644
--- a/libbutl/fdstream.hxx
+++ b/libbutl/fdstream.hxx
@@ -10,7 +10,7 @@
#include <istream>
#include <ostream>
#include <utility> // move()
-#include <cstdint> // uint16_t
+#include <cstdint> // uint16_t, uint64_t
#include <libbutl/export.hxx>
@@ -143,6 +143,11 @@ namespace butl
using base::egptr;
using base::gbump;
+ // Return the (logical) position of the next byte to be read.
+ //
+ uint64_t
+ tellg () const {return off_ - (egptr () - gptr ());}
+
private:
bool
load ();
@@ -159,12 +164,18 @@ namespace butl
virtual std::streamsize
xsputn (const char_type*, std::streamsize);
+ // Return the (logical) position of the next byte to be written.
+ //
+ uint64_t
+ tellp () const {return off_ + (pptr () - buf_);}
+
private:
bool
save ();
private:
auto_fd fd_;
+ uint64_t off_;
char buf_[8192];
bool non_blocking_ = false;
};