1 /*
2 nameidmapping.cpp
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4 */
5 
6 /*
7 This file is part of Freeminer.
8 
9 Freeminer is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13 
14 Freeminer  is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with Freeminer.  If not, see <http://www.gnu.org/licenses/>.
21 */
22 
23 #include "nameidmapping.h"
24 #include "exceptions.h"
25 #include "util/serialize.h"
26 
serialize(std::ostream & os) const27 void NameIdMapping::serialize(std::ostream &os) const
28 {
29 	writeU8(os, 0); // version
30 	writeU16(os, m_id_to_name.size());
31 	for(std::map<u16, std::string>::const_iterator
32 			i = m_id_to_name.begin();
33 			i != m_id_to_name.end(); i++){
34 		writeU16(os, i->first);
35 		os<<serializeString(i->second);
36 	}
37 }
38 
deSerialize(std::istream & is)39 void NameIdMapping::deSerialize(std::istream &is)
40 {
41 	int version = readU8(is);
42 	if(version != 0)
43 		throw SerializationError("unsupported NameIdMapping version");
44 	u32 count = readU16(is);
45 	m_id_to_name.clear();
46 	m_name_to_id.clear();
47 	for(u32 i=0; i<count; i++){
48 		u16 id = readU16(is);
49 		std::string name = deSerializeString(is);
50 		m_id_to_name[id] = name;
51 		m_name_to_id[name] = id;
52 	}
53 }
54 
55