Skip to content

Commit

Permalink
update includes for cstdio, use ftell/fseek instead of ftello/fseeko (#…
Browse files Browse the repository at this point in the history
…362)

**Public-Facing Changes**
None

**Description**
Fixes build failure on Windows (conan-io/conan-center-index#10104 (comment))
  • Loading branch information
jtbandes authored Apr 5, 2022
1 parent fa3c73c commit 743b1f8
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 19 deletions.
7 changes: 4 additions & 3 deletions cpp/mcap/include/mcap/reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "intervaltree.hpp"
#include "types.hpp"
#include <cstdio>
#include <fstream>
#include <map>
#include <memory>
Expand Down Expand Up @@ -70,13 +71,13 @@ struct IReadable {
*/
class FileReader final : public IReadable {
public:
FileReader(FILE* file);
FileReader(std::FILE* file);

uint64_t size() const override;
uint64_t read(std::byte** output, uint64_t offset, uint64_t size) override;

private:
FILE* file_;
std::FILE* file_;
std::vector<std::byte> buffer_;
uint64_t size_;
uint64_t position_;
Expand Down Expand Up @@ -386,7 +387,7 @@ class McapReader final {
friend LinearMessageView;

IReadable* input_ = nullptr;
FILE* file_ = nullptr;
std::FILE* file_ = nullptr;
std::unique_ptr<FileReader> fileInput_;
std::unique_ptr<FileStreamReader> fileStreamInput_;
std::optional<Header> header_;
Expand Down
20 changes: 10 additions & 10 deletions cpp/mcap/include/mcap/reader.inl
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,16 @@ Status BufferReader::status() const {

// FileReader //////////////////////////////////////////////////////////////////

FileReader::FileReader(FILE* file)
FileReader::FileReader(std::FILE* file)
: file_(file)
, size_(0)
, position_(0) {
assert(file_);

// Determine the size of the file
fseeko(file_, 0, SEEK_END);
size_ = ftello(file_);
fseeko(file_, 0, SEEK_SET);
std::fseek(file_, 0, SEEK_END);
size_ = std::ftell(file_);
std::fseek(file_, 0, SEEK_SET);
}

uint64_t FileReader::size() const {
Expand All @@ -64,16 +64,16 @@ uint64_t FileReader::read(std::byte** output, uint64_t offset, uint64_t size) {
}

if (offset != position_) {
fseeko(file_, offset, SEEK_SET);
fflush(file_);
std::fseek(file_, offset, SEEK_SET);
std::fflush(file_);
position_ = offset;
}

if (size > buffer_.size()) {
buffer_.resize(size);
}

const uint64_t bytesRead = uint64_t(fread(buffer_.data(), 1, size, file_));
const uint64_t bytesRead = uint64_t(std::fread(buffer_.data(), 1, size, file_));
*output = buffer_.data();

position_ += bytesRead;
Expand Down Expand Up @@ -278,10 +278,10 @@ Status McapReader::open(IReadable& reader) {

Status McapReader::open(std::string_view filename) {
if (file_) {
fclose(file_);
std::fclose(file_);
file_ = nullptr;
}
file_ = fopen(filename.data(), "rb");
file_ = std::fopen(filename.data(), "rb");
if (!file_) {
const auto msg = internal::StrCat("failed to open \"", filename, "\"");
return Status{StatusCode::OpenFailed, msg};
Expand All @@ -299,7 +299,7 @@ Status McapReader::open(std::ifstream& stream) {
void McapReader::close() {
input_ = nullptr;
if (file_) {
fclose(file_);
std::fclose(file_);
file_ = nullptr;
}
fileInput_.reset();
Expand Down
3 changes: 2 additions & 1 deletion cpp/mcap/include/mcap/writer.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "types.hpp"
#include <cstdio>
#include <memory>
#include <string>
#include <unordered_set>
Expand Down Expand Up @@ -152,7 +153,7 @@ class FileWriter final : public IWritable {
private:
std::vector<std::byte> buffer_;
size_t bufferCapacity_;
FILE* file_ = nullptr;
std::FILE* file_ = nullptr;
uint64_t size_ = 0;
};

Expand Down
10 changes: 5 additions & 5 deletions cpp/mcap/include/mcap/writer.inl
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ FileWriter::~FileWriter() {

Status FileWriter::open(std::string_view filename, size_t bufferCapacity) {
end();
file_ = fopen(filename.data(), "wb");
file_ = std::fopen(filename.data(), "wb");
if (!file_) {
const auto msg = internal::StrCat("failed to open file \"", filename, "\" for writing");
return Status(StatusCode::OpenFailed, msg);
Expand All @@ -56,7 +56,7 @@ void FileWriter::handleWrite(const std::byte* data, uint64_t size) {

// If this will overflow the buffer, flush it
if (buffer_.size() > 0 && buffer_.size() + size > bufferCapacity_) {
const size_t written = fwrite(buffer_.data(), 1, buffer_.size(), file_);
const size_t written = std::fwrite(buffer_.data(), 1, buffer_.size(), file_);
(void)written;
assert(written == buffer_.size());
buffer_.clear();
Expand All @@ -65,7 +65,7 @@ void FileWriter::handleWrite(const std::byte* data, uint64_t size) {
if (buffer_.size() + size <= bufferCapacity_) {
buffer_.insert(buffer_.end(), data, data + size);
} else {
const size_t written = fwrite(data, 1, size, file_);
const size_t written = std::fwrite(data, 1, size, file_);
(void)written;
assert(written == size);
}
Expand All @@ -76,10 +76,10 @@ void FileWriter::handleWrite(const std::byte* data, uint64_t size) {
void FileWriter::end() {
if (file_) {
if (buffer_.size() > 0) {
fwrite(buffer_.data(), 1, buffer_.size(), file_);
std::fwrite(buffer_.data(), 1, buffer_.size(), file_);
}

fclose(file_);
std::fclose(file_);
file_ = nullptr;
}
buffer_.clear();
Expand Down

0 comments on commit 743b1f8

Please sign in to comment.