Updated copyright text/header in most source files.
[geda-gaf/peter-b.git] / gattrib / src / s_rename.c
bloba647303067dd7bf06202f6dea4ef3a0a0515c260
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
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"
48 #ifdef HAVE_LIBDMALLOC
49 #include <dmalloc.h>
50 #endif
54 typedef struct {
55 char *src;
56 char *dest;
57 } RENAME;
59 #define MAX_RENAME 64
60 #define MAX_SETS 10
62 /*! size is fixed...
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
73 * to NULL.
75 void s_rename_init(void)
77 int i, j;
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;
85 rename_counter = 0;
86 cur_set = 0;
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)
96 int i, j;
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;
112 rename_counter = 0;
113 cur_set = 0;
116 void s_rename_next_set(void)
118 if (cur_set == MAX_SETS) {
119 fprintf(stderr,
120 "Increase number of rename_pair sets in s_net.c\n");
121 exit(-1);
123 cur_set++;
124 rename_counter = 0;
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)
133 int i,j;
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
157 * user
159 int s_rename_search(char *src, char *dest, int quiet_flag)
161 int i;
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) {
167 return (TRUE);
170 if (strcmp(dest, rename_pairs[cur_set][i].src) == 0) {
171 if (!quiet_flag) {
172 fprintf(stderr,
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);
175 fprintf(stderr,
176 "This warning is okay if you have multiple levels of hierarchy!\n");
178 return (TRUE);
184 return (FALSE);
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)
196 int flag;
197 int i;
199 if (src == NULL || dest == NULL) {
200 return;
203 flag = s_rename_search(src, dest, FALSE);
205 if (flag) {
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) {
214 #if DEBUG
215 printf
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);
220 #endif
222 rename_pairs[cur_set][rename_counter].src =
223 g_strdup(src);
224 rename_pairs[cur_set][rename_counter].dest =
225 g_strdup(rename_pairs[cur_set][i].dest);
226 rename_counter++;
230 } else {
232 rename_pairs[cur_set][rename_counter].src =
233 g_strdup(src);
234 rename_pairs[cur_set][rename_counter].dest =
235 g_strdup(dest);
236 rename_counter++;
238 if (rename_counter == MAX_RENAME) {
239 fprintf(stderr,
240 "Increase number of rename_pairs (MAX_RENAME) in s_rename.c\n");
241 exit(-1);
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 =
270 g_strdup(dest);
274 pl_current = pl_current->next;
277 nl_current = nl_current->next;
282 void s_rename_all(TOPLEVEL * pr_current, NETLIST * netlist_head)
284 int i;
286 #if DEBUG
287 s_rename_print();
288 #endif
290 for (i = 0; i < rename_counter; i++) {
292 verbose_print("R");
294 #if DEBUG
295 printf("%d Renaming: %s -> %s\n", i, rename_pairs[cur_set][i].src,
296 rename_pairs[cur_set][i].dest);
297 #endif
299 s_rename_all_lowlevel(netlist_head,
300 rename_pairs[cur_set][i].src,
301 rename_pairs[cur_set][i].dest);