From 51a03f27ab13f7caeaa9466cbe13ca3e7a389e7c Mon Sep 17 00:00:00 2001 From: David Capello Date: Tue, 17 Feb 2015 13:16:33 -0300 Subject: [PATCH] Add doc::read/write_string() --- src/doc/CMakeLists.txt | 1 + src/doc/layer_io.cpp | 20 +++++--------------- src/doc/string_io.cpp | 43 ++++++++++++++++++++++++++++++++++++++++++ src/doc/string_io.h | 20 ++++++++++++++++++++ 4 files changed, 69 insertions(+), 15 deletions(-) create mode 100644 src/doc/string_io.cpp create mode 100644 src/doc/string_io.h diff --git a/src/doc/CMakeLists.txt b/src/doc/CMakeLists.txt index 5c31d67ff..e937ad77f 100644 --- a/src/doc/CMakeLists.txt +++ b/src/doc/CMakeLists.txt @@ -42,4 +42,5 @@ add_library(doc-lib rgbmap.cpp sprite.cpp sprites.cpp + string_io.cpp subobjects_io.cpp) diff --git a/src/doc/layer_io.cpp b/src/doc/layer_io.cpp index a21928550..9e6eeda97 100644 --- a/src/doc/layer_io.cpp +++ b/src/doc/layer_io.cpp @@ -1,5 +1,5 @@ // Aseprite Document Library -// Copyright (c) 2001-2014 David Capello +// Copyright (c) 2001-2015 David Capello // // This file is released under the terms of the MIT license. // Read LICENSE.txt for more information. @@ -20,6 +20,7 @@ #include "doc/layer.h" #include "doc/layer_io.h" #include "doc/sprite.h" +#include "doc/string_io.h" #include "doc/subobjects_io.h" #include @@ -37,10 +38,7 @@ void write_layer(std::ostream& os, Layer* layer) std::string name = layer->name(); write32(os, layer->id()); - - write16(os, name.size()); // Name length - if (!name.empty()) - os.write(name.c_str(), name.size()); // Name + write_string(os, layer->name()); write32(os, static_cast(layer->flags())); // Flags write16(os, static_cast(layer->type())); // Type @@ -103,15 +101,7 @@ void write_layer(std::ostream& os, Layer* layer) Layer* read_layer(std::istream& is, SubObjectsIO* subObjects) { ObjectId id = read32(is); - uint16_t name_length = read16(is); // Name length - std::vector name(name_length+1); - if (name_length > 0) { - is.read(&name[0], name_length); // Name - name[name_length] = 0; - } - else - name[0] = 0; - + std::string name = read_string(is); uint32_t flags = read32(is); // Flags uint16_t layer_type = read16(is); // Type @@ -171,7 +161,7 @@ Layer* read_layer(std::istream& is, SubObjectsIO* subObjects) } if (layer) { - layer->setName(&name[0]); + layer->setName(name); layer->setFlags(static_cast(flags)); layer->setId(id); } diff --git a/src/doc/string_io.cpp b/src/doc/string_io.cpp new file mode 100644 index 000000000..2b96c423b --- /dev/null +++ b/src/doc/string_io.cpp @@ -0,0 +1,43 @@ +// Aseprite Document Library +// Copyright (c) 2001-2015 David Capello +// +// This file is released under the terms of the MIT license. +// Read LICENSE.txt for more information. + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "doc/string_io.h" + +#include "base/serialization.h" + +#include + +namespace doc { + +using namespace base::serialization; +using namespace base::serialization::little_endian; + +void write_string(std::ostream& os, const std::string& str) +{ + write16(os, str.size()); + if (!str.empty()) + os.write(str.c_str(), str.size()); +} + +std::string read_string(std::istream& is) +{ + uint16_t length = read16(is); + std::vector str(length+1); + if (length > 0) { + is.read(&str[0], length); + str[length] = 0; + } + else + str[0] = 0; + + return std::string(&str[0]); +} + +} diff --git a/src/doc/string_io.h b/src/doc/string_io.h new file mode 100644 index 000000000..0e2822f42 --- /dev/null +++ b/src/doc/string_io.h @@ -0,0 +1,20 @@ +// Aseprite Document Library +// Copyright (c) 2001-2015 David Capello +// +// This file is released under the terms of the MIT license. +// Read LICENSE.txt for more information. + +#ifndef DOC_STRING_IO_H_INCLUDED +#define DOC_STRING_IO_H_INCLUDED +#pragma once + +#include + +namespace doc { + + void write_string(std::ostream& os, const std::string& str); + std::string read_string(std::istream& is); + +} // namespace doc + +#endif