Some more refactoring of MSG_Write() function

This commit is contained in:
maron2000 2025-04-20 22:02:27 +09:00
parent 86146dcaea
commit 53a5d3dd30

View File

@ -463,13 +463,19 @@ void LoadMessageFile(const char * fname) {
bool MSG_Write(const char* location, const char* name) { bool MSG_Write(const char* location, const char* name) {
char temp[4096]; std::unique_ptr<char[]> temp(new char[4096]);
FILE* out = fopen(location, "w+t");
if(out == NULL) return false; // Failed to open file for writing FILE* out = fopen(location, "w+t");
if(out == nullptr) return false; // Failed to open file for writing
if(name != nullptr) langname = std::string(name);
if(!langname.empty())
fprintf(out, ":DOSBOX-X:LANGUAGE:%s\n", langname.c_str());
if(dos.loaded_codepage)
fprintf(out, ":DOSBOX-X:CODEPAGE:%d\n", dos.loaded_codepage);
if(name != NULL) langname = std::string(name);
if(langname != "") fprintf(out, ":DOSBOX-X:LANGUAGE:%s\n", langname.c_str());
if(dos.loaded_codepage) fprintf(out, ":DOSBOX-X:CODEPAGE:%d\n", dos.loaded_codepage);
fprintf(out, ":DOSBOX-X:VERSION:%s\n", VERSION); fprintf(out, ":DOSBOX-X:VERSION:%s\n", VERSION);
fprintf(out, ":DOSBOX-X:REMARK:%s\n", langnote.c_str()); fprintf(out, ":DOSBOX-X:REMARK:%s\n", langnote.c_str());
@ -477,42 +483,42 @@ bool MSG_Write(const char* location, const char* name) {
// Output messages in insertion order using LangList // Output messages in insertion order using LangList
for(const auto& msg : LangList) { for(const auto& msg : LangList) {
if(!CodePageGuestToHostUTF8(temp, msg.val.c_str())) const char* out_text = msg.val.c_str();
fprintf(out, ":%s\n%s\n.\n", msg.name.c_str(), msg.val.c_str()); if(!CodePageGuestToHostUTF8(temp.get(), out_text))
fprintf(out, ":%s\n%s\n.\n", msg.name.c_str(), out_text);
else else
fprintf(out, ":%s\n%s\n.\n", msg.name.c_str(), temp); fprintf(out, ":%s\n%s\n.\n", msg.name.c_str(), temp.get());
} }
// Output menu items (menu filtering conditions remain unchanged) // Output menu items (menu filtering conditions remain unchanged)
std::vector<DOSBoxMenu::item> master_list = mainMenu.get_master_list(); std::vector<DOSBoxMenu::item> master_list = mainMenu.get_master_list();
for(auto& id : master_list) { for(auto& id : master_list) {
if(id.is_allocated() && if(!id.is_allocated() || id.get_type() == DOSBoxMenu::separator_type_id ||
id.get_type() != DOSBoxMenu::separator_type_id && id.get_type() == DOSBoxMenu::vseparator_type_id ||
id.get_type() != DOSBoxMenu::vseparator_type_id && (id.get_name().size() == 5 && id.get_name().substr(0, 4) == "slot") ||
!(id.get_name().size() == 5 && id.get_name().substr(0, 4) == "slot") && (id.get_name().size() > 9 && id.get_name().substr(0, 8) == "command_") ||
!(id.get_name().size() > 9 && id.get_name().substr(0, 8) == "command_") && (id.get_name().size() == 6 && id.get_name().substr(0, 5) == "Drive" &&
!(id.get_name().size() == 6 && id.get_name().substr(0, 5) == "Drive" && id.get_name().back() >= 'A' && id.get_name().back() <= 'Z') ||
id.get_name().back() >= 'A' && id.get_name().back() <= 'Z') && (id.get_name().size() > 9 && id.get_name().substr(0, 6) == "drive_" &&
!(id.get_name().size() > 9 && id.get_name().substr(0, 6) == "drive_" && id.get_name()[6] >= 'B' && id.get_name()[6] <= 'Z' && id.get_name()[7] == '_') ||
id.get_name()[6] >= 'B' && id.get_name()[6] <= 'Z' && id.get_name()[7] == '_') && id.get_name() == "mapper_cycauto")
id.get_name() != "mapper_cycauto") { continue;
std::string text = id.get_text(); std::string text = id.get_text();
if(id.get_name() == "hostkey_mapper" || id.get_name() == "clipboard_device") { if(id.get_name() == "hostkey_mapper" || id.get_name() == "clipboard_device") {
std::size_t found = text.find(":"); size_t found = text.find(":");
if(found != std::string::npos) if(found != std::string::npos) text = text.substr(0, found);
text = text.substr(0, found);
} }
std::string idname = (id.get_name().size() > 9 && id.get_name().substr(0, 8) == "drive_A_") std::string idname = (id.get_name().size() > 9 && id.get_name().substr(0, 8) == "drive_A_")
? "drive_" + id.get_name().substr(8) ? "drive_" + id.get_name().substr(8)
: id.get_name(); : id.get_name();
if(!CodePageGuestToHostUTF8(temp, text.c_str())) const char* out_text = text.c_str();
fprintf(out, ":MENU:%s\n%s\n.\n", idname.c_str(), text.c_str()); if(!CodePageGuestToHostUTF8(temp.get(), out_text))
fprintf(out, ":MENU:%s\n%s\n.\n", idname.c_str(), out_text);
else else
fprintf(out, ":MENU:%s\n%s\n.\n", idname.c_str(), temp); fprintf(out, ":MENU:%s\n%s\n.\n", idname.c_str(), temp.get());
}
} }
// Output MAPPER items that differ from mainMenu mappings // Output MAPPER items that differ from mainMenu mappings
@ -522,10 +528,11 @@ bool MSG_Write(const char* location, const char* name) {
mainMenu.get_item("mapper_" + it.first).get_text() == it.second) mainMenu.get_item("mapper_" + it.first).get_text() == it.second)
continue; continue;
if(!CodePageGuestToHostUTF8(temp, it.second.c_str())) const char* out_text = it.second.c_str();
fprintf(out, ":MAPPER:%s\n%s\n.\n", it.first.c_str(), it.second.c_str()); if(!CodePageGuestToHostUTF8(temp.get(), out_text))
fprintf(out, ":MAPPER:%s\n%s\n.\n", it.first.c_str(), out_text);
else else
fprintf(out, ":MAPPER:%s\n%s\n.\n", it.first.c_str(), temp); fprintf(out, ":MAPPER:%s\n%s\n.\n", it.first.c_str(), temp.get());
} }
morelen = inmsg = false; morelen = inmsg = false;
@ -533,6 +540,7 @@ bool MSG_Write(const char* location, const char* name) {
return true; return true;
} }
void ResolvePath(std::string& in); void ResolvePath(std::string& in);
void MSG_Init() { void MSG_Init() {
Section_prop *section=static_cast<Section_prop *>(control->GetSection("dosbox")); Section_prop *section=static_cast<Section_prop *>(control->GetSection("dosbox"));