File: input_plain.sml

package info (click to toggle)
smlsharp 4.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 123,732 kB
  • sloc: ansic: 16,725; sh: 4,347; makefile: 2,191; java: 742; haskell: 493; ruby: 305; cpp: 284; pascal: 256; ml: 255; lisp: 141; asm: 97; sql: 74
file content (48 lines) | stat: -rw-r--r-- 1,037 bytes parent folder | download | duplicates (2)
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
(**
 * input_plain.sml
 *
 * @copyright (C) 2021 SML# Development Team.
 * @author UENO Katsuhiro
 * @version $Id: input_plain.sml,v 1.2 2007/04/02 09:42:29 katsu Exp $
 *)

functor Input(
  val numSamples : int
) :> INPUT =
struct

  type input =
       Libc.c_file * bool ref
       
  fun openInput () =
      let
        val c_file = Libc.fdopen (0, "rb")
      in
        if Pointer.isNull c_file then raise Fail "fdopen" else ();
        (c_file, ref false)
      end

  fun startInput (_:input) = ()
        
  fun closeInput ((c_file, _):input) =
      Libc.fclose c_file

  fun fill ((c_file, eof):input) = false

  val buffer = Array.array (numSamples, 0w0)

  fun read ((c_file, eof):input) =
      let
        val n = if !eof
                then 0
                else Libc.fread (buffer, numSamples, c_file)
      in
        if n < numSamples
        then Array.modifyi (fn (i,x) => if i < n then x else 0w0) buffer
        else ();
        eof := n < numSamples
      end

  fun finished ((c_file, eof):input) = !eof

end