-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstar-generator.c
285 lines (240 loc) · 6.28 KB
/
star-generator.c
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
* moto-design star generator.
*/
/*
PROJECT="${HOME}/projects/moto-design/svg-generators"
(cd ${PROJECT} && ./bootstrap) && ${PROJECT}/configure --enable-debug
*/
#define _GNU_SOURCE
#define _ISOC99_SOURCE
#if defined(HAVE_CONFIG_H)
#include "config.h"
#endif
#include <assert.h>
#include <errno.h>
#include <getopt.h>
#include <limits.h>
#include <string.h>
#include <time.h>
#include "svg-utils.h"
static const char program_name[] = "star-generator";
static void print_version(void)
{
printf("%s (" PACKAGE_NAME ") " PACKAGE_VERSION "\n", program_name);
}
static void print_bugreport(void)
{
fprintf(stderr, "Report bugs at " PACKAGE_BUGREPORT ".\n");
}
enum opt_value {opt_undef = 0, opt_yes, opt_no};
struct opts {
struct star_params star_params;
char *output_file;
enum opt_value help;
enum opt_value verbose;
enum opt_value version;
};
static const struct star_params init_star_params = {
.points = UINT_MAX,
.density = UINT_MAX,
.radius = HUGE_VALF,
.rotation = HUGE_VALF,
};
static const struct star_params default_star_params = {
.points = 5,
.density = 2,
.radius = 307.6923075,
.rotation = -90.0,
};
static void print_usage(const struct opts *opts)
{
print_version();
fprintf(stderr,
"%s - Generates SVG file of a star.\n"
"Usage: %s [flags]\n"
"Option flags:\n"
" --points - Number of points (vertices). Default: '%u'.\n"
" --density - Polygon density. Default: '%u'.\n"
" --radius - Radius. Default: '%f'.\n"
" --rotation - Rotation. Default: '%f'.\n"
" -o --output-file - Output file. Default: '%s'.\n"
" -h --help - Show this help and exit.\n"
" -v --verbose - Verbose execution.\n"
" -V --version - Display the program version number.\n",
program_name, program_name,
opts->star_params.points,
opts->star_params.density,
opts->star_params.radius,
opts->star_params.rotation,
opts->output_file
);
print_bugreport();
}
static int opts_parse(struct opts *opts, int argc, char *argv[])
{
static const struct option long_options[] = {
{"points", required_argument, NULL, '1'},
{"density", required_argument, NULL, '2'},
{"radius", required_argument, NULL, '3'},
{"rotation", required_argument, NULL, '4'},
{"output-file", required_argument, NULL, 'o'},
{"config-file", required_argument, NULL, 'f'},
{"background", no_argument, NULL, 'b'},
{"help", no_argument, NULL, 'h'},
{"verbose", no_argument, NULL, 'v'},
{"version", no_argument, NULL, 'V'},
{ NULL, 0, NULL, 0},
};
static const char short_options[] = "bo:f:hvV";
*opts = (struct opts){
.star_params = init_star_params,
.output_file = "-",
.help = opt_no,
.verbose = opt_no,
.version = opt_no,
};
if (0) {
int i;
debug("argc = %d\n", argc);
for (i = 0; i < argc; i++) {
debug(" %d: %p = '%s'\n", i, &argv[i],
argv[i]);
}
}
while (1) {
int c = getopt_long(argc, argv, short_options, long_options,
NULL);
if (c == EOF)
break;
if (0) {
debug("%c => '%s'\n", c, optarg);
}
switch (c) {
// star
case '1':
opts->star_params.points = to_unsigned(optarg);
if (opts->star_params.points == UINT_MAX) {
opts->help = opt_yes;
return -1;
}
break;
case '2':
opts->star_params.density = to_unsigned(optarg);
if (opts->star_params.density == UINT_MAX) {
opts->help = opt_yes;
return -1;
}
break;
case '3':
opts->star_params.radius = to_float(optarg);
if (opts->star_params.radius == HUGE_VALF) {
opts->help = opt_yes;
return -1;
}
break;
case '4':
opts->star_params.rotation = to_float(optarg);
if (opts->star_params.rotation == HUGE_VALF) {
opts->help = opt_yes;
return -1;
}
break;
// admin
case 'o': {
size_t len;
if (0) {
debug("p: %p = '%s'\n", optarg, optarg);
}
if (!optarg) {
error("Missing required argument <output-file>.'\n");
opts->help = opt_yes;
return -1;
}
len = strlen(optarg) + 1;
opts->output_file = mem_alloc(len);
strcpy(opts->output_file, optarg);
break;
}
case 'h':
opts->help = opt_yes;
break;
case 'v':
opts->verbose = opt_yes;
break;
case 'V':
opts->version = opt_yes;
break;
default:
assert(0);
opts->help = opt_yes;
return -1;
}
}
return optind != argc;
}
static void write_svg(FILE* out_stream, const struct star_params *star_params)
{
struct svg_rect background_rect;
char star_id[256];
snprintf(star_id, sizeof(star_id), "star_%d_%d", star_params->points,
star_params->density);
background_rect.width = 2.2 * star_params->radius;
background_rect.height = background_rect.width;
background_rect.x = -background_rect.width / 2;
background_rect.y = -background_rect.height / 2;
// FIXME: for debug
//background_rect.x = 0.0;
//background_rect.y = -background_rect.height;
svg_open_svg(out_stream, &background_rect);
//svg_debug_stream_set(out_stream);
svg_write_star(out_stream, &svg_style_yellow_blue, NULL, star_id,
star_params);
svg_close_svg(out_stream);
}
int main(int argc, char *argv[])
{
struct opts opts;
FILE *out_stream;
log_set_exit_on_error(true);
if (opts_parse(&opts, argc, argv)) {
print_usage(&opts);
return EXIT_FAILURE;
}
if (opts.version == opt_yes) {
print_version();
return EXIT_SUCCESS;
}
if (opts.verbose == opt_yes) {
log_set_verbose(true);
}
if (opts.star_params.points == init_star_params.points) {
opts.star_params.points = default_star_params.points;
}
if (opts.star_params.density == init_star_params.density) {
opts.star_params.density = default_star_params.density;
}
if (opts.star_params.radius == init_star_params.radius) {
opts.star_params.radius = default_star_params.radius;
}
if (opts.star_params.rotation == init_star_params.rotation) {
opts.star_params.rotation = default_star_params.rotation;
}
if (!strcmp(opts.output_file, "-")) {
out_stream = stdout;
} else {
out_stream = fopen(opts.output_file, "w");
if (!out_stream) {
error("open <output-file> '%s' failed: %s\n",
opts.output_file, strerror(errno));
assert(0);
return EXIT_FAILURE;
}
}
if (opts.help == opt_yes) {
print_usage(&opts);
return EXIT_SUCCESS;
}
srand((unsigned int)time(NULL));
write_svg(out_stream, &opts.star_params);
return EXIT_SUCCESS;
}