2014-01-17 06:58:31 -05:00
|
|
|
/**
|
|
|
|
* See Copyright Notice in picrin.h
|
|
|
|
*/
|
|
|
|
|
2013-10-22 02:16:35 -04:00
|
|
|
#include "picrin.h"
|
2013-10-22 02:44:03 -04:00
|
|
|
#include "picrin/port.h"
|
2014-07-29 04:45:40 -04:00
|
|
|
#include "picrin/error.h"
|
|
|
|
|
|
|
|
static noreturn void
|
|
|
|
file_error(pic_state *pic, const char *msg)
|
|
|
|
{
|
|
|
|
pic_throw(pic, PIC_ERROR_FILE, msg, pic_nil_value());
|
|
|
|
}
|
2013-10-22 02:44:03 -04:00
|
|
|
|
|
|
|
static pic_value
|
|
|
|
generic_open_file(pic_state *pic, const char *fname, char *mode, short flags)
|
|
|
|
{
|
|
|
|
struct pic_port *port;
|
2014-02-25 07:07:32 -05:00
|
|
|
xFILE *file;
|
2013-10-22 02:44:03 -04:00
|
|
|
|
2014-02-01 08:16:09 -05:00
|
|
|
file = xfopen(fname, mode);
|
2014-01-16 04:32:05 -05:00
|
|
|
if (! file) {
|
2014-07-29 04:45:40 -04:00
|
|
|
file_error(pic, "could not open file");
|
2013-10-22 02:44:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
port = (struct pic_port *)pic_obj_alloc(pic, sizeof(struct pic_port), PIC_TT_PORT);
|
2014-01-16 04:32:05 -05:00
|
|
|
port->file = file;
|
2013-10-22 02:44:03 -04:00
|
|
|
port->flags = flags;
|
|
|
|
port->status = PIC_PORT_OPEN;
|
|
|
|
|
|
|
|
return pic_obj_value(port);
|
|
|
|
}
|
|
|
|
|
|
|
|
pic_value
|
|
|
|
pic_file_open_input_file(pic_state *pic)
|
|
|
|
{
|
|
|
|
static const short flags = PIC_PORT_IN | PIC_PORT_TEXT;
|
|
|
|
char *fname;
|
|
|
|
|
2014-02-22 21:52:15 -05:00
|
|
|
pic_get_args(pic, "z", &fname);
|
2013-10-22 02:44:03 -04:00
|
|
|
|
|
|
|
return generic_open_file(pic, fname, "r", flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
pic_value
|
|
|
|
pic_file_open_input_binary_file(pic_state *pic)
|
|
|
|
{
|
|
|
|
static const short flags = PIC_PORT_IN | PIC_PORT_BINARY;
|
|
|
|
char *fname;
|
|
|
|
|
2014-02-22 21:52:15 -05:00
|
|
|
pic_get_args(pic, "z", &fname);
|
2013-10-22 02:44:03 -04:00
|
|
|
|
|
|
|
return generic_open_file(pic, fname, "rb", flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
pic_value
|
|
|
|
pic_file_open_output_file(pic_state *pic)
|
|
|
|
{
|
|
|
|
static const short flags = PIC_PORT_OUT | PIC_PORT_TEXT;
|
|
|
|
char *fname;
|
|
|
|
|
2014-02-22 21:52:15 -05:00
|
|
|
pic_get_args(pic, "z", &fname);
|
2013-10-22 02:44:03 -04:00
|
|
|
|
|
|
|
return generic_open_file(pic, fname, "w", flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
pic_value
|
|
|
|
pic_file_open_output_binary_file(pic_state *pic)
|
|
|
|
{
|
|
|
|
static const short flags = PIC_PORT_OUT | PIC_PORT_BINARY;
|
|
|
|
char *fname;
|
|
|
|
|
2014-02-22 21:52:15 -05:00
|
|
|
pic_get_args(pic, "z", &fname);
|
2013-10-22 02:44:03 -04:00
|
|
|
|
|
|
|
return generic_open_file(pic, fname, "wb", flags);
|
|
|
|
}
|
2013-10-22 02:16:35 -04:00
|
|
|
|
|
|
|
pic_value
|
|
|
|
pic_file_exists_p(pic_state *pic)
|
|
|
|
{
|
|
|
|
char *fname;
|
|
|
|
FILE *fp;
|
|
|
|
|
2014-02-22 21:52:15 -05:00
|
|
|
pic_get_args(pic, "z", &fname);
|
2013-10-22 02:16:35 -04:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2014-02-22 21:52:15 -05:00
|
|
|
pic_get_args(pic, "z", &fname);
|
2013-10-22 02:16:35 -04:00
|
|
|
|
|
|
|
if (remove(fname) != 0) {
|
2014-07-29 04:45:40 -04:00
|
|
|
file_error(pic, "file cannot be deleted");
|
2013-10-22 02:16:35 -04:00
|
|
|
}
|
2014-01-08 01:22:23 -05:00
|
|
|
return pic_none_value();
|
2013-10-22 02:16:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
pic_init_file(pic_state *pic)
|
|
|
|
{
|
2014-07-27 01:47:14 -04:00
|
|
|
pic_deflibrary (pic, "(scheme file)") {
|
2013-12-08 02:17:28 -05:00
|
|
|
pic_defun(pic, "open-input-file", pic_file_open_input_file);
|
|
|
|
pic_defun(pic, "open-input-binary-file", pic_file_open_input_binary_file);
|
|
|
|
pic_defun(pic, "open-output-file", pic_file_open_output_file);
|
|
|
|
pic_defun(pic, "open-output-binary-file", pic_file_open_output_binary_file);
|
|
|
|
pic_defun(pic, "file-exists?", pic_file_exists_p);
|
|
|
|
pic_defun(pic, "delete-file", pic_file_delete);
|
|
|
|
}
|
2013-10-22 02:16:35 -04:00
|
|
|
}
|