Initial import of gattrib 20040806
[geda-gaf/whiteaudio.git] / gattrib / src / s_string_list.c
blob76d6859801538ee5d91ec688940c034f1b4f9473
1 /* gEDA - GPL Electronic Design Automation
2 * gattrib -- gEDA component and net attribute manipulation using spreadsheet.
3 * Copyright (C) 2003 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., 59 Temple Place, Suite 330, Boston, MA 02111 USA
20 /*------------------------------------------------------------------
21 * This file holds fcns involved in manipulating the STRING_LIST
22 * structure. STRING_LIST is basically a linked list of strings
23 * (text).
24 *------------------------------------------------------------------*/
26 #include <config.h>
28 #include <stdio.h>
29 #ifdef HAVE_STRING_H
30 #include <string.h>
31 #endif
32 #include <math.h>
34 /*------------------------------------------------------------------
35 * Gattrib specific includes
36 *------------------------------------------------------------------*/
37 #include <libgeda/libgeda.h> /* geda library fcns */
38 #include "../include/struct.h" /* typdef and struct declarations */
39 #include "../include/prototype.h" /* function prototypes */
40 #include "../include/globals.h"
44 /*------------------------------------------------------------------
45 * This returns a pointer to a new STRING_LIST object.
46 *------------------------------------------------------------------*/
47 STRING_LIST *s_string_list_new() {
48 STRING_LIST *local_string_list;
50 local_string_list = malloc(sizeof(STRING_LIST));
51 local_string_list->data = NULL;
52 local_string_list->next = NULL;
53 local_string_list->prev = NULL;
54 local_string_list->pos = -1; /* can look for this later . . . */
56 return local_string_list;
60 /*------------------------------------------------------------------
61 * This fcn inserts the item into a char* list. It first cycles through the
62 * list to make sure that there are no duplications. The list is assumed
63 * to be a STRING_LIST:
64 * struct STRING_LIST
65 * {
66 * char *data;
67 * int pos;
68 * STRING_LIST *next;
69 * STRING_LIST *prev;
70 * };
71 *------------------------------------------------------------------*/
72 void s_string_list_add_item(STRING_LIST *list, int *count, char *item) {
74 gchar *trial_item = NULL;
75 STRING_LIST *prev;
76 STRING_LIST *local_list;
78 /* First check to see if list is empty. Handle insertion of first item
79 into empty list separately. (Is this necessary?) */
80 if (list->data == NULL) {
81 #ifdef DEBUG
82 printf("In s_string_list_add_item, about to place first item in list.\n");
83 #endif
84 list->data = (gchar *) u_basic_strdup(item);
85 list->next = NULL;
86 list->prev = NULL; /* this may have already been initialized. . . . */
87 list->pos = (int) count; /* This enumerates the pos on the list. Value is reset later by sorting. */
88 (*count)++; /* increment count to 1 */
89 return;
92 /* Otherwise, loop through list looking for duplicates */
93 prev = list;
94 while (list != NULL) {
95 trial_item = (gchar *) u_basic_strdup(list->data);
96 if (strcmp(trial_item, item) == 0) {
97 /* Found item already in list. Just return. */
98 free(trial_item);
99 return;
101 free(trial_item);
102 prev = list;
103 list = list->next;
106 /* If we are here, it's 'cause we didn't find the item pre-existing in the list. */
107 /* In this case, we insert it. */
109 local_list = malloc(sizeof(STRING_LIST)); /* allocate space for this list entry */
110 local_list->data = (gchar *) u_basic_strdup(item); /* copy data into list */
111 local_list->next = NULL;
112 local_list->prev = prev; /* point this item to last entry in old list */
113 prev->next = local_list; /* make last item in old list point to this one. */
114 local_list->pos = (int) count; /* This enumerates the pos on the list. Value is reset later by sorting. */
115 (*count)++; /* increment count */
116 list = local_list;
117 return;
121 /*------------------------------------------------------------------
122 * This fcn looks for item in the list. It returns 1 if item is
123 * present, 0 if absent.
124 *------------------------------------------------------------------*/
125 int s_string_list_in_list(STRING_LIST *list, char *item) {
127 gchar *trial_item = NULL;
129 /* First check to see if list is empty. If empty, return
130 * 0 automatically. (I probably don't need to handle this
131 * separately.) */
132 if (list->data == NULL) {
133 return 0;
136 /* Otherwise, loop through list looking for duplicates */
137 while (list != NULL) {
138 trial_item = (gchar *) u_basic_strdup(list->data);
139 if (strcmp(trial_item, item) == 0) {
140 /* Found item already in list. return 1. */
141 free(trial_item);
142 return 1;
144 free(trial_item);
145 list = list->next;
148 /* If we are here, it's 'cause we didn't find the item
149 * pre-existing in the list. In this case, return 0 */
150 return 0;
156 /*------------------------------------------------------------------
157 * This fcn takes the master comp list sheet_head->master_comp_list_head
158 * and sorts it in this order:
159 * <all refdeses in alphabetical order>
160 * Right now it does nothing other than fill in the "position"
161 * and "length" variables.
162 *------------------------------------------------------------------*/
163 void s_string_list_sort_master_comp_list() {
164 int i = 0;
165 STRING_LIST *local_list;
167 /* Here's where we do the sort. The sort is done using a fcn found on the web. */
168 local_list = sheet_head->master_comp_list_head;
169 local_list = listsort(local_list, 0, 1);
171 /* Do this after sorting is done. This resets the order of the individual items
172 * in the list. */
173 while (local_list != NULL) { /* make sure item is not null */
174 local_list->pos = i;
175 if (local_list->next != NULL) {
176 i++;
177 local_list = local_list->next;
178 } else {
179 break; /* leave loop *before* iterating to NULL EOL marker */
183 /* Now go to first item in local list and reassign list head to new first element */
184 while (local_list->prev != NULL) {
185 local_list = local_list->prev;
188 sheet_head->master_comp_list_head = local_list;
190 return;
193 /*------------------------------------------------------------------
194 * This fcn takes the master comp attrib list sheet_head->master_comp_attrib_list_head
195 * and sorts it in this order:
196 * <all refdeses in alphabetical order>
197 * Right now it does nothing other than fill in the "position"
198 * and "length" variables.
199 *------------------------------------------------------------------*/
200 void s_string_list_sort_master_comp_attrib_list() {
201 int i = 0;
202 STRING_LIST *local_list;
204 /* Here's where we do the sort */
207 * Note that this sort is TBD -- it is more than just an alphabetic sort 'cause we want
208 * certain attribs to go first.
212 /* Do this after sorting is done. This resets the order of the individual items
213 * in the list. */
214 local_list = sheet_head->master_comp_attrib_list_head;
215 while (local_list != NULL) {
216 local_list->pos = i;
217 i++;
218 local_list = local_list->next;
221 return;
224 /*------------------------------------------------------------------
225 * This fcn takes the master net list sheet_head->master_net_list_head
226 * and sorts it in this order:
227 * <all nets in alphabetical order>
228 *------------------------------------------------------------------*/
229 void s_string_list_sort_master_net_list() {
230 int i = 0;
231 STRING_LIST *local_list;
234 /* Do this after sorting is done. This resets the order of the individual items
235 * in the list. */
236 local_list = sheet_head->master_net_list_head;
237 while (local_list != NULL) {
238 local_list->pos = i;
239 i++;
240 local_list = local_list->next;
243 return;
246 /*------------------------------------------------------------------
247 * This fcn takes the master net attrib list sheet_head->master_net_attrib_list_head
248 * and sorts it in this order:
249 * value, footprint, model-name, file, <all other attribs in alphabetical order>
250 *------------------------------------------------------------------*/
251 void s_string_list_sort_master_net_attrib_list() {
252 int i = 0;
253 STRING_LIST *local_list;
256 /* Do this after sorting is done. This resets the order of the individual items
257 * in the list. */
258 local_list = sheet_head->master_net_attrib_list_head;
259 while (local_list != NULL) {
260 local_list->pos = i;
261 i++;
262 local_list = local_list->next;
265 return;
269 /*------------------------------------------------------------------
270 * This fcn takes the master pin list sheet_head->master_pin_list_head
271 * and sorts it in this order:
272 * <all refdeses in alphabetical order>
273 * Right now it does nothing other than fill in the "position"
274 * and "length" variables.
275 *------------------------------------------------------------------*/
276 void s_string_list_sort_master_pin_list() {
277 int i = 0;
278 STRING_LIST *local_list;
280 /* Here's where we do the sort. The sort is done using a fcn found on the web. */
281 local_list = sheet_head->master_pin_list_head;
282 local_list = listsort(local_list, 0, 1);
284 /* Do this after sorting is done. This resets the order of the individual items
285 * in the list. */
286 while (local_list != NULL) { /* make sure item is not null */
287 local_list->pos = i;
288 if (local_list->next != NULL) {
289 i++;
290 local_list = local_list->next;
291 } else {
292 break; /* leave loop *before* iterating to NULL EOL marker */
296 /* Now go to first item in local list and reassign list head to new first element */
297 while (local_list->prev != NULL) {
298 local_list = local_list->prev;
301 sheet_head->master_pin_list_head = local_list;
303 return;
306 /*------------------------------------------------------------------
307 * This fcn takes the master pin attrib list sheet_head->master_pin_attrib_list_head
308 * and sorts it in this order:
309 * <all pin attribs in alphabetical order>
310 * Right now it does nothing other than fill in the "position"
311 * and "length" variables.
312 *------------------------------------------------------------------*/
313 void s_string_list_sort_master_pin_attrib_list() {
314 int i = 0;
315 STRING_LIST *local_list;
317 /* Here's where we do the sort */
320 * Note that this sort is TBD -- it is more than just an alphabetic sort 'cause we want
321 * certain attribs to go first.
325 /* Do this after sorting is done. This resets the order of the individual items
326 * in the list. */
327 local_list = sheet_head->master_pin_attrib_list_head;
328 while (local_list != NULL) {
329 local_list->pos = i;
330 i++;
331 local_list = local_list->next;
334 return;