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