diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/glob.cpp | 9 | ||||
-rw-r--r-- | src/ioview.h | 23 |
2 files changed, 27 insertions, 5 deletions
diff --git a/src/glob.cpp b/src/glob.cpp index af8fbe9..9ef3f89 100644 --- a/src/glob.cpp +++ b/src/glob.cpp @@ -28,6 +28,7 @@ #include "interface.h" #include "parser.h" #include "ring.h" +#include "ioview.h" #include "glob.h" namespace SciTECO { @@ -38,7 +39,7 @@ namespace States { Globber::Globber(const gchar *pattern) { - gsize dirname_len = 0; + gsize dirname_len; /* * This finds the directory component including @@ -49,11 +50,9 @@ Globber::Globber(const gchar *pattern) * file names with the exact same directory * prefix as the input pattern. */ - for (const gchar *p = pattern; *p; p++) - if (G_IS_DIR_SEPARATOR(*p)) - dirname_len = p - pattern + 1; - + dirname_len = file_get_dirname_len(pattern); dirname = g_strndup(pattern, dirname_len); + dir = g_dir_open(*dirname ? dirname : ".", 0, NULL); /* if dirname does not exist, dir may be NULL */ diff --git a/src/ioview.h b/src/ioview.h index 6a87b7b..22e9e14 100644 --- a/src/ioview.h +++ b/src/ioview.h @@ -45,6 +45,29 @@ gchar *get_absolute_path(const gchar *path); bool file_is_visible(const gchar *path); +/** + * This gets the length of a file name's directory + * component including any trailing directory separator. + * It returns 0 if the file name does not have a directory + * separator. + * This is useful when constructing file names in the same + * directory as an existing one, keeping the exact same + * directory component (globbing, tab completion...). + * Also if it returns non-0, this can be used to look up + * the last used directory separator in the file name. + */ +static inline gsize +file_get_dirname_len(const gchar *path) +{ + gsize len = 0; + + for (const gchar *p = path; *p; p++) + if (G_IS_DIR_SEPARATOR(*p)) + len = p - path + 1; + + return len; +} + class IOView : public ViewCurrent { class UndoTokenRemoveFile : public UndoToken { gchar *filename; |