Updated all po files by running make update-po (in prep for a release)
[geda-gaf/whiteaudio.git] / gattrib / src / s_rename.c
blobd8bd735982f3646c08e43acc63f256cafc9484e9
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
20 #include <config.h>
22 #include <stdio.h>
23 #include <ctype.h>
24 #ifdef HAVE_STRING_H
25 #include <string.h>
26 #endif
27 #ifdef HAVE_STDLIB_H
28 #include <stdlib.h>
29 #endif
30 #ifdef HAVE_ASSERT_H
31 #include <assert.h>
32 #endif
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
43 #include <dmalloc.h>
44 #endif
48 typedef struct {
49 char *src;
50 char *dest;
51 } RENAME;
53 #define MAX_RENAME 64
54 #define MAX_SETS 10
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)
64 int i, j;
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;
72 rename_counter = 0;
73 cur_set = 0;
76 void s_rename_destroy_all(void)
78 int i, j;
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;
94 rename_counter = 0;
95 cur_set = 0;
98 void s_rename_next_set(void)
100 if (cur_set == MAX_SETS) {
101 fprintf(stderr,
102 "Increase number of rename_pair sets in s_net.c\n");
103 exit(-1);
105 cur_set++;
106 rename_counter = 0;
109 void s_rename_print(void)
111 int i,j;
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)
131 int i;
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) {
137 return (TRUE);
140 if (strcmp(dest, rename_pairs[cur_set][i].src) == 0) {
141 if (!quiet_flag) {
142 fprintf(stderr,
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);
145 fprintf(stderr,
146 "This warning is okay if you have multiple levels of hierarchy!\n");
148 return (TRUE);
154 return (FALSE);
157 void s_rename_add(char *src, char *dest)
159 int flag;
160 int i;
162 if (src == NULL || dest == NULL) {
163 return;
166 flag = s_rename_search(src, dest, FALSE);
168 if (flag) {
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) {
177 #if DEBUG
178 printf
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);
183 #endif
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) *
190 (strlen
191 (rename_pairs[cur_set][i].dest) +
192 1));
193 strcpy(rename_pairs[cur_set][rename_counter].dest,
194 rename_pairs[cur_set][i].dest);
195 rename_counter++;
199 } else {
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);
207 rename_counter++;
209 if (rename_counter == MAX_RENAME) {
210 fprintf(stderr,
211 "Increase number of rename_pairs (MAX_RENAME) in s_rename.c\n");
212 exit(-1);
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)
254 int i;
256 #if DEBUG
257 s_rename_print();
258 #endif
260 for (i = 0; i < rename_counter; i++) {
262 verbose_print("R");
264 #if DEBUG
265 printf("%d Renaming: %s -> %s\n", i, rename_pairs[cur_set][i].src,
266 rename_pairs[cur_set][i].dest);
267 #endif
269 s_rename_all_lowlevel(netlist_head,
270 rename_pairs[cur_set][i].src,
271 rename_pairs[cur_set][i].dest);