Skip to content

Commit 332e5b1

Browse files
committed
Delimiter functionality in ninc
1 parent 0fca66b commit 332e5b1

File tree

2 files changed

+58
-24
lines changed

2 files changed

+58
-24
lines changed

inc/nin.c

Lines changed: 55 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,12 @@
2828
*/
2929

3030
#include <stdlib.h>
31+
#include <unistd.h>
3132
#include <stdio.h>
3233
#include <string.h>
3334
#include <ctype.h>
3435

35-
#define VERSION "v1.2.1"
36+
#define VERSION "v1.2.2"
3637
#define BUFFER_SIZE 256
3738

3839
static void
@@ -41,11 +42,12 @@ help(char *exename)
4142
printf( "ninc - Narthex incrementor %s\n"
4243
"By Michael Constantine Dimopoulos <mk@mcdim.xyz>\n\n"
4344

45+
"-d use delimiters (specify in a string)\n"
4446
"-n increment numerical lines as well\n"
4547
"-h print this panel & exit\n"
4648
"-v print current version & exit\n\n"
4749

48-
"Usage: cat [FILENAME] | %s [MIN] [MAX] [-n]\n",
50+
"Usage: cat [FILENAME] | %s [MIN] [MAX] [OPTIONS]\n",
4951
VERSION, exename);
5052
exit(EXIT_SUCCESS);
5153
}
@@ -86,14 +88,18 @@ check_num(int num, char * buffer)
8688
}
8789

8890
static void
89-
ninc(FILE *f, int min, int max, int numerical)
91+
ninc(FILE *f, int min, int max, int numerical, char del)
9092
{
9193
char buffer[BUFFER_SIZE];
9294
while (fgets(buffer, sizeof(buffer), f) != NULL) {
9395
strtok(buffer, "\n");
9496
for (int i = min; i <= max; i++) {
95-
if (check_num(numerical, buffer) == 1)
96-
printf("%s%d\n", buffer, i);
97+
if (check_num(numerical, buffer) == 1) {
98+
if (del == ' ')
99+
printf("%s%d\n", buffer, i);
100+
else
101+
printf("%s%c%d\n", buffer, del, i);
102+
}
97103
}
98104
}
99105
}
@@ -112,34 +118,60 @@ main(int argc, char * argv[])
112118
{
113119
int min = 1;
114120
int max = 10;
121+
int d = 0;
122+
char del[10];
115123
int numerical = 0;
124+
int index;
125+
int c;
116126

117-
if (argc == 2) {
118-
if (strcmp(argv[1], "-h") == 0)
119-
help(argv[0]);
120-
else if (strcmp(argv[1], "-v") ==0)
127+
opterr = 0;
128+
129+
while ((c = getopt(argc, argv, "d:nvh")) != -1 )
130+
switch (c) {
131+
case 'v':
121132
die(VERSION);
122-
} else if (argc == 3) {
123-
min = atoi(argv[1]);
124-
max = atoi(argv[2]);
125-
} else if (argc == 4) {
126-
min = atoi(argv[1]);
127-
max = atoi(argv[2]);
128-
if (strcmp(argv[3], "-n") == 0)
129-
numerical = 1;
133+
case 'h':
134+
help(argv[0]);
135+
case 'd':
136+
d=1;
137+
strncpy(del, optarg, 10);
138+
break;
139+
case 'n':
140+
numerical=1;
141+
break;
142+
case '?':
143+
exit(EXIT_FAILURE);
144+
break;
145+
}
146+
147+
148+
if (optind < argc) {
149+
min = atoi(argv[optind]);
150+
max = atoi(argv[optind+1]);
130151
} else {
131152
fprintf(stderr, "%s: wrong number of arguments\n", argv[0]);
132153
exit(EXIT_FAILURE);
133154
}
134155

135-
if (min <= max) {
136-
FILE *f;
137-
f = save_stdin(stdin);
138-
rewind(f);
139-
print_only(f);
156+
if (min > max) {
157+
int temp = max;
158+
max = min;
159+
min = temp;
160+
}
161+
162+
FILE *f;
163+
f = save_stdin(stdin);
164+
rewind(f);
165+
print_only(f);
166+
rewind(f);
167+
ninc(f, min, max, numerical, ' ');
168+
169+
for (int i = 0; i < strlen(del); i++) {
140170
rewind(f);
141-
ninc(f, min, max, numerical);
171+
ninc(f, min, max, numerical, del[i]);
142172
}
143173

174+
144175
exit(EXIT_SUCCESS);
176+
145177
}

inc/ninc.1

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
.\" Manpage for ninc
22

3-
.TH man 8 "15 Jul 2021" "1.2.1" "ninc manual page"
3+
.TH man 8 "15 Jul 2021" "1.2.2" "ninc manual page"
44
.SH NAME
55
ninc \- Narthex incrementor
66
.SH SYNOPSIS
@@ -9,6 +9,8 @@ cat dictionary.txt | ninc [MIN] [MAX] [OPTIONS] > output.txt
99
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.
1010

1111
.SH OPTIONS
12+
-d user delimiters (specify in a string)
13+
1214
-n increment numerical lines
1315

1416
-v print version and exit

0 commit comments

Comments
 (0)