[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

gEDA-dev: gattrib sort functions




I submitted this to the tracker too.  It enhances gattrib's sort
functions.  When sorting the refdes, the numeric parts sort
numerically, so C10 comes *after* C2 but before CR1.

Also, it sorts the attribute columns, allowing for "certain
attributes" which go in certain positions.  I guessed at which go
where, change them as you see fit.

Index: listsort.c
===================================================================
RCS file: /home/cvspsrv/cvsroot/eda/geda/gaf/gattrib/src/listsort.c,v
retrieving revision 1.4
diff -p -U2 -r1.4  listsort.c
--- listsort.c	21 Feb 2005 05:29:33 -0000	1.4
+++ listsort.c	17 Jan 2007 06:10:14 -0000
@@ -57,4 +57,5 @@
 
 #include <stdio.h>
+#include <ctype.h>
 
 #ifdef HAVE_STRING_H
@@ -78,6 +79,34 @@
  * Comparison function -- compare values of string data.
  *----------------------------------------------------------------*/
-int cmp(STRING_LIST *a, STRING_LIST *b) {
-    return strcmp(a->data, b->data);
+int cmp(STRING_LIST *al, STRING_LIST *bl) {
+  char *a = al->data;
+  char *b = bl->data;
+
+  if (al->pos != bl->pos)
+    return al->pos - bl->pos;
+
+  while (*a && *b)
+    {
+      if (isdigit ((int) *a) && isdigit ((int) *b))
+	{
+	  int ia = atoi (a);
+	  int ib = atoi (b);
+	  if (ia != ib)
+	    return ia - ib;
+	  while (isdigit ((int) *a))
+	    a++;
+	  while (isdigit ((int) *b))
+	    b++;
+	}
+      else if (tolower (*a) != tolower (*b))
+	return tolower (*a) - tolower (*b);
+      a++;
+      b++;
+    }
+  if (*a)
+    return 1;
+  if (*b)
+    return -1;
+  return 0;
 }
 
Index: s_string_list.c
===================================================================
RCS file: /home/cvspsrv/cvsroot/eda/geda/gaf/gattrib/src/s_string_list.c,v
retrieving revision 1.7
diff -p -U2 -r1.7  s_string_list.c
--- s_string_list.c	26 Jul 2006 22:56:39 -0000	1.7
+++ s_string_list.c	17 Jan 2007 06:10:14 -0000
@@ -311,8 +311,10 @@ gchar *s_string_list_get_data_at_index(S
 void s_string_list_sort_master_comp_list() {
   int i = 0;
-  STRING_LIST *local_list;
+  STRING_LIST *local_list, *p;
 
   /* Here's where we do the sort.  The sort is done using a fcn found on the web. */
   local_list = sheet_head->master_comp_list_head;
+  for (p=local_list; p; p=p->next)
+    p->pos = 0;
   local_list = listsort(local_list, 0, 1);
 
@@ -346,9 +348,26 @@ void s_string_list_sort_master_comp_list
  * and "length" variables.
  *------------------------------------------------------------------*/
+
+/* This list overrides the alphanumeric sort.  Attribs not found in
+   this list are sorted as if they had a value of DEFAULT_ATTRIB_POS
+   within this list, but alphanumerically relative to each other.  */
+static struct {
+  const char *attrib;
+  int pos;
+} certain_attribs[] = {
+  {"device", 1},
+  {"footprint", 2},
+  {"value", 3},
+  {"symversion", 200}
+};
+#define NUM_CERTAINS (sizeof(certain_attribs)/sizeof(certain_attribs[0]))
+#define DEFAULT_ATTRIB_POS 100
+
 void s_string_list_sort_master_comp_attrib_list() {
   int i = 0;
-  STRING_LIST *local_list;
+  STRING_LIST *local_list, *p;
 
   /* Here's where we do the sort */
+  local_list = sheet_head->master_comp_attrib_list_head;
 
   /*
@@ -356,9 +375,20 @@ void s_string_list_sort_master_comp_attr
    * certain attribs to go first. 
    */
-  
+  for (p=local_list; p; p=p->next) {
+    int i;
+    p->pos = DEFAULT_ATTRIB_POS;
+    for (i=0; i<NUM_CERTAINS; i++)
+      if (strcmp (certain_attribs[i].attrib, p->data) == 0)
+	{
+	  p->pos = certain_attribs[i].pos;
+	  break;
+	}
+  }
+
+  local_list = listsort(local_list, 0, 1);
+  sheet_head->master_comp_attrib_list_head = local_list;
 
   /* Do this after sorting is done.  This resets the order of the individual items
    * in the list.  */
-  local_list = sheet_head->master_comp_attrib_list_head;
   while (local_list != NULL) {
     local_list->pos = i;
@@ -424,8 +454,10 @@ void s_string_list_sort_master_net_attri
 void s_string_list_sort_master_pin_list() {
   int i = 0;
-  STRING_LIST *local_list;
+  STRING_LIST *local_list, *p;
 
   /* Here's where we do the sort.  The sort is done using a fcn found on the web. */
   local_list = sheet_head->master_pin_list_head;
+  for (p=local_list; p; p=p->next)
+    p->pos = 0;
   local_list = listsort(local_list, 0, 1);
 


_______________________________________________
geda-dev mailing list
geda-dev@moria.seul.org
http://www.seul.org/cgi-bin/mailman/listinfo/geda-dev