Updated copyright text/header in most source files.
[geda-gaf/peter-b.git] / gnetlist / src / s_rename.c
blob8fae09a9183d572a2e433b44199a172a6db7b06b
1 /* gEDA - GPL Electronic Design Automation
2 * gnetlist - gEDA Netlist
3 * Copyright (C) 1998-2010 Ales Hvezda
4 * Copyright (C) 1998-2010 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,"WARNING: Trying to rename something twice:\n\t%s and %s\nare both a src and dest name\n", dest, temp->src);
150 fprintf(stderr,"This warning is okay if you have multiple levels of hierarchy!\n");
152 return (TRUE);
156 return (FALSE);
159 void s_rename_add(char *src, char *dest)
161 int flag;
162 RENAME * last;
163 RENAME * temp;
164 RENAME * new_rename;
165 SET * new_set;
167 if (src == NULL || dest == NULL)
169 return;
172 flag = s_rename_search(src, dest, FALSE);
174 if (flag)
176 /* If found follow the original behaviour, limiting the operation to the current end-of-list */
177 last = last_set->last_rename;
178 for (temp = last_set->first_rename; ; temp = temp->next)
180 if (strcmp(dest, temp->src) == 0)
182 #if DEBUG
183 printf("Found dest [%s] in src [%s] and that had a dest as: [%s]\nSo you want rename [%s] to [%s]\n",
184 dest, temp->src, temp->dest, src, temp->dest);
185 #endif
186 new_rename = g_malloc(sizeof(RENAME));
187 new_rename->next = NULL;
188 new_rename->src = g_strdup(src);
189 new_rename->dest = g_strdup(temp->dest);
190 /* If the rename pair was found then a set already exists, so there's no need the check it */
191 if (last_set->first_rename == NULL)
193 last_set->first_rename = last_set->last_rename = new_rename;
195 else
197 last_set->last_rename->next = new_rename;
198 last_set->last_rename = new_rename;
201 if (temp == last)
203 break;
207 else
209 /* Check for a valid set */
210 if (first_set == NULL)
212 new_set = g_malloc(sizeof(SET));
213 memset(new_set,0,sizeof(SET));
214 first_set = last_set = new_set;
216 new_rename = g_malloc(sizeof(RENAME));
217 new_rename->next = NULL;
218 new_rename->src = g_strdup(src);
219 new_rename->dest = g_strdup(dest);
220 if (last_set->first_rename == NULL)
222 last_set->first_rename = last_set->last_rename = new_rename;
224 else
226 last_set->last_rename->next = new_rename;
227 last_set->last_rename = new_rename;
232 void s_rename_all_lowlevel(NETLIST * netlist_head, char *src, char *dest)
234 NETLIST *nl_current = NULL;
235 CPINLIST *pl_current;
237 nl_current = netlist_head;
239 while (nl_current != NULL)
241 if (nl_current->cpins)
243 pl_current = nl_current->cpins;
244 while (pl_current != NULL)
246 if (pl_current->net_name != NULL)
248 if (strcmp(pl_current->net_name, src) == 0)
250 pl_current->net_name = g_strdup(dest);
253 pl_current = pl_current->next;
256 nl_current = nl_current->next;
260 void s_rename_all(TOPLEVEL * pr_current, NETLIST * netlist_head)
262 RENAME * temp;
264 #if DEBUG
265 s_rename_print();
266 #endif
268 if (last_set)
270 for (temp = last_set->first_rename; temp; temp = temp->next)
272 verbose_print("R");
273 s_rename_all_lowlevel(netlist_head, temp->src, temp->dest);
279 SCM g_get_renamed_nets(SCM scm_level)
281 SCM pairlist = SCM_EOL;
282 SCM outerlist = SCM_EOL;
283 SET * temp_set;
284 RENAME * temp_rename;
285 char *level;
287 level = SCM_STRING_CHARS (scm_level);
289 for (temp_set = first_set; temp_set; temp_set = temp_set->next_set)
291 for (temp_rename = temp_set->first_rename; temp_rename; temp_rename = temp_rename->next)
293 pairlist = scm_list_n (scm_makfrom0str (temp_rename->src), scm_makfrom0str (temp_rename->dest), SCM_UNDEFINED);
294 outerlist = scm_cons (pairlist, outerlist);
297 return (outerlist);