<commit_before>! Copyright (C) 2004, 2008 <NAME>, <NAME>.
! See http://factorcode.org/license.txt for BSD license.
USING: io.backend io.files.private io hashtables kernel math
memory namespaces sequences strings assocs arrays definitions
system combinators splitting sbufs continuations io.encodings
io.encodings.binary ;
IN: io.files

HOOK: (file-reader) io-backend ( path -- stream )

HOOK: (file-writer) io-backend ( path -- stream )

HOOK: (file-appender) io-backend ( path -- stream )

: <file-reader> ( path encoding -- stream )
    swap (file-reader) swap <decoder> ;

: <file-writer> ( path encoding -- stream )
    swap (file-writer) swap <encoder> ;

: <file-appender> ( path encoding -- stream )
    swap (file-appender) swap <encoder> ;

HOOK: rename-file io-backend ( from to -- )

! Pathnames
: path-separator? ( ch -- ? ) windows? "/\\" "/" ? member? ;

: right-trim-separators ( str -- newstr )
    [ path-separator? ] right-trim ;

: left-trim-separators ( str -- newstr )
    [ path-separator? ] left-trim ;

: path+ ( str1 str2 -- str )
    >r right-trim-separators "/" r>
    left-trim-separators 3append ;

: last-path-separator ( path -- n ? )
    [ length 1- ] keep [ path-separator? ] find-last* ;

HOOK: root-directory? io-backend ( path -- ? )

M: object root-directory? ( path -- ? ) path-separator? ;

: special-directory? ( name -- ? ) { "." ".." } member? ;

TUPLE: no-parent-directory path ;

: no-parent-directory ( path -- * )
    \ no-parent-directory construct-boa throw ;

: parent-directory ( path -- parent )
    right-trim-separators {
        { [ dup empty? ] [ drop "/" ] }
        { [ dup root-directory? ] [ ] }
        { [ dup [ path-separator? ] contains? not ] [ drop "." ] }
        { [ t ] [
            dup last-path-separator drop 1+ cut
            special-directory? [ no-parent-directory ] when
        ] }
    } cond ;

: file-name ( path -- string )
    right-trim-separators {
        { [ dup empty? ] [ drop "/" ] }
        { [ dup last-path-separator ] [ 1+ tail ] }
        { [ t ] [ drop ] }
    } cond ;

TUPLE: file-info type size permissions modified ;

HOOK: file-info io-backend ( path -- info )
HOOK: link-info io-backend ( path -- info )

SYMBOL: +regular-file+
SYMBOL: +directory+
SYMBOL: +character-device+
SYMBOL: +block-device+
SYMBOL: +fifo+
SYMBOL: +symbolic-link+
SYMBOL: +socket+
SYMBOL: +unknown+

! File metadata
: stat ( path -- directory? permissions length modified )
    normalize-pathname (stat) ;

! : file-length ( path -- n ) stat drop 2nip ;

: file-modified ( path -- n ) stat >r 3drop r> ;

! : file-permissions ( path -- perm ) stat 2drop nip ;

: exists? ( path -- ? ) file-modified >boolean ;

: directory? ( path -- ? ) stat 3drop ;

! Current working directory
HOOK: cd io-backend ( path -- )

HOOK: cwd io-backend ( -- path )<commit_msg>io.files: 'directory?' uses file-info<commit_after>! Copyright (C) 2004, 2008 <NAME>, <NAME>.
! See http://factorcode.org/license.txt for BSD license.
USING: io.backend io.files.private io hashtables kernel math
memory namespaces sequences strings assocs arrays definitions
system combinators splitting sbufs continuations io.encodings
io.encodings.binary ;
IN: io.files

HOOK: (file-reader) io-backend ( path -- stream )

HOOK: (file-writer) io-backend ( path -- stream )

HOOK: (file-appender) io-backend ( path -- stream )

: <file-reader> ( path encoding -- stream )
    swap (file-reader) swap <decoder> ;

: <file-writer> ( path encoding -- stream )
    swap (file-writer) swap <encoder> ;

: <file-appender> ( path encoding -- stream )
    swap (file-appender) swap <encoder> ;

HOOK: rename-file io-backend ( from to -- )

! Pathnames
: path-separator? ( ch -- ? ) windows? "/\\" "/" ? member? ;

: right-trim-separators ( str -- newstr )
    [ path-separator? ] right-trim ;

: left-trim-separators ( str -- newstr )
    [ path-separator? ] left-trim ;

: path+ ( str1 str2 -- str )
    >r right-trim-separators "/" r>
    left-trim-separators 3append ;

: last-path-separator ( path -- n ? )
    [ length 1- ] keep [ path-separator? ] find-last* ;

HOOK: root-directory? io-backend ( path -- ? )

M: object root-directory? ( path -- ? ) path-separator? ;

: special-directory? ( name -- ? ) { "." ".." } member? ;

TUPLE: no-parent-directory path ;

: no-parent-directory ( path -- * )
    \ no-parent-directory construct-boa throw ;

: parent-directory ( path -- parent )
    right-trim-separators {
        { [ dup empty? ] [ drop "/" ] }
        { [ dup root-directory? ] [ ] }
        { [ dup [ path-separator? ] contains? not ] [ drop "." ] }
        { [ t ] [
            dup last-path-separator drop 1+ cut
            special-directory? [ no-parent-directory ] when
        ] }
    } cond ;

: file-name ( path -- string )
    right-trim-separators {
        { [ dup empty? ] [ drop "/" ] }
        { [ dup last-path-separator ] [ 1+ tail ] }
        { [ t ] [ drop ] }
    } cond ;

TUPLE: file-info type size permissions modified ;

HOOK: file-info io-backend ( path -- info )
HOOK: link-info io-backend ( path -- info )

SYMBOL: +regular-file+
SYMBOL: +directory+
SYMBOL: +character-device+
SYMBOL: +block-device+
SYMBOL: +fifo+
SYMBOL: +symbolic-link+
SYMBOL: +socket+
SYMBOL: +unknown+

! File metadata
: stat ( path -- directory? permissions length modified )
    normalize-pathname (stat) ;

! : file-length ( path -- n ) stat drop 2nip ;

: file-modified ( path -- n ) stat >r 3drop r> ;

! : file-permissions ( path -- perm ) stat 2drop nip ;

: exists? ( path -- ? ) file-modified >boolean ;

! : directory? ( path -- ? ) stat 3drop ;

: directory? ( path -- ? ) file-info file-info-type +directory+ = ;

! Current working directory
HOOK: cd io-backend ( path -- )

HOOK: cwd io-backend ( -- path )
{'fraction_non_alphanumeric': 0.15997915581031788, 'fraction_numerical': 0.006253256904637832, 'mean_word_length': 2.6010006253908693, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '327641', 'n_tokens_mistral': 1947, 'n_tokens_neox': 1882, 'n_words': 506}