File: slist.c

package info (click to toggle)
cxref 1.5c-9
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,504 kB
  • ctags: 1,359
  • sloc: ansic: 16,721; yacc: 1,930; sh: 722; lex: 416; makefile: 345; lisp: 256; python: 80; perl: 78
file content (252 lines) | stat: -rw-r--r-- 5,504 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/***************************************
  $Header: /home/amb/cxref/RCS/slist.c 1.3 1997/05/11 15:23:11 amb Exp $

  C Cross Referencing & Documentation tool. Version 1.4.

  Handle lists of strings.
  ******************/ /******************
  Written by Andrew M. Bishop

  This file Copyright 1995,96,97 Andrew M. Bishop
  It may be distributed under the GNU Public License, version 2, or
  any higher version.  See section COPYING of the GNU Public license
  for conditions under which this file may be redistributed.
  ***************************************/

/*+ Control the debugging information from this file. +*/
#define DEBUG 0

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "memory.h"
#include "datatype.h"
#include "cxref.h"

/*++++++++++++++++++++++++++++++++++++++
  Called to initialise a new string list.

  StringList NewStringList Returns an initialised string list.
  ++++++++++++++++++++++++++++++++++++++*/

StringList NewStringList(void)
{
 StringList sl=(StringList)Calloc(1,sizeof(struct _StringList));

#if DEBUG
 printf("#Slist.c# Initialise string list\n");
#endif

 return(sl);
}


/*++++++++++++++++++++++++++++++++++++++
  Called to initialise a new string list 2.

  StringList2 NewStringList2 Returns an initialised string list 2.
  ++++++++++++++++++++++++++++++++++++++*/

StringList2 NewStringList2(void)
{
 StringList2 sl=(StringList2)Calloc(1,sizeof(struct _StringList2));

#if DEBUG
 printf("#Slist.c# Initialise string list 2\n");
#endif

 return(sl);
}


/*++++++++++++++++++++++++++++++++++++++
  Add a string to the string list, the list stores a Malloced copy of str.

  StringList sl The string list to add to.

  char* str The string to add.

  int alphalist If true then the list is sorted into alphabetical order.

  int uniqlist If true then duplicated entries are not allowed to be added.
  ++++++++++++++++++++++++++++++++++++++*/

void AddToStringList(StringList sl,char* str,int alphalist,int uniqlist)
{
 int i;

#if DEBUG
 printf("#Slist.c# Add string %s to the string list\n",str);
#endif

 if(uniqlist)
    for(i=0;i<sl->n;i++)
       if(!strcmp(str,sl->s[i]))
          return;

 if(!sl->n)
    sl->s=(char**)Malloc(8*sizeof(char*));
 else
    if(sl->n%8==0)
       sl->s=(char**)Realloc(sl->s,(sl->n+8)*sizeof(char*));

 if(alphalist)
   {
    char *shuffle=NULL;

    for(i=0;i<sl->n;i++)
       if(shuffle)
         {
          char* temp=sl->s[i];
          sl->s[i]=shuffle;
          shuffle=temp;
         }
       else
          if(strcmp(str,sl->s[i])<0)
            {
             shuffle=sl->s[i];
             sl->s[i]=MallocString(str);
            }

    if(shuffle)
       sl->s[sl->n]=shuffle;
    else
       sl->s[sl->n]=MallocString(str);
   }
 else
    sl->s[sl->n]=MallocString(str);

 sl->n++;
}


/*++++++++++++++++++++++++++++++++++++++
  Add a pair of strings to the string list 2, the list stores a Malloced copy of the arguments.

  StringList2 sl The string list 2 to add to.

  char* str1 The first string to add.

  char* str2 The second string to add.

  int alphalist If true then the list is sorted into alphabetical order of the first string, then second string.

  int uniqlist If true then duplicated entries of the first string are not allowed to be added.
  ++++++++++++++++++++++++++++++++++++++*/

void AddToStringList2(StringList2 sl,char* str1,char* str2,int alphalist,int uniqlist)
{
 int i;

#if DEBUG
 printf("#Slist.c# Add strings %s and %s to the string list 2\n",str1,str2);
#endif

 if(uniqlist)
    for(i=0;i<sl->n;i++)
       if(!strcmp(str1,sl->s1[i]))
          return;

 if(!sl->n)
   {
    sl->s1=(char**)Malloc(8*sizeof(char*));
    sl->s2=(char**)Malloc(8*sizeof(char*));
   }
 else
    if(sl->n%8==0)
      {
       sl->s1=(char**)Realloc(sl->s1,(sl->n+8)*sizeof(char*));
       sl->s2=(char**)Realloc(sl->s2,(sl->n+8)*sizeof(char*));
      }

 if(alphalist)
   {
    char *shuffle1=NULL;
    char *shuffle2=NULL;

    for(i=0;i<sl->n;i++)
       if(shuffle1)
         {
          char* temp1=sl->s1[i];
          char* temp2=sl->s2[i];
          sl->s1[i]=shuffle1;
          sl->s2[i]=shuffle2;
          shuffle1=temp1;
          shuffle2=temp2;
         }
       else
          if(strcmp(str1,sl->s1[i])<0 ||
             (str2 && sl->s2[i] && strcmp(str1,sl->s1[i])==0 && strcmp(str2,sl->s2[i])<0))
            {
             shuffle1=sl->s1[i];
             shuffle2=sl->s2[i];
             sl->s1[i]=MallocString(str1);
             sl->s2[i]=MallocString(str2);
            }

    if(shuffle1)
      {
       sl->s1[sl->n]=shuffle1;
       sl->s2[sl->n]=shuffle2;
      }
    else
      {
       sl->s1[sl->n]=MallocString(str1);
       sl->s2[sl->n]=MallocString(str2);
      }
   }
 else
   {
    sl->s1[sl->n]=MallocString(str1);
    sl->s2[sl->n]=MallocString(str2);
   }

 sl->n++;
}


/*++++++++++++++++++++++++++++++++++++++
  Delete a string list.

  StringList sl The string list to delete.
  ++++++++++++++++++++++++++++++++++++++*/

void DeleteStringList(StringList sl)
{
 int i;

 for(i=0;i<sl->n;i++)
    Free(sl->s[i]);

 if(sl->s)
    Free(sl->s);

 Free(sl);
}


/*++++++++++++++++++++++++++++++++++++++
  Delete a string list 2.

  StringList2 sl The string list 2 to delete.
  ++++++++++++++++++++++++++++++++++++++*/

void DeleteStringList2(StringList2 sl)
{
 int i;

 for(i=0;i<sl->n;i++)
   {
    Free(sl->s1[i]);
    if(sl->s2[i])
       Free(sl->s2[i]);
   }

 if(sl->s1)
    Free(sl->s1);
 if(sl->s2)
    Free(sl->s2);

 Free(sl);
}