Mercurial > ~dholland > hg > tradcpp > index.cgi
view files.c @ 12:6c15ca895585
improve places more
author | David A. Holland |
---|---|
date | Sun, 19 Dec 2010 19:39:26 -0500 |
parents | 800f3a560a3b |
children | 120629a5d6bf |
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; }; DECLARRAY(incdir); DEFARRAY(incdir, ); static struct incdirarray quotepath, bracketpath; //////////////////////////////////////////////////////////// // management 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); } void files_init(void) { incdirarray_init("epath); incdirarray_init(&bracketpath); } DESTROYALL_ARRAY(incdir, ); void files_cleanup(void) { 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); } //////////////////////////////////////////////////////////// // 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 = place_seen_file(place, file, id->issystem); 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 = place_seen_file(place, dostrdup(name), false); file_read(sf, fd); close(fd); }