blob: 333b18f8f3bc24a93c2ac2ecf98e1e945e92634c [file] [log] [blame]
Armando Montanez68de0712019-11-14 18:29:39 -08001// Copyright 2019 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
Wyatt Hepler1a960942019-11-26 14:13:38 -08004// use this file except in compliance with the License. You may obtain a copy of
5// the License at
Armando Montanez68de0712019-11-14 18:29:39 -08006//
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
Wyatt Hepler1a960942019-11-26 14:13:38 -080012// License for the specific language governing permissions and limitations under
13// the License.
Armando Montanez68de0712019-11-14 18:29:39 -080014
Armando Montanezf7a5a742020-03-02 14:58:59 -080015#include "pw_sys_io/sys_io.h"
Armando Montanez68de0712019-11-14 18:29:39 -080016
17#include <cstdio>
18
Armando Montanezf7a5a742020-03-02 14:58:59 -080019namespace pw::sys_io {
Armando Montanez68de0712019-11-14 18:29:39 -080020
Armando Montanez759ff772019-11-18 15:05:53 -080021Status ReadByte(std::byte* dest) {
Armando Montanez68de0712019-11-14 18:29:39 -080022 if (dest == nullptr) {
Wyatt Heplerd78f7c62020-09-28 14:27:32 -070023 return Status::FailedPrecondition();
Armando Montanez68de0712019-11-14 18:29:39 -080024 }
25
26 int value = std::getchar();
27 if (value == EOF) {
Wyatt Heplerd78f7c62020-09-28 14:27:32 -070028 return Status::ResourceExhausted();
Armando Montanez68de0712019-11-14 18:29:39 -080029 }
30 *dest = static_cast<std::byte>(value);
Wyatt Hepler1b3da3a2021-01-07 13:26:57 -080031 return OkStatus();
Armando Montanez68de0712019-11-14 18:29:39 -080032}
33
Keir Mierle6909f182020-11-06 14:04:00 -080034Status TryReadByte(std::byte*) {
35 // TryReadByte() is not (yet) supported on hosts.
36 return Status::Unimplemented();
37}
38
Armando Montanez759ff772019-11-18 15:05:53 -080039Status WriteByte(std::byte b) {
Armando Montanez68de0712019-11-14 18:29:39 -080040 if (std::putchar(static_cast<char>(b)) == EOF) {
Wyatt Heplerd78f7c62020-09-28 14:27:32 -070041 return Status::Internal();
Armando Montanez68de0712019-11-14 18:29:39 -080042 }
Wyatt Hepler1b3da3a2021-01-07 13:26:57 -080043 return OkStatus();
Armando Montanez68de0712019-11-14 18:29:39 -080044}
45
Armando Montanez759ff772019-11-18 15:05:53 -080046StatusWithSize WriteLine(const std::string_view& s) {
47 size_t chars_written = 0;
Wyatt Heplere2cbadf2020-06-22 11:21:45 -070048 StatusWithSize size_result = WriteBytes(std::as_bytes(std::span(s)));
Armando Montanez759ff772019-11-18 15:05:53 -080049 if (!size_result.ok()) {
50 return size_result;
51 }
52 chars_written += size_result.size();
53
54 // Write trailing newline character.
55 Status result = WriteByte(static_cast<std::byte>('\n'));
56 if (result.ok()) {
57 chars_written++;
58 }
59
60 return StatusWithSize(result, chars_written);
61}
62
Armando Montanezf7a5a742020-03-02 14:58:59 -080063} // namespace pw::sys_io