src/sn-merge.c: Removed unused function.
[sort-networks.git] / src / sn-merge.c
1 /**
2  * libsortnetwork - src/sn-merge.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 <unistd.h>
28
29 #include "sn_network.h"
30
31 static _Bool use_bitonic = 0;
32 static const char *file0 = NULL;
33 static const char *file1 = NULL;
34
35 static void exit_usage (void) /* {{{ */
36 {
37   printf ("sn-merge [options] <file0> <file1>\n"
38       "\n"
39       "Options:\n"
40       "  -b        Use the bitonic merger.\n"
41       "  -o        Use the odd-even merger. (default)\n"
42       "  -h        Display this help and exit.\n"
43       "\n");
44   exit (EXIT_FAILURE);
45 } /* }}} void exit_usage */
46
47 static int read_options (int argc, char **argv) /* {{{ */
48 {
49   int option;
50
51   while ((option = getopt (argc, argv, "boh")) != -1)
52   {
53     switch (option)
54     {
55       case 'b':
56         use_bitonic = 1;
57         break;
58
59       case 'o':
60         use_bitonic = 0;
61         break;
62
63       case 'h':
64       default:
65         exit_usage ();
66     }
67   }
68
69   if ((argc - optind) != 2)
70     exit_usage ();
71
72   file0 = argv[optind];
73   file1 = argv[optind + 1];
74
75   if ((file0 == NULL) || (file1 == NULL))
76     exit_usage ();
77
78   return (0);
79 } /* }}} int read_options */
80
81 int main (int argc, char **argv)
82 {
83   sn_network_t *n0;
84   sn_network_t *n1;
85   sn_network_t *n;
86
87   read_options (argc, argv);
88
89   if (strcmp ("-", file0) == 0)
90     n0 = sn_network_read (stdin);
91   else
92     n0 = sn_network_read_file (file0);
93   if (n0 == NULL)
94   {
95     fprintf (stderr, "Unable to read first network.\n");
96     exit (EXIT_FAILURE);
97   }
98
99   if (strcmp ("-", file1) == 0)
100   {
101     if (strcmp ("-", file0) == 0)
102       n1 = sn_network_clone (n0);
103     else
104       n1 = sn_network_read (stdin);
105   }
106   else
107     n1 = sn_network_read_file (file1);
108   if (n1 == NULL)
109   {
110     fprintf (stderr, "Unable to read second network.\n");
111     exit (EXIT_FAILURE);
112   }
113
114   if (use_bitonic)
115     n = sn_network_combine_bitonic_merge (n0, n1);
116   else
117     n = sn_network_combine_odd_even_merge (n0, n1);
118
119   if (n == NULL)
120   {
121     fprintf (stderr, "Combining the networks faild.\n");
122     exit (EXIT_FAILURE);
123   }
124
125   sn_network_destroy (n0);
126   sn_network_destroy (n1);
127
128   sn_network_write (n, stdout);
129
130   return (0);
131 } /* int main */
132
133 /* vim: set shiftwidth=2 softtabstop=2 : */