gattrib: Multiple printf() concatenated into single printf() in s_rename.c.
[geda-gaf/pcjc2.git] / gattrib / src / s_rename.c
blobe1b8dc521562a6e8b20dd92c558ffd6fce5dedf3
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"
49 #ifdef HAVE_LIBDMALLOC
50 #include <dmalloc.h>
51 #endif
55 typedef struct {
56 char *src;
57 char *dest;
58 } RENAME;
60 #define MAX_RENAME 64
61 #define MAX_SETS 10
63 /*! size is fixed...
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
74 * to NULL.
76 void s_rename_init(void)
78 int i, j;
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;
86 rename_counter = 0;
87 cur_set = 0;
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)
97 int i, j;
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;
113 rename_counter = 0;
114 cur_set = 0;
117 void s_rename_next_set(void)
119 if (cur_set == MAX_SETS) {
120 fprintf(stderr,
121 _("Increase number of rename_pair sets in s_net.c\n"));
122 exit(-1);
124 cur_set++;
125 rename_counter = 0;
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)
134 int i,j;
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
158 * user
160 int s_rename_search(char *src, char *dest, int quiet_flag)
162 int i;
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) {
168 return (TRUE);
171 if (strcmp(dest, rename_pairs[cur_set][i].src) == 0) {
172 if (!quiet_flag) {
173 fprintf(stderr,
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);
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 *toplevel, 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);