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_population.h"
39 #include "sn_random.h"
41 static sn_network_t *network = NULL;
43 static void exit_usage (const char *name)
45 printf ("Usage: %s [options]\n"
47 "Valid options are:\n"
48 " -i <file> File holding the network (REQUIRED)\n"
52 } /* void exit_usage */
54 int read_options (int argc, char **argv)
58 while ((option = getopt (argc, argv, "i:")) != -1)
65 sn_network_destroy (network);
66 network = sn_network_read_file (optarg);
78 fprintf (stderr, "No network (`-i' option) was given or failed to read.\n");
83 } /* int read_options */
85 static int read_value (int *ret_value)
91 bufptr = fgets (buffer, sizeof (buffer), stdin);
98 fprintf (stderr, "fgets failed.\n");
104 *ret_value = strtol (bufptr, &endptr, 0);
105 if (endptr == bufptr)
107 fprintf (stderr, "strtol failed.\n");
112 } /* int read_value */
114 static int show_values (int values_num, const int *values)
118 assert (values_num > 0);
120 fprintf (stdout, "%i", values[0]);
121 for (i = 1; i < values_num; i++)
122 fprintf (stdout, " %i", values[i]);
123 fprintf (stdout, "\n");
127 } /* int show_values */
129 static int show_sort (int *values)
134 stages_num = SN_NETWORK_STAGE_NUM (network);
136 show_values (SN_NETWORK_INPUT_NUM (network), values);
137 for (i = 0; i < stages_num; i++)
141 s = SN_NETWORK_STAGE_GET (network, i);
142 sn_stage_sort (s, values);
144 show_values (SN_NETWORK_INPUT_NUM (network), values);
148 } /* int show_sort */
150 static int read_values (int values_num, int *ret_values)
156 for (i = 0; i < values_num; i++)
158 status = read_value (ret_values + i);
164 } /* int read_values */
166 int main (int argc, char **argv)
172 status = read_options (argc, argv);
176 inputs_num = SN_NETWORK_INPUT_NUM (network);
178 values = (int *) malloc (inputs_num * sizeof (int));
181 fprintf (stderr, "malloc failed.\n");
187 status = read_values (inputs_num, values);
199 /* vim: set shiftwidth=2 softtabstop=2 : */