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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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"
47 #include "../include/gettext.h"
60 * \todo maybe make this dynamic */
61 static RENAME rename_pairs
[MAX_SETS
][MAX_RENAME
];
63 static int rename_counter
= 0;
64 static int cur_set
= 0;
67 /*! \brief Initialize the renaming data space
69 * Initialise the renaming data space by setting all the pair pointers
72 void s_rename_init(void)
76 for (i
= 0; i
< MAX_SETS
; i
++) {
77 for (j
= 0; j
< MAX_RENAME
; j
++) {
78 rename_pairs
[i
][j
].src
= NULL
;
79 rename_pairs
[i
][j
].dest
= NULL
;
86 /*! \brief Free all data referred to by the rename pairs
88 * Runs through the rename pairs and calls g_free() on the non-NULL
89 * entries, then sets the entry to NULL.
91 void s_rename_destroy_all(void)
95 for (i
= 0; i
< MAX_SETS
; i
++) {
96 for (j
= 0; j
< MAX_RENAME
; j
++) {
98 if (rename_pairs
[i
][j
].src
) {
99 g_free(rename_pairs
[i
][j
].src
);
100 rename_pairs
[i
][j
].src
= NULL
;
103 if (rename_pairs
[i
][j
].dest
) {
104 g_free(rename_pairs
[i
][j
].dest
);
105 rename_pairs
[i
][j
].dest
= NULL
;
113 void s_rename_next_set(void)
115 if (cur_set
== MAX_SETS
) {
117 _("Increase number of rename_pair sets in s_net.c\n"));
124 /*! \brief Print all rename sets
126 * Iterate through the array and print all the rename sets to stdout.
128 void s_rename_print(void)
132 for (i
= 0; i
< MAX_SETS
; i
++) {
133 for (j
= 0; j
< MAX_RENAME
; j
++) {
134 if (rename_pairs
[i
][j
].src
) {
135 printf(_("%d) Source: _%s_"), i
, rename_pairs
[i
][j
].src
);
138 if (rename_pairs
[i
][j
].dest
) {
139 printf(_(" -> Dest: _%s_\n"), rename_pairs
[i
][j
].dest
);
145 /*! \brief Search the rename sets
147 * Search through the rename sets looking for src and dest. If
148 * quiet_flag is true than don't print anything.
149 * \param src Source to search for
150 * \param dest Destination to search for
151 * \param quiet_flag Suppress printing if set to TRUE
152 * \returns TRUE if the
153 * src is found. If the dest is found, also return true, but warn
156 int s_rename_search(char *src
, char *dest
, int quiet_flag
)
159 for (i
= 0; i
< rename_counter
; i
++) {
161 if (rename_pairs
[cur_set
][i
].src
&& rename_pairs
[cur_set
][i
].dest
) {
163 if (strcmp(src
, rename_pairs
[cur_set
][i
].src
) == 0) {
167 if (strcmp(dest
, rename_pairs
[cur_set
][i
].src
) == 0) {
170 _("WARNING: Trying to rename something twice:\n\t%s and %s\nare both a src and dest name\n"
171 "This warning is okay if you have multiple levels of hierarchy!\n"),
172 dest
, rename_pairs
[cur_set
][i
].src
);
183 /*! \brief Add to the rename pairs
185 * Add a source and destination to the rename pairs.
186 * \param src Source to add
187 * \param dest Destination to add
190 void s_rename_add(char *src
, char *dest
)
195 if (src
== NULL
|| dest
== NULL
) {
199 flag
= s_rename_search(src
, dest
, FALSE
);
202 // Rename_counter may be incremented within this loop, so it cannot
203 // be used in the loop exit condition. Just iterate over the number
204 // of renames that were in the list at the start of the loop.
205 int orig_rename_counter
= rename_counter
;
206 for (i
= 0; i
< orig_rename_counter
; i
++) {
207 if (rename_pairs
[cur_set
][i
].src
208 && rename_pairs
[cur_set
][i
].dest
) {
209 if (strcmp(dest
, rename_pairs
[cur_set
][i
].src
) == 0) {
212 ("Found dest [%s] in src [%s] and that had a dest as: [%s]\nSo you want rename [%s] to [%s]\n",
213 dest
, rename_pairs
[cur_set
][i
].src
,
214 rename_pairs
[cur_set
][i
].dest
,
215 src
, rename_pairs
[cur_set
][i
].dest
);
218 rename_pairs
[cur_set
][rename_counter
].src
=
220 rename_pairs
[cur_set
][rename_counter
].dest
=
221 g_strdup(rename_pairs
[cur_set
][i
].dest
);
228 rename_pairs
[cur_set
][rename_counter
].src
=
230 rename_pairs
[cur_set
][rename_counter
].dest
=
234 if (rename_counter
== MAX_RENAME
) {
236 _("Increase number of rename_pairs (MAX_RENAME) in s_rename.c\n"));
244 void s_rename_all_lowlevel(NETLIST
* netlist_head
, char *src
, char *dest
)
246 NETLIST
*nl_current
= NULL
;
247 CPINLIST
*pl_current
;
249 nl_current
= netlist_head
;
251 while (nl_current
!= NULL
) {
252 if (nl_current
->cpins
) {
253 pl_current
= nl_current
->cpins
;
254 while (pl_current
!= NULL
) {
256 if (pl_current
->net_name
!= NULL
) {
258 if (strcmp(pl_current
->net_name
, src
) == 0) {
260 /* this is a bad idea */
261 /* because inside nets-> */
262 /* there is another pointer */
263 /*g_free(pl_current->net_name); */
265 pl_current
->net_name
=
270 pl_current
= pl_current
->next
;
273 nl_current
= nl_current
->next
;
278 void s_rename_all (TOPLEVEL
*toplevel
, NETLIST
* netlist_head
)
286 for (i
= 0; i
< rename_counter
; i
++) {
291 printf("%d Renaming: %s -> %s\n", i
, rename_pairs
[cur_set
][i
].src
,
292 rename_pairs
[cur_set
][i
].dest
);
295 s_rename_all_lowlevel(netlist_head
,
296 rename_pairs
[cur_set
][i
].src
,
297 rename_pairs
[cur_set
][i
].dest
);