]> granicus.if.org Git - neomutt/commitdiff
add mutt_path_abbr_folder
authorRichard Russon <rich@flatcap.org>
Sat, 25 Aug 2018 10:47:07 +0000 (11:47 +0100)
committerRichard Russon <rich@flatcap.org>
Sun, 26 Aug 2018 21:11:03 +0000 (22:11 +0100)
mutt/path.c
mutt/path.h

index 343c1c46d4c4af5483e93ee1f5bafa3e966b394e..fac54781d2d0b3d77e32010306ec12890112b9c8 100644 (file)
@@ -499,3 +499,38 @@ void mutt_path_get_parent(char *path, char *buf, size_t buflen)
     buf[1] = '\0';
   }
 }
+
+/**
+ * mutt_path_abbr_folder - Create a folder abbreviation
+ * @param buf    Path to modify
+ * @param buflen Length of buffer
+ * @param folder Base path for '=' substitution
+ * @retval true Path was abbreviated
+ *
+ * Abbreviate a path using '=' to represent the 'folder'.
+ * If the folder path is passed, it won't be abbreviated to just '='
+ */
+bool mutt_path_abbr_folder(char *buf, size_t buflen, const char *folder)
+{
+  if (!buf || !folder)
+    return false;
+
+  size_t flen = mutt_str_strlen(folder);
+  if (flen < 2)
+    return false;
+
+  if (folder[flen - 1] == '/')
+    flen--;
+
+  if (mutt_str_strncmp(buf, folder, flen) != 0)
+    return false;
+
+  if (buf[flen + 1] == '\0') // Don't abbreviate to '=/'
+    return false;
+
+  size_t rlen = mutt_str_strlen(buf + flen + 1);
+
+  buf[0] = '=';
+  memmove(buf + 1, buf + flen + 1, rlen + 1);
+  return true;
+}
index e762a015a3ff92a1c675cb171cd8c152358e86de..ae83bdb555b59ebe49bfe2ccbd5c006332d962d8 100644 (file)
@@ -26,6 +26,7 @@
 #include <stdbool.h>
 #include <stdio.h>
 
+bool        mutt_path_abbr_folder(char *buf, size_t buflen, const char *folder);
 const char *mutt_path_basename(const char *f);
 bool        mutt_path_canon(char *buf, size_t buflen, const char *homedir);
 char *      mutt_path_concat(char *d, const char *dir, const char *fname, size_t l);