From: Florian Forster Date: Fri, 17 Dec 2010 21:36:24 +0000 (+0100) Subject: src/sn-cut.c: Use the new cut-interface to do all the cuts at once. X-Git-Tag: v1.0.0~18 X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=ddf73351b9914f972238af5a11e697e24a448332;p=sort-networks.git src/sn-cut.c: Use the new cut-interface to do all the cuts at once. This greatly simplifies line numbering when doing multiple cuts. --- diff --git a/src/sn-cut.c b/src/sn-cut.c index 9cf9813..e888365 100644 --- a/src/sn-cut.c +++ b/src/sn-cut.c @@ -21,40 +21,80 @@ #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); + 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);