| 1 | // Aseprite Document Library |
| 2 | // Copyright (c) 2001-2016 David Capello |
| 3 | // |
| 4 | // This file is released under the terms of the MIT license. |
| 5 | // Read LICENSE.txt for more information. |
| 6 | |
| 7 | #ifdef HAVE_CONFIG_H |
| 8 | #include "config.h" |
| 9 | #endif |
| 10 | |
| 11 | #include "doc/string_io.h" |
| 12 | |
| 13 | #include "base/serialization.h" |
| 14 | |
| 15 | #include <vector> |
| 16 | #include <iostream> |
| 17 | |
| 18 | namespace doc { |
| 19 | |
| 20 | using namespace base::serialization; |
| 21 | using namespace base::serialization::little_endian; |
| 22 | |
| 23 | void write_string(std::ostream& os, const std::string& str) |
| 24 | { |
| 25 | write16(os, (uint16_t)str.size()); |
| 26 | if (!str.empty()) |
| 27 | os.write(str.c_str(), str.size()); |
| 28 | } |
| 29 | |
| 30 | std::string read_string(std::istream& is) |
| 31 | { |
| 32 | uint16_t length = read16(is); |
| 33 | std::vector<char> str(length+1); |
| 34 | if (length > 0) { |
| 35 | is.read(&str[0], length); |
| 36 | str[length] = 0; |
| 37 | } |
| 38 | else |
| 39 | str[0] = 0; |
| 40 | |
| 41 | return std::string(&str[0]); |
| 42 | } |
| 43 | |
| 44 | } |
| 45 | |