2010-03-08 04:55:21 -06:00
|
|
|
/* Simple string sorting support, thanks to qsort(). */
|
|
|
|
#include "../config.h"
|
|
|
|
#include "HTUtils.h"
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#define SIZE_OF_HUNK 100
|
|
|
|
|
|
|
|
static char **hunk = NULL;
|
|
|
|
static int size_of_hunk;
|
|
|
|
static int count;
|
|
|
|
|
|
|
|
void HTSortInit (void)
|
|
|
|
{
|
|
|
|
count = 0;
|
|
|
|
|
|
|
|
if (!hunk)
|
|
|
|
{
|
|
|
|
size_of_hunk = SIZE_OF_HUNK;
|
|
|
|
hunk = (char **)malloc (sizeof (char *) * size_of_hunk);
|
|
|
|
}
|
2013-03-09 18:59:42 -06:00
|
|
|
|
2010-03-08 04:55:21 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void expand_hunk (void)
|
|
|
|
{
|
|
|
|
/* Make hunk bigger by SIZE_OF_HUNK elements. */
|
|
|
|
size_of_hunk += SIZE_OF_HUNK;
|
|
|
|
hunk = (char **)realloc (hunk, sizeof (char *) * size_of_hunk);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void HTSortAdd (char *str)
|
|
|
|
{
|
|
|
|
/* If we don't have room, expand. */
|
|
|
|
if (count == size_of_hunk)
|
|
|
|
expand_hunk ();
|
|
|
|
|
|
|
|
hunk[count++] = str;
|
2013-03-09 18:59:42 -06:00
|
|
|
|
2010-03-08 04:55:21 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int dsortf (char **s1, char **s2)
|
|
|
|
{
|
|
|
|
return (strcmp (*(char **)s1, *(char **)s2));
|
|
|
|
}
|
|
|
|
|
|
|
|
void HTSortSort (void)
|
|
|
|
{
|
2013-03-09 18:59:42 -06:00
|
|
|
qsort ((void *)hunk,
|
|
|
|
count,
|
|
|
|
sizeof (char *),
|
2010-03-08 04:55:21 -06:00
|
|
|
(void *)dsortf);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int HTSortCurrentCount (void)
|
|
|
|
{
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *HTSortFetch (int i)
|
|
|
|
{
|
|
|
|
if (i < count)
|
|
|
|
return hunk[i];
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|