changeset 13:120629a5d6bf

seenfile -> placefile (clearer)
author David A. Holland
date Sun, 19 Dec 2010 19:49:43 -0500
parents 6c15ca895585
children 5045b9678bb0
files files.c place.c place.h
diffstat 3 files changed, 63 insertions(+), 35 deletions(-) [+]
line wrap: on
line diff
--- a/files.c	Sun Dec 19 19:39:26 2010 -0500
+++ b/files.c	Sun Dec 19 19:49:43 2010 -0500
@@ -1,6 +1,7 @@
 #include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <unistd.h>
 #include <fcntl.h>
 #include <err.h>
@@ -84,12 +85,36 @@
 // parsing
 
 void
-file_read(struct seenfile *sf, int fd);
+file_read(const struct placefile *pf, int fd);
 
 ////////////////////////////////////////////////////////////
 // path search
 
 static
+char *
+mkfilename(const char *dir, const char *file)
+{
+	size_t dlen, flen, rlen;
+	char *ret;
+	bool needslash = false;
+
+	dlen = strlen(dir);
+	flen = strlen(file);
+	if (dlen > 0 && dir[dlen-1] != '/') {
+		needslash = true;
+	}
+
+	rlen = dlen + (needslash ? 1 : 0) + flen;
+	ret = domalloc(rlen + 1);
+	strcpy(ret, dir);
+	if (needslash) {
+		strcat(ret, "/");
+	}
+	strcat(ret, file);
+	return ret;
+}
+
+static
 int
 file_tryopen(const char *file)
 {
@@ -109,7 +134,7 @@
 {
 	unsigned i, num;
 	struct incdir *id;
-	struct seenfile *sf;
+	const struct placefile *pf;
 	char *file;
 	int fd;
 
@@ -118,11 +143,12 @@
 	num = incdirarray_num(path);
 	for (i=0; i<num; i++) {
 		id = incdirarray_get(path, i);
-		file = dostrdup3(id->name, "/", name);
+		file = mkfilename(id->name, name);
 		fd = file_tryopen(file);
 		if (fd >= 0) {
-			sf = place_seen_file(place, file, id->issystem);
-			file_read(sf, fd);
+			pf = place_addfile(place, file, id->issystem);
+			free(file);
+			file_read(pf, fd);
 			close(fd);
 			return;
 		}
@@ -147,7 +173,7 @@
 void
 file_readabsolute(struct place *place, const char *name)
 {
-	struct seenfile *sf;
+	const struct placefile *pf;
 	int fd;
 
 	assert(place != NULL);
@@ -157,7 +183,7 @@
 		warn("%s", name);
 		die();
 	}
-	sf = place_seen_file(place, dostrdup(name), false);
-	file_read(sf, fd);
+	pf = place_addfile(place, name, false);
+	file_read(pf, fd);
 	close(fd);
 }
--- a/place.c	Sun Dec 19 19:39:26 2010 -0500
+++ b/place.c	Sun Dec 19 19:49:43 2010 -0500
@@ -11,51 +11,52 @@
 #define BUILTIN_LINE      1
 #define COMMANDLINE_LINE  2
 
-struct seenfile {
+struct placefile {
 	struct place includedfrom;
 	char *name;
 	bool fromsystemdir;
 };
-DECLARRAY(seenfile);
-DEFARRAY(seenfile, );
+DECLARRAY(placefile);
+DEFARRAY(placefile, );
 
-static struct seenfilearray seenfiles;
+static struct placefilearray placefiles;
 static bool overall_failure;
 
 ////////////////////////////////////////////////////////////
 // seenfiles
 
 static
-struct seenfile *
-seenfile_create(const struct place *from, char *name, bool fromsystemdir)
+struct placefile *
+placefile_create(const struct place *from, const char *name,
+		 bool fromsystemdir)
 {
-	struct seenfile *sf;
+	struct placefile *pf;
 
-	sf = domalloc(sizeof(*sf));
-	sf->includedfrom = *from;
-	sf->name = name;
-	sf->fromsystemdir = fromsystemdir;
-	return sf;
+	pf = domalloc(sizeof(*pf));
+	pf->includedfrom = *from;
+	pf->name = dostrdup(name);
+	pf->fromsystemdir = fromsystemdir;
+	return pf;
 }
 
 static
 void
-seenfile_destroy(struct seenfile *sf)
+placefile_destroy(struct placefile *pf)
 {
-	free(sf->name);
-	free(sf);
+	free(pf->name);
+	free(pf);
 }
 
-DESTROYALL_ARRAY(seenfile, );
+DESTROYALL_ARRAY(placefile, );
 
-struct seenfile *
-place_seen_file(const struct place *place, char *file, bool issystem)
+const struct placefile *
+place_addfile(const struct place *place, const char *file, bool issystem)
 {
-	struct seenfile *sf;
+	struct placefile *pf;
 
-	sf = seenfile_create(place, file, issystem);
-	seenfilearray_add(&seenfiles, sf, NULL);
-	return sf;
+	pf = placefile_create(place, file, issystem);
+	placefilearray_add(&placefiles, pf, NULL);
+	return pf;
 }
 
 ////////////////////////////////////////////////////////////
@@ -149,12 +150,12 @@
 void
 place_init(void)
 {
-	seenfilearray_init(&seenfiles);
+	placefilearray_init(&placefiles);
 }
 
 void
 place_cleanup(void)
 {
-	seenfilearray_destroyall(&seenfiles);
-	seenfilearray_cleanup(&seenfiles);
+	placefilearray_destroyall(&placefiles);
+	placefilearray_cleanup(&placefiles);
 }
--- a/place.h	Sun Dec 19 19:39:26 2010 -0500
+++ b/place.h	Sun Dec 19 19:49:43 2010 -0500
@@ -7,7 +7,7 @@
 };
 struct place {
 	enum places type;
-	struct seenfile *file;
+	const struct placefile *file;
 	unsigned line;
 	unsigned column;
 };
@@ -19,4 +19,5 @@
 void place_setbuiltin(struct place *p, unsigned num);
 void place_setcommandline(struct place *p, unsigned word);
 
-struct seenfile *place_seen_file(const struct place *p, char *name, bool fromsystemdir);
+const struct placefile *place_addfile(const struct place *incplace,
+				      const char *name, bool fromsystemdir);