Files
fs2open.github.com/code/utils/string_utils.h
T
Goober5000 5c737a5bf9 improve campaign file extension handling (#6443)
As discovered by WouterSmits, campaigns with periods in their filenames caused issues with FSO's campaign handling.  Specifically, in The Babylon Project, the EA-Minbari war v1.0.fc2 campaign would not save progress because FSO expected EA-Minbari war v1.fc2 in certain places.  This is because the `_splitpath()` function attempted to remove an extension from a filename that already had it removed.  For most campaigns this did not matter, but WouterSmits found a campaign where it did.

To solve this, there is now a new `util::get_file_part()` function to remove the path without removing the extension.  This replaces `_splitpath()` for campaign files, as well as the similar `clean_filename()` and `GetFilePart()` functions in other places.  All code paths were inspected for any chance of inadvertent extension adding, and Assertions were added to verify future code.  The only potential problem area was `player:loadCampaignSavefile()` which now pre-removes the campaign extension if needed.
2024-12-22 22:31:58 +01:00

37 lines
695 B
C++

#pragma once
#include "globalincs/pstypes.h"
namespace util {
template <typename Out>
void split_string(const SCP_string& s, char delim, Out result)
{
SCP_stringstream ss(s);
SCP_string item;
while (std::getline(ss, item, delim)) {
if (!item.empty()) {
*(result++) = item;
}
}
}
std::vector<std::string> split_string(const std::string& s, char delim);
// get a filename minus any leading path
template <typename T>
T *get_file_part(T *path)
{
T *p = path + strlen(path)-1;
// Move p to point to first character of filename (check both types of dir separator)
while( (p >= path) && (*p != '\\') && (*p != '/') && (*p != ':') )
p--;
p++;
return p;
}
} // namespace util