Skip to content

Commit b8b46af

Browse files
committed
Some text msg file cleanup
Also fixing some memory leaks on it.
1 parent 4931982 commit b8b46af

File tree

10 files changed

+191
-214
lines changed

10 files changed

+191
-214
lines changed

files/U7obj.cc

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ using std::unique_ptr;
3838
*/
3939
size_t U7object::number_of_objects() {
4040
U7file* uf = U7FileManager::get_ptr()->get_file_object(identifier, true);
41-
return uf ? uf->number_of_objects() : 0UL;
41+
return (uf != nullptr) ? uf->number_of_objects() : 0UL;
4242
}
4343

4444
/**
@@ -51,7 +51,7 @@ size_t U7object::number_of_objects() {
5151
unique_ptr<unsigned char[]> U7object::retrieve(size_t& len) const {
5252
U7file* uf = U7FileManager::get_ptr()->get_file_object(identifier, true);
5353
len = 0;
54-
return uf ? uf->retrieve(objnumber, len) : nullptr;
54+
return (uf != nullptr) ? uf->retrieve(objnumber, len) : nullptr;
5555
}
5656

5757
/**
@@ -63,14 +63,14 @@ unique_ptr<unsigned char[]> U7object::retrieve(size_t& len) const {
6363
* @param objects Vector containing U7objects we will test.
6464
*/
6565
void U7multiobject::set_object(const std::vector<U7object>& objects) {
66-
for (const auto& object : objects) {
66+
for (const auto& obj : objects) {
6767
size_t len;
6868
auto buf = object.retrieve(len);
6969
// Only len > 0 means a valid object.
7070
if (buf && len > 0) {
71-
buffer = std::move(buf); // Gets deleted with class.
72-
length = len;
73-
identifier = object.get_identifier();
71+
buffer = std::move(buf); // Gets deleted with class.
72+
length = len;
73+
object.set_identifier(obj.get_identifier());
7474
break;
7575
}
7676
}
@@ -82,9 +82,9 @@ void U7multiobject::set_object(const std::vector<U7object>& objects) {
8282
* @param objnum Object number we are looking for.
8383
*/
8484
U7multiobject::U7multiobject(const File_spec& file0, int objnum)
85-
: U7object(file0, objnum), buffer(nullptr), length(0) {
85+
: buffer(nullptr), length(0), object(file0, objnum) {
8686
size_t len;
87-
auto buf = U7object::retrieve(len);
87+
auto buf = object.retrieve(len);
8888
// Only len > 0 means a valid object.
8989
if (buf && len > 0) {
9090
buffer = std::move(buf); // Gets deleted with class.
@@ -100,10 +100,9 @@ U7multiobject::U7multiobject(const File_spec& file0, int objnum)
100100
*/
101101
U7multiobject::U7multiobject(
102102
const File_spec& file0, const File_spec& file1, int objnum)
103-
: U7object(file0, objnum), buffer(nullptr), length(0) {
104-
std::vector<U7object> objects;
105-
objects.emplace_back(file1, objnum);
106-
objects.emplace_back(file0, objnum);
103+
: buffer(nullptr), length(0), object(file0, objnum) {
104+
std::vector<U7object> objects{
105+
U7object(file1, objnum), U7object(file0, objnum)};
107106
set_object(objects);
108107
}
109108

@@ -117,11 +116,10 @@ U7multiobject::U7multiobject(
117116
U7multiobject::U7multiobject(
118117
const File_spec& file0, const File_spec& file1, const File_spec& file2,
119118
int objnum)
120-
: U7object(file0, objnum), buffer(nullptr), length(0) {
121-
std::vector<U7object> objects;
122-
objects.emplace_back(file2, objnum);
123-
objects.emplace_back(file1, objnum);
124-
objects.emplace_back(file0, objnum);
119+
: buffer(nullptr), length(0), object(file0, objnum) {
120+
std::vector<U7object> objects{
121+
U7object(file2, objnum), U7object(file1, objnum),
122+
U7object(file0, objnum)};
125123
set_object(objects);
126124
}
127125

@@ -136,12 +134,10 @@ U7multiobject::U7multiobject(
136134
U7multiobject::U7multiobject(
137135
const File_spec& file0, const File_spec& file1, const File_spec& file2,
138136
const File_spec& file3, int objnum)
139-
: U7object(file0, objnum), buffer(nullptr), length(0) {
140-
std::vector<U7object> objects;
141-
objects.emplace_back(file3, objnum);
142-
objects.emplace_back(file2, objnum);
143-
objects.emplace_back(file1, objnum);
144-
objects.emplace_back(file0, objnum);
137+
: buffer(nullptr), length(0), object(file0, objnum) {
138+
std::vector<U7object> objects{
139+
U7object(file3, objnum), U7object(file2, objnum),
140+
U7object(file1, objnum), U7object(file0, objnum)};
145141
set_object(objects);
146142
}
147143

@@ -151,9 +147,9 @@ U7multiobject::U7multiobject(
151147
* @param objnum Object number we are looking for.
152148
*/
153149
U7multiobject::U7multiobject(const std::vector<File_spec>& files, int objnum)
154-
: U7object("", objnum), buffer(nullptr), length(0) {
150+
: buffer(nullptr), length(0), object("", objnum) {
155151
if (!files.empty()) {
156-
identifier = files[0];
152+
object.set_identifier(files[0]);
157153
std::vector<U7object> objects;
158154
objects.reserve(files.size());
159155
for (const auto& file : files) {
@@ -163,6 +159,18 @@ U7multiobject::U7multiobject(const std::vector<File_spec>& files, int objnum)
163159
}
164160
}
165161

162+
U7multiobject::U7multiobject(const U7multiobject& other)
163+
: buffer(nullptr), length(0), object(other.object) {
164+
buffer = make_unique<unsigned char[]>(other.length);
165+
std::copy_n(other.buffer.get(), other.length, buffer.get());
166+
}
167+
168+
U7multiobject& U7multiobject::operator=(const U7multiobject& rhs) {
169+
U7multiobject tmp(rhs);
170+
std::swap(*this, tmp);
171+
return *this;
172+
}
173+
166174
/**
167175
* Uses U7FileManager to get an U7file for the desired file.
168176
* @param len Receives the size of desired object, if it exists
@@ -175,9 +183,8 @@ unique_ptr<unsigned char[]> U7multiobject::retrieve(size_t& len) const {
175183
if (length == 0) {
176184
// This means we didn't find the object on construction.
177185
return nullptr;
178-
} else {
179-
auto buf = make_unique<unsigned char[]>(len);
180-
std::copy_n(buffer.get(), len, buf.get());
181-
return buf;
182186
}
187+
auto buf = make_unique<unsigned char[]>(len);
188+
std::copy_n(buffer.get(), len, buf.get());
189+
return buf;
183190
}

files/U7obj.h

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
#ifndef U7OBJ_H
2424
#define U7OBJ_H
2525

26-
#include "common_types.h"
27-
2826
#include <memory>
2927
#include <string>
3028
#include <utility>
@@ -59,7 +57,7 @@ struct File_spec {
5957
* This class loads a file of undefined type and retrieves
6058
* a given object contained in said file.
6159
*/
62-
class U7object {
60+
class U7object final {
6361
protected:
6462
/// Name of desired file.
6563
File_spec identifier;
@@ -70,25 +68,28 @@ class U7object {
7068
/// Setups to load an object from a buffer or file.
7169
/// @param spec Specification of the data source.
7270
/// @param objnum Index of object in the data object.
73-
/// @param p Optional, defaults to false. If true, indicates
7471
/// the object comes from the patch dir.
7572
U7object(File_spec spec, int objnum)
7673
: identifier(std::move(spec)), objnumber(objnum) {}
7774

78-
virtual ~U7object() noexcept = default;
79-
U7object(const U7object&) = delete;
80-
U7object& operator=(const U7object&) = delete;
75+
~U7object() noexcept = default;
76+
U7object(const U7object&) = default;
77+
U7object& operator=(const U7object&) = default;
8178
U7object(U7object&&) = default;
8279
U7object& operator=(U7object&&) = default;
8380

8481
File_spec get_identifier() const {
8582
return identifier;
8683
}
8784

85+
void set_identifier(File_spec spec) {
86+
identifier = std::move(spec);
87+
}
88+
8889
// TODO: This may need to be overriden by U7multiobject, in case the
8990
// patching files have more objects than the base.
90-
size_t number_of_objects();
91-
virtual std::unique_ptr<unsigned char[]> retrieve(std::size_t& len) const;
91+
size_t number_of_objects();
92+
std::unique_ptr<unsigned char[]> retrieve(std::size_t& len) const;
9293
};
9394

9495
/**
@@ -102,10 +103,11 @@ class U7object {
102103
* An 'object' it loads may be an object in a file or an object
103104
* in a buffer.
104105
*/
105-
class U7multiobject final : public U7object {
106+
class U7multiobject final {
106107
private:
107108
std::unique_ptr<unsigned char[]> buffer;
108109
size_t length;
110+
U7object object;
109111
void set_object(const std::vector<U7object>& objects);
110112

111113
public:
@@ -118,13 +120,13 @@ class U7multiobject final : public U7object {
118120
const File_spec& file0, const File_spec& file1,
119121
const File_spec& file2, const File_spec& file3, int objnum);
120122
U7multiobject(const std::vector<File_spec>& files, int objnum);
121-
~U7multiobject() noexcept final = default;
122-
U7multiobject(const U7multiobject&) = delete;
123-
U7multiobject& operator=(const U7multiobject&) = delete;
124-
U7multiobject(U7multiobject&&) = default;
125-
U7multiobject& operator=(U7multiobject&&) = default;
123+
~U7multiobject() noexcept = default;
124+
U7multiobject(const U7multiobject&);
125+
U7multiobject& operator=(const U7multiobject&);
126+
U7multiobject(U7multiobject&&) = default;
127+
U7multiobject& operator=(U7multiobject&&) = default;
126128

127-
std::unique_ptr<unsigned char[]> retrieve(std::size_t& len) const final;
129+
std::unique_ptr<unsigned char[]> retrieve(std::size_t& len) const;
128130
};
129131

130132
#endif

files/msgfile.cc

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
2323
*/
2424

25+
#include "msgfile.h"
26+
2527
#include "databuf.h"
2628
#include "ios_state.hpp"
2729

@@ -164,17 +166,6 @@ bool Search_text_msg_section(IDataSource* in, const char* section) {
164166
return false;
165167
}
166168

167-
int Read_text_msg_file(
168-
istream& in,
169-
vector<string>& strings, // Strings returned here, each
170-
// allocated on heap.
171-
const char* section // Section name, or nullptr. If given
172-
// the section must be next infile.
173-
) {
174-
IStreamDataSource ds(&in);
175-
return Read_text_msg_file(&ds, strings, section);
176-
}
177-
178169
int Read_text_msg_file_sections(
179170
IDataSource* in,
180171
vector<vector<string>>& strings, // Strings returned here
@@ -203,15 +194,6 @@ int Read_text_msg_file_sections(
203194
return version;
204195
}
205196

206-
int Read_text_msg_file_sections(
207-
istream& in,
208-
vector<vector<string>>& strings, // Strings returned here
209-
const char* sections[], // Section names
210-
int numsections) {
211-
IStreamDataSource ds(&in);
212-
return Read_text_msg_file_sections(&ds, strings, sections, numsections);
213-
}
214-
215197
/*
216198
* Write one section.
217199
*/

files/msgfile.h

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,24 +36,13 @@ int Read_text_msg_file(
3636
std::vector<std::string>& strings, // Strings returned here, each
3737
// allocated on heap.
3838
const char* section = nullptr);
39-
int Read_text_msg_file(
40-
std::istream& in,
41-
std::vector<std::string>& strings, // Strings returned here, each
42-
// allocated on heap.
43-
const char* section = nullptr);
4439
bool Search_text_msg_section(IDataSource* in, const char* section = nullptr);
4540
int Read_text_msg_file_sections(
4641
IDataSource* in,
4742
std::vector<std::vector<std::string>>&
4843
strings, // Strings returned here
4944
const char* sections[], // Section names
5045
int numsections);
51-
int Read_text_msg_file_sections(
52-
std::istream& in,
53-
std::vector<std::vector<std::string>>&
54-
strings, // Strings returned here
55-
const char* sections[], // Section names
56-
int numsections);
5746
void Write_msg_file_section(
5847
std::ostream& out, const char* section,
5948
std::vector<std::string>& items);

gumps/Notebook_gump.cc

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -840,19 +840,17 @@ void Notebook_gump::read() {
840840
void Notebook_gump::read_auto_text_file(const char* filename) {
841841
if (gwin->get_allow_autonotes()) {
842842
initialized_auto_text = true;
843-
std::unique_ptr<std::istream> pNotesfile;
844-
if (is_system_path_defined("<PATCH>") && U7exists(PATCH_AUTONOTES)) {
845-
cout << "Loading patch autonotes" << endl;
846-
pNotesfile = U7open_in(PATCH_AUTONOTES, true);
847-
} else {
843+
IFileDataSource notesfile = [&]() -> IFileDataSource {
844+
if (is_system_path_defined("<PATCH>") && U7exists(PATCH_AUTONOTES)) {
845+
cout << "Loading patch autonotes" << endl;
846+
return IFileDataSource(PATCH_AUTONOTES, true);
847+
}
848848
cout << "Loading autonotes from file " << filename << endl;
849-
pNotesfile = U7open_in(filename, true);
850-
}
851-
if (!pNotesfile) {
852-
return;
849+
return IFileDataSource(filename, true);
850+
}();
851+
if (notesfile.good()) {
852+
Read_text_msg_file(&notesfile, auto_text);
853853
}
854-
auto& notesfile = *pNotesfile;
855-
Read_text_msg_file(notesfile, auto_text);
856854
}
857855
}
858856

@@ -862,12 +860,10 @@ void Notebook_gump::read_auto_text() {
862860
initialized_auto_text = true;
863861
if (is_system_path_defined("<PATCH>") && U7exists(PATCH_AUTONOTES)) {
864862
cout << "Loading patch autonotes" << endl;
865-
auto pNotesfile = U7open_in(PATCH_AUTONOTES, true);
866-
if (!pNotesfile) {
867-
return;
863+
IFileDataSource notesfile(PATCH_AUTONOTES, true);
864+
if (notesfile.good()) {
865+
Read_text_msg_file(&notesfile, auto_text);
868866
}
869-
auto& notesfile = *pNotesfile;
870-
Read_text_msg_file(notesfile, auto_text);
871867
} else {
872868
const str_int_pair& resource
873869
= game->get_resource("config/autonotes");

0 commit comments

Comments
 (0)