1 // Copyright 2019 The Pigweed Authors
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
7 // https://www.apache.org/licenses/LICENSE-2.0
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
15 #include "pw_sys_io/sys_io.h"
19 namespace pw::sys_io {
21 Status ReadByte(std::byte* dest) {
22 if (dest == nullptr) {
23 return Status::FailedPrecondition();
26 int value = std::getchar();
28 return Status::ResourceExhausted();
30 *dest = static_cast<std::byte>(value);
34 Status TryReadByte(std::byte*) {
35 // TryReadByte() is not (yet) supported on hosts.
36 return Status::Unimplemented();
39 Status WriteByte(std::byte b) {
40 if (std::putchar(static_cast<char>(b)) == EOF) {
41 return Status::Internal();
46 StatusWithSize WriteLine(const std::string_view& s) {
47 size_t chars_written = 0;
48 StatusWithSize size_result = WriteBytes(std::as_bytes(std::span(s)));
49 if (!size_result.ok()) {
52 chars_written += size_result.size();
54 // Write trailing newline character.
55 Status result = WriteByte(static_cast<std::byte>('\n'));
60 return StatusWithSize(result, chars_written);
63 } // namespace pw::sys_io