dc0ede2198c9250e762a65abf6c86cda5ede07bf
[sort-networks.git] / src / sn-apply.c
1 /**
2  * collectd - 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 #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
146     s = SN_NETWORK_STAGE_GET (network, i);
147     sn_stage_sort (s, values);
148
149     show_values (SN_NETWORK_INPUT_NUM (network), values);
150   } /* for (stages) */
151
152   return (0);
153 } /* int show_sort */
154
155 static int read_values (int values_num, int *ret_values)
156 {
157   int status;
158   int i;
159
160   status = 0;
161   for (i = 0; i < values_num; i++)
162   {
163     status = read_value (ret_values + i);
164     if (status != 0)
165       break;
166   }
167
168   return (status);
169 } /* int read_values */
170
171 int main (int argc, char **argv)
172 {
173   int inputs_num;
174   int *values;
175   int status;
176
177   status = read_options (argc, argv);
178   if (status != 0)
179     return (1);
180
181   inputs_num = SN_NETWORK_INPUT_NUM (network);
182
183   values = (int *) malloc (inputs_num * sizeof (int));
184   if (values == NULL)
185   {
186     fprintf (stderr, "malloc failed.\n");
187     return (1);
188   }
189
190   while (42)
191   {
192     status = read_values (inputs_num, values);
193     if (status != 0)
194       break;
195
196     show_sort (values);
197   }
198
199   if (status < 0)
200     return (1);
201   return (0);
202 } /* int main */
203
204 /* vim: set shiftwidth=2 softtabstop=2 : */