2014-09-08 05:46:47 -04:00
|
|
|
/**
|
|
|
|
* See Copyright Notice in picrin.h
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "picrin.h"
|
|
|
|
|
2015-06-18 16:13:57 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2015-01-25 22:22:38 -05:00
|
|
|
PIC_NORETURN static void
|
2014-09-08 05:46:47 -04:00
|
|
|
file_error(pic_state *pic, const char *msg)
|
|
|
|
{
|
2015-06-04 00:23:20 -04:00
|
|
|
struct pic_error *e;
|
|
|
|
|
2015-07-12 19:16:04 -04:00
|
|
|
e = pic_make_error(pic, pic_intern(pic, "file"), msg, pic_nil_value());
|
2015-06-04 00:23:20 -04:00
|
|
|
|
|
|
|
pic_raise(pic, pic_obj_value(e));
|
2014-09-08 05:46:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pic_value
|
|
|
|
pic_file_open_input_file(pic_state *pic)
|
|
|
|
{
|
|
|
|
static const short flags = PIC_PORT_IN | PIC_PORT_TEXT;
|
|
|
|
char *fname;
|
|
|
|
|
|
|
|
pic_get_args(pic, "z", &fname);
|
|
|
|
|
2015-06-18 16:00:36 -04:00
|
|
|
return pic_obj_value(pic_open_file(pic, fname, flags));
|
2014-09-08 05:46:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pic_value
|
2014-09-08 06:46:40 -04:00
|
|
|
pic_file_open_binary_input_file(pic_state *pic)
|
2014-09-08 05:46:47 -04:00
|
|
|
{
|
|
|
|
static const short flags = PIC_PORT_IN | PIC_PORT_BINARY;
|
|
|
|
char *fname;
|
|
|
|
|
|
|
|
pic_get_args(pic, "z", &fname);
|
|
|
|
|
2015-06-18 16:00:36 -04:00
|
|
|
return pic_obj_value(pic_open_file(pic, fname, flags));
|
2014-09-08 05:46:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pic_value
|
|
|
|
pic_file_open_output_file(pic_state *pic)
|
|
|
|
{
|
|
|
|
static const short flags = PIC_PORT_OUT | PIC_PORT_TEXT;
|
|
|
|
char *fname;
|
|
|
|
|
|
|
|
pic_get_args(pic, "z", &fname);
|
|
|
|
|
2015-06-18 16:00:36 -04:00
|
|
|
return pic_obj_value(pic_open_file(pic, fname, flags));
|
2014-09-08 05:46:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pic_value
|
2014-09-08 06:46:40 -04:00
|
|
|
pic_file_open_binary_output_file(pic_state *pic)
|
2014-09-08 05:46:47 -04:00
|
|
|
{
|
|
|
|
static const short flags = PIC_PORT_OUT | PIC_PORT_BINARY;
|
|
|
|
char *fname;
|
|
|
|
|
|
|
|
pic_get_args(pic, "z", &fname);
|
|
|
|
|
2015-06-18 16:00:36 -04:00
|
|
|
return pic_obj_value(pic_open_file(pic, fname, flags));
|
2014-09-08 05:46:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pic_value
|
|
|
|
pic_file_exists_p(pic_state *pic)
|
|
|
|
{
|
|
|
|
char *fname;
|
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
pic_get_args(pic, "z", &fname);
|
|
|
|
|
|
|
|
fp = fopen(fname, "r");
|
|
|
|
if (fp) {
|
|
|
|
fclose(fp);
|
|
|
|
return pic_true_value();
|
|
|
|
} else {
|
|
|
|
return pic_false_value();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pic_value
|
|
|
|
pic_file_delete(pic_state *pic)
|
|
|
|
{
|
|
|
|
char *fname;
|
|
|
|
|
|
|
|
pic_get_args(pic, "z", &fname);
|
|
|
|
|
|
|
|
if (remove(fname) != 0) {
|
|
|
|
file_error(pic, "file cannot be deleted");
|
|
|
|
}
|
2015-06-09 03:34:45 -04:00
|
|
|
return pic_undef_value();
|
2014-09-08 05:46:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
pic_init_file(pic_state *pic)
|
|
|
|
{
|
2015-01-17 10:32:52 -05:00
|
|
|
pic_deflibrary (pic, "(scheme file)") {
|
|
|
|
pic_defun(pic, "open-input-file", pic_file_open_input_file);
|
|
|
|
pic_defun(pic, "open-binary-input-file", pic_file_open_binary_input_file);
|
|
|
|
pic_defun(pic, "open-output-file", pic_file_open_output_file);
|
|
|
|
pic_defun(pic, "open-binary-output-file", pic_file_open_binary_output_file);
|
|
|
|
pic_defun(pic, "file-exists?", pic_file_exists_p);
|
|
|
|
pic_defun(pic, "delete-file", pic_file_delete);
|
|
|
|
}
|
2014-09-08 05:46:47 -04:00
|
|
|
}
|