X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fsn-cut.c;h=e88836562b97ffe8687cbf81f2b34f5531712b12;hb=4f5cca3d0af8783bc0d44ea9f78467fd80228d09;hp=1227723ef45148cd79918408fc6710ee586327f0;hpb=02dd5a8e9890f7b16b950db238eeff42a57e9142;p=sort-networks.git diff --git a/src/sn-cut.c b/src/sn-cut.c index 1227723..e888365 100644 --- a/src/sn-cut.c +++ b/src/sn-cut.c @@ -1,42 +1,105 @@ +/** + * libsortnetwork - src/sn-cut.c + * Copyright (C) 2008-2010 Florian octo Forster + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; only version 2 of the License is applicable. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * Authors: + * Florian octo Forster + **/ + +#include "config.h" + +#ifndef _ISOC99_SOURCE +# define _ISOC99_SOURCE +#endif +#ifndef _POSIX_C_SOURCE +# define _POSIX_C_SOURCE 200809L +#endif +#ifdef _XOPEN_SOURCE +# define _XOPEN_SOURCE 700 +#endif + #include #include +#include #include #include "sn_network.h" -void exit_usage (const char *name) +static void exit_usage (const char *name) { - printf ("%s \n", name); - exit (1); + printf ("Usage: %s [:min|max] [[:min|max] ...]\n", name); + exit (EXIT_FAILURE); } +static int do_cut (sn_network_t *n, int defs_num, char **defs) +{ + int mask[SN_NETWORK_INPUT_NUM (n)]; + int status; + int i; + + memset (mask, 0, sizeof (mask)); + for (i = 0; i < defs_num; i++) + { + char *endptr = NULL; + int pos; + int val = 1; + + pos = (int) strtol (defs[i], &endptr, /* base = */ 0); + if (strcasecmp (":min", endptr) == 0) + val = -1; + + if ((pos < 0) || (pos >= SN_NETWORK_INPUT_NUM (n))) + { + fprintf (stderr, "Invalid line number: %i\n", pos); + return (-1); + } + + mask[pos] = val; + } + + status = sn_network_cut (n, mask); + if (status != 0) + { + fprintf (stderr, "sn_network_cut failed with status %i.\n", status); + return (-1); + } + + return (0); +} /* }}} int do_cut */ + int main (int argc, char **argv) { sn_network_t *n; - int pos = 0; - enum sn_network_cut_dir_e dir = DIR_MIN; - - if (argc != 3) + if (argc < 2) exit_usage (argv[0]); - pos = atoi (argv[1]); - if (strcasecmp ("max", argv[2]) == 0) - dir = DIR_MAX; - n = sn_network_read (stdin); if (n == NULL) { - printf ("n == NULL!\n"); - return (1); + fprintf (stderr, "Unable to read network from STDIN.\n"); + exit (EXIT_FAILURE); } - sn_network_cut_at (n, pos, dir); + do_cut (n, argc - 1, argv + 1); sn_network_compress (n); sn_network_write (n, stdout); - return (0); + exit (EXIT_SUCCESS); } /* int main */ /* vim: set shiftwidth=2 softtabstop=2 : */