Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
487 changes: 5 additions & 482 deletions code/lab/dialogs/lab_ui_helpers.cpp

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions code/source_groups.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -1728,6 +1728,8 @@ add_file_folder("Utils"
utils/string_utils.cpp
utils/string_utils.h
utils/strings.h
utils/table_viewer.cpp
utils/table_viewer.h
utils/threading.cpp
utils/threading.h
utils/tuples.h
Expand Down
173 changes: 173 additions & 0 deletions code/utils/table_viewer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
#include "table_viewer.h"

#include "cfile/cfile.h"
#include "parse/parselo.h"

namespace {

void append_header(SCP_string& output, const SCP_string& filename)
{
output += "-- ";
output += filename;
output += " -------------------------------\r\n";
}

void trim_newline(char* line)
{
size_t len = strlen(line);
while (len > 0 && line[len - 1] == '\n') {
line[len - 1] = 0;
--len;
}
}

bool strip_and_match_entry_prefix(const char* line, int& comment, const char* entry_prefix, SCP_string* extracted_name = nullptr)
{
char line_without_comments[256];
int i;
int j;

for (i = j = 0; line[i] && j < static_cast<int>(sizeof(line_without_comments)) - 1; ++i) {
if (line[i] == '/' && line[i + 1] == '/') {
break;
}

if (line[i] == '/' && line[i + 1] == '*') {
comment = 1;
++i;
continue;
}

if (line[i] == '*' && line[i + 1] == '/') {
comment = 0;
++i;
continue;
}

if (!comment) {
line_without_comments[j++] = line[i];
}
}

line_without_comments[j] = 0;
const auto prefix_len = static_cast<int>(strlen(entry_prefix));
if (strnicmp(line_without_comments, entry_prefix, prefix_len) != 0) {
return false;
}

drop_trailing_white_space(line_without_comments);
i = prefix_len;
while (line_without_comments[i] == ' ' || line_without_comments[i] == '\t' || line_without_comments[i] == '@') {
++i;
}

if (extracted_name != nullptr) {
*extracted_name = line_without_comments + i;
}

return true;
}

void append_file_contents(SCP_string& output, CFILE* fp)
{
char line[256];
while (cfgets(line, 255, fp)) {
trim_newline(line);
output += line;
output += "\r\n";
}
}

void append_matching_entry(SCP_string& output, CFILE* fp, const SCP_string& file_name, const char* entry_name, const char* entry_prefix)
{
char line[256];
int found = 0;
int comment = 0;

while (cfgets(line, 255, fp)) {
trim_newline(line);

SCP_string found_name;
if (strip_and_match_entry_prefix(line, comment, entry_prefix, &found_name)) {
found = 0;
if (stricmp(found_name.c_str(), entry_name) == 0) {
append_header(output, file_name);
found = 1;
}
}

if (found) {
output += line;
output += "\r\n";
}
}
}

} // namespace

namespace table_viewer {

SCP_string get_table_entry_text(const char* table_filename,
const char* modular_pattern,
const char* entry_name,
const char* missing_table_message,
const char* entry_prefix)
{
SCP_string output;

auto fp = cfopen(table_filename, "r");
if (!fp) {
return missing_table_message != nullptr ? SCP_string(missing_table_message) : SCP_string();
}

append_matching_entry(output, fp, table_filename, entry_name, entry_prefix);
cfclose(fp);

SCP_vector<SCP_string> table_files;
const auto num_files = cf_get_file_list(table_files, CF_TYPE_TABLES, NOX(modular_pattern), CF_SORT_REVERSE);

for (int n = 0; n < num_files; ++n) {
table_files[n] += ".tbm";
fp = cfopen(table_files[n].c_str(), "r");
if (!fp) {
continue;
}

append_matching_entry(output, fp, table_files[n], entry_name, entry_prefix);
cfclose(fp);
}

return output;
}

SCP_string get_complete_table_text(const char* table_filename, const char* modular_pattern, const char* missing_table_message)
{
SCP_string output;
auto fp = cfopen(table_filename, "r");
if (!fp) {
return missing_table_message != nullptr ? SCP_string(missing_table_message) : SCP_string();
}

append_header(output, table_filename);
append_file_contents(output, fp);
cfclose(fp);

SCP_vector<SCP_string> table_files;
const auto num_files = cf_get_file_list(table_files, CF_TYPE_TABLES, NOX(modular_pattern), CF_SORT_REVERSE);

for (int n = 0; n < num_files; ++n) {
table_files[n] += ".tbm";
fp = cfopen(table_files[n].c_str(), "r");
if (!fp) {
continue;
}

append_header(output, table_files[n]);
append_file_contents(output, fp);
cfclose(fp);
}

return output;
}

} // namespace table_viewer
17 changes: 17 additions & 0 deletions code/utils/table_viewer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include "globalincs/pstypes.h"

namespace table_viewer {

SCP_string get_table_entry_text(const char* table_filename,
const char* modular_pattern,
const char* entry_name,
const char* missing_table_message = nullptr,
const char* entry_prefix = "$Name:");

SCP_string get_complete_table_text(const char* table_filename,
const char* modular_pattern,
const char* missing_table_message = nullptr);

} // namespace table_viewer
169 changes: 3 additions & 166 deletions fred2/textviewdlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include "stdafx.h"
#include "FRED.h"
#include "TextViewDlg.h"
#include "cfile/cfile.h"
#include "utils/table_viewer.h"

#ifdef _DEBUG
#undef THIS_FILE
Expand Down Expand Up @@ -74,181 +74,18 @@ void TextViewDlg::OnClose()

void TextViewDlg::LoadShipsTblText(const ship_info *sip)
{
char line[256], line2[256], file_text[82];
int i, j, n, found = 0, comment = 0, num_files = 0;
SCP_vector<SCP_string> tbl_file_names;
CFILE *fp;

SetCaption("Ship Table Data");

if (!sip)
return;

fp = cfopen("ships.tbl", "r");
Assert(fp);


while (cfgets(line, 255, fp)) {
while (line[strlen(line) - 1] == '\n')
line[strlen(line) - 1] = 0;

for (i=j=0; line[i]; i++) {
if (line[i] == '/' && line[i+1] == '/')
break;

if (line[i] == '/' && line[i+1] == '*') {
comment = 1;
i++;
continue;
}

if (line[i] == '*' && line[i+1] == '/') {
comment = 0;
i++;
continue;
}

if (!comment)
line2[j++] = line[i];
}

line2[j] = 0;
if (!strnicmp(line2, "$Name:", 6)) {
drop_trailing_white_space(line2);
found = 0;
i = 6;

while (line2[i] == ' ' || line2[i] == '\t' || line2[i] == '@')
i++;

if (!stricmp(line2 + i, sip->name)) {
m_edit += "-- ships.tbl -------------------------------\r\n";
found = 1;
}
}

if (found) {
m_edit += line;
m_edit += "\r\n";
}
}

cfclose(fp);


// done with ships.tbl, so now check all modular ship tables...
num_files = cf_get_file_list(tbl_file_names, CF_TYPE_TABLES, NOX("*-shp.tbm"), CF_SORT_REVERSE);

for (n = 0; n < num_files; n++){
tbl_file_names[n] += ".tbm";

fp = cfopen(tbl_file_names[n].c_str(), "r");
Assert(fp);

memset( line, 0, sizeof(line) );
memset( line2, 0, sizeof(line2) );
found = 0;
comment = 0;

while (cfgets(line, 255, fp)) {
while (line[strlen(line) - 1] == '\n')
line[strlen(line) - 1] = 0;

for (i=j=0; line[i]; i++) {
if (line[i] == '/' && line[i+1] == '/')
break;

if (line[i] == '/' && line[i+1] == '*') {
comment = 1;
i++;
continue;
}

if (line[i] == '*' && line[i+1] == '/') {
comment = 0;
i++;
continue;
}

if (!comment)
line2[j++] = line[i];
}

line2[j] = 0;
if (!strnicmp(line2, "$Name:", 6)) {
drop_trailing_white_space(line2);
found = 0;
i = 6;

while (line2[i] == ' ' || line2[i] == '\t' || line2[i] == '@')
i++;

if (!stricmp(line2 + i, sip->name)) {
memset( file_text, 0, sizeof(file_text) );
snprintf(file_text, sizeof(file_text)-1, "-- %s -------------------------------\r\n", tbl_file_names[n].c_str());
m_edit += file_text;
found = 1;
}
}

if (found) {
m_edit += line;
m_edit += "\r\n";
}
}

cfclose(fp);
}
m_edit += table_viewer::get_table_entry_text("ships.tbl", "*-shp.tbm", sip->name).c_str();
}

void TextViewDlg::LoadMusicTblText()
{
char line[256];
CFILE* fp;

SetCaption("Music Table Data");

fp = cfopen("music.tbl", "r");
Assert(fp);

// print the header
m_edit += "-- music.tbl -------------------------------\r\n";

// now get the file content
while (cfgets(line, 255, fp)) {
m_edit += line;
m_edit += "\r\n";
}

cfclose(fp);

SCP_vector<SCP_string> tbl_file_names;

// done with music.tbl, so now check all modular music tables...
int num_files = cf_get_file_list(tbl_file_names, CF_TYPE_TABLES, NOX("*-mus.tbm"), CF_SORT_REVERSE);

for (int n = 0; n < num_files; n++) {
tbl_file_names[n] += ".tbm";

fp = cfopen(tbl_file_names[n].c_str(), "r");
Assert(fp);

memset(line, 0, sizeof(line));

// get the name of the current file and print it
char file_text[82];
memset(file_text, 0, sizeof(file_text));
snprintf(file_text, sizeof(file_text) - 1, "-- %s -------------------------------\r\n", tbl_file_names[n].c_str());
m_edit += file_text;

// now get the file content
while (cfgets(line, 255, fp)) {
m_edit += line;
m_edit += "\r\n";
}

cfclose(fp);
}
m_edit += table_viewer::get_complete_table_text("music.tbl", "*-mus.tbm").c_str();
}

void TextViewDlg::OnSetfocusEdit1()
Expand Down
Loading