File: stream.c

package info (click to toggle)
marlais 0.6.4-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,192 kB
  • ctags: 2,068
  • sloc: ansic: 16,958; yacc: 1,150; lex: 870; makefile: 639; lisp: 248
file content (146 lines) | stat: -rw-r--r-- 3,466 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
/*

   stream.c

   This software is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This software 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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with this software; if not, write to the Free
   Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

   Original copyright notice follows:

   Copyright, 1993, Brent Benson.  All Rights Reserved.
   0.4 & 0.5 Revisions Copyright 1994, Joseph N. Wilson.  All Rights Reserved.

   Permission to use, copy, and modify this software and its
   documentation is hereby granted only under the following terms and
   conditions.  Both the above copyright notice and this permission
   notice must appear in all copies of the software, derivative works
   or modified version, and both notices must appear in supporting
   documentation.  Users of this software agree to the terms and
   conditions set forth in this notice.

 */

#include "stream.h"

#include "error.h"
#include "prim.h"

/* primitives */

static Object eof_object_p (Object obj);
static Object standard_input (void);
static Object standard_output (void);
static Object standard_error (void);

static struct primitive stream_prims[] =
{
    {"%open-input-file", prim_1, open_input_file},
    {"%open-output-file", prim_1, open_output_file},
    {"%close-stream", prim_1, close_stream},
    {"%eof-object?", prim_1, eof_object_p},
    {"%standard-input", prim_0, standard_input},
    {"%standard-output", prim_0, standard_output},
    {"%standard-error", prim_0, standard_error},
};

/* globals */
Object standard_input_stream;
Object standard_output_stream;
Object standard_error_stream;

void
init_stream_prims (void)
{
    int num;

    num = sizeof (stream_prims) / sizeof (struct primitive);

    init_prims (num, stream_prims);
}

Object
make_stream (enum streamtype type, FILE * fp)
{
    Object obj;

    obj = allocate_object (sizeof (struct stream));

    STREAMTYPE (obj) = Stream;
    STREAMSTYPE (obj) = type;
    STREAMFP (obj) = fp;
    return (obj);
}

Object
open_input_file (Object filename)
{
    FILE *fp;
    char *str;

    str = BYTESTRVAL (filename);
    fp = fopen (str, "r");
    if (!fp) {
	error ("open-input-file: could not open file", filename, NULL);
    }
    return (make_stream (Input, fp));
}

Object
open_output_file (Object filename)
{
    FILE *fp;
    char *str;

    str = BYTESTRVAL (filename);
    fp = fopen (str, "w");
    if (!fp) {
	error ("open-output-file: could not open file", filename, NULL);
    }
    return (make_stream (Output, fp));
}

Object
close_stream (Object stream)
{
    fclose (STREAMFP (stream));
    return (unspecified_object);
}

static Object
eof_object_p (Object obj)
{
    if (obj == eof_object) {
	return (true_object);
    } else {
	return (false_object);
    }
}

static Object
standard_input (void)
{
    return (standard_input_stream);
}

static Object
standard_output (void)
{
    return (standard_output_stream);
}

static Object
standard_error (void)
{
    return (standard_error_stream);
}