1 /* gEDA - GPL Electronic Design Automation
2 * gattrib -- gEDA component and net attribute manipulation using spreadsheet.
3 * Copyright (C) 2003-2007 Stuart D. Brorson.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
34 /*------------------------------------------------------------------
35 * Gattrib specific includes
36 *------------------------------------------------------------------*/
37 #include <libgeda/libgeda.h> /* geda library fcns */
38 #include "../include/struct.h" /* typdef and struct declarations */
39 #include "../include/prototype.h" /* function prototypes */
40 #include "../include/globals.h"
42 #ifdef HAVE_LIBDMALLOC
56 /* size is fixed... TODO: maybe make this dynamic */
57 static RENAME rename_pairs
[MAX_SETS
][MAX_RENAME
];
59 static int rename_counter
= 0;
60 static int cur_set
= 0;
62 void s_rename_init(void)
66 for (i
= 0; i
< MAX_SETS
; i
++) {
67 for (j
= 0; j
< MAX_RENAME
; j
++) {
68 rename_pairs
[i
][j
].src
= NULL
;
69 rename_pairs
[i
][j
].dest
= NULL
;
76 void s_rename_destroy_all(void)
80 for (i
= 0; i
< MAX_SETS
; i
++) {
81 for (j
= 0; j
< MAX_RENAME
; j
++) {
83 if (rename_pairs
[i
][j
].src
) {
84 g_free(rename_pairs
[i
][j
].src
);
85 rename_pairs
[i
][j
].src
= NULL
;
88 if (rename_pairs
[i
][j
].dest
) {
89 g_free(rename_pairs
[i
][j
].dest
);
90 rename_pairs
[i
][j
].dest
= NULL
;
98 void s_rename_next_set(void)
100 if (cur_set
== MAX_SETS
) {
102 "Increase number of rename_pair sets in s_net.c\n");
109 void s_rename_print(void)
113 for (i
= 0; i
< MAX_SETS
; i
++) {
114 for (j
= 0; j
< MAX_RENAME
; j
++) {
115 if (rename_pairs
[i
][j
].src
) {
116 printf("%d) Source: _%s_", i
, rename_pairs
[i
][j
].src
);
119 if (rename_pairs
[i
][j
].dest
) {
120 printf(" -> Dest: _%s_\n", rename_pairs
[i
][j
].dest
);
126 /* if the src is found, return true */
127 /* if the dest is found, also return true, but warn user */
128 /* If quiet_flag is true than don't print anything */
129 int s_rename_search(char *src
, char *dest
, int quiet_flag
)
132 for (i
= 0; i
< rename_counter
; i
++) {
134 if (rename_pairs
[cur_set
][i
].src
&& rename_pairs
[cur_set
][i
].dest
) {
136 if (strcmp(src
, rename_pairs
[cur_set
][i
].src
) == 0) {
140 if (strcmp(dest
, rename_pairs
[cur_set
][i
].src
) == 0) {
143 "WARNING: Trying to rename something twice:\n\t%s and %s\nare both a src and dest name\n",
144 dest
, rename_pairs
[cur_set
][i
].src
);
146 "This warning is okay if you have multiple levels of hierarchy!\n");
157 void s_rename_add(char *src
, char *dest
)
162 if (src
== NULL
|| dest
== NULL
) {
166 flag
= s_rename_search(src
, dest
, FALSE
);
169 // Rename_counter may be incremented within this loop, so it cannot
170 // be used in the loop exit condition. Just iterate over the number
171 // of renames that were in the list at the start of the loop.
172 int orig_rename_counter
= rename_counter
;
173 for (i
= 0; i
< orig_rename_counter
; i
++) {
174 if (rename_pairs
[cur_set
][i
].src
175 && rename_pairs
[cur_set
][i
].dest
) {
176 if (strcmp(dest
, rename_pairs
[cur_set
][i
].src
) == 0) {
179 ("Found dest [%s] in src [%s] and that had a dest as: [%s]\nSo you want rename [%s] to [%s]\n",
180 dest
, rename_pairs
[cur_set
][i
].src
,
181 rename_pairs
[cur_set
][i
].dest
,
182 src
, rename_pairs
[cur_set
][i
].dest
);
185 rename_pairs
[cur_set
][rename_counter
].src
=
186 (char *) g_malloc(sizeof(char) * (strlen(src
) + 1));
187 strcpy(rename_pairs
[cur_set
][rename_counter
].src
, src
);
188 rename_pairs
[cur_set
][rename_counter
].dest
=
189 (char *) g_malloc(sizeof(char) *
191 (rename_pairs
[cur_set
][i
].dest
) +
193 strcpy(rename_pairs
[cur_set
][rename_counter
].dest
,
194 rename_pairs
[cur_set
][i
].dest
);
201 rename_pairs
[cur_set
][rename_counter
].src
=
202 (char *) g_malloc(sizeof(char) * (strlen(src
) + 1));
203 strcpy(rename_pairs
[cur_set
][rename_counter
].src
, src
);
204 rename_pairs
[cur_set
][rename_counter
].dest
=
205 (char *) g_malloc(sizeof(char) * (strlen(dest
) + 1));
206 strcpy(rename_pairs
[cur_set
][rename_counter
].dest
, dest
);
209 if (rename_counter
== MAX_RENAME
) {
211 "Increase number of rename_pairs (MAX_RENAME) in s_rename.c\n");
217 void s_rename_all_lowlevel(NETLIST
* netlist_head
, char *src
, char *dest
)
219 NETLIST
*nl_current
= NULL
;
220 CPINLIST
*pl_current
;
222 nl_current
= netlist_head
;
224 while (nl_current
!= NULL
) {
225 if (nl_current
->cpins
) {
226 pl_current
= nl_current
->cpins
;
227 while (pl_current
!= NULL
) {
229 if (pl_current
->net_name
!= NULL
) {
231 if (strcmp(pl_current
->net_name
, src
) == 0) {
233 /* this is a bad idea */
234 /* because inside nets-> */
235 /* there is another pointer */
236 /*g_free(pl_current->net_name); */
238 pl_current
->net_name
=
239 g_malloc(sizeof(char) * (strlen(dest
) + 1));
240 strcpy(pl_current
->net_name
, dest
);
244 pl_current
= pl_current
->next
;
247 nl_current
= nl_current
->next
;
252 void s_rename_all(TOPLEVEL
* pr_current
, NETLIST
* netlist_head
)
260 for (i
= 0; i
< rename_counter
; i
++) {
265 printf("%d Renaming: %s -> %s\n", i
, rename_pairs
[cur_set
][i
].src
,
266 rename_pairs
[cur_set
][i
].dest
);
269 s_rename_all_lowlevel(netlist_head
,
270 rename_pairs
[cur_set
][i
].src
,
271 rename_pairs
[cur_set
][i
].dest
);