Paste: dir-tbl (treat a directory as an assoc)

Author: dharmatech
Mode: factor
Date: Mon, 6 Oct 2008 01:46:23
Plain Text |
USING: kernel arrays sequences accessors assocs
       prettyprint io.files io.encodings.utf8
       eval locals ;

IN: dir-tbl

! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

TUPLE: dir-tbl path ;

! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

:: dir-tbl-at* ( KEY TBL -- val ? )
   KEY  TBL path>> directory keys  member?
     [
       TBL path>>  KEY  append-path
       utf8 file-contents
       t
     ]
     [ f f ]
   if ;

: dir-tbl-size ( tbl -- n ) path>> directory length ;

:: dir-tbl>alist ( TBL -- assoc )
   TBL path>> directory keys
   [| KEY | KEY   KEY TBL dir-tbl-at* drop   2array ]
   map ;

:: dir-tbl-set-at ( VAL KEY TBL -- )
   VAL
   TBL path>> KEY append-path
   utf8
   set-file-contents ;

:: dir-tbl-delete-at ( KEY TBL -- ) TBL path>> KEY append-path delete-file ;

: dir-tbl-clear-assoc ( tbl -- ) path>> directory* keys [ delete-file ] each ;
   
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

INSTANCE: dir-tbl assoc

M: dir-tbl at*         ( key dir-tbl -- val/f ? ) dir-tbl-at* ;
M: dir-tbl assoc-size  ( dir-tbl -- n )           dir-tbl-size ;
M: dir-tbl >alist      ( dir-tbl -- alist )       dir-tbl>alist ;
M: dir-tbl set-at      ( val key tbl -- )         dir-tbl-set-at ;
M: dir-tbl delete-at   ( key tbl -- )             dir-tbl-delete-at ;
M: dir-tbl clear-assoc ( tbl -- )                 dir-tbl-clear-assoc ;

New Annotation

Summary:
Author:
Mode:
Body: