Skip to content

Commit 333b7a5

Browse files
committed
Use tellp
1 parent 4456ce6 commit 333b7a5

File tree

3 files changed

+21
-22
lines changed

3 files changed

+21
-22
lines changed

core/include/core/dataio.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,6 @@ void
5959
g3_ostream_to_path(std::ostream &stream, const std::string &path, bool append=false,
6060
size_t buffersize=1024*1024, const std::string &ext=".g3");
6161

62-
/**
63-
* Count the number of bytes written to the output file stream.
64-
*
65-
* @param stream A reference to the output stream, as configured by
66-
* g3_ostream_to_path with the counter argument set to true.
67-
* @return Number of bytes written to disk.
68-
*/
69-
size_t g3_ostream_count(std::ostream &stream);
70-
7162
/**
7263
* Flush the output file stream.
7364
*

core/src/G3MultiFileWriter.cxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ G3MultiFileWriter::CheckNewFile(G3FramePtr frame)
8686
if (stream_) {
8787
bool start_new_ = false;
8888

89-
if (g3_ostream_count(stream_) > size_limit_)
89+
if (stream_.tellp() > size_limit_)
9090
start_new_ = true;
9191

9292
if (newfile_callback_.ptr() != Py_None &&

core/src/dataio.cxx

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ class RemoteInputStreamBuffer : public std::streambuf {
225225
std::streampos seekoff(std::streamoff off, std::ios_base::seekdir way,
226226
std::ios_base::openmode mode) {
227227
// short-circuit for tellg
228-
if (off == 0 && way == std::ios_base::cur)
228+
if ((mode & std::ios_base::in) && off == 0 && way == std::ios_base::cur)
229229
return bytes_;
230230
log_fatal("Seek not implemented for remote stream");
231231
}
@@ -403,6 +403,8 @@ class InputStreamCounter : public std::streambuf {
403403

404404
std::streampos seekoff(std::streamoff off, std::ios_base::seekdir way,
405405
std::ios_base::openmode mode) {
406+
if (!(mode & std::ios_base::in))
407+
log_fatal("Seek not implemented for output stream");
406408
// short-circuit for tellg
407409
if (off == 0 && way == std::ios_base::cur)
408410
return bytes_;
@@ -413,6 +415,8 @@ class InputStreamCounter : public std::streambuf {
413415
}
414416

415417
std::streampos seekpos(std::streampos pos, std::ios_base::openmode mode) {
418+
if (!(mode & std::ios_base::in))
419+
log_fatal("Seek not implemented for output stream");
416420
std::streampos n = buffer_->pubseekpos(pos, mode);
417421
if (n != std::streampos(std::streamoff(-1)))
418422
bytes_ = n;
@@ -524,20 +528,34 @@ class OutputStreamCounter : public std::streambuf {
524528

525529
protected:
526530
virtual int_type overflow(int_type c) {
527-
if (buffer_->sputc(c) != traits_type::eof()) {
531+
if (buffer_ && buffer_->sputc(c) != traits_type::eof()) {
528532
bytes_++;
529533
return c;
530534
}
531535
return traits_type::eof();
532536
}
533537

534538
virtual std::streamsize xsputn(const char* s, std::streamsize n) {
539+
if (!buffer_)
540+
return 0;
535541
std::streamsize nput = buffer_->sputn(s, n);
536542
if (nput > 0)
537543
bytes_ += nput;
538544
return nput;
539545
}
540546

547+
std::streampos seekoff(std::streamoff off, std::ios_base::seekdir way,
548+
std::ios_base::openmode mode) {
549+
// short-circuit for tellp
550+
if ((mode & std::ios_base::out) && off == 0 && way == std::ios_base::cur)
551+
return bytes_;
552+
log_fatal("Seek not implemented for output stream");
553+
}
554+
555+
std::streampos seekpos(std::streampos pos, std::ios_base::openmode mode) {
556+
log_fatal("Seek not implemented for output stream");
557+
}
558+
541559
size_t bytes_;
542560

543561
private:
@@ -721,16 +739,6 @@ g3_ostream_to_path(std::ostream &stream, const std::string &path, bool append,
721739
stream.pword(1) = sbuf;
722740
}
723741

724-
size_t
725-
g3_ostream_count(std::ostream &stream)
726-
{
727-
OutputStreamCounter* cbuf = static_cast<OutputStreamCounter*>(stream.rdbuf());
728-
if (!cbuf)
729-
log_fatal("Could not get stream counter");
730-
731-
return cbuf->bytes();
732-
}
733-
734742
void
735743
g3_ostream_flush(std::ostream &stream)
736744
{

0 commit comments

Comments
 (0)