Mercurial > ~dholland > hg > tradcpp > index.cgi
view files.c @ 8:97243badae69
split place stuff to its own file
author | David A. Holland |
---|---|
date | Sun, 19 Dec 2010 19:15:55 -0500 |
parents | b8167949474a |
children | 1fbcbd58742e |
line wrap: on
line source
#include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <fcntl.h> #include <err.h> #include "array.h" #include "place.h" #include "files.h" struct incdir { const char *name; bool issystem; }; struct seenfile { struct place includedfrom; char *name; bool fromsystemdir; }; DECLARRAY(incdir); DECLARRAY(seenfile); DEFARRAY(incdir, ); DEFARRAY(seenfile, ); static struct incdirarray quotepath, bracketpath; static struct seenfilearray seenfiles; //////////////////////////////////////////////////////////// // management #define DESTROYALL(T) \ static \ void \ T##array_destroyall(struct T##array *arr) \ { \ unsigned i, num; \ struct T *t; \ \ num = T##array_num(arr); \ for (i=0; i<num; i++) { \ t = T##array_get(arr, i); \ T##_destroy(t); \ } \ T##array_setsize(arr, 0); \ } static struct incdir * incdir_create(const char *name, bool issystem) { struct incdir *id; id = domalloc(sizeof(*id)); id->name = name; id->issystem = issystem; return id; } static void incdir_destroy(struct incdir *id) { free(id); } static struct seenfile * seenfile_create(const struct place *from, char *name, bool fromsystemdir) { struct seenfile *sf; sf = domalloc(sizeof(*sf)); sf->includedfrom = *from; sf->name = name; sf->fromsystemdir = fromsystemdir; return sf; } static void seenfile_destroy(struct seenfile *sf) { free(sf->name); free(sf); } void files_init(void) { incdirarray_init("epath); incdirarray_init(&bracketpath); } DESTROYALL(incdir); DESTROYALL(seenfile); void files_cleanup(void) { seenfilearray_destroyall(&seenfiles); seenfilearray_cleanup(&seenfiles); incdirarray_destroyall("epath); incdirarray_cleanup("epath); incdirarray_destroyall(&bracketpath); incdirarray_cleanup(&bracketpath); } //////////////////////////////////////////////////////////// // path setup void files_addquotepath(const char *dir, bool issystem) { struct incdir *id; id = incdir_create(dir, issystem); incdirarray_add("epath, id, NULL); } void files_addbracketpath(const char *dir, bool issystem) { struct incdir *id; id = incdir_create(dir, issystem); incdirarray_add(&bracketpath, id, NULL); } //////////////////////////////////////////////////////////// // seenfile functions exposed for places.c const char * seenfile_getname(const struct seenfile *file) { return file->name; } const struct place * seenfile_getincludeplace(const struct seenfile *file) { return &file->includedfrom; } //////////////////////////////////////////////////////////// // parsing void file_read(struct seenfile *sf, int fd); //////////////////////////////////////////////////////////// // path search static int file_tryopen(const char *file) { int fd; fd = open(file, O_RDONLY); if (fd < 0) { return -1; } /* XXX: do we need to do anything here or is this function pointless?*/ return fd; } static void file_search(struct place *place, struct incdirarray *path, const char *name) { unsigned i, num; struct incdir *id; struct seenfile *sf; char *file; int fd; assert(place != NULL); num = incdirarray_num(path); for (i=0; i<num; i++) { id = incdirarray_get(path, i); file = dostrdup3(id->name, "/", name); fd = file_tryopen(file); if (fd >= 0) { sf = seenfile_create(place, file, id->issystem); seenfilearray_add(&seenfiles, sf, NULL); file_read(sf, fd); close(fd); return; } free(file); } complain(place, "Include file %s not found", name); complain_fail(); } void file_readquote(struct place *place, const char *name) { file_search(place, "epath, name); } void file_readbracket(struct place *place, const char *name) { file_search(place, &bracketpath, name); } void file_readabsolute(struct place *place, const char *name) { struct seenfile *sf; int fd; assert(place != NULL); fd = file_tryopen(name); if (fd < 0) { warn("%s", name); die(); } sf = seenfile_create(place, dostrdup(name), false); seenfilearray_add(&seenfiles, sf, NULL); file_read(sf, fd); close(fd); }