2 * collectd - src/sn-apply.c
3 * Copyright (C) 2008 Florian octo Forster
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.
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.
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
19 * Florian octo Forster <octo at verplant.org>
22 #define _ISOC99_SOURCE
23 #define _POSIX_C_SOURCE 200112L
30 #include <sys/types.h>
38 #include "sn_network.h"
39 #include "sn_population.h"
40 #include "sn_random.h"
42 static sn_network_t *network = NULL;
44 static void exit_usage (const char *name)
46 printf ("Usage: %s [options]\n"
48 "Valid options are:\n"
49 " -i <file> File holding the network (REQUIRED)\n"
53 } /* void exit_usage */
55 int read_options (int argc, char **argv)
59 while ((option = getopt (argc, argv, "i:")) != -1)
66 sn_network_destroy (network);
67 network = sn_network_read_file (optarg);
79 fprintf (stderr, "No network (`-i' option) was given or failed to read.\n");
84 } /* int read_options */
86 static int read_value (int *ret_value)
92 bufptr = fgets (buffer, sizeof (buffer), stdin);
99 fprintf (stderr, "fgets failed.\n");
105 *ret_value = strtol (bufptr, &endptr, 0);
106 if (endptr == bufptr)
108 fprintf (stderr, "strtol failed.\n");
113 } /* int read_value */
115 static int show_values (int values_num, const int *values)
119 assert (values_num > 0);
121 fprintf (stdout, "%i", values[0]);
122 for (i = 1; i < values_num; i++)
123 fprintf (stdout, " %i", values[i]);
124 fprintf (stdout, "\n");
128 } /* int show_values */
130 static int show_sort (int *values)
135 stages_num = SN_NETWORK_STAGE_NUM (network);
137 show_values (SN_NETWORK_INPUT_NUM (network), values);
138 for (i = 0; i < stages_num; i++)
143 s = SN_NETWORK_STAGE_GET (network, i);
145 for (j = 0; j < SN_STAGE_COMP_NUM (s); j++)
149 c = SN_STAGE_COMP_GET (s, j);
151 if (values[c->min] > values[c->max])
153 int temp = values[c->min];
154 values[c->min] = values[c->max];
155 values[c->max] = temp;
157 } /* for (comparators) */
159 show_values (SN_NETWORK_INPUT_NUM (network), values);
163 } /* int show_sort */
165 static int read_values (int values_num, int *ret_values)
171 for (i = 0; i < values_num; i++)
173 status = read_value (ret_values + i);
179 } /* int read_values */
181 int main (int argc, char **argv)
187 status = read_options (argc, argv);
191 inputs_num = SN_NETWORK_INPUT_NUM (network);
193 values = (int *) malloc (inputs_num * sizeof (int));
196 fprintf (stderr, "malloc failed.\n");
202 status = read_values (inputs_num, values);
214 /* vim: set shiftwidth=2 softtabstop=2 : */