From: Matthew Fernandez Date: Sat, 3 Dec 2022 19:10:46 +0000 (-0800) Subject: prune: use generalized list implementation for nodes list X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=8b0e4daa45568d7f31aa630eed9b3e6ea15f63c0;p=graphviz prune: use generalized list implementation for nodes list This allows preserving type safety (no more `char*` casts needed). We can also entirely remove prune’s generic list implementation which is no longer used. --- diff --git a/ci/clang_format.py b/ci/clang_format.py index 08842f075..b69fc5c60 100644 --- a/ci/clang_format.py +++ b/ci/clang_format.py @@ -123,8 +123,6 @@ EXCLUDE = ( "cmd/tools/unflatten.c", "contrib/diffimg/diffimg.c", "contrib/pangotest/pangotest.c", - "contrib/prune/generic_list.c", - "contrib/prune/generic_list.h", "contrib/prune/prune.c", "doc/libgraph/agmemread.c", "doc/libgraph/sccmap.c", diff --git a/contrib/prune/CMakeLists.txt b/contrib/prune/CMakeLists.txt index c5b183f00..8551ae7d6 100644 --- a/contrib/prune/CMakeLists.txt +++ b/contrib/prune/CMakeLists.txt @@ -1,4 +1,4 @@ -add_executable(prune generic_list.c prune.c) +add_executable(prune prune.c) target_include_directories(prune PRIVATE ../../lib ../../lib/cdt) diff --git a/contrib/prune/Makefile.am b/contrib/prune/Makefile.am index e39a1115d..cd2493aaf 100644 --- a/contrib/prune/Makefile.am +++ b/contrib/prune/Makefile.am @@ -5,13 +5,12 @@ AM_CPPFLAGS = \ -I$(top_srcdir)/lib/cdt bin_PROGRAMS = prune -noinst_HEADERS = generic_list.h dist_man_MANS = prune.1 if ENABLE_MAN_PDFS pdf_DATA = prune.1.pdf endif -prune_SOURCES = generic_list.c prune.c +prune_SOURCES = prune.c prune_LDADD = $(top_builddir)/lib/ingraphs/libingraphs_C.la \ $(top_builddir)/lib/cgraph/libcgraph.la \ diff --git a/contrib/prune/generic_list.c b/contrib/prune/generic_list.c deleted file mode 100644 index 5b6324469..000000000 --- a/contrib/prune/generic_list.c +++ /dev/null @@ -1,49 +0,0 @@ -/************************************************************************* - * Copyright (c) 2011 AT&T Intellectual Property - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: Details at http://www.graphviz.org/ - *************************************************************************/ - -#include -#include - -#include "generic_list.h" - -#define DFLT_SIZE 100 - -generic_list_t new_generic_list(size_t size) { - generic_list_t list = {0}; - if (size != 0) { - list.data = gv_calloc(size, sizeof(gl_data)); - } - list.size = size; - return list; -} - -void free_generic_list(generic_list_t * list) -{ - if (list->size > 0) { - free(list->data); - } -} - -void add_to_generic_list(generic_list_t *list, gl_data element) { - size_t new_size; - - if (list->size == list->used) { - if (list->size == 0) { - new_size = DFLT_SIZE; - } else { - new_size = list->size * 2; - } - gl_data *new_data = gv_recalloc(list->data, list->size, new_size, - sizeof(gl_data)); - list->data = new_data; - list->size = new_size; - } - list->data[list->used++] = element; -} diff --git a/contrib/prune/generic_list.h b/contrib/prune/generic_list.h deleted file mode 100644 index 047efc126..000000000 --- a/contrib/prune/generic_list.h +++ /dev/null @@ -1,26 +0,0 @@ -/************************************************************************* - * Copyright (c) 2011 AT&T Intellectual Property - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: Details at http://www.graphviz.org/ - *************************************************************************/ - -#pragma once - -#include - - typedef void *gl_data; - - typedef struct { - size_t used; /* number of elements in the list */ - size_t size; /* number of elements that the list can hold */ - gl_data *data; /* pointer to first element */ - } generic_list_t; - - extern generic_list_t new_generic_list(size_t size); - extern void add_to_generic_list(generic_list_t * list, - gl_data element); - extern void free_generic_list(generic_list_t * list); diff --git a/contrib/prune/prune.c b/contrib/prune/prune.c index 48e317d12..efe77ffde 100644 --- a/contrib/prune/prune.c +++ b/contrib/prune/prune.c @@ -19,7 +19,6 @@ #include #include #include -#include "generic_list.h" /* structure to hold an attribute specified on the commandline */ typedef struct { @@ -28,12 +27,13 @@ typedef struct { } strattr_t; DEFINE_LIST(attrs, strattr_t) +DEFINE_LIST(nodes, char*) static int remove_child(Agraph_t * graph, Agnode_t * node); static void help_message(const char *progname); static void addattr(attrs_t *l, char *a); -static void addnode(generic_list_t * l, char *n); +static void addnode(nodes_t *l, char *n); int verbose = 0; /* Flag to indicate verbose message output */ @@ -70,8 +70,6 @@ int main(int argc, char **argv) char **files; - unsigned long i; - opterr = 0; progname = strrchr(argv[0], '/'); @@ -82,7 +80,7 @@ int main(int argc, char **argv) } attrs_t attr_list = {0}; - generic_list_t node_list = new_generic_list(16); + nodes_t node_list = {0}; while ((c = getopt(argc, argv, "hvn:N:")) != -1) { switch (c) { @@ -138,16 +136,16 @@ int main(int argc, char **argv) aginit(graph, AGNODE, NDNAME, sizeof(ndata), 1); /* prune all nodes specified on the commandline */ - for (i = 0; i < node_list.used; i++) { + for (size_t i = 0; i < nodes_size(&node_list); ++i) { if (verbose == 1) - fprintf(stderr, "Pruning node %s\n", (char*)node_list.data[i]); + fprintf(stderr, "Pruning node %s\n", nodes_get(&node_list, i)); /* check whether a node of that name exists at all */ - node = agnode(graph, node_list.data[i], 0); + node = agnode(graph, nodes_get(&node_list, i), 0); if (node == NULL) { fprintf(stderr, "*** Warning: No such node: %s -- gracefully skipping this one\n", - (char*)node_list.data[i]); + nodes_get(&node_list, i)); } else { MARK(node); /* Avoid cycles */ /* Iterate over all outgoing edges */ @@ -180,7 +178,7 @@ int main(int argc, char **argv) agclose(graph); } attrs_free(&attr_list); - free_generic_list(&node_list); + nodes_free(&node_list); graphviz_exit(EXIT_SUCCESS); } @@ -259,8 +257,8 @@ static void addattr(attrs_t *l, char *a) { } /* add element to node list */ -static void addnode(generic_list_t *l, char *n) { +static void addnode(nodes_t *l, char *n) { char *sp = gv_strdup(n); - add_to_generic_list(l, sp); + nodes_append(l, sp); } diff --git a/contrib/prune/prune.vcxproj b/contrib/prune/prune.vcxproj index d2a3ba39b..708e7620c 100644 --- a/contrib/prune/prune.vcxproj +++ b/contrib/prune/prune.vcxproj @@ -97,10 +97,6 @@ - - - - diff --git a/contrib/prune/prune.vcxproj.filters b/contrib/prune/prune.vcxproj.filters index c822f317d..fc55769e0 100644 --- a/contrib/prune/prune.vcxproj.filters +++ b/contrib/prune/prune.vcxproj.filters @@ -15,14 +15,6 @@ - - Header Files - - - - - Source Files - Source Files