( DiskIOequns)
( 9th July 2015)

CR ." DiskIOequns: "

: show ( low addr, top addr ..)
 SWAP  HEX DO I CR DUP . SPACE C@ DUP . SPACE EMIT LOOP CR DECIMAL ; 
: doshow ( low addr,#bytes....)
  OVER + show ;

CODE OSGBPB8   ( r0,r2,r3,r4...r3,r4)
 R4 POP,
 R3 POP,
 R2 POP,
 R0 POP,
 OS_GBPB SWI,
 R3 PUSH,
 R4 PUSH,
PC R14 MOV,
END-CODE

CODE OSGBPB9   ( r0,r1,r2,r3,r4,r5.r6...r3,r4)
 R6 POP,       ( pointer to name to match)
 R5 POP,       ( results buffer length)
 R4 POP,       ( offset, usually 0)
 R3 POP,       ( no. of object names to be read)
 R2 POP,       ( pointer to results buffer)
 R1 POP,       ( pointer to directory name)
 R0 POP,       ( 9)
 OS_GBPB SWI,
 R3 PUSH,      ( number of objects read)
 R4 PUSH,      ( offset, or -1 if finished)
PC R14 MOV,
END-CODE

( : do_oscli)  ( addr of command...)  ( eg CDIR fred)
   ( CR DUP .STR KEY DROP)
  ( DUP 4+ SWAP LEN OSCLI ; )    ( ****)

( : cat cat$ do_oscli ;)   ( ****)

( : daydir    obj$ prefday$ STRCPY  obj$ day$ STRCAT ;
: yrpath    parent$ path$ STRCPY  parent$ dot$ STRCAT parent$ year$  STRCAT ;
: mnthpath  yrpath   parent$ dot$ STRCAT parent$  month$  STRCAT ;
: daypath   mnthpath parent$ dot$ STRCAT daydir parent$  obj$ STRCAT ; )
 
( select directory given by path$:)
( complete path...)
: todir  >R cmnd$ DUP DUP seldir$  STRCPY   ( cmnd$,cmnd$...)
         R> STRCAT                          ( cmnd$...)
         do_oscli ; ( Works OK)
 
( create directory dir$ - from parent directory:)
: cdir   cmnd$ DUP DUP cdir$ STRCPY    dir$ STRCAT   do_oscli ;  ( Works OK)

( get date directory names:)
: dirnames   ( max no. to read ... no. read)
  2044 0 DO 0 dir_buff% I + ! 4 +LOOP
  DUP     ( max no.)
  8 dir_buff% ROT 0 OSGBPB8    ( no. unread,next offset or -1, ...)
  DROP  -
  ( CR DUP ." no. read=" .)
;

: do_getname ( ...)
     ptr @ C@ DUP nchars !   ( no. chars...)
     obj$ C!                 ( nchars in 1st byte)
     ptr @ 1+                ( start of text in Dir_buff%)
     obj$ 4 +                ( start of text on word boundary for Forth)  
     nchars @                ( no. chars...)    
     CMOVE
     nchars @ 4 +      ( no. chars from start to put trailing zero)
     obj$ +
     0 SWAP C! ;

: show_names    ( no. objects...)
  dir_buff% ptr !
  1 DO
     I 5 /MOD DROP 0= IF CR ENDIF       ( tabulate display - 5 wide)
     do_getname 
     I . ." - " obj$ .STR 2 SPACES     ( display name)
     ptr @   nchars @ 1+  +  ptr !  ( point to next #chars)
    LOOP ;

: objs obj$ 16 doshow ;

: read_nth ( no. found,n...)  ( display nth filename found)
  2DUP < IF DROP DROP QUIT
         ELSE
          dir_buff% ptr !
          SWAP DROP DUP 1 =         ( n,flg...)
          IF DROP do_getname ( CR obj$ .STR)     ( display name)
          ELSE
            dir_buff% ptr !
          1-  0 DO
             ptr @ C@ DUP nchars !          ( no. chars...)
             ptr @   nchars @ 1+  +  ptr !  ( point to next #chars)
             DROP
            LOOP
            do_getname  ( CR obj$ .STR)     ( display name)
          ENDIF
         ENDIF ;

( words for checking & creating directories for tracking database:)

( does directory dir$ exist at path$?)
: qdirexist  ( parent path,no. to be read...flg)  ( flg=0 or 1)
   255 0 DO 0 dir_buff% I + ! 4 +LOOP 
   >R >R      ( save no. to be read & parent path)
   9
   R> 4+      ( parent directory)
   dir_buff%  ( results buffer)
   R>         ( no. to be read restored)
   0          ( offset)
   256        ( buffer length)
   dir$ 4+    ( pointer to name to be matched)
   OSGBPB9
   DROP ;

: store_word  ( handle,n...)
  2DUP        255 AND BPUT#1
  2DUP 8 >>   255 AND BPUT#1
  2DUP 16 >>  255 AND BPUT#1
       24 >>  255 AND BPUT#1 ;

: read_word  ( handle...h,n)
  DUP BGET#  OVER                ( h,b1,h...)
      BGET#  8  <<  OR OVER      ( h,b1+b2,h...)
      BGET#  16 <<  OR OVER      ( h,b1+b2+b3,h...)
      BGET#  24 <<  OR ;         ( ...h,n)


