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 #ifndef _ISOC99_SOURCE
23 # define _ISOC99_SOURCE
25 #ifndef _POSIX_C_SOURCE
26 # define _POSIX_C_SOURCE 200112L
34 #include <sys/types.h>
42 #include "sn_network.h"
43 #include "sn_population.h"
44 #include "sn_random.h"
46 static sn_network_t *network = NULL;
48 static void exit_usage (const char *name)
50 printf ("Usage: %s [options]\n"
52 "Valid options are:\n"
53 " -i <file> File holding the network (REQUIRED)\n"
57 } /* void exit_usage */
59 int read_options (int argc, char **argv)
63 while ((option = getopt (argc, argv, "i:")) != -1)
70 sn_network_destroy (network);
71 network = sn_network_read_file (optarg);
83 fprintf (stderr, "No network (`-i' option) was given or failed to read.\n");
88 } /* int read_options */
90 static int read_value (int *ret_value)
96 bufptr = fgets (buffer, sizeof (buffer), stdin);
103 fprintf (stderr, "fgets failed.\n");
109 *ret_value = strtol (bufptr, &endptr, 0);
110 if (endptr == bufptr)
112 fprintf (stderr, "strtol failed.\n");
117 } /* int read_value */
119 static int show_values (int values_num, const int *values)
123 assert (values_num > 0);
125 fprintf (stdout, "%i", values[0]);
126 for (i = 1; i < values_num; i++)
127 fprintf (stdout, " %i", values[i]);
128 fprintf (stdout, "\n");
132 } /* int show_values */
134 static int show_sort (int *values)
139 stages_num = SN_NETWORK_STAGE_NUM (network);
141 show_values (SN_NETWORK_INPUT_NUM (network), values);
142 for (i = 0; i < stages_num; i++)
147 s = SN_NETWORK_STAGE_GET (network, i);
149 for (j = 0; j < SN_STAGE_COMP_NUM (s); j++)
153 c = SN_STAGE_COMP_GET (s, j);
155 if (values[c->min] > values[c->max])
157 int temp = values[c->min];
158 values[c->min] = values[c->max];
159 values[c->max] = temp;
161 } /* for (comparators) */
163 show_values (SN_NETWORK_INPUT_NUM (network), values);
167 } /* int show_sort */
169 static int read_values (int values_num, int *ret_values)
175 for (i = 0; i < values_num; i++)
177 status = read_value (ret_values + i);
183 } /* int read_values */
185 int main (int argc, char **argv)
191 status = read_options (argc, argv);
195 inputs_num = SN_NETWORK_INPUT_NUM (network);
197 values = (int *) malloc (inputs_num * sizeof (int));
200 fprintf (stderr, "malloc failed.\n");
206 status = read_values (inputs_num, values);
218 /* vim: set shiftwidth=2 softtabstop=2 : */