;; An AFile (abstract file) is ;; -- File, or ;; -- Directory ;; A File is structure with a name and a size. ;; (make-File string num) (define-struct File (name size)) ;; A Directory is ;; a name and a contents, which is a list of AFiles. ;; (make-Directory symbol list-of-AFile) (define-struct Directory (name contents)) ;; list-of-AFile (loAF) is ;; -- empty ;; -- (cons AFile loAF) #| ;; Template for AFile (define (f-AFile aFile ...) (cond [(File? aFile) (f-File aFile ...)] [(Directory? aFile) (f-Directory aFile ...)])) ;; Template for File (define (f-File file ...) (...(File-name file)...(File-size file)...)) ;; Template for Directory (define (f-Directory dir ...) (...(Directory-name dir)...(f-LoAF (Directory-contents dir))...)) ;; Template for List-of-AFile (define (f-LoAF loAF ...) (cond [(empty? loAF) ...] [(cons? loAF) (...(f-LoAF (first loAF))...(f-LoAF (rest loAF)))])) |# (define myFS (make-Directory "folder0" (list (make-File "file1" 100) (make-File "file2" 200) (make-Directory "folder1" (list (make-File "file3" 300) (make-Directory "folder1" (list (make-File "file3" 300))))) (make-File "file3" 400)))) ;; totalSize-AFile: AFile --> num ;; returns the total size of an AFile and all sub-AFile. (define (totalSize-AFile aFile) (cond [(File? aFile) (totalSize-File aFile )] [(Directory? aFile) (totalSize-Directory aFile)])) ;;totalSize-File: File --> num ;; Returns size the File. (define (totalSize-File file ) (File-size file)) ;; This is redundant -- could have called this in totalSize-AFile. ;; totalSize-Directory: Directory --> num ;; Returns the total size of all AFiles in the contents list. (define (totalSize-Directory dir) (totalSize-LoAF (Directory-contents dir))) ;; totalSize-LoAF: list-of-AFile --> num ;; Calculates the total size of all the AFiles in the list. (define (totalSize-LoAF loAF) (cond [(empty? loAF) 0 ] [(cons? loAF) (+ (totalSize-AFile (first loAF)) (totalSize-LoAF (rest loAF)))])) "totalSize-AFile test cases:" (= 1300 (totalSize-AFile myFS)) (= 300 (totalSize-AFile (make-File "zz" 300))) ;; find-AFile: AFile string --> list-of-string ;; Returns the full pathname of all aFiles found that ;; match the given name ;; Empty list returned if no matches (define (find-AFile aFile name) (cond [(File? aFile) (find-File aFile name)] [(Directory? aFile) (find-Directory aFile name)])) ;; find-File: AFile string --> list-of-string ;; Returns a list with the name of the file if it ;; matches the given name. ;; Empty list returned if no match (define (find-File file name) (cond [(equal? name (File-name file)) (list (File-name file))] [else empty])) ;; find-Directory: Directory string --> list-of-string ;; Returns a list of the full pathname of all AFiles found that ;; match the given name. Directorys have "[d]" after their names. ;; Empty list returned if no matches (define (find-Directory dir name) (append (cond [(equal? name (Directory-name dir)) (list (string-append (Directory-name dir) " [d]"))] [else empty]) (find-LoAF (Directory-contents dir) name (Directory-name dir)))) ;; find-LoAF: Dir string --> list-of-string ;; Returns a list of the full pathname of all AFiles found that ;; match the given name ;; Empty list returned if no matches (define (find-LoAF loAF name dir-name) (cond [(empty? loAF) empty] [(cons? loAF) (append (find-LoAF (rest loAF) name dir-name) (prepend-name dir-name (find-AFile (first loAF) name)))])) ;;prepend: string list-of-string --> list-of-string ;;pre-pends the given name string onto all the elements of the ;; given list of strings, with a "/" separator. (define (prepend-name name los) (cond [(empty? los) empty] [(cons? los) (cons (string-append name "/" (first los)) (prepend-name name (rest los)))])) "find-AFile test cases:" (equal? (list "folder0/file2") (find-AFile myFS "file2")) (equal? (list "folder0/file3" "folder0/folder1/folder1/file3" "folder0/folder1/file3") (find-AFile myFS "file3")) (equal? (list "folder0/folder1 [d]" "folder0/folder1/folder1 [d]") (find-AFile myFS "folder1")) (empty? (find-AFile myFS "zz")) (equal? (list "zz") (find-AFile (make-File "zz" 300) "zz"))