forked from gabordemooij/citrine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile.c
443 lines (425 loc) · 12.5 KB
/
file.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
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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdarg.h>
#include <math.h>
#include <unistd.h>
#include <stdint.h>
#include <time.h>
#include "citrine.h"
#include "siphash.h"
/**
* File
*
* Represents a File object.
* Creates a new file object based on the specified path.
*
* Usage:
*
* File new: '/example/path/to/file.txt'.
*/
ctr_object* ctr_file_new(ctr_object* myself, ctr_argument* argumentList) {
ctr_object* s = ctr_object_make(myself, argumentList);
ctr_object* pathObject;
s->info.type = CTR_OBJECT_TYPE_OTOBJECT;
s->link = myself;
s->value.rvalue = NULL;
pathObject = ctr_internal_create_object(CTR_OBJECT_TYPE_OTSTRING);
pathObject->info.type = CTR_OBJECT_TYPE_OTSTRING;
pathObject->value.svalue = (ctr_string*) malloc(sizeof(ctr_string));
pathObject->value.svalue->value = (char*) malloc(sizeof(char) * argumentList->object->value.svalue->vlen);
memcpy(pathObject->value.svalue->value, argumentList->object->value.svalue->value, argumentList->object->value.svalue->vlen);
pathObject->value.svalue->vlen = argumentList->object->value.svalue->vlen;
ctr_internal_object_add_property(s, ctr_build_string("path",4), pathObject, 0);
return s;
}
/**
* [File] path
*
* Returns the path of a file.
*/
ctr_object* ctr_file_path(ctr_object* myself, ctr_argument* argumentList) {
ctr_object* path = ctr_internal_object_find_property(myself, ctr_build_string("path",4), 0);
if (path == NULL) return CtrStdNil;
return path;
}
/**
* [File] read
*
* Reads contents of a file.
*/
ctr_object* ctr_file_read(ctr_object* myself, ctr_argument* argumentList) {
ctr_object* path = ctr_internal_object_find_property(myself, ctr_build_string("path",4), 0);
ctr_object* str;
ctr_size vlen, fileLen;
char* pathString;
char *buffer;
FILE* f;
if (path == NULL) return CtrStdNil;
vlen = path->value.svalue->vlen;
pathString = malloc(vlen + 1);
memcpy(pathString, path->value.svalue->value, vlen);
memcpy(pathString+vlen,"\0",1);
f = fopen(pathString, "rb");
free(pathString);
if (!f) {
CtrStdError = ctr_build_string_from_cstring("Unable to open file.\0");
return CtrStdNil;
}
fseek(f, 0, SEEK_END);
fileLen=ftell(f);
fseek(f, 0, SEEK_SET);
buffer=(char *)malloc(fileLen+1);
if (!buffer){
printf("Out of memory\n");
fclose(f);exit(1);
}
fread(buffer, fileLen, 1, f);
fclose(f);
str = ctr_build_string(buffer, fileLen);
free(buffer);
return str;
}
/**
* [File] write: [String]
*
* Writes content to a file.
*/
ctr_object* ctr_file_write(ctr_object* myself, ctr_argument* argumentList) {
ctr_object* str = ctr_internal_cast2string(argumentList->object);
ctr_object* path = ctr_internal_object_find_property(myself, ctr_build_string("path",4), 0);
FILE* f;
ctr_size vlen;
char* pathString;
if (path == NULL) return CtrStdNil;
vlen = path->value.svalue->vlen;
pathString = malloc(vlen + 1);
memcpy(pathString, path->value.svalue->value, vlen);
memcpy(pathString+vlen,"\0",1);
f = fopen(pathString, "wb+");
free(pathString);
if (!f) {
CtrStdError = ctr_build_string_from_cstring("Unable to open file.\0");
return CtrStdNil;
}
fwrite(str->value.svalue->value, sizeof(char), str->value.svalue->vlen, f);
fclose(f);
return myself;
}
/**
* [File] append: [String]
*
* Appends content to a file.
*/
ctr_object* ctr_file_append(ctr_object* myself, ctr_argument* argumentList) {
ctr_object* str = ctr_internal_cast2string(argumentList->object);
ctr_object* path = ctr_internal_object_find_property(myself, ctr_build_string("path",4), 0);
ctr_size vlen;
char* pathString;
FILE* f;
if (path == NULL) return myself;
vlen = path->value.svalue->vlen;
pathString = malloc(vlen + 1);
memcpy(pathString, path->value.svalue->value, vlen);
memcpy(pathString+vlen,"\0",1);
f = fopen(pathString, "ab+");
free(pathString);
if (!f) {
CtrStdError = ctr_build_string_from_cstring("Unable to open file.\0");
return CtrStdNil;
}
fwrite(str->value.svalue->value, sizeof(char), str->value.svalue->vlen, f);
fclose(f);
return myself;
}
/**
* [File] exists
*
* Returns True if the file exists and False otherwise.
*/
ctr_object* ctr_file_exists(ctr_object* myself, ctr_argument* argumentList) {
ctr_object* path = ctr_internal_object_find_property(myself, ctr_build_string("path",4), 0);
ctr_size vlen;
char* pathString;
FILE* f;
int exists;
if (path == NULL) return ctr_build_bool(0);
vlen = path->value.svalue->vlen;
pathString = malloc(vlen + 1);
memcpy(pathString, path->value.svalue->value, vlen);
memcpy(pathString+vlen,"\0",1);
f = fopen(pathString, "r");
free(pathString);
exists = (f != NULL );
if (f) {
fclose(f);
}
return ctr_build_bool(exists);
}
/**
* [File] include
*
* Includes the file as a piece of executable code.
*/
ctr_object* ctr_file_include(ctr_object* myself, ctr_argument* argumentList) {
ctr_object* path = ctr_internal_object_find_property(myself, ctr_build_string("path",4), 0);
ctr_tnode* parsedCode;
ctr_size vlen;
char* pathString;
char* prg;
if (path == NULL) return myself;
vlen = path->value.svalue->vlen;
pathString = malloc(vlen + 1);
memcpy(pathString, path->value.svalue->value, vlen);
memcpy(pathString+vlen,"\0",1);
prg = ctr_internal_readf(pathString);
free(pathString);
parsedCode = ctr_dparse_parse(prg);
ctr_cwlk_run(parsedCode);
return myself;
}
/**
* [File] run
*
* Includes the file as a piece of executable code.
*/
ctr_object* ctr_file_include_ast(ctr_object* myself, ctr_argument* argumentList) {
ctr_object* path = ctr_internal_object_find_property(myself, ctr_build_string("path",4), 0);
ctr_tnode* parsedCode;
ctr_size vlen;
char* pathString;
if (path == NULL) return myself;
vlen = path->value.svalue->vlen;
pathString = malloc(vlen + 1);
memcpy(pathString, path->value.svalue->value, vlen);
memcpy(pathString+vlen,"\0",1);
parsedCode = ctr_serializer_unserialize(pathString);
ctr_cwlk_run(parsedCode);
return myself;
}
/**
* [File] delete
*
* Deletes the file.
*/
ctr_object* ctr_file_delete(ctr_object* myself, ctr_argument* argumentList) {
ctr_object* path = ctr_internal_object_find_property(myself, ctr_build_string("path",4), 0);
ctr_size vlen;
char* pathString;
int r;
if (path == NULL) return myself;
vlen = path->value.svalue->vlen;
pathString = malloc(vlen + 1);
memcpy(pathString, path->value.svalue->value, vlen);
memcpy(pathString+vlen,"\0",1);
r = remove(pathString);
if (r!=0) {
CtrStdError = ctr_build_string_from_cstring("Unable to delete file.\0");
return CtrStdNil;
}
return myself;
}
/**
* [File] size
*
* Returns the size of the file.
*/
ctr_object* ctr_file_size(ctr_object* myself, ctr_argument* argumentList) {
ctr_object* path = ctr_internal_object_find_property(myself, ctr_build_string("path",4), 0);
ctr_size vlen;
char* pathString;
FILE* f;
int prev, sz;
if (path == NULL) return ctr_build_number_from_float(0);
vlen = path->value.svalue->vlen;
pathString = malloc(vlen + 1);
memcpy(pathString, path->value.svalue->value, vlen);
memcpy(pathString+vlen,"\0",1);
f = fopen(pathString, "r");
if (f == NULL) return ctr_build_number_from_float(0);
prev = ftell(f);
fseek(f, 0L, SEEK_END);
sz=ftell(f);
fseek(f,prev,SEEK_SET);
if (f) {
fclose(f);
}
return ctr_build_number_from_float( (ctr_number) sz );
}
/**
* [File] open: [string]
*
* Open a file with using the specified mode.
*
* Usage:
*
* f := File new: '/path/to/file'.
* f open: 'r+'. #opens file for reading and writing
*
* The example above opens the file in f for reading and writing.
*/
ctr_object* ctr_file_open(ctr_object* myself, ctr_argument* argumentList) {
ctr_object* pathObj = ctr_internal_object_find_property(myself, ctr_build_string("path",4), 0);
char* mode;
FILE* handle;
ctr_resource* rs = malloc(sizeof(ctr_resource));
if (pathObj == NULL) return myself;
char* path;
CTR_2CSTR(path, pathObj);
CTR_2CSTR(mode, ctr_internal_cast2string(argumentList->object));
handle = fopen(path,mode);
rs->type = 1;
rs->ptr = handle;
myself->value.rvalue = rs;
return myself;
}
/**
* [File] close.
*
* Closes the file represented by the recipient.
*
* Usage:
*
* f := File new: '/path/to/file.txt'.
* f open: 'r+'.
* f close.
*
* The example above opens and closes a file.
*/
ctr_object* ctr_file_close(ctr_object* myself, ctr_argument* argumentList) {
if (myself->value.rvalue == NULL) return myself;
if (myself->value.rvalue->type != 1) return myself;
fclose((FILE*)myself->value.rvalue->ptr);
myself->value.rvalue = NULL;
return myself;
}
/**
* [File] readBytes: [Number].
*
* Reads a number of bytes from the file.
*
* Usage:
*
* f := File new: '/path/to/file.txt'.
* f open: 'r+'.
* x := f readBytes: 10.
* f close.
*
* The example above reads 10 bytes from the file represented by f
* and puts them in buffer x.
*/
ctr_object* ctr_file_read_bytes(ctr_object* myself, ctr_argument* argumentList) {
int bytes;
char* buffer;
if (myself->value.rvalue == NULL) return myself;
if (myself->value.rvalue->type != 1) return myself;
bytes = ctr_internal_cast2number(argumentList->object)->value.nvalue;
if (bytes < 0) return ctr_build_string_from_cstring("");
buffer = (char*) malloc(bytes);
if (buffer == NULL) {
CtrStdError = ctr_build_string_from_cstring("Cannot allocate memory for file buffer.");
return ctr_build_string_from_cstring("");
}
fread(buffer, sizeof(char), (int)bytes, (FILE*)myself->value.rvalue->ptr);
return ctr_build_string_from_cstring(buffer);
}
/**
* [File] writeBytes: [String].
*
* Takes a string and writes the bytes in the string to the file
* object. Returns the number of bytes actually written.
*
* Usage:
*
* f := File new: '/path/to/file.txt'.
* f open: 'r+'.
* n := f writeBytes: 'Hello World'.
* f close.
*
* The example above writes 'Hello World' to the specified file as bytes.
* The number of bytes written is returned in variable n.
*/
ctr_object* ctr_file_write_bytes(ctr_object* myself, ctr_argument* argumentList) {
int bytes, written;
char* buffer;
if (myself->value.rvalue == NULL) return myself;
if (myself->value.rvalue->type != 1) return myself;
ctr_object* string2write = ctr_internal_cast2string(argumentList->object);
CTR_2CSTR(buffer, string2write);
bytes = string2write->value.svalue->vlen;
written = fwrite(buffer, sizeof(char), (int)bytes, (FILE*)myself->value.rvalue->ptr);
return ctr_build_number_from_float((double_t) written);
}
/**
* [File] seek: [Number].
*
* Moves the file pointer to the specified position in the file
* (relative to the current position).
*
* Usage:
*
* file open: 'r', seek: 10.
*
* The example above opens a file for reading and moves the
* pointer to position 10 (meaning 10 bytes from the beginning of the file).
* The seek value may be negative.
*/
ctr_object* ctr_file_seek(ctr_object* myself, ctr_argument* argumentList) {
int offset;
int error;
if (myself->value.rvalue == NULL) return myself;
if (myself->value.rvalue->type != 1) return myself;
offset = (long int) ctr_internal_cast2number(argumentList->object)->value.nvalue;
error = fseek((FILE*)myself->value.rvalue->ptr, offset, SEEK_CUR);
if (error) CtrStdError = ctr_build_string_from_cstring("Seek failed.");
return myself;
}
/**
* [File] rewind.
*
* Rewinds the file. Moves the file pointer to the beginning of the file.
*
* Usage:
*
* file open: 'r'.
* x := file readBytes: 10. #read 10 bytes
* file rewind. #rewind, set pointer to begin again
* y := file readBytes: 10. #re-read same 10 bytes
*
* The example above reads the same sequence of 10 bytes twice, resulting
* in variable x and y being equal.
*/
ctr_object* ctr_file_seek_rewind(ctr_object* myself, ctr_argument* argumentList) {
int error;
if (myself->value.rvalue == NULL) return myself;
if (myself->value.rvalue->type != 1) return myself;
error = fseek((FILE*)myself->value.rvalue->ptr, 0, SEEK_SET);
if (error) CtrStdError = ctr_build_string_from_cstring("Seek rewind failed.");
return myself;
}
/**
* [File] end.
*
* Moves the file pointer to the end of the file. Use this in combination with
* negative seek operations.
*
* Usage:
*
* file open: 'r'.
* file end.
* x := file seek: -10, readBytes: 10.
*
* The example above will read the last 10 bytes of the file. This is
* accomplished by first moving the file pointer to the end of the file,
* then putting it back 10 bytes (negative number), and then reading 10
* bytes.
*/
ctr_object* ctr_file_seek_end(ctr_object* myself, ctr_argument* argumentList) {
int error;
if (myself->value.rvalue == NULL) return myself;
if (myself->value.rvalue->type != 1) return myself;
error = fseek((FILE*)myself->value.rvalue->ptr, 0, SEEK_END);
if (error) CtrStdError = ctr_build_string_from_cstring("Seek end failed.");
return myself;
}