sn-show: Make it possible to display more than one network at once.
[sort-networks.git] / src / sn-show.c
1 /**
2  * collectd - src/sn-show.c
3  * Copyright (C) 2008-2010  Florian octo Forster
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; only version 2 of the License is applicable.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
17  *
18  * Authors:
19  *   Florian octo Forster <ff at octo.it>
20  **/
21
22 #include "config.h"
23
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <errno.h>
28
29 #include "sn_network.h"
30
31 static int show_fh (FILE *fh) /* {{{ */
32 {
33   sn_network_t *n;
34
35   if (fh == NULL)
36     return (EINVAL);
37
38   n = sn_network_read (fh);
39   if (n == NULL)
40   {
41     fprintf (stderr, "Parsing comparator network failed.\n");
42     return (EINVAL);
43   }
44
45   sn_network_show (n);
46
47   sn_network_destroy (n);
48
49   return (0);
50 } /* }}} int show_fh */
51
52 static int show_file (const char *file) /* {{{ */
53 {
54   FILE *fh;
55   int status;
56
57   if (file == NULL)
58     return (EINVAL);
59
60   fh = fopen (file, "r");
61   if (fh == NULL)
62   {
63     fprintf (stderr, "Opening file \"%s\" failed: %s\n",
64         file, strerror (errno));
65     return (errno);
66   }
67
68   status = show_fh (fh);
69
70   fclose (fh);
71   return (status);
72 } /* }}} int show_file */
73
74 int main (int argc, char **argv)
75 {
76   if (argc == 1)
77   {
78     show_fh (stdin);
79   }
80   else
81   {
82     int i;
83     for (i = 1; i < argc; i++)
84     {
85       if (i > 1)
86         puts ("\n");
87
88       if (argc > 2)
89         printf ("=== %s ===\n\n", argv[i]);
90
91       show_file (argv[i]);
92     }
93   }
94
95   exit (EXIT_SUCCESS);
96 } /* int main */
97
98 /* vim: set shiftwidth=2 softtabstop=2 : */