Pārlūkot izejas kodu

Implement path normalization for file browser

rexim 2 gadi atpakaļ
vecāks
revīzija
57d23fa580
5 mainītis faili ar 422 papildinājumiem un 4 dzēšanām
  1. 2 0
      src/common.c
  2. 15 0
      src/common.h
  3. 78 1
      src/file_browser.c
  4. 8 3
      src/main.c
  5. 319 0
      src/sv.h

+ 2 - 0
src/common.c

@@ -16,6 +16,8 @@
 #include "common.h"
 #define ARENA_IMPLEMENTATION
 #include "./arena.h"
+#define SV_IMPLEMENTATION
+#include "sv.h"
 
 static Arena temporary_arena = {0};
 

+ 15 - 0
src/common.h

@@ -32,6 +32,16 @@ typedef int Errno;
 
 #define DA_INIT_CAP 256
 
+#define da_last(da) (assert((da)->count > 0), (da)->items[(da)->count - 1])
+
+#define da_move(dst, src)                \
+    do {                                 \
+       free((dst)->items);               \
+       (dst)->items = (src).items;       \
+       (dst)->count = (src).count;       \
+       (dst)->capacity = (src).capacity; \
+    } while (0)
+
 #define da_append(da, item)                                                          \
     do {                                                                             \
         if ((da)->count >= (da)->capacity) {                                         \
@@ -68,6 +78,9 @@ typedef struct {
     size_t capacity;
 } String_Builder;
 
+#define SB_Fmt "%.*s"
+#define SB_Arg(sb) (int) (sb).count, (sb).items
+
 #define sb_append_buf da_append_many
 #define sb_append_cstr(sb, cstr)  \
     do {                          \
@@ -77,6 +90,8 @@ typedef struct {
     } while (0)
 #define sb_append_null(sb) da_append_many(sb, "", 1)
 
+#define sb_to_sv(sb) sv_from_parts((sb).items, (sb).count)
+
 typedef struct {
     const char **items;
     size_t count;

+ 78 - 1
src/file_browser.c

@@ -1,5 +1,6 @@
 #include <string.h>
 #include "file_browser.h"
+#include "sv.h"
 
 static int file_cmp(const void *ap, const void *bp)
 {
@@ -25,6 +26,76 @@ Errno fb_open_dir(File_Browser *fb, const char *dir_path)
     return 0;
 }
 
+#define PATH_SEP "/"
+#define PATH_EMPTY ""
+#define PATH_DOT "."
+#define PATH_DOTDOT ".."
+
+typedef struct {
+    String_View *items;
+    size_t count;
+    size_t capacity;
+} Comps;
+
+void normpath(String_View path, String_Builder *result)
+{
+    size_t original_sb_size = result->count;
+
+    if (path.count == 0) {
+        sb_append_cstr(result, PATH_DOT);
+        return;
+    }
+
+    int initial_slashes = 0;
+    while (path.count > 0 && *path.data == *PATH_SEP) {
+        initial_slashes += 1;
+        sv_chop_left(&path, 1);
+    }
+    if (initial_slashes > 2) {
+        initial_slashes = 1;
+    }
+
+    Comps new_comps = {0};
+
+    while (path.count > 0) {
+        String_View comp = sv_chop_by_delim(&path, '/');
+        if (comp.count == 0 || sv_eq(comp, SV(PATH_DOT))) {
+            continue;
+        }
+        if (!sv_eq(comp, SV(PATH_DOTDOT))) {
+            da_append(&new_comps, comp);
+            continue;
+        }
+        if (initial_slashes == 0 && new_comps.count == 0) {
+            da_append(&new_comps, comp);
+            continue;
+        }
+        if (new_comps.count > 0 && sv_eq(da_last(&new_comps), SV(PATH_DOTDOT))) {
+            da_append(&new_comps, comp);
+            continue;
+        }
+        if (new_comps.count > 0) {
+            new_comps.count -= 1;
+            continue;
+        }
+    }
+
+    for (int i = 0; i < initial_slashes; ++i) {
+        sb_append_cstr(result, PATH_SEP);
+    }
+
+    for (size_t i = 0; i < new_comps.count; ++i) {
+        if (i > 0) sb_append_cstr(result, PATH_SEP);
+        sb_append_buf(result, new_comps.items[i].data, new_comps.items[i].count);
+    }
+
+    if (original_sb_size == result->count) {
+        sb_append_cstr(result, PATH_DOT);
+    }
+
+    free(new_comps.items);
+}
+
 Errno fb_change_dir(File_Browser *fb)
 {
     assert(fb->dir_path.count > 0 && "You need to call fb_open_dir() before fb_change_dir()");
@@ -36,11 +107,17 @@ Errno fb_change_dir(File_Browser *fb)
 
     fb->dir_path.count -= 1;
 
-    // TODO: fb_change_dir() does not support .. and . properly
+    // TODO: fb->dir_path grows indefinitely if we hit the root
     sb_append_cstr(&fb->dir_path, "/");
     sb_append_cstr(&fb->dir_path, dir_name);
+
+    String_Builder result = {0};
+    normpath(sb_to_sv(fb->dir_path), &result);
+    da_move(&fb->dir_path, result);
     sb_append_null(&fb->dir_path);
 
+    printf("Changed dir to %s\n", fb->dir_path.items);
+
     fb->files.count = 0;
     fb->cursor = 0;
     Errno err = read_entire_dir(fb->dir_path.items, &fb->files);

+ 8 - 3
src/main.c

@@ -20,6 +20,7 @@
 #include "./simple_renderer.h"
 #include "./common.h"
 #include "./lexer.h"
+#include "./sv.h"
 
 // TODO: Save file dialog
 // Needed when ded is ran without any file so it does not know where to save.
@@ -55,6 +56,7 @@ static File_Browser fb = {0};
 // TODO: display errors reported via flash_error right in the text editor window somehow
 #define flash_error(...) do { fprintf(stderr, __VA_ARGS__); fprintf(stderr, "\n"); } while(0)
 
+
 int main(int argc, char **argv)
 {
     Errno err;
@@ -294,19 +296,22 @@ int main(int argc, char **argv)
                         for (size_t i = 0; i < 4; ++i) {
                             editor_insert_char(&editor, ' ');
                         }
-                    } break;
+                    }
+                    break;
 
                     case SDLK_c: {
                         if (event.key.keysym.mod & KMOD_CTRL) {
                             editor_clipboard_copy(&editor);
                         }
-                    } break;
+                    }
+                    break;
 
                     case SDLK_v: {
                         if (event.key.keysym.mod & KMOD_CTRL) {
                             editor_clipboard_paste(&editor);
                         }
-                    } break;
+                    }
+                    break;
 
                     case SDLK_UP: {
                         editor_update_selection(&editor, event.key.keysym.mod & KMOD_SHIFT);

+ 319 - 0
src/sv.h

@@ -0,0 +1,319 @@
+// Copyright 2021 Alexey Kutepov <[email protected]>
+
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+#ifndef SV_H_
+#define SV_H_
+
+#include <stdint.h>
+#include <stdlib.h>
+#include <stdbool.h>
+#include <string.h>
+#include <ctype.h>
+
+#ifndef SVDEF
+#define SVDEF
+#endif // SVDEF
+
+typedef struct {
+    size_t count;
+    const char *data;
+} String_View;
+
+#define SV(cstr_lit) sv_from_parts(cstr_lit, sizeof(cstr_lit) - 1)
+#define SV_STATIC(cstr_lit)   \
+    {                         \
+        sizeof(cstr_lit) - 1, \
+        (cstr_lit)            \
+    }
+
+#define SV_NULL sv_from_parts(NULL, 0)
+
+// printf macros for String_View
+#define SV_Fmt "%.*s"
+#define SV_Arg(sv) (int) (sv).count, (sv).data
+// USAGE:
+//   String_View name = ...;
+//   printf("Name: "SV_Fmt"\n", SV_Arg(name));
+
+SVDEF String_View sv_from_parts(const char *data, size_t count);
+SVDEF String_View sv_from_cstr(const char *cstr);
+SVDEF String_View sv_trim_left(String_View sv);
+SVDEF String_View sv_trim_right(String_View sv);
+SVDEF String_View sv_trim(String_View sv);
+SVDEF String_View sv_take_left_while(String_View sv, bool (*predicate)(char x));
+SVDEF String_View sv_chop_by_delim(String_View *sv, char delim);
+SVDEF String_View sv_chop_by_sv(String_View *sv, String_View thicc_delim);
+SVDEF bool sv_try_chop_by_delim(String_View *sv, char delim, String_View *chunk);
+SVDEF String_View sv_chop_left(String_View *sv, size_t n);
+SVDEF String_View sv_chop_right(String_View *sv, size_t n);
+SVDEF String_View sv_chop_left_while(String_View *sv, bool (*predicate)(char x));
+SVDEF bool sv_index_of(String_View sv, char c, size_t *index);
+SVDEF bool sv_eq(String_View a, String_View b);
+SVDEF bool sv_eq_ignorecase(String_View a, String_View b);
+SVDEF bool sv_starts_with(String_View sv, String_View prefix);
+SVDEF bool sv_ends_with(String_View sv, String_View suffix);
+SVDEF uint64_t sv_to_u64(String_View sv);
+uint64_t sv_chop_u64(String_View *sv);
+
+#endif  // SV_H_
+
+#ifdef SV_IMPLEMENTATION
+
+SVDEF String_View sv_from_parts(const char *data, size_t count)
+{
+    String_View sv;
+    sv.count = count;
+    sv.data = data;
+    return sv;
+}
+
+SVDEF String_View sv_from_cstr(const char *cstr)
+{
+    return sv_from_parts(cstr, strlen(cstr));
+}
+
+SVDEF String_View sv_trim_left(String_View sv)
+{
+    size_t i = 0;
+    while (i < sv.count && isspace(sv.data[i])) {
+        i += 1;
+    }
+
+    return sv_from_parts(sv.data + i, sv.count - i);
+}
+
+SVDEF String_View sv_trim_right(String_View sv)
+{
+    size_t i = 0;
+    while (i < sv.count && isspace(sv.data[sv.count - 1 - i])) {
+        i += 1;
+    }
+
+    return sv_from_parts(sv.data, sv.count - i);
+}
+
+SVDEF String_View sv_trim(String_View sv)
+{
+    return sv_trim_right(sv_trim_left(sv));
+}
+
+SVDEF String_View sv_chop_left(String_View *sv, size_t n)
+{
+    if (n > sv->count) {
+        n = sv->count;
+    }
+
+    String_View result = sv_from_parts(sv->data, n);
+
+    sv->data  += n;
+    sv->count -= n;
+
+    return result;
+}
+
+SVDEF String_View sv_chop_right(String_View *sv, size_t n)
+{
+    if (n > sv->count) {
+        n = sv->count;
+    }
+
+    String_View result = sv_from_parts(sv->data + sv->count - n, n);
+
+    sv->count -= n;
+
+    return result;
+}
+
+SVDEF bool sv_index_of(String_View sv, char c, size_t *index)
+{
+    size_t i = 0;
+    while (i < sv.count && sv.data[i] != c) {
+        i += 1;
+    }
+
+    if (i < sv.count) {
+        if (index) {
+            *index = i;
+        }
+        return true;
+    } else {
+        return false;
+    }
+}
+
+SVDEF bool sv_try_chop_by_delim(String_View *sv, char delim, String_View *chunk)
+{
+    size_t i = 0;
+    while (i < sv->count && sv->data[i] != delim) {
+        i += 1;
+    }
+
+    String_View result = sv_from_parts(sv->data, i);
+
+    if (i < sv->count) {
+        sv->count -= i + 1;
+        sv->data  += i + 1;
+        if (chunk) {
+            *chunk = result;
+        }
+        return true;
+    }
+
+    return false;
+}
+
+SVDEF String_View sv_chop_by_delim(String_View *sv, char delim)
+{
+    size_t i = 0;
+    while (i < sv->count && sv->data[i] != delim) {
+        i += 1;
+    }
+
+    String_View result = sv_from_parts(sv->data, i);
+
+    if (i < sv->count) {
+        sv->count -= i + 1;
+        sv->data  += i + 1;
+    } else {
+        sv->count -= i;
+        sv->data  += i;
+    }
+
+    return result;
+}
+
+SVDEF String_View sv_chop_by_sv(String_View *sv, String_View thicc_delim)
+{
+    String_View window = sv_from_parts(sv->data, thicc_delim.count);
+    size_t i = 0;
+    while (i + thicc_delim.count < sv->count
+        && !(sv_eq(window, thicc_delim)))
+    {
+        i++;
+        window.data++;
+    }
+
+    String_View result = sv_from_parts(sv->data, i);
+
+    if (i + thicc_delim.count == sv->count) {
+        // include last <thicc_delim.count> characters if they aren't
+        //  equal to thicc_delim
+        result.count += thicc_delim.count;
+    }
+
+    // Chop!
+    sv->data  += i + thicc_delim.count;
+    sv->count -= i + thicc_delim.count;
+
+    return result;
+}
+
+SVDEF bool sv_starts_with(String_View sv, String_View expected_prefix)
+{
+    if (expected_prefix.count <= sv.count) {
+        String_View actual_prefix = sv_from_parts(sv.data, expected_prefix.count);
+        return sv_eq(expected_prefix, actual_prefix);
+    }
+
+    return false;
+}
+
+SVDEF bool sv_ends_with(String_View sv, String_View expected_suffix)
+{
+    if (expected_suffix.count <= sv.count) {
+        String_View actual_suffix = sv_from_parts(sv.data + sv.count - expected_suffix.count, expected_suffix.count);
+        return sv_eq(expected_suffix, actual_suffix);
+    }
+
+    return false;
+}
+
+SVDEF bool sv_eq(String_View a, String_View b)
+{
+    if (a.count != b.count) {
+        return false;
+    } else {
+        return memcmp(a.data, b.data, a.count) == 0;
+    }
+}
+
+SVDEF bool sv_eq_ignorecase(String_View a, String_View b)
+{
+    if (a.count != b.count) {
+        return false;
+    }
+
+    char x, y;
+    for (size_t i = 0; i < a.count; i++) {
+        x = 'A' <= a.data[i] && a.data[i] <= 'Z'
+              ? a.data[i] + 32
+              : a.data[i];
+
+        y = 'A' <= b.data[i] && b.data[i] <= 'Z'
+              ? b.data[i] + 32
+              : b.data[i];
+
+        if (x != y) return false;
+    }
+    return true;
+}
+
+SVDEF uint64_t sv_to_u64(String_View sv)
+{
+    uint64_t result = 0;
+
+    for (size_t i = 0; i < sv.count && isdigit(sv.data[i]); ++i) {
+        result = result * 10 + (uint64_t) sv.data[i] - '0';
+    }
+
+    return result;
+}
+
+uint64_t sv_chop_u64(String_View *sv)
+{
+    uint64_t result = 0;
+    while (sv->count > 0 && isdigit(*sv->data)) {
+        result = result*10 + *sv->data - '0';
+        sv->count -= 1;
+        sv->data += 1;
+    }
+    return result;
+}
+
+SVDEF String_View sv_chop_left_while(String_View *sv, bool (*predicate)(char x))
+{
+    size_t i = 0;
+    while (i < sv->count && predicate(sv->data[i])) {
+        i += 1;
+    }
+    return sv_chop_left(sv, i);
+}
+
+SVDEF String_View sv_take_left_while(String_View sv, bool (*predicate)(char x))
+{
+    size_t i = 0;
+    while (i < sv.count && predicate(sv.data[i])) {
+        i += 1;
+    }
+    return sv_from_parts(sv.data, i);
+}
+
+#endif // SV_IMPLEMENTATION