summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Nicholson <dbn.lists@gmail.com>2008-02-20 18:37:58 -0800
committerDan Nicholson <dbn.lists@gmail.com>2008-02-20 18:37:58 -0800
commit50503444864518f98d4c8f7b92047ce72561fce3 (patch)
tree532edf6c4ecbd05bfdbeb368f9bafb038b70adad
parent004e5539f82cc780d41fa4f012ccf98e1e75e2ee (diff)
Function to verify all the sub-dependencies are defined in a list
When working with a list of deps, it's important to know that all the sub-dependencies have an associated dep_t of the same name in the list. This means that we can easily find that ordering the deps won't result stop in an undefined reference. A new test program has been created for this function.
-rw-r--r--.gitignore1
-rw-r--r--Makefile.am4
-rw-r--r--dep-list.c26
-rw-r--r--dep.h1
-rw-r--r--tdep2.c46
5 files changed, 77 insertions, 1 deletions
diff --git a/.gitignore b/.gitignore
index ace42b2..5509773 100644
--- a/.gitignore
+++ b/.gitignore
@@ -15,3 +15,4 @@ configure
libtool
tstr
tdep
+tdep2
diff --git a/Makefile.am b/Makefile.am
index 3bd5e03..d89e3c8 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1,8 +1,10 @@
noinst_LTLIBRARIES = libdep.la
libdep_la_SOURCES = str.c dep.c dep-list.c
-noinst_PROGRAMS = tstr tdep
+noinst_PROGRAMS = tstr tdep tdep2
tstr_SOURCES = tstr.c
tstr_LDADD = libdep.la
tdep_SOURCES = tdep.c
tdep_LDADD = libdep.la
+tdep2_SOURCES = tdep2.c
+tdep2_LDADD = libdep.la
diff --git a/dep-list.c b/dep-list.c
index 0827893..432a526 100644
--- a/dep-list.c
+++ b/dep-list.c
@@ -94,3 +94,29 @@ int dep_list_exists_name(dep_list_t *dlp, const char *name)
else
return 1;
}
+
+/* Ensure that the sub-dependencies for each dep_t in the list exist in
+ * the list as their own dep_t. Returns NULL if all the sub-deps have
+ * been found or the name of the first missing dep if not.
+ */
+char *dep_list_verify_all(dep_list_t *dlp)
+{
+ int n;
+ char *missing = NULL;
+ dep_t *dp;
+
+ if (!dlp)
+ goto out;
+
+ for (dp = dlp->first; dp; dp = dp->next) {
+ for (n = 0; n < dp->ndeps; n++) {
+ if (dep_list_exists_name(dlp, dp->deps[n]) != 0) {
+ missing = d_string_new(dp->deps[n]);
+ break;
+ }
+ }
+ }
+
+out:
+ return missing;
+}
diff --git a/dep.h b/dep.h
index 78d79a7..d03c3d5 100644
--- a/dep.h
+++ b/dep.h
@@ -24,3 +24,4 @@ extern void dep_list_add(dep_list_t *dlp, dep_t *dp);
extern dep_list_t *dep_list_copy(dep_list_t *old);
extern dep_t *dep_list_find_name(dep_list_t *dlp, const char *name);
extern int dep_list_exists_name(dep_list_t *dlp, const char *name);
+extern char *dep_list_verify_all(dep_list_t *dlp);
diff --git a/tdep2.c b/tdep2.c
new file mode 100644
index 0000000..e598537
--- /dev/null
+++ b/tdep2.c
@@ -0,0 +1,46 @@
+#include <stdio.h>
+#include "dep.h"
+
+int main(int argc, char *argv[])
+{
+ dep_t *d1, *d2, *d3, *d4;
+ dep_list_t *dl1, *dl2;
+ char *miss;
+
+ /* test dep list */
+ d1 = dep_new("d1");
+ dep_add(d1, "d2");
+ d2 = dep_new("d2");
+ dep_add(d2, "d3");
+ d3 = dep_new("d3");
+ d4 = dep_new("d4");
+ dep_add(d4, "barf");
+
+ dl1 = dep_list_new();
+ dep_list_add(dl1, d1);
+ dep_list_add(dl1, d2);
+ dep_list_add(dl1, d3);
+
+ /* another list, but with the bogus d4 element */
+ dl2 = dep_list_copy(dl1);
+ dep_list_add(dl2, d4);
+
+ miss = dep_list_verify_all(dl1);
+ if (miss)
+ printf("Dep %s was not found in the dl1 list\n", miss);
+ else
+ printf("Found all deps in the dl1 list\n");
+ free(miss);
+
+ miss = dep_list_verify_all(dl2);
+ if (miss)
+ printf("Dep %s was not found in the dl2 list\n", miss);
+ else
+ printf("Found all deps in the dl2 list\n");
+ free(miss);
+
+ dep_list_free(dl1);
+ dep_list_free(dl2);
+
+ return 0;
+}