Add doc::read/write_string()

This commit is contained in:
David Capello 2015-02-17 13:16:33 -03:00
parent 9106f3ada6
commit 51a03f27ab
4 changed files with 69 additions and 15 deletions

View File

@ -42,4 +42,5 @@ add_library(doc-lib
rgbmap.cpp
sprite.cpp
sprites.cpp
string_io.cpp
subobjects_io.cpp)

View File

@ -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 <iostream>
@ -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<int>(layer->flags())); // Flags
write16(os, static_cast<int>(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<char> 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<LayerFlags>(flags));
layer->setId(id);
}

43
src/doc/string_io.cpp Normal file
View File

@ -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 <vector>
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<char> str(length+1);
if (length > 0) {
is.read(&str[0], length);
str[length] = 0;
}
else
str[0] = 0;
return std::string(&str[0]);
}
}

20
src/doc/string_io.h Normal file
View File

@ -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 <string>
namespace doc {
void write_string(std::ostream& os, const std::string& str);
std::string read_string(std::istream& is);
} // namespace doc
#endif