diff --git a/code/lab/dialogs/lab_ui_helpers.cpp b/code/lab/dialogs/lab_ui_helpers.cpp index 6a43c2ce582..57147506f47 100644 --- a/code/lab/dialogs/lab_ui_helpers.cpp +++ b/code/lab/dialogs/lab_ui_helpers.cpp @@ -1,7 +1,7 @@ #include "lab_ui_helpers.h" -#include "cfile/cfile.h" #include "lab/labv2_internal.h" +#include "utils/table_viewer.h" SCP_map get_docking_point_map(int model_index) { @@ -38,499 +38,22 @@ SCP_map get_bay_paths_map(int model_index) SCP_string get_ship_table_text(ship_info* sip) { - char line[256], line2[256], file_text[82]; - int i, j, n, found = 0, comment = 0, num_files = 0; - SCP_vector tbl_file_names; - SCP_string result; - - auto 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)) { - result += "-- ships.tbl -------------------------------\r\n"; - found = 1; - } - } - - if (found) { - result += line; - result += "\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), - "-- %s -------------------------------\r\n", - tbl_file_names[n].c_str()); - result += file_text; - found = 1; - } - } - - if (found) { - result += line; - result += "\r\n"; - } - } - - cfclose(fp); - } - - return result; + return table_viewer::get_table_entry_text("ships.tbl", "*-shp.tbm", sip->name); } SCP_string get_weapon_table_text(weapon_info* wip) { - char line[256], line2[256], file_text[82]; - int i, j, n, found = 0, comment = 0, num_files = 0; - SCP_vector tbl_file_names; - SCP_string result; - - auto fp = cfopen("weapons.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, wip->name)) { - result += "-- weapons.tbl -------------------------------\r\n"; - found = 1; - } - } - - if (found) { - result += line; - result += "\r\n"; - } - } - - cfclose(fp); - - num_files = cf_get_file_list(tbl_file_names, CF_TYPE_TABLES, NOX("*-wep.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, wip->name)) { - memset(file_text, 0, sizeof(file_text)); - snprintf(file_text, - sizeof(file_text), - "-- %s -------------------------------\r\n", - tbl_file_names[n].c_str()); - result += file_text; - found = 1; - } - } - - if (found) { - result += line; - result += "\r\n"; - } - } - - cfclose(fp); - } - - return result; + return table_viewer::get_table_entry_text("weapons.tbl", "*-wep.tbm", wip->name); } SCP_string get_asteroid_table_text(const asteroid_info* aip) { - char line[256], line2[256], file_text[82]; - int i, j, n, found = 0, comment = 0, num_files = 0; - SCP_vector tbl_file_names; - SCP_string result; - - auto fp = cfopen("asteroid.tbl", "r"); - if (!fp) - return "No asteroids.tbl found.\r\n"; - - 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, aip->name)) { - result += "-- asteroid.tbl -------------------------------\r\n"; - found = 1; - } - } - - if (found) { - result += line; - result += "\r\n"; - } - } - - cfclose(fp); - - num_files = cf_get_file_list(tbl_file_names, CF_TYPE_TABLES, NOX("*-ast.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"); - if (!fp) - continue; - - 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, aip->name)) { - memset(file_text, 0, sizeof(file_text)); - snprintf(file_text, - sizeof(file_text), - "-- %s -------------------------------\r\n", - tbl_file_names[n].c_str()); - result += file_text; - found = 1; - } - } - - if (found) { - result += line; - result += "\r\n"; - } - } - - cfclose(fp); - } - - return result; + return table_viewer::get_table_entry_text("asteroid.tbl", "*-ast.tbm", aip->name, "No asteroids.tbl found.\r\n"); } SCP_string get_prop_table_text(const prop_info* pip) { - char line[256], line2[256], file_text[82]; - int i, j, n, found = 0, comment = 0, num_files = 0; - SCP_vector tbl_file_names; - SCP_string result; - - auto fp = cfopen("props.tbl", "r"); - if (!fp) - return "No props.tbl found.\r\n"; - - 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, pip->name.c_str())) { - result += "-- props.tbl -------------------------------\r\n"; - found = 1; - } - } - - if (found) { - result += line; - result += "\r\n"; - } - } - - cfclose(fp); - - num_files = cf_get_file_list(tbl_file_names, CF_TYPE_TABLES, NOX("*-prp.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"); - if (!fp) - continue; - - 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, pip->name.c_str())) { - memset(file_text, 0, sizeof(file_text)); - snprintf(file_text, - sizeof(file_text) - 1, - "-- %s -------------------------------\r\n", - tbl_file_names[n].c_str()); - result += file_text; - found = 1; - } - } - - if (found) { - result += line; - result += "\r\n"; - } - } - - cfclose(fp); - } - - return result; + return table_viewer::get_table_entry_text("props.tbl", "*-prp.tbm", pip->name.c_str(), "No props.tbl found.\r\n"); } SCP_string get_directory_or_vp(const char* path) diff --git a/code/source_groups.cmake b/code/source_groups.cmake index 2f23cbd95fd..aa9bb445d8b 100644 --- a/code/source_groups.cmake +++ b/code/source_groups.cmake @@ -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 diff --git a/code/utils/table_viewer.cpp b/code/utils/table_viewer.cpp new file mode 100644 index 00000000000..18a57669721 --- /dev/null +++ b/code/utils/table_viewer.cpp @@ -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(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(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 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 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 diff --git a/code/utils/table_viewer.h b/code/utils/table_viewer.h new file mode 100644 index 00000000000..6daf6980680 --- /dev/null +++ b/code/utils/table_viewer.h @@ -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 diff --git a/fred2/textviewdlg.cpp b/fred2/textviewdlg.cpp index ee80b95d535..82434b7cab8 100644 --- a/fred2/textviewdlg.cpp +++ b/fred2/textviewdlg.cpp @@ -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 @@ -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 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 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() diff --git a/qtfred/src/mission/dialogs/MusicTBLViewerModel.cpp b/qtfred/src/mission/dialogs/MusicTBLViewerModel.cpp index c2e9b26d23d..f3283f465ad 100644 --- a/qtfred/src/mission/dialogs/MusicTBLViewerModel.cpp +++ b/qtfred/src/mission/dialogs/MusicTBLViewerModel.cpp @@ -1,6 +1,6 @@ #include "MusicTBLViewerModel.h" -#include +#include namespace fso::fred::dialogs { MusicTBLViewerModel::MusicTBLViewerModel(QObject* parent, EditorViewport* viewport) : AbstractDialogModel(parent, viewport) @@ -14,46 +14,11 @@ bool MusicTBLViewerModel::apply() void MusicTBLViewerModel::reject() {} void MusicTBLViewerModel::initializeData() { - char line[256]{}; - CFILE* fp = nullptr; - SCP_vector tbl_file_names; - - text.clear(); - - // Base table - text += "-- music.tbl -------------------------------\r\n"; - fp = cfopen("music.tbl", "r"); - Assert(fp); - while (cfgets(line, 255, fp)) { - text += line; - text += "\r\n"; - } - cfclose(fp); - - // Modular tables (*-mus.tbm), reverse sorted to match legacy behavior - const 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"; - - text += "-- "; - text += tbl_file_names[n]; - text += " -------------------------------\r\n"; - - fp = cfopen(tbl_file_names[n].c_str(), "r"); - Assert(fp); - - memset(line, 0, sizeof(line)); - while (cfgets(line, 255, fp)) { - text += line; - text += "\r\n"; - } - cfclose(fp); - } - + text = table_viewer::get_complete_table_text("music.tbl", "*-mus.tbm"); modelChanged(); } SCP_string MusicTBLViewerModel::getText() const { return text; } -} // namespace fso::fred::dialogs \ No newline at end of file +} // namespace fso::fred::dialogs diff --git a/qtfred/src/mission/dialogs/ShipEditor/ShipTBLViewerModel.cpp b/qtfred/src/mission/dialogs/ShipEditor/ShipTBLViewerModel.cpp index bc50015a0cd..764aa66c76d 100644 --- a/qtfred/src/mission/dialogs/ShipEditor/ShipTBLViewerModel.cpp +++ b/qtfred/src/mission/dialogs/ShipEditor/ShipTBLViewerModel.cpp @@ -1,4 +1,8 @@ #include "ShipTBLViewerModel.h" + +#include +#include + namespace fso { namespace fred { namespace dialogs { @@ -16,134 +20,15 @@ void ShipTBLViewerModel::reject() } void ShipTBLViewerModel::initializeData(const int ship_class) { - char line[256], line2[256]{}, file_text[82]{}; const ship_info* sip = &Ship_info[ship_class]; - int i, j, n, found = 0, comment = 0, num_files = 0; - CFILE* fp = nullptr; - SCP_vector tbl_file_names; + text.clear(); 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)) { - text += "-- ships.tbl -------------------------------\r\n"; - found = 1; - } - } - - if (found) { - text += line; - text += "\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()); - text += file_text; - found = 1; - } - } - - if (found) { - text += line; - text += "\r\n"; - } - } - - cfclose(fp); - } + text = table_viewer::get_table_entry_text("ships.tbl", "*-shp.tbm", sip->name); modelChanged(); } SCP_string ShipTBLViewerModel::getText() const @@ -152,4 +37,4 @@ SCP_string ShipTBLViewerModel::getText() const } } // namespace dialogs } // namespace fred -} // namespace fso \ No newline at end of file +} // namespace fso diff --git a/qtfred/src/mission/dialogs/ShipEditor/WeaponsTBLViewerModel.cpp b/qtfred/src/mission/dialogs/ShipEditor/WeaponsTBLViewerModel.cpp index 54f5a292eca..3cecf5876e6 100644 --- a/qtfred/src/mission/dialogs/ShipEditor/WeaponsTBLViewerModel.cpp +++ b/qtfred/src/mission/dialogs/ShipEditor/WeaponsTBLViewerModel.cpp @@ -1,6 +1,7 @@ #include "WeaponsTBLViewerModel.h" #include +#include namespace fso::fred::dialogs { WeaponsTBLViewerModel::WeaponsTBLViewerModel(QObject* parent, EditorViewport* viewport, int wc) : AbstractDialogModel(parent, viewport) @@ -14,137 +15,18 @@ bool WeaponsTBLViewerModel::apply() void WeaponsTBLViewerModel::reject() {} void WeaponsTBLViewerModel::initializeData(const int wc) { - char line[256], line2[256]{}, file_text[82]{}; const weapon_info* sip = &Weapon_info[wc]; - int i, j, n, found = 0, comment = 0, num_files = 0; - CFILE* fp = nullptr; - SCP_vector tbl_file_names; text.clear(); if (!sip) { return; } - fp = cfopen("weapons.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)) { - text += "-- weapons.tbl -------------------------------\r\n"; - found = 1; - } - } - - if (found) { - text += line; - text += "\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("*-wep.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()); - text += file_text; - found = 1; - } - } - - if (found) { - text += line; - text += "\r\n"; - } - } - - cfclose(fp); - } + text = table_viewer::get_table_entry_text("weapons.tbl", "*-wep.tbm", sip->name); modelChanged(); } SCP_string WeaponsTBLViewerModel::getText() const { return text; } -} // namespace fso::fred::dialogs \ No newline at end of file +} // namespace fso::fred::dialogs