File: readdirall.dats

package info (click to toggle)
ats2-lang 0.1.3-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 28,136 kB
  • ctags: 20,441
  • sloc: ansic: 354,696; makefile: 2,679; sh: 638
file content (171 lines) | stat: -rw-r--r-- 2,954 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
(*
//
// It is a bit like [scandir] ...
//
*)

(* ****** ****** *)

(*
** Author: Hongwei Xi
** Authoremail: hwxi AT cs DOT bu DOT edu
** Time: May, 2013
*)

(* ****** ****** *)
//
#include
"share/atspre_staload.hats"
//
(* ****** ****** *)

staload
UN = "prelude/SATS/unsafe.sats"

(* ****** ****** *)

staload "libc/SATS/dirent.sats"
staload _ = "libc/DATS/dirent.dats"

(* ****** ****** *)

staload "libats/SATS/dynarray.sats"
staload _ = "libats/DATS/dynarray.dats"

(* ****** ****** *)

%{^
//
#undef ATSextfcall
#define ATSextfcall(fun, funarg) fun funarg
//
%} // end of [%{^]

(* ****** ****** *)

extern
fun{}
readdirall$pred (x: !Direntp1): bool
implement
readdirall$pred<> (x) = true

(* ****** ****** *)

extern
fun{}
readdirall (dirp: !DIRptr1): dynarray (Direntp1)

(* ****** ****** *)

implement{}
readdirall (dirp) = let
//
implement
readdirall$pred<> (x) = let
  val (
    fpf | str
  ) = direntp_get_d_name (x)
  val ans = (str != "." && str != "..")
  prval () = fpf (str)
in
  ans
end // end of [readdirall]
//
vtypedef elt = Direntp1
vtypedef res = dynarray (elt)
//
fun loop
(
  dirp: !DIRptr1, DA: res
) : res = let
  val entp = readdir_r_gc (dirp)
in
//
if direntp2ptr(entp) > 0 then let
  val ans = readdirall$pred (entp)
  val () =
  (
    if ans then
    {
      val-~None_vt () = dynarray_insert_atend_opt (DA, entp)
    } else
      direntp_free (entp)
    // end of [if]
  ) : void (* end of [val] *) 
in
  loop (dirp, DA)
end else let
  val () = direntp_free (entp) in DA
end (* end of [if] *)
//
end (* end of [loop] *)
//
val DA = dynarray_make_nil<elt> (i2sz(1024))
//
in
  loop (dirp, DA)
end // end of [readdirall]

(* ****** ****** *)

implement
main0 (argc, argv) =
{
//
val dname =
(
  if argc >= 2 then argv[1] else "."
) : string // end of [val]
//
val dirp = opendir_exn (dname)
//
val DA = readdirall (dirp)
//
implement(a:vtype)
dynarray_quicksort$cmp<a> (x, y) = let
  val x2 = $UN.castvwtp1{Direntp1}(x)
  val (xfpf | xstr) = direntp_get_d_name (x2)
  val y2 = $UN.castvwtp1{Direntp1}(y)
  val (yfpf | ystr) = direntp_get_d_name (y2)
  prval () = $UN.cast2void(x2)
  prval () = $UN.cast2void(y2)
  val sgn = compare (xstr, ystr)
  prval () = xfpf (xstr) and () = yfpf (ystr)
in
  sgn
end // end of [...]
val () = dynarray_quicksort (DA)
//
var n: size_t
val A = dynarray_getfree_arrayptr (DA, n)
//
val out = stdout_ref
//
implement{}
fprint_array$sep (out) = fprint_newline (out)
//
implement(a:vtype)
fprint_ref<a> (out, x) =
{
  val x2 =
    $UN.castvwtp1{Direntp1}(x)
  val (fpf | str) = direntp_get_d_name (x2)
  prval () = $UN.cast2void(x2)
  val () = fprint_strptr (out, str)
  prval () = fpf (str)
}
val (
) = fprint_arrayptr (out, A, n)
val () = fprint_newline (out)
//
implement(a:vtype)
array_uninitize$clear<a> (i, x) = direntp_free ($UN.castvwtp0(x))
val () = arrayptr_freelin (A, n)
//
val () = closedir_exn (dirp)
//
} (* end of [main0] *)

(* ****** ****** *)

(* end of [readdirall.dats] *)