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"
49 #ifdef HAVE_LIBDMALLOC
64 * \todo maybe make this dynamic */
65 static RENAME rename_pairs
[MAX_SETS
][MAX_RENAME
];
67 static int rename_counter
= 0;
68 static int cur_set
= 0;
71 /*! \brief Initialize the renaming data space
73 * Initialise the renaming data space by setting all the pair pointers
76 void s_rename_init(void)
80 for (i
= 0; i
< MAX_SETS
; i
++) {
81 for (j
= 0; j
< MAX_RENAME
; j
++) {
82 rename_pairs
[i
][j
].src
= NULL
;
83 rename_pairs
[i
][j
].dest
= NULL
;
90 /*! \brief Free all data referred to by the rename pairs
92 * Runs through the rename pairs and calls g_free() on the non-NULL
93 * entries, then sets the entry to NULL.
95 void s_rename_destroy_all(void)
99 for (i
= 0; i
< MAX_SETS
; i
++) {
100 for (j
= 0; j
< MAX_RENAME
; j
++) {
102 if (rename_pairs
[i
][j
].src
) {
103 g_free(rename_pairs
[i
][j
].src
);
104 rename_pairs
[i
][j
].src
= NULL
;
107 if (rename_pairs
[i
][j
].dest
) {
108 g_free(rename_pairs
[i
][j
].dest
);
109 rename_pairs
[i
][j
].dest
= NULL
;
117 void s_rename_next_set(void)
119 if (cur_set
== MAX_SETS
) {
121 _("Increase number of rename_pair sets in s_net.c\n"));
128 /*! \brief Print all rename sets
130 * Iterate through the array and print all the rename sets to stdout.
132 void s_rename_print(void)
136 for (i
= 0; i
< MAX_SETS
; i
++) {
137 for (j
= 0; j
< MAX_RENAME
; j
++) {
138 if (rename_pairs
[i
][j
].src
) {
139 printf(_("%d) Source: _%s_"), i
, rename_pairs
[i
][j
].src
);
142 if (rename_pairs
[i
][j
].dest
) {
143 printf(_(" -> Dest: _%s_\n"), rename_pairs
[i
][j
].dest
);
149 /*! \brief Search the rename sets
151 * Search through the rename sets looking for src and dest. If
152 * quiet_flag is true than don't print anything.
153 * \param src Source to search for
154 * \param dest Destination to search for
155 * \param quiet_flag Suppress printing if set to TRUE
156 * \returns TRUE if the
157 * src is found. If the dest is found, also return true, but warn
160 int s_rename_search(char *src
, char *dest
, int quiet_flag
)
163 for (i
= 0; i
< rename_counter
; i
++) {
165 if (rename_pairs
[cur_set
][i
].src
&& rename_pairs
[cur_set
][i
].dest
) {
167 if (strcmp(src
, rename_pairs
[cur_set
][i
].src
) == 0) {
171 if (strcmp(dest
, rename_pairs
[cur_set
][i
].src
) == 0) {
174 _("WARNING: Trying to rename something twice:\n\t%s and %s\nare both a src and dest name\n"
175 "This warning is okay if you have multiple levels of hierarchy!\n"),
176 dest
, rename_pairs
[cur_set
][i
].src
);
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
*toplevel
, 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
);