Expose permanent debugging code to the C compiler.
[geda-gaf/berndj.git] / gnetlist / src / s_rename.c
blob16ff01eb3bda132f1ea7d000ea2b72b26dcb41c7
1 /* gEDA - GPL Electronic Design Automation
2 * gnetlist - gEDA Netlist
3 * Copyright (C) 1998-2007 Ales Hvezda
4 * Copyright (C) 1998-2007 gEDA Contributors (see ChangeLog for details)
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
22 * 2005/05/02 Almost totally reimplemented to support dynamic allocation.
24 * Changes are Copyright (C) 2005 Carlos A. R. Azevedo
27 #include <config.h>
29 #include <stdio.h>
30 #include <ctype.h>
31 #ifdef HAVE_STRING_H
32 #include <string.h>
33 #endif
34 #ifdef HAVE_STDLIB_H
35 #include <stdlib.h>
36 #endif
37 #ifdef HAVE_ASSERT_H
38 #include <assert.h>
39 #endif
41 #include <libgeda/libgeda.h>
43 #include "../include/globals.h"
44 #include "../include/prototype.h"
46 #ifdef HAVE_LIBDMALLOC
47 #include <dmalloc.h>
48 #endif
50 typedef struct {
51 void * next;
52 char * src;
53 char * dest;
54 } RENAME;
56 typedef struct {
57 void * next_set;
58 RENAME * first_rename;
59 RENAME * last_rename;
60 } SET;
62 static SET * first_set = NULL;
63 static SET * last_set = NULL;
65 void s_rename_init(void)
67 if (first_set)
69 fprintf(stderr,"ERROR: Overwriting a valid rename list.\n");
70 exit(-1);
74 void s_rename_destroy_all(void)
76 RENAME * temp;
77 void * to_free;
79 for (; first_set;)
81 for (temp = first_set->first_rename; temp;)
83 g_free(temp->src);
84 g_free(temp->dest);
85 to_free = temp;
86 temp = temp->next;
87 g_free(to_free);
89 to_free = first_set;
90 first_set = first_set->next_set;
91 g_free(to_free);
93 last_set = NULL;
96 void s_rename_next_set(void)
98 SET * new_set;
100 new_set = g_malloc(sizeof(SET));
101 memset(new_set,0,sizeof(SET));
102 if (first_set)
104 last_set->next_set = new_set;
105 last_set = new_set;
107 else
109 first_set = last_set = new_set;
113 void s_rename_print(void)
115 SET * temp_set;
116 RENAME * temp_rename;
117 int i;
119 for (i = 0, temp_set = first_set; temp_set; temp_set = temp_set->next_set, i++)
121 for (temp_rename = temp_set->first_rename; temp_rename; temp_rename = temp_rename->next)
123 printf("%d) Source: _%s_", i, temp_rename->src);
124 printf(" -> Dest: _%s_\n", temp_rename->dest);
129 /* if the src is found, return true */
130 /* if the dest is found, also return true, but warn user */
131 /* If quiet_flag is true than don't print anything */
132 int s_rename_search(char *src, char *dest, int quiet_flag)
134 RENAME * temp;
136 if (last_set)
138 for (temp = last_set->first_rename; temp; temp = temp->next)
140 if (strcmp(src, temp->src) == 0)
142 return (TRUE);
145 if (strcmp(dest, temp->src) == 0)
147 if (!quiet_flag)
149 fprintf(stderr,
150 "WARNING: Trying to rename something twice:\n"
151 "\t%s and %s\n"
152 "are both a src and dest name\n", dest, temp->src);
153 fprintf(stderr,"This warning is okay if you have multiple levels of hierarchy!\n");
155 return (TRUE);
159 return (FALSE);
162 void s_rename_add(char *src, char *dest)
164 int flag;
165 RENAME * last;
166 RENAME * temp;
167 RENAME * new_rename;
168 SET * new_set;
170 if (src == NULL || dest == NULL)
172 return;
175 flag = s_rename_search(src, dest, FALSE);
177 if (flag)
179 /* If found follow the original behaviour, limiting the operation to the current end-of-list */
180 last = last_set->last_rename;
181 for (temp = last_set->first_rename; ; temp = temp->next)
183 if (strcmp(dest, temp->src) == 0)
185 if (GEDA_DEBUG) {
186 printf("Found dest [%s] in src [%s] and that had a dest as: [%s]\n"
187 "So you want rename [%s] to [%s]\n",
188 dest, temp->src, temp->dest, src, temp->dest);
190 new_rename = g_malloc(sizeof(RENAME));
191 new_rename->next = NULL;
192 new_rename->src = g_strdup(src);
193 new_rename->dest = g_strdup(temp->dest);
194 /* If the rename pair was found then a set already exists, so there's no need the check it */
195 if (last_set->first_rename == NULL)
197 last_set->first_rename = last_set->last_rename = new_rename;
199 else
201 last_set->last_rename->next = new_rename;
202 last_set->last_rename = new_rename;
205 if (temp == last)
207 break;
211 else
213 /* Check for a valid set */
214 if (first_set == NULL)
216 new_set = g_malloc(sizeof(SET));
217 memset(new_set,0,sizeof(SET));
218 first_set = last_set = new_set;
220 new_rename = g_malloc(sizeof(RENAME));
221 new_rename->next = NULL;
222 new_rename->src = g_strdup(src);
223 new_rename->dest = g_strdup(dest);
224 if (last_set->first_rename == NULL)
226 last_set->first_rename = last_set->last_rename = new_rename;
228 else
230 last_set->last_rename->next = new_rename;
231 last_set->last_rename = new_rename;
236 void s_rename_all_lowlevel(NETLIST *netlist, char *src, char *dest)
238 NETLIST *nl_current = NULL;
239 CPINLIST *pl_current;
241 nl_current = netlist;
243 while (nl_current != NULL)
245 if (nl_current->cpins)
247 pl_current = nl_current->cpins;
248 while (pl_current != NULL)
250 if (pl_current->net_name != NULL)
252 if (strcmp(pl_current->net_name, src) == 0)
254 pl_current->net_name = g_strdup(dest);
257 pl_current = pl_current->next;
260 nl_current = nl_current->next;
264 void s_rename_all(TOPLEVEL *pr_current, NETLIST *netlist)
266 RENAME * temp;
268 if (GEDA_DEBUG) {
269 s_rename_print();
272 if (last_set)
274 for (temp = last_set->first_rename; temp; temp = temp->next)
276 verbose_print("R");
277 s_rename_all_lowlevel(netlist, temp->src, temp->dest);
283 SCM g_get_renamed_nets(SCM scm_level)
285 SCM pairlist = SCM_EOL;
286 SCM outerlist = SCM_EOL;
287 SET * temp_set;
288 RENAME * temp_rename;
290 for (temp_set = first_set; temp_set; temp_set = temp_set->next_set)
292 for (temp_rename = temp_set->first_rename; temp_rename; temp_rename = temp_rename->next)
294 pairlist = scm_list_n(scm_from_locale_string(temp_rename->src),
295 scm_from_locale_string(temp_rename->dest),
296 SCM_UNDEFINED);
297 outerlist = scm_cons (pairlist, outerlist);
300 return (outerlist);