From cf7a26506c54a7bf943e4de3af94c7767ec976fa Mon Sep 17 00:00:00 2001 From: Goober5000 Date: Wed, 11 Mar 2026 22:58:15 -0400 Subject: [PATCH] assorted `const` fixes 1. Change `ai_profile_lookup` and `multi_xfer_lookup` to use `const char *`, since all the other lookup functions do 2. Remove several unneeded `const_cast`s, some of which are now made possible by the non-const `data()` overload in C++17 3. Change `campaign_link` to use `const char*` 4. Use better string management in `test::FSTestFixture::init_cmdline()` --- code/ai/ai_profiles.cpp | 2 +- code/ai/ai_profiles.h | 2 +- code/graphics/software/NVGFont.cpp | 4 ++-- code/lab/dialogs/lab_ui.cpp | 8 ++++---- code/missioneditor/campaignsave.h | 6 +++--- code/network/multi_xfer.cpp | 2 +- code/network/multi_xfer.h | 2 +- code/scripting/api/libs/graphics.cpp | 2 +- fred2/sexp_tree.cpp | 2 +- freespace2/freespace.cpp | 2 +- .../src/mission/dialogs/CampaignEditorDialogModel.cpp | 10 +++------- qtfred/src/ui/widgets/sexp_tree.cpp | 2 +- test/src/util/FSTestFixture.cpp | 6 +++--- 13 files changed, 23 insertions(+), 27 deletions(-) diff --git a/code/ai/ai_profiles.cpp b/code/ai/ai_profiles.cpp index 5dafff7a15a..57303161423 100644 --- a/code/ai/ai_profiles.cpp +++ b/code/ai/ai_profiles.cpp @@ -812,7 +812,7 @@ void ai_profiles_init() Ai_profiles_initted = 1; } -int ai_profile_lookup(char *name) +int ai_profile_lookup(const char *name) { for (int i = 0; i < Num_ai_profiles; i++) if (!stricmp(name, Ai_profiles[i].profile_name)) diff --git a/code/ai/ai_profiles.h b/code/ai/ai_profiles.h index db0c3fad946..09dd784591d 100644 --- a/code/ai/ai_profiles.h +++ b/code/ai/ai_profiles.h @@ -164,6 +164,6 @@ extern ai_profile_t Ai_profiles[MAX_AI_PROFILES]; void ai_profiles_init(); -int ai_profile_lookup(char *name); +int ai_profile_lookup(const char *name); #endif diff --git a/code/graphics/software/NVGFont.cpp b/code/graphics/software/NVGFont.cpp index a9fcd816537..02defa8b1f4 100644 --- a/code/graphics/software/NVGFont.cpp +++ b/code/graphics/software/NVGFont.cpp @@ -32,8 +32,8 @@ namespace font return 1; } - const char *nullPtr = strchr(const_cast(string), '\0'); - const char *nextToken = strpbrk(const_cast(string), TOKEN_SEPARATORS); + const char *nullPtr = strchr(string, '\0'); + const char *nextToken = strpbrk(string, TOKEN_SEPARATORS); // WOHOO! Pointer arithmetic!!! if (nullPtr != NULL && (nextToken == NULL || nullPtr < nextToken)) diff --git a/code/lab/dialogs/lab_ui.cpp b/code/lab/dialogs/lab_ui.cpp index e696391f3bb..d553c385eac 100644 --- a/code/lab/dialogs/lab_ui.cpp +++ b/code/lab/dialogs/lab_ui.cpp @@ -660,7 +660,7 @@ static void build_ship_table_info_txtbox(ship_info* sip) } InputTextMultiline("##table_text", - const_cast(table_text.c_str()), + table_text.data(), table_text.length(), ImVec2(-FLT_MIN, GetTextLineHeight() * 16), ImGuiInputTextFlags_ReadOnly); @@ -681,7 +681,7 @@ static void build_weapon_table_info_txtbox(weapon_info* wip) } InputTextMultiline("##weapon_table_text", - const_cast(table_text.c_str()), + table_text.data(), table_text.length(), ImVec2(-FLT_MIN, GetTextLineHeight() * 16), ImGuiInputTextFlags_ReadOnly); @@ -1506,7 +1506,7 @@ void LabUi::show_object_options() const } InputTextMultiline("##asteroid_table_text", - const_cast(table_text.c_str()), + table_text.data(), table_text.length(), ImVec2(-FLT_MIN, GetTextLineHeight() * 16), ImGuiInputTextFlags_ReadOnly); @@ -1544,7 +1544,7 @@ void LabUi::show_object_options() const } InputTextMultiline("##prop_table_text", - const_cast(table_text.c_str()), + table_text.data(), table_text.length(), ImVec2(-FLT_MIN, GetTextLineHeight() * 16), ImGuiInputTextFlags_ReadOnly); diff --git a/code/missioneditor/campaignsave.h b/code/missioneditor/campaignsave.h index bc8c1f92850..86bdfe8e975 100644 --- a/code/missioneditor/campaignsave.h +++ b/code/missioneditor/campaignsave.h @@ -8,9 +8,9 @@ typedef struct campaign_link { int node; // node tracker when link is in sexp tree window bool is_mission_loop; // whether link leads to mission loop bool is_mission_fork; // whether link leads to mission fork - char* mission_branch_txt; // text describing mission loop - char* mission_branch_brief_anim; // filename of anim to play in the brief - char* mission_branch_brief_sound; // filename of anim to play in the brief + const char* mission_branch_txt; // text describing mission loop + const char* mission_branch_brief_anim; // filename of anim to play in the brief + const char* mission_branch_brief_sound; // filename of anim to play in the brief } campaign_link; class Fred_campaign_save : public Fred_mission_save { diff --git a/code/network/multi_xfer.cpp b/code/network/multi_xfer.cpp index 3bb2dbd6d26..96c78c4997d 100644 --- a/code/network/multi_xfer.cpp +++ b/code/network/multi_xfer.cpp @@ -450,7 +450,7 @@ int multi_xfer_get_flags(int handle) } // if the passed filename is being xferred, return the xfer handle, otherwise return -1 -int multi_xfer_lookup(char *filename) +int multi_xfer_lookup(const char *filename) { int idx; diff --git a/code/network/multi_xfer.h b/code/network/multi_xfer.h index 9a89c091997..cfce7f59e1b 100644 --- a/code/network/multi_xfer.h +++ b/code/network/multi_xfer.h @@ -87,7 +87,7 @@ void multi_xfer_xor_flags(int handle,int flags); int multi_xfer_get_flags(int handle); // if the passed filename is being xferred, return the xfer handle, otherwise return -1 -int multi_xfer_lookup(char *filename); +int multi_xfer_lookup(const char *filename); // get the % of completion of the passed file handle, return < 0 if invalid float multi_xfer_pct_complete(int handle); diff --git a/code/scripting/api/libs/graphics.cpp b/code/scripting/api/libs/graphics.cpp index ec82897307e..4e9102b8b47 100644 --- a/code/scripting/api/libs/graphics.cpp +++ b/code/scripting/api/libs/graphics.cpp @@ -185,7 +185,7 @@ ADE_INDEXER(l_Graphics_Posteffects, "number index", "Gets the name of the specif if (index >= (int) names.size()) return ade_set_error(L, "s", ""); - return ade_set_args(L, "s", const_cast(names[index].c_str())); + return ade_set_args(L, "s", names[index].c_str()); } ADE_FUNC(__len, l_Graphics_Posteffects, nullptr, "Gets the number of available post-processing effects", "number", "number of post-processing effects or 0 on error") diff --git a/fred2/sexp_tree.cpp b/fred2/sexp_tree.cpp index a37a1d03cdf..2503b336467 100644 --- a/fred2/sexp_tree.cpp +++ b/fred2/sexp_tree.cpp @@ -3421,7 +3421,7 @@ int sexp_tree::get_default_value(sexp_list_item *item, char *text_buf, int op, i break; case OPF_FONT: - str = const_cast(font::FontManager::getFont(0)->getName().c_str()); + str = font::FontManager::getFont(0)->getName().c_str(); break; case OPF_AUDIO_VOLUME_OPTION: diff --git a/freespace2/freespace.cpp b/freespace2/freespace.cpp index 6e89a2153e7..2de45a56cd4 100644 --- a/freespace2/freespace.cpp +++ b/freespace2/freespace.cpp @@ -7553,7 +7553,7 @@ int detect_lang() // try and open the file to verify font::stuff_first(first_font); - CFILE *detect = cfopen(const_cast(first_font.c_str()), "rb"); + CFILE *detect = cfopen(first_font.c_str(), "rb"); // will use default setting if something went wrong if (!detect) diff --git a/qtfred/src/mission/dialogs/CampaignEditorDialogModel.cpp b/qtfred/src/mission/dialogs/CampaignEditorDialogModel.cpp index f03188e186e..21196c3e58d 100644 --- a/qtfred/src/mission/dialogs/CampaignEditorDialogModel.cpp +++ b/qtfred/src/mission/dialogs/CampaignEditorDialogModel.cpp @@ -507,13 +507,9 @@ void CampaignEditorDialogModel::saveCampaign(const SCP_string& filename) // The descriptive text fields only apply to special (loop/fork) branches. if (branch.is_loop || branch.is_fork) { - link.mission_branch_txt = - branch.loop_description.empty() ? nullptr : const_cast(branch.loop_description.c_str()); - link.mission_branch_brief_anim = - branch.loop_briefing_anim.empty() ? nullptr : const_cast(branch.loop_briefing_anim.c_str()); - link.mission_branch_brief_sound = branch.loop_briefing_sound.empty() - ? nullptr - : const_cast(branch.loop_briefing_sound.c_str()); + link.mission_branch_txt = branch.loop_description.empty() ? nullptr : branch.loop_description.c_str(); + link.mission_branch_brief_anim = branch.loop_briefing_anim.empty() ? nullptr : branch.loop_briefing_anim.c_str(); + link.mission_branch_brief_sound = branch.loop_briefing_sound.empty() ? nullptr : branch.loop_briefing_sound.c_str(); } else { link.mission_branch_txt = nullptr; link.mission_branch_brief_anim = nullptr; diff --git a/qtfred/src/ui/widgets/sexp_tree.cpp b/qtfred/src/ui/widgets/sexp_tree.cpp index d56bb6af773..5f59917ff93 100644 --- a/qtfred/src/ui/widgets/sexp_tree.cpp +++ b/qtfred/src/ui/widgets/sexp_tree.cpp @@ -1457,7 +1457,7 @@ int sexp_tree::get_default_value(sexp_list_item* item, char* text_buf, int op, i break; case OPF_FONT: - str = const_cast(font::FontManager::getFont(0)->getName().c_str()); + str = font::FontManager::getFont(0)->getName().c_str(); break; case OPF_AUDIO_VOLUME_OPTION: diff --git a/test/src/util/FSTestFixture.cpp b/test/src/util/FSTestFixture.cpp index e511ba93572..7d2b203fec2 100644 --- a/test/src/util/FSTestFixture.cpp +++ b/test/src/util/FSTestFixture.cpp @@ -113,13 +113,13 @@ void test::FSTestFixture::addCommandlineArg(const SCP_string& arg) { _cmdlineArgs.push_back(arg); } void test::FSTestFixture::init_cmdline() { - std::unique_ptr parts(new char* [_cmdlineArgs.size()]); + std::vector parts(_cmdlineArgs.size()); for (size_t i = 0; i < _cmdlineArgs.size(); ++i) { - parts[i] = const_cast(_cmdlineArgs[i].c_str()); + parts[i] = _cmdlineArgs[i].data(); } - parse_cmdline((int) _cmdlineArgs.size(), parts.get()); + parse_cmdline(sz2i(_cmdlineArgs.size()), parts.data()); } void test::FSTestFixture::pushModDir(const SCP_string& mod) { if (!_currentModDir.empty()) {