* Moved export stuff in export.c.

* Bail out with an error if the requested export format is unsupported.
This commit is contained in:
Sam Hocevar 2006-11-10 07:56:55 +00:00 committed by sam
parent 7c880dc3f5
commit 41ffd5d7b8
4 changed files with 84 additions and 17 deletions

View file

@ -5,6 +5,7 @@ toilet_SOURCES = main.c toilet.h \
io.c io.h \
render.c render.h \
filter.c filter.h \
export.c export.h \
term.c figlet.c
toilet_CFLAGS = `pkg-config --cflags cucul`
toilet_LDFLAGS = `pkg-config --libs cucul` @GETOPT_LIBS@ @ZLIB_LIBS@

58
src/export.c Normal file
View file

@ -0,0 +1,58 @@
/*
* TOIlet The Other Implementations letters
* Copyright (c) 2006 Sam Hocevar <sam@zoy.org>
* All Rights Reserved
*
* $Id$
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the Do What The Fuck You Want To
* Public License, Version 2, as published by Sam Hocevar. See
* http://sam.zoy.org/wtfpl/COPYING for more details.
*/
/*
* This file contains export functions.
*/
#include "config.h"
#if defined(HAVE_INTTYPES_H)
# include <inttypes.h>
#endif
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <cucul.h>
#include "toilet.h"
#include "export.h"
int export_list(void)
{
char const * const * exports, * const * p;
printf("Available export formats:\n");
exports = cucul_get_export_list();
for(p = exports; *p; p += 2)
printf("\"%s\": %s\n", *p, *(p + 1));
return 0;
}
int export_set(context_t *cx, char const *format)
{
char const * const * exports, * const * p;
cx->export = format;
exports = cucul_get_export_list();
for(p = exports; *p; p += 2)
if(!strcmp(*p, format))
return 0;
fprintf(stderr, "unknown export format `%s'\n", format);
return -1;
}

20
src/export.h Normal file
View file

@ -0,0 +1,20 @@
/*
* TOIlet The Other Implementations letters
* Copyright (c) 2006 Sam Hocevar <sam@zoy.org>
* All Rights Reserved
*
* $Id$
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the Do What The Fuck You Want To
* Public License, Version 2, as published by Sam Hocevar. See
* http://sam.zoy.org/wtfpl/COPYING for more details.
*/
/*
* This header defines export functions.
*/
extern int export_list(void);
extern int export_set(context_t *, char const *);

View file

@ -34,12 +34,12 @@
#include "toilet.h"
#include "render.h"
#include "filter.h"
#include "export.h"
static void version(void);
#if defined(HAVE_GETOPT_H)
static void usage(void);
#endif
static int export_list(void);
int main(int argc, char *argv[])
{
@ -138,13 +138,14 @@ int main(int argc, char *argv[])
case 'E': /* --export */
if(!strcmp(optarg, "list"))
return export_list();
cx->export = optarg;
if(export_set(cx, optarg) < 0)
return -1;
break;
case 140: /* --irc */
cx->export = "irc";
export_set(cx, "irc");
break;
case 141: /* --html */
cx->export = "html";
export_set(cx, "html");
break;
case '?':
printf(MOREINFO, argv[0]);
@ -260,16 +261,3 @@ static void usage(void)
}
#endif
static int export_list(void)
{
char const * const * exports, * const * p;
exports = cucul_get_export_list();
printf("Available export formats:\n");
for(p = exports; *p; p += 2)
printf("\"%s\": %s\n", *p, *(p + 1));
return 0;
}