1 /* gEDA - GPL Electronic Design Automation
2 * gattrib -- gEDA component and net attribute manipulation using spreadsheet.
3 * Copyright (C) 2003-2010 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
22 * \brief Functions to rename STRING_LIST contents
24 * Functions to rename STRING_LIST contents
40 /*------------------------------------------------------------------
41 * Gattrib specific includes
42 *------------------------------------------------------------------*/
43 #include <libgeda/libgeda.h> /* geda library fcns */
44 #include "../include/struct.h" /* typdef and struct declarations */
45 #include "../include/prototype.h" /* function prototypes */
46 #include "../include/globals.h"
48 #ifdef HAVE_LIBDMALLOC
63 * \todo maybe make this dynamic */
64 static RENAME rename_pairs
[MAX_SETS
][MAX_RENAME
];
66 static int rename_counter
= 0;
67 static int cur_set
= 0;
70 /*! \brief Initialize the renaming data space
72 * Initialise the renaming data space by setting all the pair pointers
75 void s_rename_init(void)
79 for (i
= 0; i
< MAX_SETS
; i
++) {
80 for (j
= 0; j
< MAX_RENAME
; j
++) {
81 rename_pairs
[i
][j
].src
= NULL
;
82 rename_pairs
[i
][j
].dest
= NULL
;
89 /*! \brief Free all data referred to by the rename pairs
91 * Runs through the rename pairs and calls g_free() on the non-NULL
92 * entries, then sets the entry to NULL.
94 void s_rename_destroy_all(void)
98 for (i
= 0; i
< MAX_SETS
; i
++) {
99 for (j
= 0; j
< MAX_RENAME
; j
++) {
101 if (rename_pairs
[i
][j
].src
) {
102 g_free(rename_pairs
[i
][j
].src
);
103 rename_pairs
[i
][j
].src
= NULL
;
106 if (rename_pairs
[i
][j
].dest
) {
107 g_free(rename_pairs
[i
][j
].dest
);
108 rename_pairs
[i
][j
].dest
= NULL
;
116 void s_rename_next_set(void)
118 if (cur_set
== MAX_SETS
) {
120 "Increase number of rename_pair sets in s_net.c\n");
127 /*! \brief Print all rename sets
129 * Iterate through the array and print all the rename sets to stdout.
131 void s_rename_print(void)
135 for (i
= 0; i
< MAX_SETS
; i
++) {
136 for (j
= 0; j
< MAX_RENAME
; j
++) {
137 if (rename_pairs
[i
][j
].src
) {
138 printf("%d) Source: _%s_", i
, rename_pairs
[i
][j
].src
);
141 if (rename_pairs
[i
][j
].dest
) {
142 printf(" -> Dest: _%s_\n", rename_pairs
[i
][j
].dest
);
148 /*! \brief Search the rename sets
150 * Search through the rename sets looking for src and dest. If
151 * quiet_flag is true than don't print anything.
152 * \param src Source to search for
153 * \param dest Destination to search for
154 * \param quiet_flag Suppress printing if set to TRUE
155 * \returns TRUE if the
156 * src is found. If the dest is found, also return true, but warn
159 int s_rename_search(char *src
, char *dest
, int quiet_flag
)
162 for (i
= 0; i
< rename_counter
; i
++) {
164 if (rename_pairs
[cur_set
][i
].src
&& rename_pairs
[cur_set
][i
].dest
) {
166 if (strcmp(src
, rename_pairs
[cur_set
][i
].src
) == 0) {
170 if (strcmp(dest
, rename_pairs
[cur_set
][i
].src
) == 0) {
173 "WARNING: Trying to rename something twice:\n\t%s and %s\nare both a src and dest name\n",
174 dest
, rename_pairs
[cur_set
][i
].src
);
176 "This warning is okay if you have multiple levels of hierarchy!\n");
187 /*! \brief Add to the rename pairs
189 * Add a source and destination to the rename pairs.
190 * \param src Source to add
191 * \param dest Destination to add
194 void s_rename_add(char *src
, char *dest
)
199 if (src
== NULL
|| dest
== NULL
) {
203 flag
= s_rename_search(src
, dest
, FALSE
);
206 // Rename_counter may be incremented within this loop, so it cannot
207 // be used in the loop exit condition. Just iterate over the number
208 // of renames that were in the list at the start of the loop.
209 int orig_rename_counter
= rename_counter
;
210 for (i
= 0; i
< orig_rename_counter
; i
++) {
211 if (rename_pairs
[cur_set
][i
].src
212 && rename_pairs
[cur_set
][i
].dest
) {
213 if (strcmp(dest
, rename_pairs
[cur_set
][i
].src
) == 0) {
216 ("Found dest [%s] in src [%s] and that had a dest as: [%s]\nSo you want rename [%s] to [%s]\n",
217 dest
, rename_pairs
[cur_set
][i
].src
,
218 rename_pairs
[cur_set
][i
].dest
,
219 src
, rename_pairs
[cur_set
][i
].dest
);
222 rename_pairs
[cur_set
][rename_counter
].src
=
224 rename_pairs
[cur_set
][rename_counter
].dest
=
225 g_strdup(rename_pairs
[cur_set
][i
].dest
);
232 rename_pairs
[cur_set
][rename_counter
].src
=
234 rename_pairs
[cur_set
][rename_counter
].dest
=
238 if (rename_counter
== MAX_RENAME
) {
240 "Increase number of rename_pairs (MAX_RENAME) in s_rename.c\n");
248 void s_rename_all_lowlevel(NETLIST
* netlist_head
, char *src
, char *dest
)
250 NETLIST
*nl_current
= NULL
;
251 CPINLIST
*pl_current
;
253 nl_current
= netlist_head
;
255 while (nl_current
!= NULL
) {
256 if (nl_current
->cpins
) {
257 pl_current
= nl_current
->cpins
;
258 while (pl_current
!= NULL
) {
260 if (pl_current
->net_name
!= NULL
) {
262 if (strcmp(pl_current
->net_name
, src
) == 0) {
264 /* this is a bad idea */
265 /* because inside nets-> */
266 /* there is another pointer */
267 /*g_free(pl_current->net_name); */
269 pl_current
->net_name
=
274 pl_current
= pl_current
->next
;
277 nl_current
= nl_current
->next
;
282 void s_rename_all(TOPLEVEL
* pr_current
, NETLIST
* netlist_head
)
290 for (i
= 0; i
< rename_counter
; i
++) {
295 printf("%d Renaming: %s -> %s\n", i
, rename_pairs
[cur_set
][i
].src
,
296 rename_pairs
[cur_set
][i
].dest
);
299 s_rename_all_lowlevel(netlist_head
,
300 rename_pairs
[cur_set
][i
].src
,
301 rename_pairs
[cur_set
][i
].dest
);