Debian Codegraph @

CorsixTH/Src/th.h

raw
1 /*
2 Copyright (c) 2009 Peter "Corsix" Cawley
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in
6 the Software without restriction, including without limitation the rights to
7 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8 of the Software, and to permit persons to whom the Software is furnished to do
9 so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 SOFTWARE.
21 */
22
23 #ifndef CORSIX_TH_TH_H_
24 #define CORSIX_TH_TH_H_
25 #include "config.h"
26
27 #include <vector>
28
29 //! Generic linked list class (for inheriting from)
30 class link_list {
31 public:
32 link_list();
33 virtual ~link_list();
34
35 link_list* prev{nullptr};
36 link_list* next{nullptr};
37
38 void remove_from_list();
39 };
40
41 //! \brief Theme Hospital localised string list
42 //!
43 //! Presents Theme Hospital strings by section and index.
44 class th_string_list {
45 public:
46 //! Construct an instance of string_list from the given data
47 //! from a Theme Hosptial string file. The format of the data is
48 //! described at:
49 //! https://github.com/alexandergitter/theme-hospital-spec/blob/master/format-specification.md#strings
50 //!
51 //! \param data A pointer to the raw data
52 //! \param length The size of the data
53 th_string_list(const uint8_t* data, size_t length);
54
55 // Delete default constructors and assignment operators. They
56 // can be implemented properly later if they are needed but
57 // for now they are unneeded so it is safer to remove them.
58 th_string_list() = delete;
59 th_string_list(const th_string_list&) = delete;
60 th_string_list(th_string_list&&) = delete;
61 th_string_list& operator=(const th_string_list&) = delete;
62 th_string_list&& operator=(th_string_list&&) = delete;
63 ~th_string_list() = default;
64
65 //! Get the number of sections in the string list
66 size_t get_section_count() const;
67
68 //! Get the number of strings in a section of the string list
69 size_t get_section_size(size_t section) const;
70
71 //! Get a string from the string list
72 /*!
73 @param section Section index in range [0, getSectionCount() - 1]
74 @param index String index in range [0, getSectionSize(iSection) - 1]
75 @return nullptr if the index is invalid, otherwise a UTF-8 encoded
76 string.
77 */
78 const char* get_string(size_t section, size_t index) const;
79
80 private:
81 //! Section information
82 std::vector<std::vector<const char*>> sections;
83
84 //! Memory block containing all the actual strings utf-8 encoded
85 std::vector<uint8_t> string_buffer;
86 };
87
88 /**
89 * Convert 4 bytes representing uint32 in little endian representation into a
90 * uint32.
91 *
92 * @param bytes A pointer to the first of 4 sequential bytes in memory making
93 * up the uint32.
94 */
95 inline uint32_t bytes_to_uint32_le(const uint8_t* bytes) {
96 uint32_t res = bytes[3];
97 res <<= 8;
98 res |= bytes[2];
99 res <<= 8;
100 res |= bytes[1];
101 res <<= 8;
102 res |= bytes[0];
103
104 return res;
105 }
106
107 /**
108 * Convert 2 bytes representing uint16 in little endian representation into a
109 * uint16.
110 *
111 * @param bytes A pointer to the first of 2 sequential bytes in memory making
112 * up the uint16.
113 */
114 inline uint16_t bytes_to_uint16_le(const uint8_t* bytes) {
115 uint16_t res = bytes[1];
116 res <<= 8;
117 res |= bytes[0];
118
119 return res;
120 }
121
122 #endif // CORSIX_TH_TH_H_