1+ /*
2+ * This file is part of Sim2Editor-CPPCore
3+ * Copyright (C) 2020-2021 SuperSaiyajinStackZ, Universal-Team
4+ *
5+ * This program is free software: you can redistribute it and/or modify
6+ * it under the terms of the GNU General Public License as published by
7+ * the Free Software Foundation, either version 3 of the License, or
8+ * (at your option) any later version.
9+ *
10+ * This program is distributed in the hope that it will be useful,
11+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+ * GNU General Public License for more details.
14+ *
15+ * You should have received a copy of the GNU General Public License
16+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
17+ *
18+ * Additional Terms 7.b and 7.c of GPLv3 apply to this file:
19+ * * Requiring preservation of specified reasonable legal notices or
20+ * author attributions in that material or in the Appropriate Legal
21+ * Notices displayed by works containing it.
22+ * * Prohibiting misrepresentation of the origin of that material,
23+ * or requiring that modified versions of such material be marked in
24+ * reasonable ways as different from the original version.
25+ */
26+
27+ #include " DataHelper.hpp"
28+
29+ namespace S2Editor {
30+ /*
31+ Return a bit from a Buffer.
32+
33+ const uint8_t *Buffer: The Buffer.
34+ const uint32_t Offs: The Offset to read from.
35+ const uint8_t BitIndex: The Bit index ( 0 - 7 ).
36+ */
37+ const bool DataHelper::ReadBit (const uint8_t *Buffer, const uint32_t Offs, const uint8_t BitIndex) {
38+ if (!Buffer || BitIndex > 7 ) return false ;
39+
40+ return (Buffer[Offs] >> BitIndex & 1 ) != 0 ;
41+ };
42+ /*
43+ Set a bit to a Buffer.
44+
45+ uint8_t *Buffer: The Buffer.
46+ const uint32_t Offs: The Offset to write to.
47+ const uint8_t BitIndex: The Bit index ( 0 - 7 ).
48+ const bool IsSet: If it's set (1) or not (0).
49+
50+ Returns true if success, false if not.
51+ */
52+ bool DataHelper::WriteBit (uint8_t *Buffer, const uint32_t Offs, const uint8_t BitIndex, const bool IsSet) {
53+ if (!Buffer || BitIndex > 7 ) return false ;
54+
55+ Buffer[Offs] &= ~(1 << BitIndex);
56+ Buffer[Offs] |= (IsSet ? 1 : 0 ) << BitIndex;
57+ return true ;
58+ };
59+
60+ /*
61+ Read Lower / Upper Bits.
62+
63+ const uint8_t *Buffer: The Buffer.
64+ const uint32_t Offs: The offset where to read from.
65+ const bool First: If Reading from the first four bits, or second.
66+ */
67+ const uint8_t DataHelper::ReadBits (const uint8_t *Buffer, const uint32_t Offs, const bool First) {
68+ if (!Buffer) return 0x0 ;
69+
70+ if (First) return (Buffer[Offs] & 0xF ); // Bit 0 - 3.
71+ else return (Buffer[Offs] >> 4 ); // Bit 4 - 7.
72+ };
73+ /*
74+ Write Lower / Upper Bits.
75+
76+ uint8_t *Buffer: The Buffer.
77+ const uint32_t Offs: The offset where to write to.
78+ const bool First: If Writing on the first four bits, or second.
79+ const uint8_t Data: The Data to write.
80+
81+ Returns true if success, false if not.
82+ */
83+ bool DataHelper::WriteBits (uint8_t *Buffer, const uint32_t Offs, const bool First, const uint8_t Data) {
84+ if (Data > 0xF || !Buffer) return false ;
85+
86+ if (First) Buffer[Offs] = (Buffer[Offs] & 0xF0 ) | (Data & 0xF ); // Bit 0 - 3.
87+ else Buffer[Offs] = (Buffer[Offs] & 0x0F ) | (Data << 4 );// Bit 4 - 7.
88+ return true ;
89+ };
90+
91+ /*
92+ Read a string from a Buffer.
93+
94+ const uint8_t *Buffer: The SAVBuffer.
95+ const uint32_t Offs: The Offset from where to read from.
96+ const uint32_t Length: The Length to read.
97+ */
98+ const std::string DataHelper::ReadString (const uint8_t *Buffer, const uint32_t Offs, const uint32_t Length) {
99+ if (!Buffer) return " " ;
100+
101+ std::string Str;
102+ for (int Idx = 0 ; Idx < (int )Length; Idx++) {
103+ if (Buffer[Offs + Idx] == 0x0 ) break ; // 0x0 -> End.
104+
105+ Str += Buffer[Offs + Idx];
106+ }
107+
108+ return Str;
109+ };
110+ /*
111+ Write a string to a Buffer.
112+
113+ uint8_t *Buffer: The SAVBuffer.
114+ const uint32_t Offs: The offset from where to write to.
115+ const uint32_t Length: The length to write.
116+ const std::string &Str: The string to write.
117+
118+ Returns true if success, false if not.
119+ */
120+ bool DataHelper::WriteString (uint8_t *Buffer, const uint32_t Offs, const uint32_t Length, const std::string &Str) {
121+ if (!Buffer) return false ;
122+
123+ for (int Idx = 0 ; Idx < (int )Length; Idx++) {
124+ if (Idx < (int )Str.size ()) Buffer[Offs + Idx] = Str[Idx]; // The index is inside the string length, so write that.
125+ else Buffer[Offs + Idx] = 0 ; // Index outside the string length.. so write 0.
126+ }
127+
128+ return true ;
129+ };
130+ };
0 commit comments