Skip to content

Commit e1b3302

Browse files
committed
Unify table viewer text loading across lab and editor UIs
1 parent 240a912 commit e1b3302

8 files changed

Lines changed: 213 additions & 929 deletions

File tree

code/lab/dialogs/lab_ui_helpers.cpp

Lines changed: 5 additions & 482 deletions
Large diffs are not rendered by default.

code/source_groups.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1731,6 +1731,8 @@ add_file_folder("Utils"
17311731
utils/RandomRange.h
17321732
utils/string_utils.cpp
17331733
utils/string_utils.h
1734+
utils/table_viewer.cpp
1735+
utils/table_viewer.h
17341736
utils/strings.h
17351737
utils/threading.cpp
17361738
utils/threading.h

code/utils/table_viewer.cpp

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
#include "table_viewer.h"
2+
3+
#include "cfile/cfile.h"
4+
#include "parse/parselo.h"
5+
6+
namespace {
7+
8+
void append_header(SCP_string& output, const SCP_string& filename)
9+
{
10+
output += "-- ";
11+
output += filename;
12+
output += " -------------------------------\r\n";
13+
}
14+
15+
void trim_newline(char* line)
16+
{
17+
size_t len = strlen(line);
18+
while (len > 0 && line[len - 1] == '\n') {
19+
line[len - 1] = 0;
20+
--len;
21+
}
22+
}
23+
24+
bool strip_and_match_entry_prefix(const char* line, int& comment, const char* entry_prefix, SCP_string* extracted_name = nullptr)
25+
{
26+
char line_without_comments[256];
27+
int i;
28+
int j;
29+
30+
for (i = j = 0; line[i] && j < static_cast<int>(sizeof(line_without_comments)) - 1; ++i) {
31+
if (line[i] == '/' && line[i + 1] == '/') {
32+
break;
33+
}
34+
35+
if (line[i] == '/' && line[i + 1] == '*') {
36+
comment = 1;
37+
++i;
38+
continue;
39+
}
40+
41+
if (line[i] == '*' && line[i + 1] == '/') {
42+
comment = 0;
43+
++i;
44+
continue;
45+
}
46+
47+
if (!comment) {
48+
line_without_comments[j++] = line[i];
49+
}
50+
}
51+
52+
line_without_comments[j] = 0;
53+
const auto prefix_len = static_cast<int>(strlen(entry_prefix));
54+
if (strnicmp(line_without_comments, entry_prefix, prefix_len) != 0) {
55+
return false;
56+
}
57+
58+
drop_trailing_white_space(line_without_comments);
59+
i = prefix_len;
60+
while (line_without_comments[i] == ' ' || line_without_comments[i] == '\t' || line_without_comments[i] == '@') {
61+
++i;
62+
}
63+
64+
if (extracted_name != nullptr) {
65+
*extracted_name = line_without_comments + i;
66+
}
67+
68+
return true;
69+
}
70+
71+
void append_file_contents(SCP_string& output, CFILE* fp)
72+
{
73+
char line[256];
74+
while (cfgets(line, 255, fp)) {
75+
trim_newline(line);
76+
output += line;
77+
output += "\r\n";
78+
}
79+
}
80+
81+
void append_matching_entry(SCP_string& output, CFILE* fp, const SCP_string& file_name, const char* entry_name, const char* entry_prefix)
82+
{
83+
char line[256];
84+
int found = 0;
85+
int comment = 0;
86+
87+
while (cfgets(line, 255, fp)) {
88+
trim_newline(line);
89+
90+
SCP_string found_name;
91+
if (strip_and_match_entry_prefix(line, comment, entry_prefix, &found_name)) {
92+
found = 0;
93+
if (stricmp(found_name.c_str(), entry_name) == 0) {
94+
append_header(output, file_name);
95+
found = 1;
96+
}
97+
}
98+
99+
if (found) {
100+
output += line;
101+
output += "\r\n";
102+
}
103+
}
104+
}
105+
106+
} // namespace
107+
108+
namespace table_viewer {
109+
110+
SCP_string get_table_entry_text(const char* table_filename,
111+
const char* modular_pattern,
112+
const char* entry_name,
113+
const char* missing_table_message,
114+
const char* entry_prefix)
115+
{
116+
SCP_string output;
117+
118+
auto fp = cfopen(table_filename, "r");
119+
if (!fp) {
120+
return missing_table_message != nullptr ? SCP_string(missing_table_message) : SCP_string();
121+
}
122+
123+
append_matching_entry(output, fp, table_filename, entry_name, entry_prefix);
124+
cfclose(fp);
125+
126+
SCP_vector<SCP_string> table_files;
127+
const auto num_files = cf_get_file_list(table_files, CF_TYPE_TABLES, NOX(modular_pattern), CF_SORT_REVERSE);
128+
129+
for (int n = 0; n < num_files; ++n) {
130+
table_files[n] += ".tbm";
131+
fp = cfopen(table_files[n].c_str(), "r");
132+
if (!fp) {
133+
continue;
134+
}
135+
136+
append_matching_entry(output, fp, table_files[n], entry_name, entry_prefix);
137+
cfclose(fp);
138+
}
139+
140+
return output;
141+
}
142+
143+
SCP_string get_complete_table_text(const char* table_filename, const char* modular_pattern, const char* missing_table_message)
144+
{
145+
SCP_string output;
146+
auto fp = cfopen(table_filename, "r");
147+
if (!fp) {
148+
return missing_table_message != nullptr ? SCP_string(missing_table_message) : SCP_string();
149+
}
150+
151+
append_header(output, table_filename);
152+
append_file_contents(output, fp);
153+
cfclose(fp);
154+
155+
SCP_vector<SCP_string> table_files;
156+
const auto num_files = cf_get_file_list(table_files, CF_TYPE_TABLES, NOX(modular_pattern), CF_SORT_REVERSE);
157+
158+
for (int n = 0; n < num_files; ++n) {
159+
table_files[n] += ".tbm";
160+
fp = cfopen(table_files[n].c_str(), "r");
161+
if (!fp) {
162+
continue;
163+
}
164+
165+
append_header(output, table_files[n]);
166+
append_file_contents(output, fp);
167+
cfclose(fp);
168+
}
169+
170+
return output;
171+
}
172+
173+
} // namespace table_viewer

code/utils/table_viewer.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#pragma once
2+
3+
#include "globalincs/pstypes.h"
4+
5+
namespace table_viewer {
6+
7+
SCP_string get_table_entry_text(const char* table_filename,
8+
const char* modular_pattern,
9+
const char* entry_name,
10+
const char* missing_table_message = nullptr,
11+
const char* entry_prefix = "$Name:");
12+
13+
SCP_string get_complete_table_text(const char* table_filename,
14+
const char* modular_pattern,
15+
const char* missing_table_message = nullptr);
16+
17+
} // namespace table_viewer

fred2/textviewdlg.cpp

Lines changed: 3 additions & 166 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#include "stdafx.h"
1414
#include "FRED.h"
1515
#include "TextViewDlg.h"
16-
#include "cfile/cfile.h"
16+
#include "utils/table_viewer.h"
1717

1818
#ifdef _DEBUG
1919
#undef THIS_FILE
@@ -74,181 +74,18 @@ void TextViewDlg::OnClose()
7474

7575
void TextViewDlg::LoadShipsTblText(const ship_info *sip)
7676
{
77-
char line[256], line2[256], file_text[82];
78-
int i, j, n, found = 0, comment = 0, num_files = 0;
79-
SCP_vector<SCP_string> tbl_file_names;
80-
CFILE *fp;
81-
8277
SetCaption("Ship Table Data");
8378

8479
if (!sip)
8580
return;
8681

87-
fp = cfopen("ships.tbl", "r");
88-
Assert(fp);
89-
90-
91-
while (cfgets(line, 255, fp)) {
92-
while (line[strlen(line) - 1] == '\n')
93-
line[strlen(line) - 1] = 0;
94-
95-
for (i=j=0; line[i]; i++) {
96-
if (line[i] == '/' && line[i+1] == '/')
97-
break;
98-
99-
if (line[i] == '/' && line[i+1] == '*') {
100-
comment = 1;
101-
i++;
102-
continue;
103-
}
104-
105-
if (line[i] == '*' && line[i+1] == '/') {
106-
comment = 0;
107-
i++;
108-
continue;
109-
}
110-
111-
if (!comment)
112-
line2[j++] = line[i];
113-
}
114-
115-
line2[j] = 0;
116-
if (!strnicmp(line2, "$Name:", 6)) {
117-
drop_trailing_white_space(line2);
118-
found = 0;
119-
i = 6;
120-
121-
while (line2[i] == ' ' || line2[i] == '\t' || line2[i] == '@')
122-
i++;
123-
124-
if (!stricmp(line2 + i, sip->name)) {
125-
m_edit += "-- ships.tbl -------------------------------\r\n";
126-
found = 1;
127-
}
128-
}
129-
130-
if (found) {
131-
m_edit += line;
132-
m_edit += "\r\n";
133-
}
134-
}
135-
136-
cfclose(fp);
137-
138-
139-
// done with ships.tbl, so now check all modular ship tables...
140-
num_files = cf_get_file_list(tbl_file_names, CF_TYPE_TABLES, NOX("*-shp.tbm"), CF_SORT_REVERSE);
141-
142-
for (n = 0; n < num_files; n++){
143-
tbl_file_names[n] += ".tbm";
144-
145-
fp = cfopen(tbl_file_names[n].c_str(), "r");
146-
Assert(fp);
147-
148-
memset( line, 0, sizeof(line) );
149-
memset( line2, 0, sizeof(line2) );
150-
found = 0;
151-
comment = 0;
152-
153-
while (cfgets(line, 255, fp)) {
154-
while (line[strlen(line) - 1] == '\n')
155-
line[strlen(line) - 1] = 0;
156-
157-
for (i=j=0; line[i]; i++) {
158-
if (line[i] == '/' && line[i+1] == '/')
159-
break;
160-
161-
if (line[i] == '/' && line[i+1] == '*') {
162-
comment = 1;
163-
i++;
164-
continue;
165-
}
166-
167-
if (line[i] == '*' && line[i+1] == '/') {
168-
comment = 0;
169-
i++;
170-
continue;
171-
}
172-
173-
if (!comment)
174-
line2[j++] = line[i];
175-
}
176-
177-
line2[j] = 0;
178-
if (!strnicmp(line2, "$Name:", 6)) {
179-
drop_trailing_white_space(line2);
180-
found = 0;
181-
i = 6;
182-
183-
while (line2[i] == ' ' || line2[i] == '\t' || line2[i] == '@')
184-
i++;
185-
186-
if (!stricmp(line2 + i, sip->name)) {
187-
memset( file_text, 0, sizeof(file_text) );
188-
snprintf(file_text, sizeof(file_text)-1, "-- %s -------------------------------\r\n", tbl_file_names[n].c_str());
189-
m_edit += file_text;
190-
found = 1;
191-
}
192-
}
193-
194-
if (found) {
195-
m_edit += line;
196-
m_edit += "\r\n";
197-
}
198-
}
199-
200-
cfclose(fp);
201-
}
82+
m_edit += table_viewer::get_table_entry_text("ships.tbl", "*-shp.tbm", sip->name).c_str();
20283
}
20384

20485
void TextViewDlg::LoadMusicTblText()
20586
{
206-
char line[256];
207-
CFILE* fp;
208-
20987
SetCaption("Music Table Data");
210-
211-
fp = cfopen("music.tbl", "r");
212-
Assert(fp);
213-
214-
// print the header
215-
m_edit += "-- music.tbl -------------------------------\r\n";
216-
217-
// now get the file content
218-
while (cfgets(line, 255, fp)) {
219-
m_edit += line;
220-
m_edit += "\r\n";
221-
}
222-
223-
cfclose(fp);
224-
225-
SCP_vector<SCP_string> tbl_file_names;
226-
227-
// done with music.tbl, so now check all modular music tables...
228-
int num_files = cf_get_file_list(tbl_file_names, CF_TYPE_TABLES, NOX("*-mus.tbm"), CF_SORT_REVERSE);
229-
230-
for (int n = 0; n < num_files; n++) {
231-
tbl_file_names[n] += ".tbm";
232-
233-
fp = cfopen(tbl_file_names[n].c_str(), "r");
234-
Assert(fp);
235-
236-
memset(line, 0, sizeof(line));
237-
238-
// get the name of the current file and print it
239-
char file_text[82];
240-
memset(file_text, 0, sizeof(file_text));
241-
snprintf(file_text, sizeof(file_text) - 1, "-- %s -------------------------------\r\n", tbl_file_names[n].c_str());
242-
m_edit += file_text;
243-
244-
// now get the file content
245-
while (cfgets(line, 255, fp)) {
246-
m_edit += line;
247-
m_edit += "\r\n";
248-
}
249-
250-
cfclose(fp);
251-
}
88+
m_edit += table_viewer::get_complete_table_text("music.tbl", "*-mus.tbm").c_str();
25289
}
25390

25491
void TextViewDlg::OnSetfocusEdit1()

0 commit comments

Comments
 (0)