Read a null-terminated string up to max_len bytes Returns false if no null terminator found within max_len
| 99 | // Read a null-terminated string up to max_len bytes |
| 100 | // Returns false if no null terminator found within max_len |
| 101 | bool read_string(std::string* str, size_t max_len = 256) { |
| 102 | if (!str) { |
| 103 | add_error("Null pointer passed to read_string"); |
| 104 | return false; |
| 105 | } |
| 106 | |
| 107 | str->clear(); |
| 108 | size_t start_pos = stream_.tell(); |
| 109 | |
| 110 | for (size_t i = 0; i < max_len; i++) { |
| 111 | uint8_t c; |
| 112 | if (!stream_.read1(&c)) { |
| 113 | add_error("Failed to read string at position " + std::to_string(start_pos)); |
| 114 | return false; |
| 115 | } |
| 116 | if (c == '\0') { |
| 117 | return true; |
| 118 | } |
| 119 | str->push_back(static_cast<char>(c)); |
| 120 | } |
| 121 | |
| 122 | add_error("String not null-terminated within " + std::to_string(max_len) + |
| 123 | " bytes at position " + std::to_string(start_pos)); |
| 124 | return false; |
| 125 | } |
| 126 | |
| 127 | // Seek to absolute position |
| 128 | bool seek(size_t pos) { |