Armando Montanez | 68de071 | 2019-11-14 18:29:39 -0800 | [diff] [blame] | 1 | // Copyright 2019 The Pigweed Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); you may not |
Wyatt Hepler | 1a96094 | 2019-11-26 14:13:38 -0800 | [diff] [blame] | 4 | // use this file except in compliance with the License. You may obtain a copy of |
| 5 | // the License at |
Armando Montanez | 68de071 | 2019-11-14 18:29:39 -0800 | [diff] [blame] | 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 |
Wyatt Hepler | 1a96094 | 2019-11-26 14:13:38 -0800 | [diff] [blame] | 12 | // License for the specific language governing permissions and limitations under |
| 13 | // the License. |
Armando Montanez | 68de071 | 2019-11-14 18:29:39 -0800 | [diff] [blame] | 14 | |
Armando Montanez | f7a5a74 | 2020-03-02 14:58:59 -0800 | [diff] [blame] | 15 | #include "pw_sys_io/sys_io.h" |
Armando Montanez | 68de071 | 2019-11-14 18:29:39 -0800 | [diff] [blame] | 16 | |
| 17 | #include <cstdio> |
| 18 | |
Armando Montanez | f7a5a74 | 2020-03-02 14:58:59 -0800 | [diff] [blame] | 19 | namespace pw::sys_io { |
Armando Montanez | 68de071 | 2019-11-14 18:29:39 -0800 | [diff] [blame] | 20 | |
Armando Montanez | 759ff77 | 2019-11-18 15:05:53 -0800 | [diff] [blame] | 21 | Status ReadByte(std::byte* dest) { |
Armando Montanez | 68de071 | 2019-11-14 18:29:39 -0800 | [diff] [blame] | 22 | if (dest == nullptr) { |
Wyatt Hepler | d78f7c6 | 2020-09-28 14:27:32 -0700 | [diff] [blame] | 23 | return Status::FailedPrecondition(); |
Armando Montanez | 68de071 | 2019-11-14 18:29:39 -0800 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | int value = std::getchar(); |
| 27 | if (value == EOF) { |
Wyatt Hepler | d78f7c6 | 2020-09-28 14:27:32 -0700 | [diff] [blame] | 28 | return Status::ResourceExhausted(); |
Armando Montanez | 68de071 | 2019-11-14 18:29:39 -0800 | [diff] [blame] | 29 | } |
| 30 | *dest = static_cast<std::byte>(value); |
Wyatt Hepler | 1b3da3a | 2021-01-07 13:26:57 -0800 | [diff] [blame] | 31 | return OkStatus(); |
Armando Montanez | 68de071 | 2019-11-14 18:29:39 -0800 | [diff] [blame] | 32 | } |
| 33 | |
Keir Mierle | 6909f18 | 2020-11-06 14:04:00 -0800 | [diff] [blame] | 34 | Status TryReadByte(std::byte*) { |
| 35 | // TryReadByte() is not (yet) supported on hosts. |
| 36 | return Status::Unimplemented(); |
| 37 | } |
| 38 | |
Armando Montanez | 759ff77 | 2019-11-18 15:05:53 -0800 | [diff] [blame] | 39 | Status WriteByte(std::byte b) { |
Armando Montanez | 68de071 | 2019-11-14 18:29:39 -0800 | [diff] [blame] | 40 | if (std::putchar(static_cast<char>(b)) == EOF) { |
Wyatt Hepler | d78f7c6 | 2020-09-28 14:27:32 -0700 | [diff] [blame] | 41 | return Status::Internal(); |
Armando Montanez | 68de071 | 2019-11-14 18:29:39 -0800 | [diff] [blame] | 42 | } |
Wyatt Hepler | 1b3da3a | 2021-01-07 13:26:57 -0800 | [diff] [blame] | 43 | return OkStatus(); |
Armando Montanez | 68de071 | 2019-11-14 18:29:39 -0800 | [diff] [blame] | 44 | } |
| 45 | |
Armando Montanez | 759ff77 | 2019-11-18 15:05:53 -0800 | [diff] [blame] | 46 | StatusWithSize WriteLine(const std::string_view& s) { |
| 47 | size_t chars_written = 0; |
Wyatt Hepler | e2cbadf | 2020-06-22 11:21:45 -0700 | [diff] [blame] | 48 | StatusWithSize size_result = WriteBytes(std::as_bytes(std::span(s))); |
Armando Montanez | 759ff77 | 2019-11-18 15:05:53 -0800 | [diff] [blame] | 49 | 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 Montanez | f7a5a74 | 2020-03-02 14:58:59 -0800 | [diff] [blame] | 63 | } // namespace pw::sys_io |