mirror of
https://github.com/subsurface/subsurface.git
synced 2024-11-30 22:20:21 +00:00
Clean up system_default_filename()
In the old implementation there were two static C-style strings, filename and path, which were initialized to NULL and filled on first call of the function (i.e. singletons). There is no sense in having two static variables indicating whether this function was called previously. Moreover, there is no point in remembering filename accross function calls, because it is not used once path is set to a non-NULL value. Therefore, make the filename variable non-static and calculate it only on first invocation (as indicated by a NULL path). Moreover, free() the filename variable after its use to fix a memory leak of the old code. The windows code is slightly different in that the temporary filename is not dynamically allocated. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
parent
7a99d7e5c3
commit
04f38d61d7
3 changed files with 14 additions and 17 deletions
11
core/linux.c
11
core/linux.c
|
@ -83,18 +83,17 @@ const char *system_default_directory(void)
|
||||||
|
|
||||||
const char *system_default_filename(void)
|
const char *system_default_filename(void)
|
||||||
{
|
{
|
||||||
static char *filename = NULL;
|
static const char *path = NULL;
|
||||||
if (!filename) {
|
if (!path) {
|
||||||
const char *user = getenv("LOGNAME");
|
const char *user = getenv("LOGNAME");
|
||||||
if (same_string(user, ""))
|
if (same_string(user, ""))
|
||||||
user = "username";
|
user = "username";
|
||||||
filename = calloc(strlen(user) + 5, 1);
|
char *filename = calloc(strlen(user) + 5, 1);
|
||||||
strcat(filename, user);
|
strcat(filename, user);
|
||||||
strcat(filename, ".xml");
|
strcat(filename, ".xml");
|
||||||
}
|
|
||||||
static const char *path = NULL;
|
|
||||||
if (!path)
|
|
||||||
path = system_default_path_append(filename);
|
path = system_default_path_append(filename);
|
||||||
|
free(filename);
|
||||||
|
}
|
||||||
return path;
|
return path;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
11
core/macos.c
11
core/macos.c
|
@ -81,18 +81,17 @@ const char *system_default_directory(void)
|
||||||
|
|
||||||
const char *system_default_filename(void)
|
const char *system_default_filename(void)
|
||||||
{
|
{
|
||||||
static char *filename = NULL;
|
static const char *path = NULL;
|
||||||
if (!filename) {
|
if (!path) {
|
||||||
const char *user = getenv("LOGNAME");
|
const char *user = getenv("LOGNAME");
|
||||||
if (same_string(user, ""))
|
if (same_string(user, ""))
|
||||||
user = "username";
|
user = "username";
|
||||||
filename = calloc(strlen(user) + 5, 1);
|
char *filename = calloc(strlen(user) + 5, 1);
|
||||||
strcat(filename, user);
|
strcat(filename, user);
|
||||||
strcat(filename, ".xml");
|
strcat(filename, ".xml");
|
||||||
}
|
|
||||||
static const char *path = NULL;
|
|
||||||
if (!path)
|
|
||||||
path = system_default_path_append(filename);
|
path = system_default_path_append(filename);
|
||||||
|
free(filename);
|
||||||
|
}
|
||||||
return path;
|
return path;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -103,17 +103,16 @@ const char *system_default_directory(void)
|
||||||
*/
|
*/
|
||||||
const char *system_default_filename(void)
|
const char *system_default_filename(void)
|
||||||
{
|
{
|
||||||
static wchar_t filename[UNLEN + 5] = { 0 };
|
static const char *path = NULL;
|
||||||
if (!*filename) {
|
if (!path) {
|
||||||
wchar_t username[UNLEN + 1] = { 0 };
|
wchar_t username[UNLEN + 1] = { 0 };
|
||||||
DWORD username_len = UNLEN + 1;
|
DWORD username_len = UNLEN + 1;
|
||||||
GetUserNameW(username, &username_len);
|
GetUserNameW(username, &username_len);
|
||||||
|
wchar_t filename[UNLEN + 5] = { 0 };
|
||||||
wcscat(filename, username);
|
wcscat(filename, username);
|
||||||
wcscat(filename, L".xml");
|
wcscat(filename, L".xml");
|
||||||
}
|
|
||||||
static const char *path = NULL;
|
|
||||||
if (!path)
|
|
||||||
path = system_default_path_append(filename);
|
path = system_default_path_append(filename);
|
||||||
|
}
|
||||||
return path;
|
return path;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue