X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fsn-show.c;h=5ecea330e4b5ce89abdfd1f96bde81c4c3ae6391;hb=706f13fcd7c0f729c25b6c1c40309243e106111c;hp=cf24a5985bdb7e05d85f3a331e64b669ba1e5a0f;hpb=8e4a1e11b964e446370df12fbc2d072eb31a7fda;p=sort-networks.git diff --git a/src/sn-show.c b/src/sn-show.c index cf24a59..5ecea33 100644 --- a/src/sn-show.c +++ b/src/sn-show.c @@ -1,6 +1,6 @@ /** - * collectd - src/sn-show.c - * Copyright (C) 2008 Florian octo Forster + * libsortnetwork - src/sn-show.c + * Copyright (C) 2008-2010 Florian octo Forster * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the @@ -16,41 +16,83 @@ * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * * Authors: - * Florian octo Forster + * Florian octo Forster **/ +#include "config.h" + #include #include +#include +#include #include "sn_network.h" -int main (int argc, char **argv) +static int show_fh (FILE *fh) /* {{{ */ { sn_network_t *n; - FILE *fh = NULL; - - if (argc == 1) - fh = stdin; - else if (argc == 2) - fh = fopen (argv[1], "r"); if (fh == NULL) - { - printf ("fh == NULL!\n"); - return (1); - } + return (EINVAL); n = sn_network_read (fh); - if (n == NULL) { - printf ("n == NULL!\n"); - return (1); + fprintf (stderr, "Parsing comparator network failed.\n"); + return (EINVAL); } sn_network_show (n); + sn_network_destroy (n); + return (0); +} /* }}} int show_fh */ + +static int show_file (const char *file) /* {{{ */ +{ + FILE *fh; + int status; + + if (file == NULL) + return (EINVAL); + + fh = fopen (file, "r"); + if (fh == NULL) + { + fprintf (stderr, "Opening file \"%s\" failed: %s\n", + file, strerror (errno)); + return (errno); + } + + status = show_fh (fh); + + fclose (fh); + return (status); +} /* }}} int show_file */ + +int main (int argc, char **argv) +{ + if (argc == 1) + { + show_fh (stdin); + } + else + { + int i; + for (i = 1; i < argc; i++) + { + if (i > 1) + puts ("\n"); + + if (argc > 2) + printf ("=== %s ===\n\n", argv[i]); + + show_file (argv[i]); + } + } + + exit (EXIT_SUCCESS); } /* int main */ /* vim: set shiftwidth=2 softtabstop=2 : */