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