gnetlist

s_netattrib.c

Go to the documentation of this file.
00001 /* gEDA - GPL Electronic Design Automation
00002  * gnetlist - gEDA Netlist
00003  * Copyright (C) 1998-2010 Ales Hvezda
00004  * Copyright (C) 1998-2010 gEDA Contributors (see ChangeLog for details)
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License 
00017  * along with this program; if not, write to the Free Software 
00018  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00019  */
00020 
00021 #include <config.h>
00022 
00023 #include <stdio.h>
00024 #include <ctype.h>
00025 #ifdef HAVE_STDLIB_H
00026 #include <stdlib.h>
00027 #endif
00028 #ifdef HAVE_ASSERT_H
00029 #include <assert.h>
00030 #endif
00031 #ifdef HAVE_STRING_H
00032 #include <string.h>
00033 #endif
00034 
00035 #include <libgeda/libgeda.h>
00036 
00037 #include "../include/globals.h"
00038 #include "../include/prototype.h"
00039 
00040 #ifdef HAVE_LIBDMALLOC
00041 #include <dmalloc.h>
00042 #endif
00043 
00044 /* used by the extract functions below */
00045 #define DELIMITERS ",; "
00046 
00047 /* things to do here : */
00048 /* write the net alias function */
00049 
00050 /* be sure to g_free returned string */
00051 char *s_netattrib_extract_netname(char *value)
00052 {
00053     char *return_value = NULL;
00054     int i = 0;
00055 
00056     /* a bit larger than needed ... */
00057     return_value = g_strdup (value);
00058 
00059     while (value[i] != ':' && value[i] != '\0') {
00060     return_value[i] = value[i];
00061     i++;
00062     }
00063 
00064     if (value[i] != ':') {
00065     fprintf(stderr, "Found malformed net attribute\n");
00066     return (g_strdup ("unknown"));
00067     }
00068 
00069     return_value[i] = '\0';
00070 
00071     return (return_value);
00072 
00073 }
00074 
00075 /* if this function creates a cpinlist list, it will not have a head node */
00076 void
00077 s_netattrib_create_pins(TOPLEVEL * pr_current, OBJECT * o_current,
00078             NETLIST * netlist, char *value,
00079             char *hierarchy_tag)
00080 {
00081     NETLIST *netlist_tail = NULL;
00082     CPINLIST *cpinlist_tail = NULL;
00083     CPINLIST *new_cpin = NULL;
00084     CPINLIST *old_cpin = NULL;
00085     char *connected_to = NULL;
00086     char *net_name = NULL;
00087     char *start_of_pinlist = NULL;
00088     char *char_ptr = NULL;
00089     char *current_pin = NULL;
00090 
00091 
00092     char_ptr = strchr(value, ':');
00093 
00094     if (char_ptr == NULL) {
00095     return;
00096     }
00097 
00098 
00099     net_name = s_netattrib_extract_netname(value);
00100 
00101     /* skip over first : */
00102     start_of_pinlist = char_ptr + 1;
00103     current_pin = strtok(start_of_pinlist, DELIMITERS);
00104     while (current_pin) {
00105 
00106     netlist_tail = s_netlist_return_tail(netlist);
00107     cpinlist_tail = s_cpinlist_return_tail(netlist_tail->cpins);
00108 
00109     if (netlist->component_uref) {
00110 
00111         old_cpin =
00112         s_cpinlist_search_pin(netlist_tail->cpins, current_pin);
00113 
00114         if (old_cpin) {
00115 
00116         g_assert (old_cpin->nets != NULL);
00117 
00118         if (old_cpin->nets->net_name) {
00119             fprintf(stderr,
00120                 "Found a cpinlist head with a netname! [%s]\n",
00121                 old_cpin->nets->net_name);
00122             g_free(old_cpin->nets->net_name);
00123         }
00124 
00125 
00126         old_cpin->nets->net_name =
00127             s_hierarchy_create_netattrib(pr_current, net_name,
00128                          hierarchy_tag);
00129         old_cpin->nets->net_name_has_priority = TRUE;
00130         connected_to = g_strdup_printf("%s %s",
00131                                    netlist->component_uref,
00132                                    current_pin);
00133         old_cpin->nets->connected_to = g_strdup(connected_to);
00134         old_cpin->nets->nid = o_current->sid;
00135         g_free(connected_to);
00136         } else {
00137 
00138 
00139         new_cpin = s_cpinlist_add(cpinlist_tail);
00140 
00141         new_cpin->pin_number = g_strdup (current_pin);
00142         new_cpin->net_name = NULL;
00143 
00144         new_cpin->plid = o_current->sid;
00145 
00146         new_cpin->nets = s_net_add(NULL);
00147         new_cpin->nets->net_name_has_priority = TRUE;
00148         new_cpin->nets->net_name =
00149             s_hierarchy_create_netattrib(pr_current, net_name,
00150                          hierarchy_tag);
00151 
00152         connected_to = g_strdup_printf("%s %s",
00153                                    netlist->component_uref,
00154                                    current_pin);
00155         new_cpin->nets->connected_to = g_strdup(connected_to);
00156         new_cpin->nets->nid = o_current->sid;
00157 
00158 #if DEBUG
00159         printf("Finished creating: %s\n", connected_to);
00160         printf("netname: %s %s\n", new_cpin->nets->net_name,
00161                hierarchy_tag);
00162 #endif
00163 
00164         g_free(connected_to);
00165         }
00166 
00167     } else {        /* no uref, means this is a special component */
00168 
00169     }
00170     current_pin = strtok(NULL, DELIMITERS);
00171     }
00172 
00173     g_free(net_name);
00174 }
00175 
00176 
00177 void
00178 s_netattrib_handle (TOPLEVEL * pr_current, OBJECT * o_current,
00179                     NETLIST * netlist, char *hierarchy_tag)
00180 {
00181   char *value;
00182   int counter;
00183 
00184   /* for now just look inside the component */
00185   for (counter = 0; ;) {
00186     value = o_attrib_search_inherited_attribs_by_name (o_current,
00187                                                        "net", counter);
00188     if (value == NULL)
00189       break;
00190 
00191     counter++;
00192 
00193     s_netattrib_create_pins (pr_current, o_current,
00194                              netlist, value, hierarchy_tag);
00195     g_free (value);
00196   }
00197 
00198   /* now look outside the component */
00199   for (counter = 0; ;) {
00200     value = o_attrib_search_attached_attribs_by_name (o_current,
00201                                                       "net", counter);
00202     if (value == NULL)
00203       break;
00204 
00205     counter++;
00206 
00207     s_netattrib_create_pins (pr_current, o_current,
00208                              netlist, value, hierarchy_tag);
00209     g_free (value);
00210   }
00211 }
00212 
00213 char *s_netattrib_net_search (OBJECT * o_current, char *wanted_pin)
00214 {
00215   char *value = NULL;
00216   char *char_ptr = NULL;
00217   char *net_name = NULL;
00218   char *current_pin = NULL;
00219   char *start_of_pinlist = NULL;
00220   char *return_value = NULL;
00221   int counter;
00222 
00223   if (o_current == NULL ||
00224       o_current->complex == NULL)
00225     return NULL;
00226 
00227   /* for now just look inside the component */
00228   for (counter = 0; ;) {
00229     value = o_attrib_search_inherited_attribs_by_name (o_current,
00230                                                        "net", counter);
00231     if (value == NULL)
00232       break;
00233 
00234     counter++;
00235 
00236     char_ptr = strchr (value, ':');
00237     if (char_ptr == NULL) {
00238       fprintf (stderr, "Got an invalid net= attrib [net=%s]\n"
00239                        "Missing : in net= attrib\n", value);
00240       g_free (value);
00241       return NULL;
00242     }
00243 
00244     net_name = s_netattrib_extract_netname (value);
00245 
00246     start_of_pinlist = char_ptr + 1;
00247     current_pin = strtok (start_of_pinlist, DELIMITERS);
00248     while (current_pin && !return_value) {
00249       if (strcmp (current_pin, wanted_pin) == 0) {
00250         return_value = net_name;
00251       }
00252       current_pin = strtok (NULL, DELIMITERS);
00253     }
00254 
00255     g_free (value);
00256   }
00257 
00258   /* now look outside the component */
00259   for (counter = 0; ;) {
00260     value = o_attrib_search_attached_attribs_by_name (o_current,
00261                                                       "net", counter);
00262     if (value == NULL)
00263       break;
00264 
00265     counter++;
00266 
00267     char_ptr = strchr (value, ':');
00268     if (char_ptr == NULL) {
00269       fprintf (stderr, "Got an invalid net= attrib [net=%s]\n"
00270                        "Missing : in net= attrib\n", value);
00271       g_free (value);
00272       return NULL;
00273     }
00274 
00275     net_name = s_netattrib_extract_netname (value);
00276 
00277     start_of_pinlist = char_ptr + 1;
00278     current_pin = strtok (start_of_pinlist, DELIMITERS);
00279     while (current_pin) {
00280       if (strcmp (current_pin, wanted_pin) == 0) {
00281         g_free (return_value);
00282         return net_name;
00283       }
00284       current_pin = strtok (NULL, DELIMITERS);
00285     }
00286 
00287     g_free (value);
00288   }
00289 
00290   return return_value;
00291 }
00292 
00293 char *s_netattrib_return_netname(TOPLEVEL * pr_current, OBJECT * o_current,
00294                  char *pinnumber, char *hierarchy_tag)
00295 {
00296     char *current_pin;
00297     char *netname;
00298     char *temp_netname;
00299 
00300 #if DEBUG
00301     printf("extract return netname here\n");
00302 #endif
00303 
00304     /* skip over POWER tag */
00305     (void) strtok(pinnumber, " ");
00306 
00307     current_pin = strtok(NULL, " ");
00308     if (current_pin == NULL) {
00309     return (NULL);
00310     }
00311 #if DEBUG
00312     printf("inside return_netname: %s\n", current_pin);
00313 #endif
00314 
00315     /* use hierarchy tag here to make this net uniq */
00316     temp_netname = s_netattrib_net_search(o_current->parent,
00317                                           current_pin);
00318 
00319     netname =
00320     s_hierarchy_create_netattrib(pr_current, temp_netname,
00321                      hierarchy_tag);
00322 
00323 #if DEBUG
00324     printf("netname: %s\n", netname);
00325 #endif
00326 
00327     return (netname);
00328 }
 All Data Structures Files Functions Variables Defines