netlist: Apply title patch to Python netlist backend
[geda-gaf.git] / gattrib / src / s_rename.c
blob5169ec413885cc310937caf2dfa0836034a29080
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
20 /*!
21 * \file
22 * \brief Functions to rename STRING_LIST contents
24 * Functions to rename STRING_LIST contents
26 #include <config.h>
28 #include <stdio.h>
29 #include <ctype.h>
30 #ifdef HAVE_STRING_H
31 #include <string.h>
32 #endif
33 #ifdef HAVE_STDLIB_H
34 #include <stdlib.h>
35 #endif
36 #ifdef HAVE_ASSERT_H
37 #include <assert.h>
38 #endif
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"
51 typedef struct {
52 char *src;
53 char *dest;
54 } RENAME;
56 #define MAX_RENAME 64
57 #define MAX_SETS 10
59 /*! size is fixed...
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
70 * to NULL.
72 void s_rename_init(void)
74 int i, j;
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;
82 rename_counter = 0;
83 cur_set = 0;
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)
93 int i, j;
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;
109 rename_counter = 0;
110 cur_set = 0;
113 void s_rename_next_set(void)
115 if (cur_set == MAX_SETS) {
116 fprintf(stderr,
117 _("Increase number of rename_pair sets in s_net.c\n"));
118 exit(-1);
120 cur_set++;
121 rename_counter = 0;
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)
130 int i,j;
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
154 * user
156 int s_rename_search(char *src, char *dest, int quiet_flag)
158 int i;
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) {
164 return (TRUE);
167 if (strcmp(dest, rename_pairs[cur_set][i].src) == 0) {
168 if (!quiet_flag) {
169 fprintf(stderr,
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);
174 return (TRUE);
180 return (FALSE);
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)
192 int flag;
193 int i;
195 if (src == NULL || dest == NULL) {
196 return;
199 flag = s_rename_search(src, dest, FALSE);
201 if (flag) {
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) {
210 #if DEBUG
211 printf
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);
216 #endif
218 rename_pairs[cur_set][rename_counter].src =
219 g_strdup(src);
220 rename_pairs[cur_set][rename_counter].dest =
221 g_strdup(rename_pairs[cur_set][i].dest);
222 rename_counter++;
226 } else {
228 rename_pairs[cur_set][rename_counter].src =
229 g_strdup(src);
230 rename_pairs[cur_set][rename_counter].dest =
231 g_strdup(dest);
232 rename_counter++;
234 if (rename_counter == MAX_RENAME) {
235 fprintf(stderr,
236 _("Increase number of rename_pairs (MAX_RENAME) in s_rename.c\n"));
237 exit(-1);
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 =
266 g_strdup(dest);
270 pl_current = pl_current->next;
273 nl_current = nl_current->next;
278 void s_rename_all (TOPLEVEL *toplevel, NETLIST * netlist_head)
280 int i;
282 #if DEBUG
283 s_rename_print();
284 #endif
286 for (i = 0; i < rename_counter; i++) {
288 verbose_print("R");
290 #if DEBUG
291 printf("%d Renaming: %s -> %s\n", i, rename_pairs[cur_set][i].src,
292 rename_pairs[cur_set][i].dest);
293 #endif
295 s_rename_all_lowlevel(netlist_head,
296 rename_pairs[cur_set][i].src,
297 rename_pairs[cur_set][i].dest);