Debian Codegraph @

sort.c

raw ◀ ▶
1 Debian patch touch this file
1 /*
2 * $Id: sort.c 747 2009-11-06 02:33:37Z dhiebert $
3 *
4 * Copyright (c) 1996-2002, Darren Hiebert
5 *
6 * This source code is released for free distribution under the terms of the
7 * GNU General Public License.
8 *
9 * This module contains functions to sort the tag entries.
10 */
11
12 /*
13 * INCLUDE FILES
14 */
15 #include "general.h" /* must always come first */
16
17 #if defined (HAVE_STDLIB_H)
18 # include <stdlib.h> /* to declare malloc () */
19 #endif
20 #include <string.h>
21 #include <stdio.h>
22
23 #include "debug.h"
24 #include "entry.h"
25 #include "options.h"
26 #include "read.h"
27 #include "routines.h"
28 #include "sort.h"
29
30 /*
31 * FUNCTION DEFINITIONS
32 */
33
34 extern void catFile (const char *const name)
35 {
36 FILE *const fp = fopen (name, "r");
37
38 if (fp != NULL)
39 {
40 int c;
41 while ((c = getc (fp)) != EOF)
42 putchar (c);
43 fflush (stdout);
44 fclose (fp);
45 }
46 }
47
48 #ifdef EXTERNAL_SORT
49
50 #ifdef NON_CONST_PUTENV_PROTOTYPE
51 # define PE_CONST
52 #else
53 # define PE_CONST const
54 #endif
55
•56 /*
57 Output file name should not be evaluated in system(3) function.
58 The name must be used as is. Quotations are required to block the
59 evaluation.
60
61 Normal single-quotes are used to quote a cstring:
62 a => 'a'
63 " => '"'
64
65 If a single-quote is included in the cstring, use double quotes for quoting it.
66 ' => ''"'"''
67 */
68 static void appendCstringWithQuotes (vString *dest, const char* cstr)
69 {
70 const char* o;
71
72 vStringPut (dest, '\'');
73 for (o = cstr; *o; o++)
74 {
75 if (*o == '\'')
76 vStringCatS (dest, "'\"'\"'");
77 else
78 vStringPut (dest, *o);
79 }
80 vStringPut (dest, '\'');
81 }
82
83 extern void externalSortTags (const boolean toStdout)
84 {
85 const char *const sortNormalCommand = "sort -u -o";
86 const char *const sortFoldedCommand = "sort -u -f -o";
87 const char *sortCommand =
88 Option.sorted == SO_FOLDSORTED ? sortFoldedCommand : sortNormalCommand;
•89 # ifndef HAVE_SETENV
90 PE_CONST char *const sortOrder1 = "LC_COLLATE=C";
91 PE_CONST char *const sortOrder2 = "LC_ALL=C";
•92 # endif
93 vString *cmd = vStringNew ();
94 int ret = -1;
95
96 if (cmd != NULL)
97 {
98 /* Ensure ASCII value sort order.
99 */
100 #ifdef HAVE_SETENV
101 setenv ("LC_COLLATE", "C", 1);
102 setenv ("LC_ALL", "C", 1);
103 #else
104 # ifdef HAVE_PUTENV
105 putenv (sortOrder1);
106 putenv (sortOrder2);
107 # else
•108 vStringCatS (cmd, sortOrder1);
109 vStringPut (cmd, ' ');
110 vStringCatS (cmd, sortOrder2);
111 vStringPut (cmd, ' ');
112 # endif
113 #endif
•114 vStringCatS (cmd, sortCommand);
115 vStringPut (cmd, ' ');
116 appendCstringWithQuotes (cmd, tagFileName ());
117 vStringPut (cmd, ' ');
118 appendCstringWithQuotes (cmd, tagFileName ());
119 verbose ("system (\"%s\")\n", vStringValue (cmd));
120 ret = system (vStringValue (cmd));
121 vStringDelete (cmd);
122 }
123 if (ret != 0)
124 error (FATAL | PERROR, "cannot sort tag file");
125 else if (toStdout)
126 catFile (tagFileName ());
127 }
128
129 #else
130
131 /*
132 * These functions provide a basic internal sort. No great memory
133 * optimization is performed (e.g. recursive subdivided sorts),
134 * so have lots of memory if you have large tag files.
135 */
136
137 static void failedSort (FILE *const fp, const char* msg)
138 {
139 const char* const cannotSort = "cannot sort tag file";
140 if (fp != NULL)
141 fclose (fp);
142 if (msg == NULL)
143 error (FATAL | PERROR, "%s", cannotSort);
144 else
145 error (FATAL, "%s: %s", msg, cannotSort);
146 }
147
148 static int compareTagsFolded(const void *const one, const void *const two)
149 {
150 const char *const line1 = *(const char* const*) one;
151 const char *const line2 = *(const char* const*) two;
152
153 return struppercmp (line1, line2);
154 }
155
156 static int compareTags (const void *const one, const void *const two)
157 {
158 const char *const line1 = *(const char* const*) one;
159 const char *const line2 = *(const char* const*) two;
160
161 return strcmp (line1, line2);
162 }
163
164 static void writeSortedTags (
165 char **const table, const size_t numTags, const boolean toStdout)
166 {
167 FILE *fp;
168 size_t i;
169
170 /* Write the sorted lines back into the tag file.
171 */
172 if (toStdout)
173 fp = stdout;
174 else
175 {
176 fp = fopen (tagFileName (), "w");
177 if (fp == NULL)
178 failedSort (fp, NULL);
179 }
180 for (i = 0 ; i < numTags ; ++i)
181 {
182 /* Here we filter out identical tag *lines* (including search
183 * pattern) if this is not an xref file.
184 */
185 if (i == 0 || Option.xref || strcmp (table [i], table [i-1]) != 0)
186 if (fputs (table [i], fp) == EOF)
187 failedSort (fp, NULL);
188 }
189 if (toStdout)
190 fflush (fp);
191 else
192 fclose (fp);
193 }
194
195 extern void internalSortTags (const boolean toStdout)
196 {
197 vString *vLine = vStringNew ();
198 FILE *fp = NULL;
199 const char *line;
200 size_t i;
201 int (*cmpFunc)(const void *, const void *);
202
203 /* Allocate a table of line pointers to be sorted.
204 */
205 size_t numTags = TagFile.numTags.added + TagFile.numTags.prev;
206 const size_t tableSize = numTags * sizeof (char *);
207 char **const table = (char **) malloc (tableSize); /* line pointers */
208 DebugStatement ( size_t mallocSize = tableSize; ) /* cumulative total */
209
210
211 cmpFunc = Option.sorted == SO_FOLDSORTED ? compareTagsFolded : compareTags;
212 if (table == NULL)
213 failedSort (fp, "out of memory");
214
215 /* Open the tag file and place its lines into allocated buffers.
216 */
217 fp = fopen (tagFileName (), "r");
218 if (fp == NULL)
219 failedSort (fp, NULL);
220 for (i = 0 ; i < numTags && ! feof (fp) ; )
221 {
222 line = readLine (vLine, fp);
223 if (line == NULL)
224 {
225 if (! feof (fp))
226 failedSort (fp, NULL);
227 break;
228 }
229 else if (*line == '\0' || strcmp (line, "\n") == 0)
230 ; /* ignore blank lines */
231 else
232 {
233 const size_t stringSize = strlen (line) + 1;
234
235 table [i] = (char *) malloc (stringSize);
236 if (table [i] == NULL)
237 failedSort (fp, "out of memory");
238 DebugStatement ( mallocSize += stringSize; )
239 strcpy (table [i], line);
240 ++i;
241 }
242 }
243 numTags = i;
244 fclose (fp);
245 vStringDelete (vLine);
246
247 /* Sort the lines.
248 */
249 qsort (table, numTags, sizeof (*table), cmpFunc);
250
251 writeSortedTags (table, numTags, toStdout);
252
253 PrintStatus (("sort memory: %ld bytes\n", (long) mallocSize));
254 for (i = 0 ; i < numTags ; ++i)
255 free (table [i]);
256 free (table);
257 }
258
259 #endif
260
261 /* vi:set tabstop=4 shiftwidth=4: */