1
0
mirror of https://github.com/urlysses/1991.git synced 2024-11-16 01:28:06 -05:00

Add sourcedir for more reliable file paths

This commit is contained in:
urlysses 2017-02-17 22:46:43 -05:00
parent 1571064535
commit 6310938b54
2 changed files with 26 additions and 2 deletions

23
1991.fs
View File

@ -12,6 +12,20 @@ include unix/socket.fs
2dup exchange
-1 /string
repeat 2drop ;
: sourcedir ( -- saddr su )
\ Returns the directory in which the file
\ invoking the word finds itself
\ relative to gforth's execution directory.
\ Useful for specifying in which dir to find
\ specific files (e.g., public/, views/).
sourcefilename \ get the name of our file
2dup reverse \ reverse and search for first /
s" /" search if \ if found, reverse string to
2dup reverse \ to strip the filename but keep dir.
else
2drop \ no slash,
s" ./" \ same dir execution.
then ;
\ User-defined routing
wordlist constant routes
@ -36,6 +50,15 @@ pubvar public
public 2! ;
: get-public-path ( -- addr u )
public 2@ ;
sourcedir s" public" s+ set-public-path
\ Views directory
pubvar views
: set-view-path ( addr u -- )
views 2! ;
: get-view-path ( -- addr u )
views 2@ ;
sourcedir s" views" s+ set-view-path
\ Query params
pubvar queryString

View File

@ -1,7 +1,8 @@
\ App demo:
include ~+/1991.fs
include ../1991.fs
s" ~+/examples/public" set-public-path
sourcedir s" public" s+ set-public-path
sourcedir s" views" s+ set-view-path
: handle-/ s" fff" ;
: handle-hi s" hi!" ;