Use utility functions

This commit is contained in:
Lucas 2020-03-08 21:24:10 +00:00
parent b13f35e0e6
commit dbd7618c9f
7 changed files with 48 additions and 201 deletions

112
julia.c
View file

@ -16,8 +16,6 @@
#endif
#include <complex.h>
#include <errno.h>
#include <inttypes.h>
#include <limits.h>
#include <math.h>
#include <stdlib.h>
@ -164,123 +162,43 @@ julia_main(int argc, char *argv[])
while ((ch = getopt(argc, argv, "a:b:c:D:d:h:Ll:n:w:x:y:z:")) != -1)
switch (ch) {
case 'a':
errno = 0;
cen_x = strtold(optarg, &end);
if (end == optarg || *end != '\0' || errno != 0) {
fprintf(stderr, "-a: invalid value \"%s\"",
optarg);
exit(1);
}
cen_x = parse_double(optarg);
break;
case 'b':
errno = 0;
cen_y = strtold(optarg, &end);
if (end == optarg || *end != '\0' || errno != 0) {
fprintf(stderr, "-b: invalid value \"%s\"",
optarg);
exit(1);
}
cen_y = parse_double(optarg);
break;
case 'c':
errno = 0;
contour = strtold(optarg, &end);
if (end == optarg || *end != '\0' || errno != 0
|| contour <= 0) {
fprintf(stderr, "-c: invalid value \"%s\"",
optarg);
exit(1);
}
contour = parse_double(optarg);
break;
case 'D':
errno = 0;
displacement = strtold(optarg, &end);
if (end == optarg || *end != '\0' || errno != 0) {
fprintf(stderr, "-d: invalid value \"%s\"",
optarg);
exit(1);
}
displacement = parse_double(optarg);
break;
case 'd':
errno = 0;
density = strtold(optarg, &end);
if (end == optarg || *end != '\0' || errno != 0) {
fprintf(stderr, "-d: invalid value \"%s\"",
optarg);
exit(1);
}
density = parse_double(optarg);
break;
case 'h':
errno = 0;
v = strtoimax(optarg, &end, 0);
if (end == optarg || *end != '\0' || errno != 0 ||
v <= 0 || v >= 65536) {
fprintf(stderr, "-h: invalid value \"%s\"",
optarg);
exit(1);
}
height = (unsigned int)v + 1;
height = parse_integer(optarg, 1, 65536);
break;
case 'L':
do_light = 1;
break;
case 'l':
errno = 0;
light_intensity = strtold(optarg, &end);
if (end == optarg || *end != '\0' || errno != 0) {
fprintf(stderr, "-l: invalid value \"%s\"",
optarg);
exit(1);
}
light_intensity = parse_double(optarg);
break;
case 'n':
errno = 0;
v = strtoimax(optarg, &end, 0);
if (end == optarg || *end != '\0' || errno != 0 ||
v <= 0 || v >= ULONG_MAX) {
fprintf(stderr, "-n: invalid value \"%s\"",
optarg);
exit(1);
}
niters = (unsigned long)v;
niters = parse_integer(optarg, 1, ULONG_MAX);
break;
case 'w':
errno = 0;
v = strtoimax(optarg, &end, 0);
if (end == optarg || *end != '\0' || errno != 0 ||
v <= 0 || v >= 65536) {
fprintf(stderr, "-w: invalid value \"%s\"",
optarg);
exit(1);
}
width = (unsigned int)v + 1;
width = parse_integer(optarg, 1, 65536);
break;
case 'x':
errno = 0;
julia_x = strtold(optarg, &end);
if (end == optarg || *end != '\0' || errno != 0) {
fprintf(stderr, "-x: invalid value \"%s\"",
optarg);
exit(1);
}
julia_x = parse_double(optarg);
break;
case 'y':
errno = 0;
julia_y = strtold(optarg, &end);
if (end == optarg || *end != '\0' || errno != 0) {
fprintf(stderr, "-y: invalid value \"%s\"",
optarg);
exit(1);
}
julia_y = parse_double(optarg);
break;
case 'z':
errno = 0;
zoom = strtold(optarg, &end);
if (end == optarg || *end != '\0' || errno != 0
|| zoom <= 0) {
fprintf(stderr, "-z: invalid value \"%s\"",
optarg);
exit(1);
}
zoom = parse_double(optarg);
break;
default:
usage();
@ -292,10 +210,8 @@ julia_main(int argc, char *argv[])
palette = palette_gen(palette_size);
if ((rescache = malloc(sizeof(*rescache) * width * height)) == NULL) {
fprintf(stderr, "out of memory\n");
exit(1);
}
if ((rescache = malloc(sizeof(*rescache) * width * height)) == NULL)
errx(1, "out of memory");
if (do_light) {
long double angle_xy, angle_z;