Friday, September 25, 2009

Reddit.com Pun-thread Response

Reddit often has threads full of puns (i.e. pun threads) in the comments sections of articles.

I've been meaning to post this as a reply to a pun thread that started in the programming subreddit, but never got a chance to use it.


/* create a witty pun thread
*
* note: remember to compile with -lreddit and -lhumorwitty
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef __REDDIT__
#include <reddit.h>
#include <humor/witty.h>
#endif

#ifndef __HUMOR_WITTY_PUNS__
typedef char* pun_t;
typedef struct { pun_t *puns; size_t npuns; } pun_list_t;
#define NOVALUESTRING "i have nothing of value to add! lol!"
#endif

/* determine all puns that can made based on the phrase specified in src */
pun_list_t* get_list_of_puns_based_on_phrase(const char *src) {
pun_list_t *list;
pun_t pun, *puns = (pun_t*)malloc(sizeof(pun_t));
int npuns = 0;

/* notice: requires libhumor and libhumorwitty to work correctly */
while((pun = humor_witty_get_new_pun()) != NULL)
puns[npuns++] = (pun_t)strdup(pun);

list = (pun_list_t*)malloc(sizeof(pun_list_t));
list->puns = puns;
list->npuns = npuns;
return list;
}

/* post our lulz to reddit */
void reddit_do_comments_pun_thread(pun_list_t *list) {
while(list->npuns-- > 0) {
#ifdef __REDDIT__
reddit_post_comment_with_comment(list->puns[list->npuns]);
#else
puts(list->puns[list->npuns]);
#endif /* __REDDIT__ */
}
}

#ifndef __REDDIT__
/* if not compiled into the brain, run this from a console to ensure it works */
int main(int argc, char *argv[]) {
pun_list_t *pun_thread;
pun_thread = get_list_of_puns_based_on_phrase("foobar");
reddit_do_comments_pun_thread(pun_thread);
return 0;
}
#endif /* __REDDIT__ */

/* some systems lack witty homor so attempt to comment anyway using stupidity for a "pun" */
#ifndef __HUMOR_WITTY__
static int humor_witty_puns_generated = 0;
pun_t humor_witty_get_new_pun(void) {
return (pun_t)((++humor_witty_puns_generated > 1) ? NULL : strdup(NOVALUESTRING));
}
#endif /* __HUMOR_WITTY__ */


The cool thing is that it will compile and run in the form presented above, but if you just read the code as if it were a pseudo-code it seems to outline how a pun-thread works.

*sigh* Get a life. I know.