Skip to content

Commit

Permalink
Clarify render_list documentation (#388)
Browse files Browse the repository at this point in the history
_Also_:
* Added checks for `min` & `max` `x`, `y` & `z` tile coordinate options in order to:
  * Ensure they are integers
  * Ensure they are positive

Resolves #387
  • Loading branch information
hummeltech authored Feb 6, 2024
1 parent 22a13e8 commit 7811deb
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 27 deletions.
4 changes: 3 additions & 1 deletion docs/man/render_list.1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.TH RENDER_LIST "1" "2023-12-19" "mod_tile v0.7.0"
.TH RENDER_LIST "1" "2024-02-06" "mod_tile v0.7.0"
.\" Please adjust this date whenever revising the manpage.

.SH NAME
Expand Down Expand Up @@ -51,6 +51,8 @@ Filter input to only render tiles greater or equal to this zoom level (default i
Filter input to only render tiles less than or equal to this zoom level (default is 20).
.PP
If you are using --all, you can restrict the tile range by adding these options:
.BR
(please note that tile coordinates must be positive integers and are not latitude and longitude values)
.BR
-x, --min-x=X minimum X tile coordinate
.BR
Expand Down
47 changes: 47 additions & 0 deletions src/gen_tile_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,53 @@ TEST_CASE("render_list", "render list")
ret = WEXITSTATUS(ret);
REQUIRE(ret == 1);
}

SECTION("render_list option is positive with --help", "should return 0") {
std::string option = GENERATE("--max-load", "--max-x", "--max-y", "--max-zoom", "--min-x", "--min-y", "--min-zoom", "--num-threads");
std::string command = "./render_list " + option + " 1 --help";

// flawfinder: ignore
int ret = system(command.c_str());
ret = WEXITSTATUS(ret);
REQUIRE(ret == 0);
}

SECTION("render_list option is negative", "should return 1") {
std::string option = GENERATE("--max-load", "--max-x", "--max-y", "--max-zoom", "--min-x", "--min-y", "--min-zoom", "--num-threads");
std::string command = "./render_list " + option + " -1";

// flawfinder: ignore
int ret = system(command.c_str());
ret = WEXITSTATUS(ret);
REQUIRE(ret == 1);
}

SECTION("render_list option is float", "should return 1") {
std::string option = GENERATE("--max-load", "--max-x", "--max-y", "--max-zoom", "--min-x", "--min-y", "--min-zoom", "--num-threads");
std::string command = "./render_list " + option + " 0.12345";

// flawfinder: ignore
int ret = system(command.c_str());
ret = WEXITSTATUS(ret);
REQUIRE(ret == 1);
}

SECTION("render_list num threads subceeds minimum of 1", "should return 1") {
// flawfinder: ignore
int ret = system("./render_list -n 0");
ret = WEXITSTATUS(ret);
REQUIRE(ret == 1);
}

SECTION("render_list zoom exceeds maximum of MAX_ZOOM", "should return 1") {
std::string option = GENERATE("--max-zoom", "--min-zoom");
std::string command = "./render_list " + option + " 1000";

// flawfinder: ignore
int ret = system(command.c_str());
ret = WEXITSTATUS(ret);
REQUIRE(ret == 1);
}
}

TEST_CASE("render_old", "render old")
Expand Down
60 changes: 34 additions & 26 deletions src/render_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,31 @@ void display_rate(struct timeval start, struct timeval end, int num)
fflush(NULL);
}

int min_max_opt(char *opt_arg, char *opt_type_name, int minimum, int maximum)
{
int opt = atoi(opt_arg);
float opt_float;

if (minimum != -1 && opt < minimum) {
fprintf(stderr, "Invalid %s, must be >= %i (%s was provided)\n", opt_type_name, minimum, opt_arg);
exit(1);
}

if (maximum != -1 && opt > maximum) {
fprintf(stderr, "Invalid %s, must be <= %i (%s was provided)\n", opt_type_name, maximum, opt_arg);
exit(1);
}

if (sscanf(opt_arg, "%f", &opt_float) != 0) {
if ((float) opt != opt_float) {
fprintf(stderr, "Invalid %s, must be an integer (%s was provided)\n", opt_type_name, opt_arg);
exit(1);
}
}

return opt;
}

int main(int argc, char **argv)
{
char *spath = strdup(RENDERD_SOCKET);
Expand Down Expand Up @@ -141,53 +166,35 @@ int main(int argc, char **argv)
break;

case 'l': /* -l, --max-load */
maxLoad = atoi(optarg);
maxLoad = min_max_opt(optarg, "maximum load", 0, -1);
break;

case 'n': /* -n, --num-threads */
numThreads = atoi(optarg);

if (numThreads <= 0) {
fprintf(stderr, "Invalid number of threads, must be at least 1\n");
return 1;
}

numThreads = min_max_opt(optarg, "number of threads", 1, -1);
break;

case 'x': /* -x, --min-x */
minX = atoi(optarg);
minX = min_max_opt(optarg, "minimum X tile coordinate", 0, -1);
break;

case 'X': /* -X, --max-x */
maxX = atoi(optarg);
maxX = min_max_opt(optarg, "maximum X tile coordinate", 0, -1);
break;

case 'y': /* -y, --min-y */
minY = atoi(optarg);
minY = min_max_opt(optarg, "minimum Y tile coordinate", 0, -1);
break;

case 'Y': /* -Y, --max-y */
maxY = atoi(optarg);
maxY = min_max_opt(optarg, "maximum Y tile coordinate", 0, -1);
break;

case 'z': /* -z, --min-zoom */
minZoom = atoi(optarg);

if (minZoom < 0 || minZoom > MAX_ZOOM) {
fprintf(stderr, "Invalid minimum zoom selected, must be between 0 and %d\n", MAX_ZOOM);
return 1;
}

minZoom = min_max_opt(optarg, "minimum zoom", 0, MAX_ZOOM);
break;

case 'Z': /* -Z, --max-zoom */
maxZoom = atoi(optarg);

if (maxZoom < 0 || maxZoom > MAX_ZOOM) {
fprintf(stderr, "Invalid maximum zoom selected, must be between 0 and %d\n", MAX_ZOOM);
return 1;
}

maxZoom = min_max_opt(optarg, "maximum zoom", 0, MAX_ZOOM);
break;

case 'f': /* -f, --force */
Expand All @@ -211,6 +218,7 @@ int main(int argc, char **argv)
fprintf(stderr, " -z, --min-zoom=ZOOM filter input to only render tiles greater or equal to this zoom level (default is 0)\n");
fprintf(stderr, "\n");
fprintf(stderr, "If you are using --all, you can restrict the tile range by adding these options:\n");
fprintf(stderr, " (please note that tile coordinates must be positive integers and are not latitude and longitude values)\n");
fprintf(stderr, " -X, --max-x=X maximum X tile coordinate\n");
fprintf(stderr, " -x, --min-x=X minimum X tile coordinate\n");
fprintf(stderr, " -Y, --max-y=Y maximum Y tile coordinate\n");
Expand Down

0 comments on commit 7811deb

Please sign in to comment.