1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
/*
BSD Zero Clause License
Copyright (c) 2023 Samuel Wirajaya
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
================================================================================
K O C H G E N
================================================================================
*/
#include <ctype.h> /* toupper */
#include <limits.h> /* strtoul */
#include <stdio.h>
#include <stdlib.h>
#include <string.h> /* strnlen */
#include <time.h>
#include <unistd.h>
#define VERSION "0.9.0"
char *progname;
int wrlenmax;
int wrlenmin;
int nwrs;
char *charset;
FILE *ofile;
void
usage(void) {
printf(
"Koch Training Generator (version " VERSION ")\n"
"usage: %s [-h] [-M WRLENMAX] [-m WRLENMIN] [-n NWRS] [-o OFILE] "
"CHARSET\n"
"CHARSET is a string containing all letters to be trained on\n"
"e.g. 'km' for the first lesson, 'kmr' for the second lesson,\n"
"etc.\n"
" -h: displays this message\n"
" -M: sets the maximum n. of letters in a word [%d]\n"
" -m: sets the minimum n. of letters in a word [%d]\n"
" -n: sets the number of words to be generated [%d]\n"
" -o: writes a copy of the output to OFILE\n"
, progname, wrlenmax, wrlenmin, nwrs);
}
void
teeputc(int c) {
fputc(c, stdout);
if (ofile) {
fputc(c, ofile);
}
}
int
main(int argc, char **argv) {
int i, j, wrlen;
int k, setsz;
int opt;
srand(time(NULL));
progname = argv[0];
wrlenmax = 5;
wrlenmin = 5;
nwrs = 50;
ofile = NULL;
while ((opt = getopt(argc, argv, "M:hm:n:o:")) != -1) {
switch (opt) {
case 'M':
wrlenmax = (int)strtoul(optarg, NULL, 10);
break;
case 'h':
usage();
return 0;
case 'm':
wrlenmin = (int)strtoul(optarg, NULL, 10);
break;
case 'n':
nwrs = (int)strtoul(optarg, NULL, 10);
break;
case 'o':
ofile = fopen(optarg, "w");
if (!ofile) {
perror("warning: cannot open output file");
}
break;
case '?':
default:
usage();
return 1;
}
}
argc -= optind;
argv += optind;
if (argc == 1) {
charset = argv[0];
} else {
fprintf(stderr, "error: one charset must be supplied\n");
usage();
return 1;
}
if ((wrlenmax < 1) || (wrlenmax > 1023)) {
fprintf(stderr, "error: wrlenmax out of range (1--1023)\n");
usage();
return 1;
}
if ((wrlenmin < 1) || (wrlenmin > wrlenmax)) {
fprintf(stderr, "error: wrlenmin out of range (1--wrlenmax)\n");
usage();
return 1;
}
setsz = (int)strnlen(charset, 127);
for (k = 0; k < setsz; ++k) {
charset[k] = toupper(charset[k]);
}
for (i = 0; i < nwrs; ++i) {
if (wrlenmin == wrlenmax) {
wrlen = wrlenmin;
} else {
wrlen = wrlenmin + rand() % (1 + wrlenmax - wrlenmin);
}
for (j = 0; j < wrlen; ++j) {
k = rand() % setsz;
teeputc(charset[k]);
}
teeputc('\n');
}
return 0;
}
|