James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 1 | // Copyright (c) FIRST and other WPILib contributors. |
| 2 | // Open Source Software; you can modify and/or share it under the terms of |
| 3 | // the WPILib BSD license file in the root directory of this project. |
| 4 | |
| 5 | #include "wpi/MessagePack.h" |
| 6 | |
| 7 | using namespace mpack; |
| 8 | |
| 9 | mpack_error_t mpack::mpack_expect_str(mpack_reader_t* reader, std::string* out, |
| 10 | uint32_t maxLen) { |
| 11 | uint32_t count = mpack_expect_str_max(reader, maxLen); |
| 12 | mpack_error_t err = mpack_reader_error(reader); |
| 13 | if (err != mpack_ok) { |
| 14 | return err; |
| 15 | } |
| 16 | const char* bytes = mpack_read_bytes_inplace(reader, count); |
| 17 | if (bytes) { |
| 18 | out->assign(bytes, count); |
| 19 | } else { |
| 20 | return mpack_reader_error(reader); |
| 21 | } |
| 22 | mpack_done_str(reader); |
| 23 | return mpack_ok; |
| 24 | } |
| 25 | |
| 26 | mpack_error_t mpack::mpack_read_str(mpack_reader_t* reader, mpack_tag_t* tag, |
| 27 | std::string* out, uint32_t maxLen) { |
| 28 | uint32_t count = mpack_tag_str_length(tag); |
| 29 | mpack_error_t err = mpack_reader_error(reader); |
| 30 | if (err != mpack_ok) { |
| 31 | return err; |
| 32 | } |
| 33 | if (count > maxLen) { |
| 34 | mpack_reader_flag_error(reader, mpack_error_too_big); |
| 35 | return mpack_error_too_big; |
| 36 | } |
| 37 | const char* bytes = mpack_read_bytes_inplace(reader, count); |
| 38 | if (bytes) { |
| 39 | out->assign(bytes, count); |
| 40 | } else { |
| 41 | return mpack_reader_error(reader); |
| 42 | } |
| 43 | mpack_done_str(reader); |
| 44 | return mpack_ok; |
| 45 | } |