add file related functions

This commit is contained in:
Yuichi Nishiwaki 2013-10-22 15:16:35 +09:00
parent 0c1babb6c5
commit d2e09fa2c7
2 changed files with 44 additions and 0 deletions

42
src/file.c Normal file
View File

@ -0,0 +1,42 @@
#include <stdio.h>
#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);
}

View File

@ -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;
}