Skip to content

Commit f2d019f

Browse files
committed
Perform mode-specific argument validations
Since they vary so much between modes, it's hard to abstract them into a shared function (that is, without duplicating even more mode-checking logic).
1 parent c0e300b commit f2d019f

6 files changed

Lines changed: 98 additions & 2 deletions

File tree

src/generate_ascii.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@
2323
#include "include/image.h"
2424
#include "include/args.h"
2525
#include "include/read_file.h"
26+
#include "include/util.h"
27+
28+
static bool validate_args(const Args* args) {
29+
if (args->block_size != ARGS_DEFAULT_BLOCK_SIZE)
30+
WRN("The current mode (%s) is not affected by the user-specified block "
31+
"size (%zu).",
32+
args_get_mode_name(args->mode),
33+
args->block_size);
34+
return true;
35+
}
2636

2737
static inline Image* alloc_and_init_image(const Args* args, ByteArray* bytes) {
2838
Image* image = malloc(sizeof(Image));
@@ -43,6 +53,9 @@ static inline Image* alloc_and_init_image(const Args* args, ByteArray* bytes) {
4353
/*----------------------------------------------------------------------------*/
4454

4555
Image* generate_ascii(const Args* args, ByteArray* bytes) {
56+
if (!validate_args(args))
57+
return NULL;
58+
4659
Image* image = alloc_and_init_image(args, bytes);
4760
if (image == NULL)
4861
return NULL;

src/generate_bigrams.c

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,24 @@
2525
#include "include/read_file.h"
2626
#include "include/util.h"
2727

28+
static bool validate_args(const Args* args) {
29+
if (args->block_size != ARGS_DEFAULT_BLOCK_SIZE)
30+
WRN("The current mode (%s) is not affected by the user-specified block "
31+
"size (%zu).",
32+
args_get_mode_name(args->mode),
33+
args->block_size);
34+
if (args->output_width != ARGS_DEFAULT_OUTPUT_WIDTH)
35+
WRN("The user-specified output width (%d) will be overwritten by the "
36+
"current mode (%s).",
37+
args->output_width,
38+
args_get_mode_name(args->mode));
39+
if (args->transform_squares_side > 1)
40+
WRN("The \"squares\" transformation is not recommended for the current "
41+
"mode (%s).",
42+
args_get_mode_name(args->mode));
43+
return true;
44+
}
45+
2846
static inline Image* alloc_and_init_image(void) {
2947
Image* image = malloc(sizeof(Image));
3048
if (image == NULL)
@@ -41,7 +59,8 @@ static inline Image* alloc_and_init_image(void) {
4159
/*----------------------------------------------------------------------------*/
4260

4361
Image* generate_bigrams(const Args* args, ByteArray* bytes) {
44-
UNUSED(args);
62+
if (!validate_args(args))
63+
return NULL;
4564

4665
Image* image = alloc_and_init_image();
4766
if (image == NULL)

src/generate_dotplot.c

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,24 @@
2525
#include "include/read_file.h"
2626
#include "include/util.h"
2727

28+
static bool validate_args(const Args* args) {
29+
if (args->block_size != ARGS_DEFAULT_BLOCK_SIZE)
30+
WRN("The current mode (%s) is not affected by the user-specified block "
31+
"size (%zu).",
32+
args_get_mode_name(args->mode),
33+
args->block_size);
34+
if (args->output_width != ARGS_DEFAULT_OUTPUT_WIDTH)
35+
WRN("The user-specified output width (%d) will be overwritten by the "
36+
"current mode (%s).",
37+
args->output_width,
38+
args_get_mode_name(args->mode));
39+
if (args->transform_squares_side > 1)
40+
WRN("The \"squares\" transformation is not recommended for the current "
41+
"mode (%s).",
42+
args_get_mode_name(args->mode));
43+
return true;
44+
}
45+
2846
static inline Image* alloc_and_init_image(ByteArray* bytes) {
2947
Image* image = malloc(sizeof(Image));
3048
if (image == NULL)
@@ -41,7 +59,8 @@ static inline Image* alloc_and_init_image(ByteArray* bytes) {
4159
/*----------------------------------------------------------------------------*/
4260

4361
Image* generate_dotplot(const Args* args, ByteArray* bytes) {
44-
UNUSED(args);
62+
if (!validate_args(args))
63+
return NULL;
4564

4665
Image* image = alloc_and_init_image(bytes);
4766
if (image == NULL)

src/generate_entropy.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,18 @@
2525
#include "include/image.h"
2626
#include "include/args.h"
2727
#include "include/read_file.h"
28+
#include "include/util.h"
29+
30+
static bool validate_args(const Args* args) {
31+
if (args->block_size <= 1) {
32+
ERR("The current block size (%zu) is too small for the current mode "
33+
"(%s).",
34+
args->block_size,
35+
args_get_mode_name(args->mode));
36+
return false;
37+
}
38+
return true;
39+
}
2840

2941
static inline Image* alloc_and_init_image(const Args* args, ByteArray* bytes) {
3042
Image* image = malloc(sizeof(Image));
@@ -77,6 +89,9 @@ static double entropy(void* data, size_t data_sz) {
7789
/*----------------------------------------------------------------------------*/
7890

7991
Image* generate_entropy(const Args* args, ByteArray* bytes) {
92+
if (!validate_args(args))
93+
return NULL;
94+
8095
Image* image = alloc_and_init_image(args, bytes);
8196
if (image == NULL)
8297
return NULL;

src/generate_grayscale.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@
2323
#include "include/image.h"
2424
#include "include/args.h"
2525
#include "include/read_file.h"
26+
#include "include/util.h"
27+
28+
static bool validate_args(const Args* args) {
29+
if (args->block_size != ARGS_DEFAULT_BLOCK_SIZE)
30+
WRN("The current mode (%s) is not affected by the user-specified block "
31+
"size (%zu).",
32+
args_get_mode_name(args->mode),
33+
args->block_size);
34+
return true;
35+
}
2636

2737
static inline Image* alloc_and_init_image(const Args* args, ByteArray* bytes) {
2838
Image* image = malloc(sizeof(Image));
@@ -43,6 +53,9 @@ static inline Image* alloc_and_init_image(const Args* args, ByteArray* bytes) {
4353
/*----------------------------------------------------------------------------*/
4454

4555
Image* generate_grayscale(const Args* args, ByteArray* bytes) {
56+
if (!validate_args(args))
57+
return NULL;
58+
4659
Image* image = alloc_and_init_image(args, bytes);
4760
if (image == NULL)
4861
return NULL;

src/generate_histogram.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,20 @@
2323
#include "include/image.h"
2424
#include "include/args.h"
2525
#include "include/read_file.h"
26+
#include "include/util.h"
27+
28+
static bool validate_args(const Args* args) {
29+
if (args->block_size != ARGS_DEFAULT_BLOCK_SIZE)
30+
WRN("The current mode (%s) is not affected by the user-specified block "
31+
"size (%zu).",
32+
args_get_mode_name(args->mode),
33+
args->block_size);
34+
if (args->transform_squares_side > 1)
35+
WRN("The \"squares\" transformation is not recommended for the current "
36+
"mode (%s).",
37+
args_get_mode_name(args->mode));
38+
return true;
39+
}
2640

2741
static inline Image* alloc_and_init_image(const Args* args) {
2842
Image* image = malloc(sizeof(Image));
@@ -40,6 +54,9 @@ static inline Image* alloc_and_init_image(const Args* args) {
4054
/*----------------------------------------------------------------------------*/
4155

4256
Image* generate_histogram(const Args* args, ByteArray* bytes) {
57+
if (!validate_args(args))
58+
return NULL;
59+
4360
Image* image = alloc_and_init_image(args);
4461
if (image == NULL)
4562
return NULL;

0 commit comments

Comments
 (0)