C/C++ API Reference
Loading...
Searching...
No Matches
std_file_stream.h
1// Copyright 2021 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14#pragma once
15
16#include <fstream>
17
18#include "pw_stream/stream.h"
19
20namespace pw::stream {
21
23
26 public:
27 StdFileReader(const char* path) : stream_(path, std::ios::binary) {}
28
29 void Close() { stream_.close(); }
30
31 private:
33 Status DoSeek(ptrdiff_t offset, Whence origin) override;
34 size_t DoTell() override;
35 size_t ConservativeLimit(LimitType limit) const override;
36
37 std::ifstream stream_;
38};
39
42 public:
43 StdFileWriter(const char* path)
44 : stream_(path, std::ios::binary | std::ios::trunc) {}
45
46 void Close() { stream_.close(); }
47
48 private:
50 Status DoSeek(ptrdiff_t offset, Whence origin) override;
51 size_t DoTell() override;
52
53 std::ofstream stream_;
54};
55
57
58} // namespace pw::stream
Definition: status.h:109
Definition: status_with_size.h:51
Definition: stream.h:400
Definition: stream.h:491
Wraps an std::ifstream with the pw::stream::Reader interface.
Definition: std_file_stream.h:25
size_t DoTell() override
size_t ConservativeLimit(LimitType limit) const override
Status DoSeek(ptrdiff_t offset, Whence origin) override
Virtual Seek() function implemented by derived classes.
StatusWithSize DoRead(ByteSpan dest) override
Virtual Read() function implemented by derived classes.
Wraps an std::ofstream with the pw::stream::Writer interface.
Definition: std_file_stream.h:41
Status DoSeek(ptrdiff_t offset, Whence origin) override
Virtual Seek() function implemented by derived classes.
Status DoWrite(ConstByteSpan data) override
Virtual Write() function implemented by derived classes.
size_t DoTell() override
Whence
Positions from which to seek.
Definition: stream.h:48