gattrib

s_rename.c

Go to the documentation of this file.
00001 /* gEDA - GPL Electronic Design Automation
00002  * gattrib -- gEDA component and net attribute manipulation using spreadsheet.
00003  * Copyright (C) 2003-2010 Stuart D. Brorson.
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation; either version 2 of the License, or
00008  * (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License 
00016  * along with this program; if not, write to the Free Software 
00017  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00018  */
00019 
00026 #include <config.h>
00027 
00028 #include <stdio.h>
00029 #include <ctype.h>
00030 #ifdef HAVE_STRING_H
00031 #include <string.h>
00032 #endif
00033 #ifdef HAVE_STDLIB_H
00034 #include <stdlib.h>
00035 #endif
00036 #ifdef HAVE_ASSERT_H
00037 #include <assert.h>
00038 #endif
00039 
00040 /*------------------------------------------------------------------
00041  * Gattrib specific includes
00042  *------------------------------------------------------------------*/
00043 #include <libgeda/libgeda.h>       /* geda library fcns  */
00044 #include "../include/struct.h"     /* typdef and struct declarations */
00045 #include "../include/prototype.h"  /* function prototypes */
00046 #include "../include/globals.h"
00047 
00048 #ifdef HAVE_LIBDMALLOC
00049 #include <dmalloc.h>
00050 #endif
00051 
00052 
00053 
00054 typedef struct {
00055     char *src;
00056     char *dest;
00057 } RENAME;
00058 
00059 #define MAX_RENAME 64
00060 #define MAX_SETS 10
00061 
00064 static RENAME rename_pairs[MAX_SETS][MAX_RENAME];
00065 
00066 static int rename_counter = 0;
00067 static int cur_set = 0;
00068 
00069 
00075 void s_rename_init(void)
00076 {
00077     int i, j;
00078 
00079     for (i = 0; i < MAX_SETS; i++) {
00080     for (j = 0; j < MAX_RENAME; j++) {
00081         rename_pairs[i][j].src = NULL;
00082         rename_pairs[i][j].dest = NULL;
00083     }
00084     }
00085     rename_counter = 0;
00086     cur_set = 0;
00087 }
00088 
00094 void s_rename_destroy_all(void)
00095 {
00096     int i, j;
00097 
00098     for (i = 0; i < MAX_SETS; i++) {
00099     for (j = 0; j < MAX_RENAME; j++) {
00100 
00101         if (rename_pairs[i][j].src) {
00102         g_free(rename_pairs[i][j].src);
00103         rename_pairs[i][j].src = NULL;
00104         }
00105 
00106         if (rename_pairs[i][j].dest) {
00107         g_free(rename_pairs[i][j].dest);
00108         rename_pairs[i][j].dest = NULL;
00109         }
00110     }
00111     }
00112     rename_counter = 0;
00113     cur_set = 0;
00114 }
00115 
00116 void s_rename_next_set(void)
00117 {
00118     if (cur_set == MAX_SETS) {
00119     fprintf(stderr,
00120         "Increase number of rename_pair sets in s_net.c\n");
00121     exit(-1);
00122     }
00123     cur_set++;
00124     rename_counter = 0;
00125 }
00126 
00131 void s_rename_print(void)
00132 {
00133     int i,j;
00134 
00135     for (i = 0; i < MAX_SETS; i++) {
00136     for (j = 0; j < MAX_RENAME; j++) {
00137         if (rename_pairs[i][j].src) {
00138         printf("%d) Source: _%s_", i, rename_pairs[i][j].src);
00139         }
00140 
00141         if (rename_pairs[i][j].dest) {
00142         printf(" -> Dest: _%s_\n", rename_pairs[i][j].dest);
00143         } 
00144     }
00145     }
00146 }
00147 
00159 int s_rename_search(char *src, char *dest, int quiet_flag)
00160 {
00161     int i;
00162     for (i = 0; i < rename_counter; i++) {
00163 
00164     if (rename_pairs[cur_set][i].src && rename_pairs[cur_set][i].dest) {
00165 
00166         if (strcmp(src, rename_pairs[cur_set][i].src) == 0) {
00167         return (TRUE);
00168         }
00169 
00170         if (strcmp(dest, rename_pairs[cur_set][i].src) == 0) {
00171         if (!quiet_flag) {
00172             fprintf(stderr,
00173                 "WARNING: Trying to rename something twice:\n\t%s and %s\nare both a src and dest name\n",
00174                 dest, rename_pairs[cur_set][i].src);
00175             fprintf(stderr,
00176                 "This warning is okay if you have multiple levels of hierarchy!\n");
00177         }
00178         return (TRUE);
00179         }
00180     }
00181 
00182     }
00183 
00184     return (FALSE);
00185 }
00186 
00194 void s_rename_add(char *src, char *dest)
00195 {
00196     int flag;
00197     int i;
00198 
00199     if (src == NULL || dest == NULL) {
00200     return;
00201     }
00202 
00203     flag = s_rename_search(src, dest, FALSE);
00204 
00205     if (flag) {
00206         // Rename_counter may be incremented within this loop, so it cannot
00207     // be used in the loop exit condition.  Just iterate over the number
00208     // of renames that were in the list at the start of the loop.
00209         int orig_rename_counter = rename_counter;
00210     for (i = 0; i < orig_rename_counter; i++) {
00211         if (rename_pairs[cur_set][i].src
00212         && rename_pairs[cur_set][i].dest) {
00213         if (strcmp(dest, rename_pairs[cur_set][i].src) == 0) {
00214 #if DEBUG
00215             printf
00216             ("Found dest [%s] in src [%s] and that had a dest as: [%s]\nSo you want rename [%s] to [%s]\n",
00217              dest, rename_pairs[cur_set][i].src,
00218              rename_pairs[cur_set][i].dest,
00219              src, rename_pairs[cur_set][i].dest);
00220 #endif
00221 
00222             rename_pairs[cur_set][rename_counter].src =
00223             g_strdup(src);
00224             rename_pairs[cur_set][rename_counter].dest =
00225             g_strdup(rename_pairs[cur_set][i].dest);
00226             rename_counter++;
00227         }
00228         }
00229     }
00230     } else {
00231 
00232     rename_pairs[cur_set][rename_counter].src =
00233         g_strdup(src);
00234     rename_pairs[cur_set][rename_counter].dest =
00235         g_strdup(dest);
00236     rename_counter++;
00237     }
00238     if (rename_counter == MAX_RENAME) {
00239     fprintf(stderr,
00240         "Increase number of rename_pairs (MAX_RENAME) in s_rename.c\n");
00241     exit(-1);
00242     }
00243 
00244 }
00245 
00246 
00247 
00248 void s_rename_all_lowlevel(NETLIST * netlist_head, char *src, char *dest)
00249 {
00250     NETLIST *nl_current = NULL;
00251     CPINLIST *pl_current;
00252 
00253     nl_current = netlist_head;
00254 
00255     while (nl_current != NULL) {
00256     if (nl_current->cpins) {
00257         pl_current = nl_current->cpins;
00258         while (pl_current != NULL) {
00259 
00260         if (pl_current->net_name != NULL) {
00261 
00262             if (strcmp(pl_current->net_name, src) == 0) {
00263 
00264             /* this is a bad idea */
00265             /* because inside nets-> */
00266             /* there is another pointer */
00267             /*g_free(pl_current->net_name); */
00268 
00269             pl_current->net_name =
00270                 g_strdup(dest);
00271             }
00272         }
00273 
00274         pl_current = pl_current->next;
00275         }
00276     }
00277     nl_current = nl_current->next;
00278     }
00279 
00280 }
00281 
00282 void s_rename_all (TOPLEVEL *toplevel, NETLIST * netlist_head)
00283 {
00284     int i;
00285 
00286 #if DEBUG
00287     s_rename_print();
00288 #endif
00289 
00290     for (i = 0; i < rename_counter; i++) {
00291 
00292     verbose_print("R");
00293 
00294 #if DEBUG 
00295     printf("%d Renaming: %s -> %s\n", i, rename_pairs[cur_set][i].src,
00296            rename_pairs[cur_set][i].dest);
00297 #endif
00298 
00299     s_rename_all_lowlevel(netlist_head,
00300                   rename_pairs[cur_set][i].src,
00301                   rename_pairs[cur_set][i].dest);
00302     }
00303 }
00304 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines