1    | /***************************************
2    |   $Header: /home/amb/cxref/RCS/slist.c 1.3 1997/05/11 15:23:11 amb Exp $
3    | 
4    |   C Cross Referencing & Documentation tool. Version 1.4.
5    | 
6    |   Handle lists of strings.
7    |   ******************/ /******************
8    |   Written by Andrew M. Bishop
9    | 
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   |   ***************************************/
15   | 
16   | /*+ Control the debugging information from this file. +*/
17   | #define DEBUG 0
18   | 
19   | #include <stdlib.h>
20   | #include <stdio.h>
21   | #include <string.h>
22   | 
23   | #include "memory.h"
24   | #include "datatype.h"
25   | #include "cxref.h"
26   | 
27   | /*++++++++++++++++++++++++++++++++++++++
28   |   Called to initialise a new string list.
29   | 
30   |   StringList NewStringList Returns an initialised string list.
31   |   ++++++++++++++++++++++++++++++++++++++*/
32   | 
33   | StringList NewStringList(void)
34   | {
35   |  StringList sl=(StringList)Calloc(1,sizeof(struct _StringList));
36   | 
37   | #if DEBUG
38   |  printf("#Slist.c# Initialise string list\n");
39   | #endif
40   | 
41   |  return(sl);
42   | }
43   | 
44   | 
45   | /*++++++++++++++++++++++++++++++++++++++
46   |   Called to initialise a new string list 2.
47   | 
48   |   StringList2 NewStringList2 Returns an initialised string list 2.
49   |   ++++++++++++++++++++++++++++++++++++++*/
50   | 
51   | StringList2 NewStringList2(void)
52   | {
53   |  StringList2 sl=(StringList2)Calloc(1,sizeof(struct _StringList2));
54   | 
55   | #if DEBUG
56   |  printf("#Slist.c# Initialise string list 2\n");
57   | #endif
58   | 
59   |  return(sl);
60   | }
61   | 
62   | 
63   | /*++++++++++++++++++++++++++++++++++++++
64   |   Add a string to the string list, the list stores a Malloced copy of str.
65   | 
66   |   StringList sl The string list to add to.
67   | 
68   |   char* str The string to add.
69   | 
70   |   int alphalist If true then the list is sorted into alphabetical order.
71   | 
72   |   int uniqlist If true then duplicated entries are not allowed to be added.
73   |   ++++++++++++++++++++++++++++++++++++++*/
74   | 
75   | void AddToStringList(StringList sl,char* str,int alphalist,int uniqlist)
76   | {
77   |  int i;
78   | 
79   | #if DEBUG
80   |  printf("#Slist.c# Add string %s to the string list\n",str);
81   | #endif
82   | 
83   |  if(uniqlist)
84   |     for(i=0;i<sl->n;i++)
85   |        if(!strcmp(str,sl->s[i]))
86   |           return;
87   | 
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*));
93   | 
94   |  if(alphalist)
95   |    {
96   |     char *shuffle=NULL;
97   | 
98   |     for(i=0;i<sl->n;i++)
99   |        if(shuffle)
100  |          {
101  |           char* temp=sl->s[i];
102  |           sl->s[i]=shuffle;
103  |           shuffle=temp;
104  |          }
105  |        else
106  |           if(strcmp(str,sl->s[i])<0)
107  |             {
108  |              shuffle=sl->s[i];
109  |              sl->s[i]=MallocString(str);
110  |             }
111  | 
112  |     if(shuffle)
113  |        sl->s[sl->n]=shuffle;
114  |     else
115  |        sl->s[sl->n]=MallocString(str);
116  |    }
117  |  else
118  |     sl->s[sl->n]=MallocString(str);
119  | 
120  |  sl->n++;
121  | }
122  | 
123  | 
124  | /*++++++++++++++++++++++++++++++++++++++
125  |   Add a pair of strings to the string list 2, the list stores a Malloced copy of the arguments.
126  | 
127  |   StringList2 sl The string list 2 to add to.
128  | 
129  |   char* str1 The first string to add.
130  | 
131  |   char* str2 The second string to add.
132  | 
133  |   int alphalist If true then the list is sorted into alphabetical order of the first string, then second string.
134  | 
135  |   int uniqlist If true then duplicated entries of the first string are not allowed to be added.
136  |   ++++++++++++++++++++++++++++++++++++++*/
137  | 
138  | void AddToStringList2(StringList2 sl,char* str1,char* str2,int alphalist,int uniqlist)
139  | {
140  |  int i;
141  | 
142  | #if DEBUG
143  |  printf("#Slist.c# Add strings %s and %s to the string list 2\n",str1,str2);
144  | #endif
145  | 
146  |  if(uniqlist)
147  |     for(i=0;i<sl->n;i++)
148  |        if(!strcmp(str1,sl->s1[i]))
149  |           return;
150  | 
151  |  if(!sl->n)
152  |    {
153  |     sl->s1=(char**)Malloc(8*sizeof(char*));
154  |     sl->s2=(char**)Malloc(8*sizeof(char*));
155  |    }
156  |  else
157  |     if(sl->n%8==0)
158  |       {
159  |        sl->s1=(char**)Realloc(sl->s1,(sl->n+8)*sizeof(char*));
160  |        sl->s2=(char**)Realloc(sl->s2,(sl->n+8)*sizeof(char*));
161  |       }
162  | 
163  |  if(alphalist)
164  |    {
165  |     char *shuffle1=NULL;
166  |     char *shuffle2=NULL;
167  | 
168  |     for(i=0;i<sl->n;i++)
169  |        if(shuffle1)
170  |          {
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;
177  |          }
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))
181  |             {
182  |              shuffle1=sl->s1[i];
183  |              shuffle2=sl->s2[i];
184  |              sl->s1[i]=MallocString(str1);
185  |              sl->s2[i]=MallocString(str2);
186  |             }
187  | 
188  |     if(shuffle1)
189  |       {
190  |        sl->s1[sl->n]=shuffle1;
191  |        sl->s2[sl->n]=shuffle2;
192  |       }
193  |     else
194  |       {
195  |        sl->s1[sl->n]=MallocString(str1);
196  |        sl->s2[sl->n]=MallocString(str2);
197  |       }
198  |    }
199  |  else
200  |    {
201  |     sl->s1[sl->n]=MallocString(str1);
202  |     sl->s2[sl->n]=MallocString(str2);
203  |    }
204  | 
205  |  sl->n++;
206  | }
207  | 
208  | 
209  | /*++++++++++++++++++++++++++++++++++++++
210  |   Delete a string list.
211  | 
212  |   StringList sl The string list to delete.
213  |   ++++++++++++++++++++++++++++++++++++++*/
214  | 
215  | void DeleteStringList(StringList sl)
216  | {
217  |  int i;
218  | 
219  |  for(i=0;i<sl->n;i++)
220  |     Free(sl->s[i]);
221  | 
222  |  if(sl->s)
223  |     Free(sl->s);
224  | 
225  |  Free(sl);
226  | }
227  | 
228  | 
229  | /*++++++++++++++++++++++++++++++++++++++
230  |   Delete a string list 2.
231  | 
232  |   StringList2 sl The string list 2 to delete.
233  |   ++++++++++++++++++++++++++++++++++++++*/
234  | 
235  | void DeleteStringList2(StringList2 sl)
236  | {
237  |  int i;
238  | 
239  |  for(i=0;i<sl->n;i++)
240  |    {
241  |     Free(sl->s1[i]);
242  |     if(sl->s2[i])
243  |        Free(sl->s2[i]);
244  |    }
245  | 
246  |  if(sl->s1)
247  |     Free(sl->s1);
248  |  if(sl->s2)
249  |     Free(sl->s2);
250  | 
251  |  Free(sl);
252  | }