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
41 #include <libgeda/libgeda.h>
43 #include "../include/globals.h"
44 #include "../include/prototype.h"
46 #ifdef HAVE_LIBDMALLOC
58 RENAME
* first_rename
;
62 static SET
* first_set
= NULL
;
63 static SET
* last_set
= NULL
;
65 void s_rename_init(void)
69 fprintf(stderr
,"ERROR: Overwriting a valid rename list.\n");
74 void s_rename_destroy_all(void)
81 for (temp
= first_set
->first_rename
; temp
;)
90 first_set
= first_set
->next_set
;
96 void s_rename_next_set(void)
100 new_set
= g_malloc(sizeof(SET
));
101 memset(new_set
,0,sizeof(SET
));
104 last_set
->next_set
= new_set
;
109 first_set
= last_set
= new_set
;
113 void s_rename_print(void)
116 RENAME
* temp_rename
;
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
)
138 for (temp
= last_set
->first_rename
; temp
; temp
= temp
->next
)
140 if (strcmp(src
, temp
->src
) == 0)
145 if (strcmp(dest
, temp
->src
) == 0)
150 "WARNING: Trying to rename something twice:\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");
162 void s_rename_add(char *src
, char *dest
)
170 if (src
== NULL
|| dest
== NULL
)
175 flag
= s_rename_search(src
, dest
, FALSE
);
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)
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
;
201 last_set
->last_rename
->next
= new_rename
;
202 last_set
->last_rename
= new_rename
;
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
;
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
)
274 for (temp
= last_set
->first_rename
; temp
; temp
= temp
->next
)
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
;
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
),
297 outerlist
= scm_cons (pairlist
, outerlist
);