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