Debian Codegraph @

src/c/file.d

raw
1 /* -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /* vim: set filetype=c tabstop=2 shiftwidth=2 expandtab: */
3
4 /*
5 * file.d - file interface
6 *
7 * Copyright (c) 1984 Taiichi Yuasa and Masami Hagiya
8 * Copyright (c) 1990 Giuseppe Attardi
9 * Copyright (c) 2001 Juan Jose Garcia Ripoll
10 *
11 * See file 'LICENSE' for the copyright details.
12 *
13 */
14
15 /* -- imports --------------------------------------------------------------- */
16
17 #include <ecl/ecl.h>
18 #include <ecl/internal.h>
19
20 /* -- File opening and closing ---------------------------------------------- */
21
22 cl_fixnum
23 ecl_normalize_stream_element_type(cl_object element_type)
24 {
25 cl_fixnum sign = 0;
26 cl_index size;
27 if (element_type == @'signed-byte' || element_type == @'ext::integer8') {
28 return -8;
29 } else if (element_type == @'unsigned-byte' || element_type == @'ext::byte8') {
30 return 8;
31 }
32 #ifdef ecl_uint16_t
33 else if (element_type == @'ext::integer16') {
34 return -16;
35 } else if (element_type == @'ext::byte16') {
36 return 16;
37 }
38 #endif
39 #ifdef ecl_uint32_t
40 else if (element_type == @'ext::integer32') {
41 return -32;
42 } else if (element_type == @'ext::byte32') {
43 return 32;
44 }
45 #endif
46 #ifdef ecl_uint64_t
47 else if (element_type == @'ext::integer64') {
48 return -64;
49 } else if (element_type == @'ext::byte64') {
50 return 64;
51 }
52 #endif
53 else if (element_type == @':default') {
54 return 0;
55 } else if (element_type == @'base-char' || element_type == @'character') {
56 return 0;
57 } else if (_ecl_funcall3(@'subtypep', element_type, @'character') != ECL_NIL) {
58 return 0;
59 } else if (_ecl_funcall3(@'subtypep', element_type, @'unsigned-byte') != ECL_NIL) {
60 sign = +1;
61 } else if (_ecl_funcall3(@'subtypep', element_type, @'signed-byte') != ECL_NIL) {
62 sign = -1;
63 } else {
64 FEerror("Not a valid stream element type: ~A", 1, element_type);
65 }
66 if (CONSP(element_type)) {
67 if (CAR(element_type) == @'unsigned-byte')
68 return ecl_to_size(cl_cadr(element_type));
69 if (CAR(element_type) == @'signed-byte')
70 return -ecl_to_size(cl_cadr(element_type));
71 }
72 for (size = 8; 1; size++) {
73 cl_object type;
74 type = cl_list(2, sign>0? @'unsigned-byte' : @'signed-byte',
75 ecl_make_fixnum(size));
76 if (_ecl_funcall3(@'subtypep', element_type, type) != ECL_NIL) {
77 return size * sign;
78 }
79 }
80 FEerror("Not a valid stream element type: ~A", 1, element_type);
81 }
82
83 @(defun open (filename
84 &key (direction @':input')
85 (element_type @'character')
86 (if_exists ECL_NIL iesp)
87 (if_does_not_exist ECL_NIL idnesp)
88 (external_format @':default')
89 (cstream ECL_T)
90 (close_on_exec ECL_T)
91 (nonblock ECL_NIL)
92 &aux strm)
93 enum ecl_smmode smm;
94 int flags = 0;
95 cl_fixnum byte_size;
96 @
97 /* INV: ecl_open_stream() checks types */
98 if (direction == @':input') {
99 smm = ecl_smm_input;
100 if (!idnesp)
101 if_does_not_exist = @':error';
102 } else if (direction == @':output') {
103 smm = ecl_smm_output;
104 if (!iesp)
105 if_exists = @':new_version';
106 if (!idnesp) {
107 if (if_exists == @':overwrite' ||
108 if_exists == @':append')
109 if_does_not_exist = @':error';
110 else
111 if_does_not_exist = @':create';
112 }
113 } else if (direction == @':io') {
114 smm = ecl_smm_io;
115 if (!iesp)
116 if_exists = @':new_version';
117 if (!idnesp) {
118 if (if_exists == @':overwrite' ||
119 if_exists == @':append')
120 if_does_not_exist = @':error';
121 else
122 if_does_not_exist = @':create';
123 }
124 } else if (direction == @':probe') {
125 smm = ecl_smm_probe;
126 if (!idnesp)
127 if_does_not_exist = ECL_NIL;
128 } else {
129 FEerror("~S is an illegal DIRECTION for OPEN.",
130 1, direction);
131 }
132 byte_size = ecl_normalize_stream_element_type(element_type);
133 if (byte_size != 0) {
134 external_format = ECL_NIL;
135 }
136 if (!Null(cstream)) {
137 flags |= ECL_STREAM_C_STREAM;
138 }
139 if (!Null(close_on_exec)) {
140 flags |= ECL_STREAM_CLOSE_ON_EXEC;
141 }
142 if (!Null(nonblock)) {
143 flags |= ECL_STREAM_NONBLOCK;
144 }
145 strm = ecl_open_stream(filename, smm, if_exists, if_does_not_exist,
146 byte_size, flags, external_format);
147 @(return strm);
148 @)
149
150 @(defun close (strm &key (abort ECL_NIL))
151 @
152 @(return ecl_stream_dispatch_table(strm)->close(strm));
153 @)