Eliminate unnecessary string copy in PHYSFSX_addRelToSearchPath

Pass a mutable buffer from the caller, and allow
PHYSFSX_addRelToSearchPath to adjust the capitalization in that buffer,
rather than creating a copy in a stack local.  This can slightly affect
status messages that use the name, but otherwise should have no effect
on the game.
This commit is contained in:
Kp 2023-05-13 15:02:55 +00:00
parent 6a794c6b63
commit 8eec1dc810
5 changed files with 36 additions and 20 deletions

View file

@ -437,7 +437,7 @@ enum class physfs_search_path : int
append,
};
PHYSFS_ErrorCode PHYSFSX_addRelToSearchPath(const char *relname, std::array<char, PATH_MAX> &realPath, physfs_search_path);
PHYSFS_ErrorCode PHYSFSX_addRelToSearchPath(char *relname, std::array<char, PATH_MAX> &realPath, physfs_search_path);
void PHYSFSX_removeRelFromSearchPath(const char *relname);
extern int PHYSFSX_fsize(const char *hogname);
extern void PHYSFSX_listSearchPathContent();
@ -483,8 +483,8 @@ using RAIIPHYSFS_LiteralMount = std::unique_ptr<const char, PHYSFS_unowned_stora
*/
using RAIIPHYSFS_ComputedPathMount = std::unique_ptr<typename PHYSFS_computed_path_mount_deleter::element_type, PHYSFS_computed_path_mount_deleter>;
RAIIPHYSFS_LiteralMount make_PHYSFSX_LiteralMount(const char *const name, physfs_search_path);
RAIIPHYSFS_ComputedPathMount make_PHYSFSX_ComputedPathMount(const char *const name, physfs_search_path position);
[[nodiscard]]
RAIIPHYSFS_ComputedPathMount make_PHYSFSX_ComputedPathMount(char *const name, physfs_search_path position);
extern int PHYSFSX_rename(const char *oldpath, const char *newpath);
@ -502,7 +502,7 @@ namespace dsx {
bool PHYSFSX_init(int argc, char *argv[]);
int PHYSFSX_checkSupportedArchiveTypes();
#if defined(DXX_BUILD_DESCENT_II)
RAIIPHYSFS_ComputedPathMount make_PHYSFSX_ComputedPathMount(const char *const name1, const char *const name2, physfs_search_path);
RAIIPHYSFS_ComputedPathMount make_PHYSFSX_ComputedPathMount(char *name1, char *name2, physfs_search_path);
#endif
}

View file

@ -350,10 +350,22 @@ void init_editor()
// first, make sure we can find the files we need
std::array<char, PATH_MAX> pathname;
PHYSFSX_addRelToSearchPath("editor/data", pathname, physfs_search_path::append); // look in source directory first (for work in progress)
PHYSFSX_addRelToSearchPath("editor", pathname, physfs_search_path::append); // then in editor directory
PHYSFSX_addRelToSearchPath("editor.zip", pathname, physfs_search_path::append); // then in a zip file
PHYSFSX_addRelToSearchPath("editor.dxa", pathname, physfs_search_path::append); // or addon pack
{
static char relname[]{"editor/data"};
PHYSFSX_addRelToSearchPath(relname, pathname, physfs_search_path::append); // look in source directory first (for work in progress)
}
{
static char relname[]{"editor"};
PHYSFSX_addRelToSearchPath(relname, pathname, physfs_search_path::append); // then in editor directory
}
{
static char relname[]{"editor.zip"};
PHYSFSX_addRelToSearchPath(relname, pathname, physfs_search_path::append); // then in a zip file
}
{
static char relname[]{"editor.dxa"};
PHYSFSX_addRelToSearchPath(relname, pathname, physfs_search_path::append); // or addon pack
}
if (!ui_init())
{

View file

@ -465,11 +465,14 @@ static int main(int argc, char *argv[])
return(0);
#if defined(DXX_BUILD_DESCENT_I)
const auto descent_hog = make_PHYSFSX_ComputedPathMount("descent.hog", physfs_search_path::append);
static char relname_descent_hog[]{"descent.hog"};
const auto descent_hog = make_PHYSFSX_ComputedPathMount(relname_descent_hog, physfs_search_path::append);
#define DXX_NAME_NUMBER "1"
#define DXX_HOGFILE_NAMES "descent.hog"
#elif defined(DXX_BUILD_DESCENT_II)
const auto descent_hog = make_PHYSFSX_ComputedPathMount("descent2.hog", "d2demo.hog", physfs_search_path::append);
static char relname_descent2_hog[]{"descent2.hog"};
static char relname_d2demo_hog[]{"d2demo.hog"};
const auto descent_hog = make_PHYSFSX_ComputedPathMount(relname_descent2_hog, relname_d2demo_hog, physfs_search_path::append);
#define DXX_NAME_NUMBER "2"
#define DXX_HOGFILE_NAMES "descent2.hog or d2demo.hog"
#endif

View file

@ -938,7 +938,8 @@ static const char *load_mission(const mle *const mission)
#endif
{
std::array<char, PATH_MAX> pathname;
if (const auto r = PHYSFSX_addRelToSearchPath("descent.hog", pathname, physfs_search_path::prepend); r != PHYSFS_ERR_OK)
static char relname[]{"descent.hog"};
if (const auto r = PHYSFSX_addRelToSearchPath(relname, pathname, physfs_search_path::prepend); r != PHYSFS_ERR_OK)
#if defined(DXX_BUILD_DESCENT_I)
Error("descent.hog not available!\n%s", PHYSFS_getErrorByCode(r));
#elif defined(DXX_BUILD_DESCENT_II)

View file

@ -235,7 +235,10 @@ bool PHYSFSX_init(int argc, char *argv[])
#endif
std::array<char, PATH_MAX> pathname;
PHYSFSX_addRelToSearchPath("data", pathname, physfs_search_path::append); // 'Data' subdirectory
{
static char relname[]{"data"};
PHYSFSX_addRelToSearchPath(relname, pathname, physfs_search_path::append); // 'Data' subdirectory
}
// For Macintosh, add the 'Resources' directory in the .app bundle to the searchpaths
#if defined(__APPLE__) && defined(__MACH__)
@ -271,7 +274,7 @@ bool PHYSFSX_init(int argc, char *argv[])
}
#if defined(DXX_BUILD_DESCENT_II)
RAIIPHYSFS_ComputedPathMount make_PHYSFSX_ComputedPathMount(const char *const name1, const char *const name2, physfs_search_path position)
RAIIPHYSFS_ComputedPathMount make_PHYSFSX_ComputedPathMount(char *const name1, char *const name2, physfs_search_path position)
{
auto pathname = std::make_unique<std::array<char, PATH_MAX>>();
if (PHYSFSX_addRelToSearchPath(name1, *pathname.get(), position) == PHYSFS_ERR_OK ||
@ -287,11 +290,8 @@ namespace dcx {
// Add a searchpath, but that searchpath is relative to an existing searchpath
// It will add the first one it finds and return 1, if it doesn't find any it returns 0
PHYSFS_ErrorCode PHYSFSX_addRelToSearchPath(const char *relname, std::array<char, PATH_MAX> &pathname, physfs_search_path add_to_end)
PHYSFS_ErrorCode PHYSFSX_addRelToSearchPath(char *const relname2, std::array<char, PATH_MAX> &pathname, physfs_search_path add_to_end)
{
char relname2[PATH_MAX];
snprintf(relname2, sizeof(relname2), "%s", relname);
PHYSFSEXT_locateCorrectCase(relname2);
if (!PHYSFSX_getRealPath(relname2, pathname))
@ -307,13 +307,13 @@ PHYSFS_ErrorCode PHYSFSX_addRelToSearchPath(const char *relname, std::array<char
const auto action = add_to_end != physfs_search_path::prepend ? "append" : "insert";
if (r)
{
con_printf(CON_DEBUG, "PHYSFS: %s canonical directory \"%s\" to search path from relative name \"%s\"", action, pathname.data(), relname);
con_printf(CON_DEBUG, "PHYSFS: %s canonical directory \"%s\" to search path from relative name \"%s\"", action, pathname.data(), relname2);
return PHYSFS_ERR_OK;
}
else
{
const auto err = PHYSFS_getLastErrorCode();
con_printf(CON_VERBOSE, "PHYSFS: failed to %s canonical directory \"%s\" to search path from relative name \"%s\": \"%s\"", action, pathname.data(), relname, PHYSFS_getErrorByCode(err));
con_printf(CON_VERBOSE, "PHYSFS: failed to %s canonical directory \"%s\" to search path from relative name \"%s\": \"%s\"", action, pathname.data(), relname2, PHYSFS_getErrorByCode(err));
return err;
}
}
@ -636,7 +636,7 @@ void PHYSFSX_read_helper_report_error(const char *const filename, const unsigned
(Error)(filename, line, func, "reading at %lu", static_cast<unsigned long>((PHYSFS_tell)(file)));
}
RAIIPHYSFS_ComputedPathMount make_PHYSFSX_ComputedPathMount(const char *const name, physfs_search_path position)
RAIIPHYSFS_ComputedPathMount make_PHYSFSX_ComputedPathMount(char *const name, physfs_search_path position)
{
auto pathname = std::make_unique<std::array<char, PATH_MAX>>();
if (PHYSFSX_addRelToSearchPath(name, *pathname.get(), position) == PHYSFS_ERR_OK)