From 332e5b1eb083a949473281480f1a5ea16c32a994 Mon Sep 17 00:00:00 2001 From: Michael Constantine Dimopoulos Date: Sat, 26 Feb 2022 12:29:28 +0000 Subject: [PATCH] Delimiter functionality in ninc --- inc/nin.c | 78 ++++++++++++++++++++++++++++++++++++++---------------- inc/ninc.1 | 4 ++- 2 files changed, 58 insertions(+), 24 deletions(-) diff --git a/inc/nin.c b/inc/nin.c index f3ea741..d1aba8a 100644 --- a/inc/nin.c +++ b/inc/nin.c @@ -28,11 +28,12 @@ */ #include +#include #include #include #include -#define VERSION "v1.2.1" +#define VERSION "v1.2.2" #define BUFFER_SIZE 256 static void @@ -41,11 +42,12 @@ help(char *exename) printf( "ninc - Narthex incrementor %s\n" "By Michael Constantine Dimopoulos \n\n" + "-d use delimiters (specify in a string)\n" "-n increment numerical lines as well\n" "-h print this panel & exit\n" "-v print current version & exit\n\n" - "Usage: cat [FILENAME] | %s [MIN] [MAX] [-n]\n", + "Usage: cat [FILENAME] | %s [MIN] [MAX] [OPTIONS]\n", VERSION, exename); exit(EXIT_SUCCESS); } @@ -86,14 +88,18 @@ check_num(int num, char * buffer) } static void -ninc(FILE *f, int min, int max, int numerical) +ninc(FILE *f, int min, int max, int numerical, char del) { char buffer[BUFFER_SIZE]; while (fgets(buffer, sizeof(buffer), f) != NULL) { strtok(buffer, "\n"); for (int i = min; i <= max; i++) { - if (check_num(numerical, buffer) == 1) - printf("%s%d\n", buffer, i); + if (check_num(numerical, buffer) == 1) { + if (del == ' ') + printf("%s%d\n", buffer, i); + else + printf("%s%c%d\n", buffer, del, i); + } } } } @@ -112,34 +118,60 @@ main(int argc, char * argv[]) { int min = 1; int max = 10; + int d = 0; + char del[10]; int numerical = 0; + int index; + int c; - if (argc == 2) { - if (strcmp(argv[1], "-h") == 0) - help(argv[0]); - else if (strcmp(argv[1], "-v") ==0) + opterr = 0; + + while ((c = getopt(argc, argv, "d:nvh")) != -1 ) + switch (c) { + case 'v': die(VERSION); - } else if (argc == 3) { - min = atoi(argv[1]); - max = atoi(argv[2]); - } else if (argc == 4) { - min = atoi(argv[1]); - max = atoi(argv[2]); - if (strcmp(argv[3], "-n") == 0) - numerical = 1; + case 'h': + help(argv[0]); + case 'd': + d=1; + strncpy(del, optarg, 10); + break; + case 'n': + numerical=1; + break; + case '?': + exit(EXIT_FAILURE); + break; + } + + + if (optind < argc) { + min = atoi(argv[optind]); + max = atoi(argv[optind+1]); } else { fprintf(stderr, "%s: wrong number of arguments\n", argv[0]); exit(EXIT_FAILURE); } - if (min <= max) { - FILE *f; - f = save_stdin(stdin); - rewind(f); - print_only(f); + if (min > max) { + int temp = max; + max = min; + min = temp; + } + + FILE *f; + f = save_stdin(stdin); + rewind(f); + print_only(f); + rewind(f); + ninc(f, min, max, numerical, ' '); + + for (int i = 0; i < strlen(del); i++) { rewind(f); - ninc(f, min, max, numerical); + ninc(f, min, max, numerical, del[i]); } + exit(EXIT_SUCCESS); + } diff --git a/inc/ninc.1 b/inc/ninc.1 index 3f6a28b..851f367 100644 --- a/inc/ninc.1 +++ b/inc/ninc.1 @@ -1,6 +1,6 @@ .\" Manpage for ninc -.TH man 8 "15 Jul 2021" "1.2.1" "ninc manual page" +.TH man 8 "15 Jul 2021" "1.2.2" "ninc manual page" .SH NAME ninc \- Narthex incrementor .SH SYNOPSIS @@ -9,6 +9,8 @@ cat dictionary.txt | ninc [MIN] [MAX] [OPTIONS] > output.txt ninc reads from standard input and, after printing the dictionary as is, it reprints it but multiplies each line with the difference of max-min, and appends n to each line, where n is increased after every line from min to max inclusive. It prints to standard output. .SH OPTIONS +-d user delimiters (specify in a string) + -n increment numerical lines -v print version and exit