|
|
@ -1,9 +1,17 @@ |
|
|
|
(import &comptime-only "CHelpers.cake" "ComptimeHelpers.cake") |
|
|
|
(import "DynamicArray.cake" |
|
|
|
&comptime-only "CHelpers.cake" "ComptimeHelpers.cake") |
|
|
|
|
|
|
|
(defstruct directory-entry |
|
|
|
name (* (const char)) |
|
|
|
is-directory bool) |
|
|
|
|
|
|
|
(comptime-cond |
|
|
|
('Unix |
|
|
|
(c-import "<dirent.h>" "<errno.h>" "<stdio.h>" "<sys/stat.h>" "<cassert>") |
|
|
|
(defun test-read-directory (directory-name (* (const char)) &return bool) |
|
|
|
(defun test-read-directory (directory-name (* (const char)) |
|
|
|
directories-dynarray (* (* directory-entry)) &return bool) |
|
|
|
(assert (and directory-name directories-dynarray)) |
|
|
|
|
|
|
|
(var directory (* DIR) (opendir directory-name)) |
|
|
|
(unless directory |
|
|
|
(perror "test-read-directory") |
|
|
@ -12,10 +20,17 @@ |
|
|
|
entry |
|
|
|
(set entry (readdir directory)) |
|
|
|
(fprintf stderr "%s" (path entry > d_name)) |
|
|
|
|
|
|
|
(var new-entry directory-entry (array 0)) |
|
|
|
|
|
|
|
;; TODO String interning? |
|
|
|
(set (field new-entry name) (strdup (path entry > d_name))) |
|
|
|
|
|
|
|
;; See man 3 readdir. This is a nice speed-up but we aren't guaranteed to have it |
|
|
|
(if-c-preprocessor-defined _DIRENT_HAVE_D_TYPE |
|
|
|
(scope |
|
|
|
(when (= DT_DIR (path entry > d_type)) |
|
|
|
(set (field new-entry is-directory) true) |
|
|
|
(fprintf stderr "\t\tdirectory"))) |
|
|
|
(scope |
|
|
|
;; TODO: Instead, this should print to a resizing string because paths can get long |
|
|
@ -25,13 +40,16 @@ |
|
|
|
(fprintf stderr "error: buffer size too small to create path\n") |
|
|
|
(return false)) |
|
|
|
"%s/%s" directory-name (path entry > d_name)) |
|
|
|
(var-with-struct-type details stat) |
|
|
|
(var details (struct stat)) |
|
|
|
(when (and (= -1 (stat entry-filename (addr details))) |
|
|
|
(!= errno ENOENT)) |
|
|
|
(perror "test-read-directory") |
|
|
|
(return false)) |
|
|
|
(when (S_ISDIR (field details st_mode)) |
|
|
|
(set (field new-entry is-directory) true) |
|
|
|
(fprintf stderr "\t\tdirectory")))) |
|
|
|
|
|
|
|
(dynarray-append (deref directories-dynarray) new-entry) |
|
|
|
(fprintf stderr "\n")) |
|
|
|
(closedir directory) |
|
|
|
(return true))) |
|
|
@ -39,17 +57,6 @@ |
|
|
|
(comptime-error "Please define e.g. (comptime-define-symbol 'Unix) for your platform, or if you |
|
|
|
have already, your platform is not supported yet"))) |
|
|
|
|
|
|
|
;; This is a hack to add "struct" in front of type |
|
|
|
(defgenerator var-with-struct-type (var-name symbol type any) |
|
|
|
(var var-statement (const ([] CStatementOperation)) |
|
|
|
(array |
|
|
|
(array Keyword "struct" -1) |
|
|
|
(array TypeNoArray null 2) |
|
|
|
(array Keyword " " -1) |
|
|
|
(array Expression null 1) |
|
|
|
(array SmartEndStatement null -1))) |
|
|
|
(return (c-statement-out var-statement))) |
|
|
|
|
|
|
|
(defmacro sprintf-to-char-array (buffer-name symbol buffer-size any on-buffer-full any |
|
|
|
format-string string &rest format-arguments any) |
|
|
|
(tokenize-push output |
|
|
|