From d2e09fa2c788e5f68e8163c6bf5648af064772bc Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Tue, 22 Oct 2013 15:16:35 +0900 Subject: [PATCH] add file related functions --- src/file.c | 42 ++++++++++++++++++++++++++++++++++++++++++ src/init.c | 2 ++ 2 files changed, 44 insertions(+) create mode 100644 src/file.c diff --git a/src/file.c b/src/file.c new file mode 100644 index 00000000..5c069711 --- /dev/null +++ b/src/file.c @@ -0,0 +1,42 @@ +#include + +#include "picrin.h" + +pic_value +pic_file_exists_p(pic_state *pic) +{ + char *fname; + size_t size; + FILE *fp; + + pic_get_args(pic, "s", &fname, &size); + + 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; + size_t size; + + pic_get_args(pic, "s", &fname, &size); + + if (remove(fname) != 0) { + pic_error(pic, "file cannot be deleted"); + } + return pic_true_value(); +} + +void +pic_init_file(pic_state *pic) +{ + pic_defun(pic, "file-exists?", pic_file_exists_p); + pic_defun(pic, "delete-file", pic_file_delete); +} diff --git a/src/init.c b/src/init.c index a4d0407d..77c845d3 100644 --- a/src/init.c +++ b/src/init.c @@ -4,6 +4,7 @@ void pic_init_port(pic_state *); void pic_init_number(pic_state *); void pic_init_time(pic_state *); void pic_init_system(pic_state *); +void pic_init_file(pic_state *); #define DONE pic_gc_arena_restore(pic, ai); @@ -17,4 +18,5 @@ pic_init_core(pic_state *pic) pic_init_number(pic); DONE; pic_init_time(pic); DONE; pic_init_system(pic); DONE; + pic_init_file(pic); DONE; }