Programming with Judy
Using Judy
Using JudySL
Chapter 346
// CHECK FOR REQUIRED INPUT FILE PARAMETER:
if (argc != 2)
{
(void) fprintf (stderr, "Usage: %s <file-to-sort>\n", argv[0]);
(void) fputs ("Uses each line as a JudySL index.\n", stderr);
exit (1);
}
// OPEN INPUT FILE:
if ((fpin = fopen (argv[1], "r")) == (FILE *) NULL)
{
(void) fprintf (stderr, "%s: Cannot open file \"%s\": %s "
"(errno = %d)\n", argv[0], argv[1], strerror (errno),
errno);
exit (1);
}
// INPUT/STORE LOOP:
//
// Read each input line (up to Index size) and save the line as an index into a
// JudySL array. If the line doesn't overflow Index, it ends with a newline,
// which must be removed for sorting comparable to sort(1).
while (fgets (Index, sizeof (Index), fpin) != (char *) NULL)
{
if ((Pc = strchr (Index, '\n')) != (char *) NULL)
*Pc = '\0'; // trim at newline.
if ((PPValue = JudySLIns (& PJArray, Index, &JError))