Browse Source

List directories in ImGui

This uses the new dynarray stuff I just coded up.
TreemapSubdirectories
Macoy Madson 1 year ago
parent
commit
f891a28e96
  1. 3
      .gitmodules
  2. 2
      Dependencies/cakelisp
  3. 2
      Dependencies/gamelib
  4. 1
      Dependencies/stb
  5. 6
      UpdateCakelispGameLib.sh
  6. 22
      src/FileHelper.cake
  7. 35
      src/FileSystem.cake

3
.gitmodules

@ -10,3 +10,6 @@
[submodule "Dependencies/imgui"]
path = Dependencies/imgui
url = https://github.com/ocornut/imgui
[submodule "Dependencies/stb"]
path = Dependencies/stb
url = https://github.com/nothings/stb

2
Dependencies/cakelisp

@ -1 +1 @@
Subproject commit 5e1cf68e2e1ee0ceb15d1298dc40d2dd2dba106b
Subproject commit ad037d0fe42e6cc2d62888d5001922c9ae8e9480

2
Dependencies/gamelib

@ -1 +1 @@
Subproject commit dcd6733b4724224e17509208e0b55b29998abd2b
Subproject commit eeedfc16cf04ea214a57e27b6605a7cdbef41301

1
Dependencies/stb

@ -0,0 +1 @@
Subproject commit c9064e317699d2e495f36ba4f9ac037e88ee371a

6
UpdateCakelispGameLib.sh

@ -0,0 +1,6 @@
#!/bin/sh
cd Dependencies/gamelib && git pull || exit $?
cd ../..
cd Dependencies/cakelisp && git pull || exit $?
cd ../..

22
src/FileHelper.cake

@ -1,7 +1,9 @@
(import
"FileSystem.cake"
;; From GameLib
"SDL.cake" "ImGui.cake")
"SDL.cake" "ImGui.cake" "DynamicArray.cake"
;; From cakelisp
&comptime-only "CHelpers.cake")
(c-import "SDL.h" "SDL_syswm.h" "SDL_timer.h")
;; TODO: Somehow inherit this from SDL.cake?
(module-use-sdl-build-options)
@ -13,16 +15,24 @@
(make-imgui-sdl-gl3w-application
main
"File Helper"
(scope ;; Initialization
(body ;; Initialization
(SDL_Log "File Helper\n\n
Created by Macoy Madson <macoy@macoy.me>.\n
https://macoy.me/code/macoy/file-helper\n
Copyright (c) 2021 Macoy Madson.\n
Licensed under GPL-3.0-or-later.\n\n")
(test-read-directory "."))
(scope ;; Once per frame
(imgui-call ShowDemoWindow))
(scope (printf "Shut down\n")))
(var directory-entries (* directory-entry) null)
(test-read-directory "." (addr directory-entries)))
(body ;; Once per frame
(imgui-call ShowDemoWindow)
(imgui-call Begin "Current Directory")
(each-in-dynarray directory-entries i
(imgui-call Text (field (at i directory-entries) name)))
(imgui-call End))
(body ;; Shut down
(dynarray-free directory-entries)))
(set-cakelisp-option executable-output "file-helper")

35
src/FileSystem.cake

@ -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

Loading…
Cancel
Save