libgeda

s_slot.c

Go to the documentation of this file.
00001 /* gEDA - GPL Electronic Design Automation
00002  * libgeda - gEDA's library
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 
00025 #include <config.h>
00026 
00027 #include <stdio.h>
00028 #ifdef HAVE_STRING_H
00029 #include <string.h>
00030 #endif
00031 #include <math.h>
00032 
00033 #include "libgeda_priv.h"
00034 
00035 #ifdef HAVE_LIBDMALLOC
00036 #include <dmalloc.h>
00037 #endif
00038 
00040 #define DELIMITERS ",; "
00041 
00042 
00056 char *s_slot_search_slot (OBJECT *object, OBJECT **return_found)
00057 {
00058   GList *attributes;
00059   OBJECT *attrib;
00060   char *value = NULL;
00061 
00062   attributes = o_attrib_return_attribs (object);
00063   attrib = o_attrib_find_attrib_by_name (attributes, "slot", 0);
00064   g_list_free (attributes);
00065 
00066   if (attrib != NULL)
00067     o_attrib_get_name_value (attrib, NULL, &value);
00068 
00069   if (return_found)
00070     *return_found = attrib;
00071 
00072   return value;
00073 }
00074 
00075 
00087 static char *s_slot_search_slotdef (OBJECT *object, int slotnumber)
00088 {
00089   int counter = 0;
00090   char *slotdef;
00091   char *search_for;
00092 
00093   search_for = g_strdup_printf ("%d:", slotnumber);
00094 
00095   while (1) {
00096     slotdef = o_attrib_search_object_attribs_by_name (object, "slotdef",
00097                                                       counter++);
00098     if (slotdef == NULL ||
00099         strncmp (slotdef, search_for, strlen (search_for)) == 0)
00100       break;
00101 
00102     g_free (slotdef);
00103   }
00104 
00105   g_free (search_for);
00106   return slotdef;
00107 }
00108 
00109 
00124 void s_slot_update_object (TOPLEVEL *toplevel, OBJECT *object)
00125 {
00126   OBJECT *o_pin_object;
00127   OBJECT *o_pinnum_attrib;
00128   GList *attributes;
00129   char *string;
00130   char *slotdef;
00131   char *pinseq;
00132   int slot;
00133   int slot_string;
00134   int pin_counter;    /* Internal pin counter private to this fcn. */
00135   char* current_pin;  /* text from slotdef= to be made into pinnumber= */
00136   char* cptr;         /* char pointer pointing to pinnumbers in slotdef=#:#,#,# string */
00137 
00138   /* For this particular graphic object (component instantiation) */
00139   /* get the slot number as a string */
00140   string = o_attrib_search_object_attribs_by_name (object, "slot", 0);
00141 
00142   if (string == NULL) {
00143     /* Did not find slot= attribute.
00144      * This happens if there is no attached slot attribute, and
00145      * there is no default slot= attribute inside the symbol.
00146      * Assume slot=1.
00147      */
00148     slot = 1;
00149     slot_string = 0;
00150   } else {
00151     slot_string = 1;
00152     slot = atoi (string);
00153     g_free (string);
00154   }
00155 
00156   /* OK, now that we have the slot number, use it to get the */
00157   /* corresponding slotdef=#:#,#,# string.  */
00158   slotdef = s_slot_search_slotdef (object, slot);
00159 
00160   if (slotdef == NULL) {
00161     if (slot_string) /* only an error if there's a slot string */
00162       s_log_message (_("Did not find slotdef=#:#,#,#... attribute\n"));
00163     return;
00164   }
00165 
00166   if (!strstr (slotdef, ":")) {
00167     /* Didn't find proper slotdef=#:... put warning into log */
00168     s_log_message (_("Improper slotdef syntax: missing \":\".\n"));
00169     g_free (slotdef);
00170     return;
00171   }
00172 
00173   /* skip over slotdef number */
00174   /* slotdef is in the form #:#,#,# */
00175   /* this code skips first #: */
00176   cptr = slotdef;
00177   while (*cptr != '\0' && *cptr != ':') {
00178     cptr++;
00179   }
00180   cptr++; /* skip colon */
00181 
00182   if (*cptr == '\0') {
00183     s_log_message (_("Did not find proper slotdef=#:#,#,#... attribute\n"));
00184     g_free (slotdef);
00185     return;
00186   }
00187 
00188   /* loop on all pins found in slotdef= attribute */
00189   pin_counter = 1;  /* internal pin_counter */
00190   /* get current pinnumber= from slotdef= attrib */
00191   current_pin = strtok (cptr, DELIMITERS);
00192   while (current_pin != NULL) {
00193     /* get pin on this component with pinseq == pin_counter */
00194     pinseq = g_strdup_printf ("%d", pin_counter);
00195     o_pin_object = o_complex_find_pin_by_attribute (object, "pinseq", pinseq);
00196     g_free (pinseq);
00197 
00198     if (o_pin_object != NULL) {
00199       /* Now rename pinnumber= attrib on this part with value found */
00200       /* in slotdef attribute  */
00201       attributes = o_attrib_return_attribs (o_pin_object);
00202       o_pinnum_attrib = o_attrib_find_attrib_by_name (attributes, "pinnumber", 0);
00203       g_list_free (attributes);
00204 
00205       if (o_pinnum_attrib != NULL) {
00206         o_text_set_string (toplevel,
00207                            o_pinnum_attrib,
00208                            g_strdup_printf ("pinnumber=%s", current_pin));
00209       }
00210 
00211       pin_counter++;
00212     } else {
00213       s_log_message (_("component missing pinseq= attribute\n"));
00214     }
00215 
00216     current_pin = strtok (NULL, DELIMITERS);
00217   }
00218 
00219   g_free (slotdef);
00220 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines