/usr/lib/ats-anairiats-0.2.11/prelude/DATS/filebas.dats is in ats-lang-anairiats 0.2.11-1build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 | (***********************************************************************)
(* *)
(* Applied Type System *)
(* *)
(* Hongwei Xi *)
(* *)
(***********************************************************************)
(*
** ATS - Unleashing the Potential of Types!
** Copyright (C) 2002-2008 Hongwei Xi, Boston University
** All rights reserved
**
** ATS is free software; you can redistribute it and/or modify it under
** the terms of the GNU LESSER GENERAL PUBLIC LICENSE as published by the
** Free Software Foundation; either version 2.1, or (at your option) any
** later version.
**
** ATS is distributed in the hope that it will be useful, but WITHOUT ANY
** WARRANTY; without even the implied warranty of MERCHANTABILITY or
** FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
** for more details.
**
** You should have received a copy of the GNU General Public License
** along with ATS; see the file COPYING. If not, please write to the
** Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
** 02110-1301, USA.
*)
(* ****** ****** *)
(* author: Hongwei Xi (hwxi AT cs DOT bu DOT edu) *)
(* ****** ****** *)
//
// HX-2010-03-14: Here are some basic IO operations which
// are mostly for prototype implementation. If something more
// efficient is needed, please use the functions in declared
// in the following file: libc/SATS/stdio.sats
//
(* ****** ****** *)
#define ATS_DYNLOADFLAG 0 // no initialization is needed
(* ****** ****** *)
%{^
#include <libc/CATS/stdio.cats>
#include <libc/sys/CATS/stat.cats>
%} // end of [%{#]
(* ****** ****** *)
staload "prelude/SATS/filebas.sats"
(* ****** ****** *)
(*
//
// HX: implemented in [prelude/DATS/basics.dats]
//
implement file_mode_lte_r_r = file_mode_lte_refl {r} ()
implement file_mode_lte_w_w = file_mode_lte_refl {w} ()
implement file_mode_lte_rw_rw = file_mode_lte_refl {rw} ()
*)
(* ****** ****** *)
%{^
typedef struct {
ats_char_type atslab_0 ; ats_ptr_type atslab_1 ;
} *charlst ; // end of [typedef]
ATSinline()
ats_ptr_type
string_make_charlst_rev
(ats_int_type sz, ats_ptr_type cs) {
char *s0, *s ; charlst cs_next ;
s0 = (char*)ATS_MALLOC (sz + 1) ; s = s0 + sz ;
*s-- = '\0' ;
while (cs != (charlst)0) {
*s-- = ((charlst)cs)->atslab_0 ;
cs_next = ((charlst)cs)->atslab_1 ;
ATS_FREE (cs) ; cs = cs_next ;
} // end of [while]
return s0 ;
} /* string_make_charlst_rev */
%} // end of [%{^]
(* ****** ****** *)
#define i2c char_of_int
macdef EOF = $extval (int, "EOF")
(* ****** ****** *)
extern
fun feof0 (f: FILEref):<> int = "mac#atslib_feof"
extern
fun fgetc0_err (fil: FILEref):<> int = "mac#atslib_fgetc_err"
// end of [fgetc0_err]
extern
fun fgetc1_err
{m:file_mode} {l:addr} (
pf_mod: file_mode_lte (m, r) | fil: &FILE m
) :<> int = "mac#atslib_fgetc_err"
extern
fun fclose0_exn (fil: FILEref):<!exn> void = "atslib_fclose_exn"
// end of [fclose0_exn]
extern
fun fclose1_exn {m:file_mode} {l:addr}
(pf_fil: FILE m @ l | p_fil: ptr l):<!exn> void = "atslib_fclose_exn"
// end of [fclose1_exn]
(* ****** ****** *)
extern
fun string_make_charlst_rev
{n:nat} (sz: int n, cs: list_vt (char, n)):<> strptr1
= "string_make_charlst_rev"
// end of [string_make_charlst_rev]
(* ****** ****** *)
//
// HX: if the last character is '\n', it is dropped
//
implement
input_line_vt (fil) = let
fun loop {n:nat} (
fil: FILEref, n: int n, cs: list_vt (char, n)
) : strptr0 = let
val c = fgetc0_err (fil)
in
if c <> EOF then let
val c = i2c c
in
if (c <> '\n') then
loop (fil, n+1, list_vt_cons (c, cs))
else
string_make_charlst_rev (n, cs) // linear string
// end of [if]
end else begin
if n = 0 then let
val+ ~list_vt_nil () = cs in strptr_null ()
end else
string_make_charlst_rev (n, cs) // linear string
// end of [if]
end // end of [if]
end // end of [loop]
in
loop (fil, 0, list_vt_nil ())
end // end of [input_line_vt]
implement
input_line (fil) = let
val str = input_line_vt (fil) in stropt_of_strptr (str)
end // end of [input_line]
(* ****** ****** *)
extern
fun fputc0_exn
(c: char, fil: FILEref):<!exn> void = "atslib_fputc_exn"
// end of [fputc0_exn]
extern
fun fputs0_exn
(str: string, fil: FILEref):<!exn> void = "atslib_fputs_exn"
// end of [fputs0_exn]
extern
fun fflush0_exn (fil: FILEref):<!exn> void = "atslib_fflush_exn"
// end of [fflush0_exn]
(* ****** ****** *)
//
// HX: the character '\n' is added at the end
//
implement
output_line (fil, line) = (
fputs0_exn (line, fil); fputc0_exn ('\n', fil); fflush0_exn (fil)
) // end of [output_line]
(* ****** ****** *)
local
//
staload "libc/sys/SATS/stat.sats"
staload T = "libc/sys/SATS/types.sats"
//
macdef u2m = $T.mode_of_uint
macdef m2u = $T.uint_of_mode
//
in // in of [local]
implement
test_file_exists (path) = let
var st: stat?
val ret = stat_err (path, st)
in
if ret = 0 then let
prval () = opt_unsome {stat} (st) in true
end else let
prval () = opt_unnone {stat} (st) in false
end (* end of [if] *)
end // end of [test_file_fun]
(* ****** ****** *)
implement
test_file_mode
(path, f) = let
var st: stat? ; val ret = stat_err (path, st)
in
if ret = 0 then let
prval () = opt_unsome {stat} (st)
in
if f (m2u(st.st_mode)) then 1 else 0
end else let
prval () = opt_unnone {stat} (st) in ~1 (*error*)
end (* end of [if] *)
end // end of [test_file_mode]
(* ****** ****** *)
implement test_file_isblk
(path) = test_file_mode (path, lam x => S_ISBLK (u2m(x)))
// end of [test_file_isblk]
implement test_file_ischr
(path) = test_file_mode (path, lam x => S_ISCHR (u2m(x)))
// end of [test_file_ischr]
implement test_file_isdir
(path) = test_file_mode (path, lam x => S_ISDIR (u2m(x)))
// end of [test_file_isdir]
implement test_file_isfifo
(path) = test_file_mode (path, lam x => S_ISFIFO (u2m(x)))
// end of [test_file_isfifo]
implement test_file_isreg
(path) = test_file_mode (path, lam x => S_ISREG (u2m(x)))
// end of [test_file_isreg]
(* ****** ****** *)
implement
test_file_isemp (path) = let
var st: stat? ; val ret = lstat_err (path, st)
in
if ret = 0 then let
prval () = opt_unsome {stat} (st)
in
if $T.lint_of_off (st.st_size) = 0L then 1 else 0
end else let
prval () = opt_unnone {stat} (st) in ~1 (*error*)
end (* end of [if] *)
end // end of [test_file_isemp]
implement
test_file_islnk (path) = let
var st: stat? ; val ret = lstat_err (path, st)
in
if ret = 0 then let
prval () = opt_unsome {stat} (st)
in
if S_ISLNK (st.st_mode) then 1 else 0
end else let
prval () = opt_unnone {stat} (st) in ~1 (*error*)
end (* end of [if] *)
end // end of [test_file_islnk]
end // end of [local]
(* ****** ****** *)
local
#define i2ssz ssize1_of_int1
#define sz2ssz ssize1_of_size1
viewtypedef res = List_vt (char)
fun loop {n:int} (
fil: FILEref, res: &res? >> res, n: ssize_t n
) : void = let
in
//
if n != (i2ssz)0 then let
val i = fgetc0_err (fil)
in
if (i >= 0) then let
val c = char_of_int (i)
val () = res :=
list_vt_cons {char}{0} (c, ?)
val+ list_vt_cons (_, !p_res) = res
val () = loop (fil, !p_res, n-(i2ssz)1)
prval () = fold@ (res)
in
// nothing
end else
res := list_vt_nil ()
// end of [if]
end else (
res := list_vt_nil ()
) // end of [if]
//
end // end of [loop]
in // in of [local]
implement
char_list_vt_make_file
(fil) = let
//
var res: res
val () = loop (fil, res, i2ssz(~1))
//
in
res
end // end of [char_list_vt_make_file]
implement
char_list_vt_make_file_len
(fil, n) = let
//
var res: res
val n = size1_of_size (n)
val () = loop (fil, res, (sz2ssz)n)
//
in
res
end // end of [char_list_vt_make_file_len]
end // end of [local]
(* ****** ****** *)
implement
char_stream_make_file
(fil) = $delay (let
val c = fgetc0_err (fil)
in
if c <> EOF then let
val c = i2c c in
stream_cons (c, char_stream_make_file fil)
end else begin
let val () = fclose0_exn (fil) in stream_nil () end
end // end of [if]
end : stream_con char
) (* end of [char_stream_make_file] *)
(* ****** ****** *)
implement
line_stream_make_file
(fil) = $delay (let
val line = $effmask_ref (input_line fil)
in
if stropt_is_some line then let
val line = stropt_unsome line in
stream_cons (line, line_stream_make_file fil)
end else let
val () = fclose0_exn fil in stream_nil ()
end // end of [if]
end : stream_con string // end of [let]
) (* end of [line_stream_make_file] *)
(* ****** ****** *)
implement
char_stream_vt_make_file
{m} {l} (
pf_mod, pf_fil | p_fil
) = $ldelay (
let
val c = fgetc1_err (pf_mod | !p_fil)
in
if c >= 0 then let // c <> EOF
val c = char_of_int (c)
in
stream_vt_cons (
c, char_stream_vt_make_file (pf_mod, pf_fil | p_fil)
) // end of [stream_vt_cons]
end else let
val () = fclose1_exn (pf_fil | p_fil) in stream_vt_nil ()
end (* end of [if] *)
end : stream_vt_con char
,
fclose1_exn (pf_fil | p_fil) // HX: for cleanup
) // end of [char_stream_vt_make_file]
(* ****** ****** *)
implement
line_stream_vt_make_file
{m} {l} (pf_mod, pf_fil | p_fil) = let
//
fun loop {n:nat} (
pf_fil: FILE m @ l
| p_fil: ptr l, n: int n, cs: list_vt (char, n)
) :<!laz> stream_vt_con (strptr1) = let
val c = fgetc1_err (pf_mod | !p_fil)
in
if c >= 0 then let
val c = char_of_int (c) // c <> EOF
in
if c <> '\n' then
loop (pf_fil | p_fil, n+1, list_vt_cons (c, cs))
else let
val line = string_make_charlst_rev (n, cs) in
stream_vt_cons (
line, line_stream_vt_make_file (pf_mod, pf_fil | p_fil)
) // end of [stream_vt_cons]
end // end of [if]
end else let
val () = fclose1_exn (pf_fil | p_fil)
in
if n > 0 then let
val line = string_make_charlst_rev (n, cs) in
stream_vt_cons (line, $ldelay stream_vt_nil)
end else let
val+ ~list_vt_nil () = cs in stream_vt_nil ()
end // end of [if]
end (* end of [if] *)
end (* end of [loop] *)
//
in
//
$ldelay (
loop (
pf_fil | p_fil, 0, list_vt_nil ()
) // end of [loop]
, // HX: separator
fclose1_exn (pf_fil | p_fil) // HX: for cleanup
) // end of [$ldelay]
//
end // end of [char_stream_vt_make_file]
(* ****** ****** *)
(* end of [filebas.dats] *)
|