File: msgfile.h

package info (click to toggle)
exult 1.12.1-1
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid
  • size: 43,856 kB
  • sloc: cpp: 170,016; xml: 7,400; yacc: 2,850; makefile: 2,419; java: 1,901; ansic: 1,654; lex: 673; sh: 550; objc: 416
file content (105 lines) | stat: -rw-r--r-- 2,772 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/**
 ** Msgfile.h - Read in text message file.
 **
 ** Written: 6/25/03
 **/

/*
 *  Copyright (C) 2002-2022  The Exult Team
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#ifndef INCL_MSGFILE_H
#define INCL_MSGFILE_H

#include "common_types.h"

#include <iosfwd>
#include <map>
#include <optional>
#include <string>
#include <vector>

class IDataSource;

class Text_msg_file_reader {
	using Section_data  = std::vector<std::string_view>;
	using Text_msg_data = std::map<std::string_view, Section_data, std::less<>>;
	using Text_msg_first = std::map<std::string_view, uint32, std::less<>>;
	std::string    contents;
	Section_data   global_section;
	uint32         global_first;
	Text_msg_data  items;
	Text_msg_first firsts;

	bool parse_contents();

public:
	Text_msg_file_reader();
	Text_msg_file_reader(IDataSource& in);

	[[nodiscard]] const Section_data& get_global_section() const {
		return global_section;
	}

	uint32 get_global_section_strings(std::vector<std::string>& strings) const {
		strings.clear();
		for (const auto& s : global_section) {
			strings.emplace_back(s);
		}
		return global_first;
	}

	[[nodiscard]] const Section_data* get_section(
			std::string_view sectionName, int& firstMsg) const {
		auto section = items.find(sectionName);
		if (section != items.end()) {
			firstMsg = firsts.at(sectionName);
			return std::addressof(section->second);
		}
		firstMsg = -1;
		return nullptr;
	}

	uint32 get_section_strings(
			std::string_view          sectionName,
			std::vector<std::string>& strings) const {
		strings.clear();
		if (has_section(sectionName)) {
			for (const auto& s : items.at(sectionName)) {
				strings.emplace_back(s);
			}
			return firsts.at(sectionName);
		}
		return 0;
	}

	const Text_msg_data& get_sections() const {
		return items;
	}

	[[nodiscard]] bool has_section(std::string_view sectionName) const {
		return items.find(sectionName) != items.end();
	}

	[[nodiscard]] std::optional<int> get_version() const;
};

void Write_msg_file_section(
		std::ostream& out, const char* section,
		std::vector<std::string>& items);

#endif