Skip to content

Commit

Permalink
Add utils/path.h with some path functions
Browse files Browse the repository at this point in the history
Also use the path_normalize function after we open a file or when we
read the recent file history, so that even on windows we use / and not
\.
  • Loading branch information
guillaumechereau committed Jul 16, 2024
1 parent 7fb02af commit fbde5a4
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 32 deletions.
1 change: 1 addition & 0 deletions src/goxel.c
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,7 @@ static void goxel_load_recent_files(void)
if (strlen(path) < 1) continue;
if (path[strlen(path) - 1] == '\n')
path[strlen(path) - 1] = '\0';
if (!path_normalize(path)) continue;
file = fopen(path, "r");
if (!file) continue;
fclose(file);
Expand Down
1 change: 1 addition & 0 deletions src/goxel.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
#include "utils/geometry.h"
#include "utils/gl.h"
#include "utils/img.h"
#include "utils/path.h"
#include "utils/plane.h"
#include "utils/sound.h"
#include "utils/texture.h"
Expand Down
38 changes: 6 additions & 32 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -289,34 +289,6 @@ static void set_clipboard_text(void *user, const char *text)
glfwSetClipboardString(window, text);
}

static void split_path_and_file(
const char *path_and_file,
char *path,
size_t path_size,
char *file,
size_t file_size)
{
assert(path_and_file != NULL);
char *sep;
int path_len;

sep = strrchr(path_and_file, '/');
// Note: I would prefer to always use unix style paths.
#ifdef WIN32
if (!sep) {
sep = strrchr(path_and_file, '\\');
}
#endif
if (!sep) {
snprintf(file, file_size, "%s", path_and_file);
path[0] = '\0';
} else {
path_len = (int)(sep - path_and_file);
snprintf(path, path_size, "%.*s", path_len, path_and_file);
snprintf(file, file_size, "%s", sep + 1);
}
}

static void filters_to_nfd_spec(
const char *const *filters, char buf[], size_t buf_size)
{
Expand Down Expand Up @@ -360,16 +332,17 @@ static bool open_dialog(
NFD_GetNativeWindowFromGLFWWindow(window, &nfd_window);

if (flags & 1) { // Save dialog.
split_path_and_file(
default_path_and_file, default_path, sizeof(default_path),
default_name, sizeof(default_name));
path_dirname(default_path_and_file, default_path,
sizeof(default_path));
path_basename(default_path_and_file, default_name,
sizeof(default_name));
err = NFD_SaveDialogU8_With(
&out_path,
&(nfdsavedialogu8args_t){
.filterList = &filter,
.filterCount = 1,
.defaultPath = default_path[0] ? default_path : NULL,
.defaultName = default_name,
.defaultName = default_name[0] ? default_name : NULL,
.parentWindow = nfd_window,
});
} else {
Expand All @@ -386,6 +359,7 @@ static bool open_dialog(
if (err == NFD_OKAY) {
snprintf(buf, buf_size, "%s", out_path);
free(out_path);
path_normalize(buf);
return true;
}
return false;
Expand Down
76 changes: 76 additions & 0 deletions src/utils/path.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/* Goxel 3D voxels editor
*
* copyright (c) 2024-present Guillaume Chereau <guillaume@noctua-software.com>
*
* Goxel is free software: you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
* Goxel is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
* You should have received a copy of the GNU General Public License along with
* goxel. If not, see <http://www.gnu.org/licenses/>.
*/

#include "path.h"

#include <assert.h>
#include <stdio.h>
#include <string.h>

char *path_dirname(const char *path, char *out, size_t size)
{
char *sep;
int len;

assert(path);
assert(path != out);
sep = strrchr(path, '/');
#ifdef WIN32
if (!sep) {
sep = strrchr(path, '\\');
}
#endif
if (sep == NULL) {
out[0] = '\0';
return NULL;
}
len = (int)(sep - path);
snprintf(out, size, "%.*s", len, path);
return out;
}

char *path_basename(const char *path, char *out, size_t size)
{
char *sep;
int len;

assert(path);
assert(path != out);
sep = strrchr(path, '/');
#ifdef WIN32
if (!sep) {
sep = strrchr(path, '\\');
}
#endif
if (sep == NULL) {
snprintf(out, size, "%s", path);
return out;
}
len = (int)(sep - path);
snprintf(out, size, "%s", path + len + 1);
return out;
}

bool path_normalize(char *path)
{
int i;
for (i = 0; i < strlen(path); i++) {
if (path[i] == '\\') path[i] = '/';
}
return true;
}
30 changes: 30 additions & 0 deletions src/utils/path.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* Goxel 3D voxels editor
*
* copyright (c) 2024-present Guillaume Chereau <guillaume@noctua-software.com>
*
* Goxel is free software: you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
* Goxel is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
* You should have received a copy of the GNU General Public License along with
* goxel. If not, see <http://www.gnu.org/licenses/>.
*/

/*
* Some utils functions to manipulate paths.
*/

#include <stdbool.h>
#include <stddef.h>

char *path_dirname(const char *path, char *out, size_t size);

char *path_basename(const char *path, char *out, size_t size);

bool path_normalize(char *path);

0 comments on commit fbde5a4

Please sign in to comment.