Refactor some pkgman path code

This commit is contained in:
ROllerozxa 2025-12-04 21:41:24 +01:00
commit 365856fffc
33 changed files with 33 additions and 71 deletions

View file

@ -26,11 +26,11 @@
#include <tms/bindings/cpp/cpp.hh> #include <tms/bindings/cpp/cpp.hh>
#endif #endif
static const char *_level_path[4]; static char _level_path[4][1024];
static char *_state_path = 0; static char _state_path[1024];
static const char *_cache_path[4]; static char _cache_path[4][1024];
static char *_cache_state_path = 0; static char _cache_state_path[1024];
static const char *_pkg_path[4]; static char _pkg_path[4][1024];
static const char *_dir_names[] = { static const char *_dir_names[] = {
"local", "db", "main", "sys" "local", "db", "main", "sys"
}; };
@ -668,22 +668,14 @@ pkgman::get_pkg_path(int type)
return ""; return "";
} }
if (!_pkg_path[type]) { if (!_pkg_path[type][0]) {
_pkg_path[type] = (char*)malloc(1024); /* XXX free this somewhere */ if (type == LEVEL_MAIN)
if (type == LEVEL_MAIN) {
/* main levels are stored internally in data/ */ /* main levels are stored internally in data/ */
snprintf((char*)_pkg_path[type], 1023, "data/pkg/%s", snprintf(_pkg_path[type], 1023, "data/pkg");
_dir_names[type]); else if (type == LEVEL_DB)
} else if (type == LEVEL_DB) { snprintf(_pkg_path[type], 1023, "%s/pkg/db", tms_storage_cache_path());
snprintf((char*)_pkg_path[type], 1023, "%s/pkg/%s", else
tms_storage_cache_path(), snprintf(_pkg_path[type], 1023, "%s/pkg/local", tms_storage_path());
_dir_names[type]);
} else {
snprintf((char*)_pkg_path[type], 1023, "%s/pkg/%s",
tms_storage_path(),
_dir_names[type]);
}
} }
return _pkg_path[type]; return _pkg_path[type];
@ -703,12 +695,8 @@ pkgman::get_level_path(int level_type)
if (level_type == LEVEL_PARTIAL) level_type = LEVEL_LOCAL; if (level_type == LEVEL_PARTIAL) level_type = LEVEL_LOCAL;
if (level_type >= LEVEL_LOCAL_STATE) { if (level_type >= LEVEL_LOCAL_STATE) {
if (!_state_path) { if (!_state_path[0])
_state_path = (char*)malloc(1024); /* XXX free this somewhere */ snprintf(_state_path, 1023, "%s/sav", tms_storage_path());
snprintf((char*)_state_path, 1023,
"%s/sav",
tms_storage_path());
}
return _state_path; return _state_path;
} }
@ -718,56 +706,34 @@ pkgman::get_level_path(int level_type)
return ""; return "";
} }
if (!_level_path[level_type]) { if (!_level_path[level_type][0]) {
_level_path[level_type] = (char*)malloc(1024); /* XXX free this somewhere */ if (level_type == LEVEL_MAIN)
if (level_type == LEVEL_MAIN) {
/* main levels are stored internally in data/ */ /* main levels are stored internally in data/ */
snprintf((char*)_level_path[level_type], 1023, snprintf(_level_path[level_type], 1023, "data/lvl");
"data/lvl/%s", else if (level_type == LEVEL_SYS)
_dir_names[level_type]); snprintf(_level_path[level_type], 1023, "%s/cache/local", tms_storage_cache_path());
} else if (level_type == LEVEL_SYS) { else if (level_type == LEVEL_DB)
snprintf((char*)_level_path[level_type], 1023, snprintf(_level_path[level_type], 1023, "%s/lvl/db", tms_storage_cache_path());
"%s/cache/local", else
tms_storage_cache_path()); snprintf(_level_path[level_type], 1023, "%s/lvl/local", tms_storage_path());
} else if (level_type == LEVEL_DB) {
snprintf((char*)_level_path[level_type], 1023,
"%s/lvl/%s",
tms_storage_cache_path(),
_dir_names[level_type]);
} else {
snprintf((char*)_level_path[level_type], 1023,
"%s/lvl/%s",
tms_storage_path(),
_dir_names[level_type]);
}
} }
return _level_path[level_type]; return _level_path[level_type];
} }
const char *state_prefixes[3] = {
"local",
"db",
"unknown",
};
const char * const char *
pkgman::get_state_prefix(int level_type) pkgman::get_state_prefix(int level_type)
{ {
if (level_type >= LEVEL_LOCAL_STATE) { if (level_type >= LEVEL_LOCAL_STATE)
level_type -= LEVEL_LOCAL_STATE; level_type -= LEVEL_LOCAL_STATE;
}
if (level_type == LEVEL_LOCAL) { if (level_type == LEVEL_LOCAL)
return state_prefixes[0]; return "local";
} else if (level_type == LEVEL_DB) { else if (level_type == LEVEL_DB)
return state_prefixes[1]; return "db";
}
tms_errorf("Unknown state prefix for level type %d", level_type); tms_errorf("Unknown state prefix for level type %d", level_type);
return "unknown";
return state_prefixes[2];
} }
const char * const char *
@ -776,10 +742,8 @@ pkgman::get_cache_path(int level_type)
if (level_type == LEVEL_PARTIAL) level_type = LEVEL_LOCAL; if (level_type == LEVEL_PARTIAL) level_type = LEVEL_LOCAL;
if (level_type >= LEVEL_LOCAL_STATE) { if (level_type >= LEVEL_LOCAL_STATE) {
if (!_cache_state_path) { if (!_cache_state_path[0]) {
_cache_state_path = (char*)malloc(1024); snprintf(_cache_state_path, 1023,
snprintf((char*)_cache_state_path, 1023,
"%s/cache/sav", "%s/cache/sav",
tms_storage_cache_path()); tms_storage_cache_path());
} }
@ -792,10 +756,8 @@ pkgman::get_cache_path(int level_type)
return ""; return "";
} }
if (!_cache_path[level_type]) { if (!_cache_path[level_type][0]) {
_cache_path[level_type] = (char*)malloc(1024); /* XXX free this somewhere */ snprintf(_cache_path[level_type], 1023,
snprintf((char*)_cache_path[level_type], 1023,
"%s/cache/%s", "%s/cache/%s",
tms_storage_cache_path(), tms_storage_cache_path(),
_dir_names[level_type]); _dir_names[level_type]);