/*
* Sort a list of 'struct blogpost' objects by newest-to-oldest msgnum.
-int blogview_sortfunc(const void *s1, const void *s2) {
- long *l1 = (long *)(s1);
- long *l2 = (long *)(s2);
+ * With big thanks to whoever wrote http://www.c.happycodings.com/Sorting_Searching/code14.html
+ */
+static int blogview_sortfunc(const void *a, const void *b) {
+ struct blogpost * const *one = a;
+ struct blogpost * const *two = b;
- if (*l1 > *l2) return(-1);
- if (*l1 < *l2) return(+1);
+ if ( (*one)->msgs[0] > (*two)->msgs[0] ) return(-1);
+ if ( (*one)->msgs[0] < (*two)->msgs[0] ) return(+1);
return(0);
}
- */
-
/*
const char *Key;
void *Data;
long len;
- struct blogpost *bp;
int i;
struct blogpost **blogposts = NULL;
int num_blogposts = 0;
it = GetNewHashPos(BLOG, 0);
while (GetNextHashPos(BLOG, it, &len, &Key, &Data)) {
- bp = (struct blogpost *) Data;
-
if (num_blogposts >= num_blogposts_alloc) {
if (num_blogposts_alloc == 0) {
num_blogposts_alloc = 100;
+ blogposts = malloc((num_blogposts_alloc * sizeof (struct blogpost *)));
}
else {
num_blogposts_alloc *= 2;
+ blogposts = realloc(blogposts, (num_blogposts_alloc * sizeof (struct blogpost *)));
}
- blogposts = realloc(blogposts, (num_blogposts_alloc * sizeof (struct blogpost *)));
}
- blogposts[num_blogposts++] = bp;
+ blogposts[num_blogposts++] = (struct blogpost *) Data;
}
-
DeleteHashPos(&it);
if (num_blogposts > 0) {
+ qsort(blogposts, num_blogposts, sizeof(void *), blogview_sortfunc);
+
+ /* FIXME this is where we handle date ranges etc */
+
for (i=0; i<num_blogposts; ++i) {
- wc_printf("Top level ID is %d", blogposts[i]->top_level_id);
- wc_printf("; top level msgnum is %ld", blogposts[i]->msgs[0]);
blogpost_render(blogposts[i]);
}
free(blogposts);
return 0;
}
+
void
InitModule_BLOGVIEWRENDERERS
(void)