2 * libsortnetwork - src/sn-apply.c
3 * Copyright (C) 2008-2010 Florian octo Forster
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.
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.
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
19 * Florian octo Forster <ff at octo.it>
29 #include <sys/types.h>
37 #include "sn_network.h"
38 #include "sn_random.h"
40 static sn_network_t *network = NULL;
42 static void exit_usage (const char *name)
44 printf ("Usage: %s [options]\n"
46 "Valid options are:\n"
47 " -i <file> File holding the network (REQUIRED)\n"
51 } /* void exit_usage */
53 int read_options (int argc, char **argv)
57 while ((option = getopt (argc, argv, "i:")) != -1)
64 sn_network_destroy (network);
65 network = sn_network_read_file (optarg);
77 fprintf (stderr, "No network (`-i' option) was given or failed to read.\n");
82 } /* int read_options */
84 static int read_value (int *ret_value)
90 bufptr = fgets (buffer, sizeof (buffer), stdin);
97 fprintf (stderr, "fgets failed.\n");
103 *ret_value = strtol (bufptr, &endptr, 0);
104 if (endptr == bufptr)
106 fprintf (stderr, "strtol failed.\n");
111 } /* int read_value */
113 static int show_values (int values_num, const int *values)
117 assert (values_num > 0);
119 fprintf (stdout, "%i", values[0]);
120 for (i = 1; i < values_num; i++)
121 fprintf (stdout, " %i", values[i]);
122 fprintf (stdout, "\n");
126 } /* int show_values */
128 static int show_sort (int *values)
133 stages_num = SN_NETWORK_STAGE_NUM (network);
135 show_values (SN_NETWORK_INPUT_NUM (network), values);
136 for (i = 0; i < stages_num; i++)
140 s = SN_NETWORK_STAGE_GET (network, i);
141 sn_stage_sort (s, values);
143 show_values (SN_NETWORK_INPUT_NUM (network), values);
147 } /* int show_sort */
149 static int read_values (int values_num, int *ret_values)
155 for (i = 0; i < values_num; i++)
157 status = read_value (ret_values + i);
163 } /* int read_values */
165 int main (int argc, char **argv)
171 status = read_options (argc, argv);
175 inputs_num = SN_NETWORK_INPUT_NUM (network);
177 values = (int *) malloc (inputs_num * sizeof (int));
180 fprintf (stderr, "malloc failed.\n");
186 status = read_values (inputs_num, values);
198 /* vim: set shiftwidth=2 softtabstop=2 : */