Start implementing the tyler map loader.

Untented and incomplete.
This commit is contained in:
King_DuckZ 2015-05-29 00:15:06 +02:00
parent 72b400ac0c
commit f1a6cac62b
4 changed files with 179 additions and 0 deletions

View file

@ -7,4 +7,5 @@ include_directories(
add_library(${PROJECT_NAME}
asciimapsource.cpp
tylermapsource.cpp
)

89
src/tylermapsource.cpp Normal file
View file

@ -0,0 +1,89 @@
#include "doorkeeper/helpers/tylermapsource.hpp"
#if defined(__GNUC__)
# include <endian.h>
#else
# error "Unsupported compiler"
#endif
#include <algorithm>
#include <iterator>
//Vorbis comment specification: https://www.xiph.org/vorbis/doc/v-comment.html
//The comment header logically is a list of eight-bit-clean vectors; the number
//of vectors is bounded to 2^32-1 and the length of each vector is limited to
//2^32-1 bytes. The vector length is encoded; the vector contents themselves
//are not null terminated. In addition to the vector list, there is a single
//vector for vendor name (also 8 bit clean, length encoded in 32 bits). For
//example, the 1.0 release of libvorbis set the vendor string to "Xiph.Org
//libVorbis I 20020717".
//
//The comment header is decoded as follows:
//
// 1) [vendor_length] = read an unsigned integer of 32 bits
// 2) [vendor_string] = read a UTF-8 vector as [vendor_length] octets
// 3) [user_comment_list_length] = read an unsigned integer of 32 bits
// 4) iterate [user_comment_list_length] times {
// 5) [length] = read an unsigned integer of 32 bits
// 6) this iteration's user comment = read a UTF-8 vector as [length] octets
// }
namespace dkh {
namespace {
const uint32_t g_signature = 0x52504B44; //DKPR
uint32_t read_uint32 (std::istream& parStream, uint32_t parMax=0xFFFFFFFF) {
uint32_t retval;
#if __BYTE_ORDER == __LITTLE_ENDIAN
parStream.read(reinterpret_cast<char*>(&retval), sizeof(uint32_t));
#else
# error "Unknown endianness, please write the implementation"
#endif
if (retval > parMax)
throw InvalidMapFileException();
return retval;
}
std::string& read_string (std::istream& parStream, std::string& parString, uint32_t parLength) {
std::istream_iterator<char> itstream(parStream);
parString.resize(parLength);
std::copy_n(itstream, parLength, std::insert_iterator<std::string>(parString, parString.begin()));
return parString;
}
} //unnamed namespace
namespace implem {
void read_header (std::istream& parStream, TylerMapHeader& parHeader) {
#if __BYTE_ORDER == __LITTLE_ENDIAN
parStream.read(reinterpret_cast<char*>(&parHeader), sizeof(TylerMapHeader));
#else
# error "Unknown endianness, please write the implementation"
#endif
if (g_signature != parHeader.signature)
throw InvalidMapFileException();
}
void read_comments (std::istream& parStream, std::string& parVendor, CommentMap& parComments) {
//Technically there is no limit on strings other than the uint32
//max value, but wth
const uint32_t max_string_length = 1024 * 2;
const uint32_t max_entries = 200;
{
const uint32_t vendor_length = read_uint32(parStream, max_string_length);
read_string(parStream, parVendor, vendor_length);
}
const uint32_t list_length = read_uint32(parStream, max_entries);
std::string full_string;
for (uint32_t z = 0; z < list_length; ++z) {
const uint32_t string_length = read_uint32(parStream, max_string_length);
read_string(parStream, full_string, string_length);
auto equal_pos = full_string.find('=');
if (std::string::npos == equal_pos)
throw InvalidMapFileException();
parComments[full_string.substr(0, equal_pos)] = full_string.substr(equal_pos + 1);
}
}
} //namespace implem
} //namespace dkh