genmodule: revert r52790
[AROS.git] / tools / cxref / slist.c
blobcbef0bd6e2b9e02c4f4fb5c22b692c1e943d01ab
1 /***************************************
2 $Header$
4 C Cross Referencing & Documentation tool. Version 1.4.
6 Handle lists of strings.
7 ******************/ /******************
8 Written by Andrew M. Bishop
10 This file Copyright 1995,96,97 Andrew M. Bishop
11 It may be distributed under the GNU Public License, version 2, or
12 any higher version. See section COPYING of the GNU Public license
13 for conditions under which this file may be redistributed.
14 ***************************************/
16 /*+ Control the debugging information from this file. +*/
17 #define DEBUG 0
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <string.h>
23 #include "memory.h"
24 #include "datatype.h"
25 #include "cxref.h"
27 /*++++++++++++++++++++++++++++++++++++++
28 Called to initialise a new string list.
30 StringList NewStringList Returns an initialised string list.
31 ++++++++++++++++++++++++++++++++++++++*/
33 StringList NewStringList(void)
35 StringList sl=(StringList)Calloc(1,sizeof(struct _StringList));
37 #if DEBUG
38 printf("#Slist.c# Initialise string list\n");
39 #endif
41 return(sl);
45 /*++++++++++++++++++++++++++++++++++++++
46 Called to initialise a new string list 2.
48 StringList2 NewStringList2 Returns an initialised string list 2.
49 ++++++++++++++++++++++++++++++++++++++*/
51 StringList2 NewStringList2(void)
53 StringList2 sl=(StringList2)Calloc(1,sizeof(struct _StringList2));
55 #if DEBUG
56 printf("#Slist.c# Initialise string list 2\n");
57 #endif
59 return(sl);
63 /*++++++++++++++++++++++++++++++++++++++
64 Add a string to the string list, the list stores a Malloced copy of str.
66 StringList sl The string list to add to.
68 char* str The string to add.
70 int alphalist If true then the list is sorted into alphabetical order.
72 int uniqlist If true then duplicated entries are not allowed to be added.
73 ++++++++++++++++++++++++++++++++++++++*/
75 void AddToStringList(StringList sl,char* str,int alphalist,int uniqlist)
77 int i;
79 #if DEBUG
80 printf("#Slist.c# Add string %s to the string list\n",str);
81 #endif
83 if(uniqlist)
84 for(i=0;i<sl->n;i++)
85 if(!strcmp(str,sl->s[i]))
86 return;
88 if(!sl->n)
89 sl->s=(char**)Malloc(8*sizeof(char*));
90 else
91 if(sl->n%8==0)
92 sl->s=(char**)Realloc(sl->s,(sl->n+8)*sizeof(char*));
94 if(alphalist)
96 char *shuffle=NULL;
98 for(i=0;i<sl->n;i++)
99 if(shuffle)
101 char* temp=sl->s[i];
102 sl->s[i]=shuffle;
103 shuffle=temp;
105 else
106 if(strcmp(str,sl->s[i])<0)
108 shuffle=sl->s[i];
109 sl->s[i]=MallocString(str);
112 if(shuffle)
113 sl->s[sl->n]=shuffle;
114 else
115 sl->s[sl->n]=MallocString(str);
117 else
118 sl->s[sl->n]=MallocString(str);
120 sl->n++;
124 /*++++++++++++++++++++++++++++++++++++++
125 Add a pair of strings to the string list 2, the list stores a Malloced copy of the arguments.
127 StringList2 sl The string list 2 to add to.
129 char* str1 The first string to add.
131 char* str2 The second string to add.
133 int alphalist If true then the list is sorted into alphabetical order of the first string, then second string.
135 int uniqlist If true then duplicated entries of the first string are not allowed to be added.
136 ++++++++++++++++++++++++++++++++++++++*/
138 void AddToStringList2(StringList2 sl,char* str1,char* str2,int alphalist,int uniqlist)
140 int i;
142 #if DEBUG
143 printf("#Slist.c# Add strings %s and %s to the string list 2\n",str1,str2);
144 #endif
146 if(uniqlist)
147 for(i=0;i<sl->n;i++)
148 if(!strcmp(str1,sl->s1[i]))
149 return;
151 if(!sl->n)
153 sl->s1=(char**)Malloc(8*sizeof(char*));
154 sl->s2=(char**)Malloc(8*sizeof(char*));
156 else
157 if(sl->n%8==0)
159 sl->s1=(char**)Realloc(sl->s1,(sl->n+8)*sizeof(char*));
160 sl->s2=(char**)Realloc(sl->s2,(sl->n+8)*sizeof(char*));
163 if(alphalist)
165 char *shuffle1=NULL;
166 char *shuffle2=NULL;
168 for(i=0;i<sl->n;i++)
169 if(shuffle1)
171 char* temp1=sl->s1[i];
172 char* temp2=sl->s2[i];
173 sl->s1[i]=shuffle1;
174 sl->s2[i]=shuffle2;
175 shuffle1=temp1;
176 shuffle2=temp2;
178 else
179 if(strcmp(str1,sl->s1[i])<0 ||
180 (str2 && sl->s2[i] && strcmp(str1,sl->s1[i])==0 && strcmp(str2,sl->s2[i])<0))
182 shuffle1=sl->s1[i];
183 shuffle2=sl->s2[i];
184 sl->s1[i]=MallocString(str1);
185 sl->s2[i]=MallocString(str2);
188 if(shuffle1)
190 sl->s1[sl->n]=shuffle1;
191 sl->s2[sl->n]=shuffle2;
193 else
195 sl->s1[sl->n]=MallocString(str1);
196 sl->s2[sl->n]=MallocString(str2);
199 else
201 sl->s1[sl->n]=MallocString(str1);
202 sl->s2[sl->n]=MallocString(str2);
205 sl->n++;
209 /*++++++++++++++++++++++++++++++++++++++
210 Delete a string list.
212 StringList sl The string list to delete.
213 ++++++++++++++++++++++++++++++++++++++*/
215 void DeleteStringList(StringList sl)
217 int i;
219 for(i=0;i<sl->n;i++)
220 Free(sl->s[i]);
222 if(sl->s)
223 Free(sl->s);
225 Free(sl);
229 /*++++++++++++++++++++++++++++++++++++++
230 Delete a string list 2.
232 StringList2 sl The string list 2 to delete.
233 ++++++++++++++++++++++++++++++++++++++*/
235 void DeleteStringList2(StringList2 sl)
237 int i;
239 for(i=0;i<sl->n;i++)
241 Free(sl->s1[i]);
242 if(sl->s2[i])
243 Free(sl->s2[i]);
246 if(sl->s1)
247 Free(sl->s1);
248 if(sl->s2)
249 Free(sl->s2);
251 Free(sl);