-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunkce-sideef.c
205 lines (155 loc) · 3.79 KB
/
funkce-sideef.c
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
#include <stdio.h>
#include <stdlib.h>
#include "error.h"
#include "execute.h"
#include "funkce.h"
#include "helpers.h"
#include "parser.h"
#include "hashovani.h"
#define TO_WRITE 1
#define TO_READ 2
static Hash *opened_files = NULL;
static int counter = 0;
static Hash *get_opened_files()
{
if (opened_files == NULL) opened_files = new_Hash();
return opened_files;
}
static t_point rw_func(const int mode)
{
static t_point w = NIL;
static t_point r = NIL;
if (mode == TO_READ && r == NIL)
r = make_Func(new_inner_Func(1, 0, f_read));
if (mode == TO_WRITE && w == NIL)
w = make_Func(new_inner_Func(2, 0, f_write));
return mode == TO_READ ? r : w;
}
// TODO prepsat tak, ze vrati seznam nazvu promenych = nutno vypsat print-string
t_point env(Cons *params)
{
Hash *h = get_basic_hash();
for (int i=0; i<h->size; i++) {
if (h->hashes[i].full != FULL_HASH) continue;
printf("\"%s\" ", h->hashes[i].name);
}
printf("\n");
return BOOL_TRUE;
}
/**
* Ladici funkce. Vypise parametry a vrati prvni.
*/
t_point f_dump(Cons *params)
{
Cons *l = params;
while (l != NULL) {
print_Symbol(l->a);
l = next(l);
}
return params->a;
}
static inline void to_output(Cons *la, FILE *fd)
{
while (la != NULL) {
la->a = resolve_Thunk(la->a);
if (!is_Num(la->a)) ERROR_RET(TYPE_ERROR);
putc(get_Num(la->a), fd);
la = next(la);
}
}
/**
* Vypise parametry od nichz pozaduje, aby byly stringy
* jako stringy.
*/
t_point f_print_string(Cons *params)
{
Cons *l = params;
Cons *la;
t_point s;
while (l != NULL) {
s = resolve_Thunk(l->a);
if (is_Cons(s)) {
la = get_Cons(s);
to_output(la, stdout);
} else
ERROR(TYPE_ERROR);
l = next(l);
}
return BOOL_TRUE;
}
static t_point open_file(t_point p, const int mode)
{
Cons *l = get_Cons(resolve_Thunk(p));
Hash *of = get_opened_files();
HashMember *hm;
FILE *f;
t_point hash;
char name[MAX_NAME_LENGTH]; // TODO jinak
char *c = name;
// nacteni jmena souboru
while ((c - name) < MAX_NAME_LENGTH && l) {
l->a = resolve_Thunk(l->a);
*c++ = get_Num(l->a);
l = next(l);
}
*c = '\0';
hash = hash_string(name);
if (mode == TO_READ)
// cist jeden soubor muzeme i vickrat najednou
hash = hash + (++counter)*8161;
else
if (get_Hash(of, hash)) ERROR(FILE_OPENED);
hash = p2n(hash) | NUMBER;
if (!(f = fopen(name, mode == TO_READ ? "r" : "w"))
|| !(hm = add_Hash(of, hash, (t_point) f)))
ERROR(CANNOT_OPEN_FILE);
if (mode == TO_READ) {
hm->name = malloc(sizeof(char) * MAX_NAME_LENGTH);
hm->name[0] = '\0';
}
return hash;
}
t_point read_open(Cons *params)
{
t_point fd = open_file(params->a, TO_READ);
return pnew_Cons(fd, pnew_Thunk(rw_func(TO_READ), new_List(fd)));
}
t_point write_open(Cons *params)
{
t_point fd = open_file(params->a, TO_WRITE);
return pnew_Cons(fd, pnew_Thunk(rw_func(TO_WRITE), new_List(fd)));
}
#define check_and_set_fd(s, hm) \
if (!is_Num((s) = resolve_Thunk(s))) ERROR(TYPE_ERROR); \
(hm) = get_Hash(get_opened_files(), (s)); \
if ((hm) == NULL) ERROR(FILE_NOT_OPENED);
t_point f_close(Cons *params)
{
HashMember *hm;
check_and_set_fd(params->a, hm);
fclose((FILE *) hm->link);
if (hm->name != NULL) free(hm->name);
del_HashMember(get_opened_files(), hm);
return BOOL_TRUE;
}
t_point f_read(Cons *params)
{
HashMember *hm;
check_and_set_fd(params->a, hm);
FILE *f = (FILE *) hm->link;
// hm->name docteno -> treba nacist dalsi
if (hm->info >= MAX_NAME_LENGTH || hm->name[hm->info] == '\0') {
hm->name[hm->info = 0] = 0;
if (fgets(hm->name, MAX_NAME_LENGTH, f) == NULL) return NIL;
}
return pnew_Cons(
make_Num(hm->name[hm->info++]),
pnew_Thunk(rw_func(TO_READ), new_List(hm->hash)));
}
t_point f_write(Cons *params)
{
HashMember *hm;
check_and_set_fd(params->a, hm);
to_output(get_Cons(resolve_Thunk(next(params)->a)), (FILE *) hm->link);
return BOOL_TRUE;
}