File: readdirall.dats

package info (click to toggle)
ats2-lang 0.4.2-3
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 40,500 kB
  • sloc: ansic: 389,898; makefile: 7,136; javascript: 1,852; lisp: 811; sh: 657; php: 573; python: 387; perl: 365
file content (193 lines) | stat: -rw-r--r-- 3,363 bytes parent folder | download | duplicates (4)
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
//usr/bin/env myatscc "$0"; exit
(* ****** ****** *)
(*
//
// It is a bit like [scandir] ...
//
*)
(* ****** ****** *)
//
(*
##myatsccdef=\
patsopt --constraint-ignore --dynamic $1 | \
tcc - -run -DATS_MEMALLOC_LIBC -I${PATSHOME} -I${PATSHOME}/ccomp/runtime -L${PATSHOME}/ccomp/atslib/lib -latslib
*)
//
(* ****** ****** *)

(*
** 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 "libats/SATS/dynarray.sats"
staload _ = "libats/DATS/dynarray.dats"

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

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

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

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

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

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

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

implement
{}(*tmp*)
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) = sgn where
{
  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)
} (* end of [...] *)
//
val () =
  dynarray_quicksort<Direntp1> (DA)
//
var n: size_t
val A0 = dynarray_getfree_arrayptr (DA, n)
//
val out = stdout_ref
//
implement
{}(*tmp*)
fprint_array$sep
  (out) = fprint_newline (out)
//
implement
(a:vtype)
fprint_ref<a> (out, x) =
{
  val x2 =
    $UN.castvwtp1{Direntp1}(x)
  // end of [val]
  val (fpf | str) = direntp_get_d_name (x2)
  prval () = $UN.cast2void(x2)
  val ((*void*)) = fprint_strptr (out, str)
  prval ((*void*)) = fpf (str)
}
//
val () =
  fprint_arrayptr (out, A0, n)
//
val () = fprint_newline (out)
//
implement(a:vtype)
array_uninitize$clear<a> (i, x) = direntp_free ($UN.castvwtp0(x))
val () = arrayptr_freelin (A0, n)
//
val () = closedir_exn (dirp)
//
} (* end of [main0] *)

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

(* end of [readdirall.dats] *)