aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorRobin Haberkorn <robin.haberkorn@googlemail.com>2015-03-10 01:46:19 +0100
committerRobin Haberkorn <robin.haberkorn@googlemail.com>2015-03-10 01:46:19 +0100
commit5837cc436b4cee01e5cc48d32e75835a14121013 (patch)
tree287e5c74acbafbe5146bb499ea113d4f6ba73038
parent3db42f84371cf7ae9c1ec6cbf3668424645c7479 (diff)
downloadsciteco-5837cc436b4cee01e5cc48d32e75835a14121013.tar.gz
dirname length calculation moved from glob.cpp to file_get_dirname_len() in ioview.h
this function is very useful in other places as well (e.g. command line tab completion)
-rw-r--r--src/glob.cpp9
-rw-r--r--src/ioview.h23
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;