Skip to content
This repository has been archived by the owner on Nov 29, 2023. It is now read-only.

Commit

Permalink
std: implement file_base
Browse files Browse the repository at this point in the history
  • Loading branch information
l1mey112 committed Dec 2, 2022
1 parent 5b30a11 commit 257e46c
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
2 changes: 1 addition & 1 deletion add.stas
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
include <std.stas>

fn main {

'eeee/eee.stas/////////s//////ee/ee' file_base dup putuln puts endl
}
36 changes: 36 additions & 0 deletions lib/stdlib/os.stas
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,31 @@ fn file_dir 2 2 {
str swap
}

fn file_base 2 2 {
dup 0 = if {
drop drop '.' ret
}

auto str 1 over pop str
auto str.len 1 dup pop str.len

`/` string.last_index_of_u8

; (idx)
dup -1 = if {
drop str str.len ret
}
dup ++ str.len = if {
drop
str str.len -- file_base
ret
}
++

dup str + swap
str.len swap -
}

; ( fd -- size bool )
fn fd.stat_size? 1 2 {
reserve struct_stat sizeof(stat)
Expand Down Expand Up @@ -820,4 +845,15 @@ fn make_path_relative_to_wd 2 2 {
} else {
str
}
}

fn assert_tmp_dir {
'/tmp/stas' path_exists ! if {
'/tmp/stas' drop S_RWE_OWNER mkdir errno? assert -> 'get_tmp_dir: `mkdir` failed'
}
}

; ( -- str len )
fn get_tmp_dir 0 2 {
'/tmp/stas'
}
5 changes: 5 additions & 0 deletions tests/os.out
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,10 @@ true
.
.
/
makedefinitions.vsh
comptime.out
.
eeeeeeeeeeee
eee
true
false
9 changes: 9 additions & 0 deletions tests/os.stas
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,20 @@ fn test_file_dir {
'/eee' file_dir puts endl
}

fn test_file_base {
'/usr/home/l-m/git/stas/lib/posix/makedefinitions.vsh' file_base puts endl
'/usr/home/l-m/git/compiler/tests/comptime.out' file_base puts endl
'' file_base puts endl
'eeeeeeeeeeee' file_base puts endl
'/eee' file_base puts endl
}

fn main {
test_normalise_path
test_join_path
test_errno
test_file_dir
test_file_base

"/hello" is_abs_path bool.to_str puts endl
"hello/heeler" is_abs_path bool.to_str puts endl
Expand Down

0 comments on commit 257e46c

Please sign in to comment.