24f01d70a2d650a8791e234b69c4b1529494e727
[sort-networks.git] / src / sn-apply.c
1 /**
2  * libsortnetwork - src/sn-apply.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 <stdint.h>
27 #include <string.h>
28
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <fcntl.h>
32 #include <unistd.h>
33 #include <signal.h>
34 #include <assert.h>
35 #include <limits.h>
36
37 #include "sn_network.h"
38 #include "sn_population.h"
39 #include "sn_random.h"
40
41 static sn_network_t *network = NULL;
42
43 static void exit_usage (const char *name)
44 {
45   printf ("Usage: %s [options]\n"
46       "\n"
47       "Valid options are:\n"
48       "  -i <file>     File holding the network (REQUIRED)\n"
49       "\n",
50       name);
51   exit (EXIT_FAILURE);
52 } /* void exit_usage */
53
54 int read_options (int argc, char **argv)
55 {
56   int option;
57
58   while ((option = getopt (argc, argv, "i:")) != -1)
59   {
60     switch (option)
61     {
62       case 'i':
63       {
64         if (network != NULL)
65           sn_network_destroy (network);
66         network = sn_network_read_file (optarg);
67         break;
68       }
69
70       case 'h':
71       default:
72         exit_usage (argv[0]);
73     }
74   }
75
76   if (network == NULL)
77   {
78     fprintf (stderr, "No network (`-i' option) was given or failed to read.\n");
79     return (-1);
80   }
81
82   return (0);
83 } /* int read_options */
84
85 static int read_value (int *ret_value)
86 {
87   char buffer[4096];
88   char *bufptr;
89   char *endptr;
90
91   bufptr = fgets (buffer, sizeof (buffer), stdin);
92   if (bufptr == NULL)
93   {
94     if (feof (stdin))
95       return (1);
96     else
97     {
98       fprintf (stderr, "fgets failed.\n");
99       return (-1);
100     }
101   }
102
103   endptr = NULL;
104   *ret_value = strtol (bufptr, &endptr, 0);
105   if (endptr == bufptr)
106   {
107     fprintf (stderr, "strtol failed.\n");
108     return (-1);
109   }
110
111   return (0);
112 } /* int read_value */
113
114 static int show_values (int values_num, const int *values)
115 {
116   int i;
117
118   assert (values_num > 0);
119
120   fprintf (stdout, "%i", values[0]);
121   for (i = 1; i < values_num; i++)
122     fprintf (stdout, " %i", values[i]);
123   fprintf (stdout, "\n");
124   fflush (stdout);
125
126   return (0);
127 } /* int show_values */
128
129 static int show_sort (int *values)
130 {
131   int stages_num;
132   int i;
133
134   stages_num = SN_NETWORK_STAGE_NUM (network);
135
136   show_values (SN_NETWORK_INPUT_NUM (network), values);
137   for (i = 0; i < stages_num; i++)
138   {
139     sn_stage_t *s;
140
141     s = SN_NETWORK_STAGE_GET (network, i);
142     sn_stage_sort (s, values);
143
144     show_values (SN_NETWORK_INPUT_NUM (network), values);
145   } /* for (stages) */
146
147   return (0);
148 } /* int show_sort */
149
150 static int read_values (int values_num, int *ret_values)
151 {
152   int status;
153   int i;
154
155   status = 0;
156   for (i = 0; i < values_num; i++)
157   {
158     status = read_value (ret_values + i);
159     if (status != 0)
160       break;
161   }
162
163   return (status);
164 } /* int read_values */
165
166 int main (int argc, char **argv)
167 {
168   int inputs_num;
169   int *values;
170   int status;
171
172   status = read_options (argc, argv);
173   if (status != 0)
174     return (1);
175
176   inputs_num = SN_NETWORK_INPUT_NUM (network);
177
178   values = (int *) malloc (inputs_num * sizeof (int));
179   if (values == NULL)
180   {
181     fprintf (stderr, "malloc failed.\n");
182     return (1);
183   }
184
185   while (42)
186   {
187     status = read_values (inputs_num, values);
188     if (status != 0)
189       break;
190
191     show_sort (values);
192   }
193
194   if (status < 0)
195     exit (EXIT_FAILURE);
196   exit (EXIT_SUCCESS);
197 } /* int main */
198
199 /* vim: set shiftwidth=2 softtabstop=2 : */