Advertisement
Guest User

Memory-clean file counting in ATS

a guest
Dec 6th, 2017
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. #include "share/atspre_staload.hats"
  2. #include "libats/libc/DATS/dirent.dats"
  3. #include "libats/ML/DATS/filebas_dirent.dats"
  4.  
  5. staload "libats/ML/DATS/string.dats"
  6.  
  7. fun
  8. bad_dir(next: string) : bool =
  9. case next of
  10. | "." => true | ".." => true | _ => false
  11.  
  12. fun
  13. step_stream
  14. (file: string, acc: int) : int =
  15. (
  16. if
  17. test_file_isdir(file) = 1
  18. then flow_stream(file, acc) else acc + 1
  19. )
  20.  
  21. and
  22. step_stream_
  23. (file: Strptr1, acc: int): int =
  24. let
  25. val acc =
  26. step_stream
  27. ($UN.strptr2string(file), acc) in free(file); acc
  28. end // end of [step_stream_]
  29.  
  30. and
  31. flow_stream
  32. (dir: string, acc: int) : int =
  33. let
  34. val
  35. files =
  36. streamize_dirname_fname(dir)
  37. val
  38. files =
  39. $UN.castvwtp0
  40. {stream_vt(Strptr1)}(files)
  41. // end of [val]
  42. in
  43. fold_stream(dir, files, acc)
  44. end
  45.  
  46. and
  47. fold_stream
  48. ( dir: string
  49. , files: stream_vt(Strptr1), acc: int): int =
  50. (
  51. case+ !files of
  52. | ~stream_vt_nil
  53. ((*void*)) => acc
  54. | ~stream_vt_cons
  55. (file, files) =>
  56. if
  57. bad_dir
  58. ($UN.strptr2string(file))
  59. then let
  60. val () = free(file)
  61. in
  62. fold_stream(dir, files, acc)
  63. end
  64. else let
  65. val
  66. dir_file =
  67. string0_append3
  68. ( dir, "/"
  69. , $UN.strptr2string(file))
  70. val acc =
  71. step_stream_(dir_file, acc)
  72. val () = free(file)
  73. in
  74. fold_stream(dir, files, acc)
  75. end // end of [stream_vt_con]
  76. )
  77.  
  78. implement main0() = println! ("step_stream(.) = ", step_stream(".", 0))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement