fixed: more bugfixed and constraints for inputs while parsing

This commit is contained in:
Cem
2026-07-08 00:52:49 +02:00
parent ecc4e267e7
commit ee3b02db2d
+26 -12
View File
@@ -201,7 +201,7 @@ int main(int argc, char** argv)
_Bool oGiven = false;
_Bool dGiven = false;
_Bool sGiven = false;
int amount_function_called = -1; // initial values, so that the compiler doesn't complain
size_t amount_function_called = 0; // initial values, so that the compiler doesn't complain
unsigned n = 0;
float starting_point_real = -1;
float starting_point_imaginary = -1;
@@ -217,14 +217,14 @@ int main(int argc, char** argv)
{
switch (opt_long)
{
case 'V': // done, function needed
case 'V': {// done, function needed
char *end;
version_to_use = strtol(optarg,&end,10);
if (version_to_use < 0 || version_to_use > 5 || end == optarg || *end != '\0')
{
fprintf(stderr,"Invalid version to use: V%d. Available versions are : V0,V1,V2,V3,V4,V5\n", version_to_use);
fprintf(stderr,"Invalid version to use: V%s. Available versions are : V0,V1,V2,V3,V4,V5\n", optarg);
return EXIT_FAILURE;
}
printf("version is %d\n", version_to_use);
@@ -234,18 +234,21 @@ int main(int argc, char** argv)
// -V 0 Ihre Hauptimplementierung verwendet werden. Wenn diese Option nicht
// gesetzt wird, soll ebenfalls die Hauptimplementierung ausgeführt werden
break;
}
case 'B': { // done, function needed
// wird die Laufzeit der angegebenen Implementierung gemessen und ausgegeben (do this later)
char *end;
amount_function_called = strtol(optarg,&end,10);
long value = strtol(optarg,&end,10);
if (amount_function_called <= 0 || end == optarg || *end != '\0') {
fprintf(stderr, "Invalid benchmarking input, the value should be greater than 0\n");
if (value <= 0 || end == optarg || *end != '\0') {
fprintf(stderr, "Invalid benchmarking input: %s\n", optarg);
return EXIT_FAILURE;
}
amount_function_called = (size_t)value;
will_benchmark = true;
printf("amount function called is %d\n", amount_function_called);
printf("amount function called is %zu\n", amount_function_called);
// call benchmarking function
// Das Argument dieser Option gibt die Anzahl an Wiederholungen des Funktionsaufrufs an, z. B.-B 5 .
@@ -282,7 +285,7 @@ int main(int argc, char** argv)
dGiven = true;
char* height_string;
char *end;
width = strtol(optarg,&height_string,10);
long widthValue = strtol(optarg,&height_string,10);
if (height_string == optarg || *height_string != ',')
{
@@ -290,13 +293,23 @@ int main(int argc, char** argv)
return EXIT_FAILURE;
}
height = strtol(height_string + 1,&end,10);
long heightValue = strtol(height_string + 1,&end,10);
if (end == height_string + 1 || *end != '\0')
{
fprintf(stderr, "Invalid height or extra characters: %s\n",optarg);
return EXIT_FAILURE;
}
if (widthValue <= 0 || heightValue == 0)
{
fprintf(stderr, "Width must be > 0 and height must not be 0: %s\n", optarg);
return EXIT_FAILURE;
}
width = (size_t)widthValue;
height = (ssize_t)heightValue;
printf("width %zu, height %zd\n", width,height);
// -d <Zahl>,<Zahl> —Breite und Höhe des Bildausschnitts
break;
@@ -305,15 +318,16 @@ int main(int argc, char** argv)
case 'n': {
nGiven = true;
char* end;
n = strtol(optarg,&end,10);
long nValue = strtol(optarg,&end,10);
if (end == optarg || *end != '\0' || n <= 0)
if (end == optarg || *end != '\0' || nValue <= 0)
{
fprintf(stderr, "Invalid n value: %s\n", optarg);
return EXIT_FAILURE;
}
printf("n:%d\n",n);
n = (unsigned) nValue;
printf("n:%u\n",n);
// -n <Zahl> —MaximaleAnzahlanIterationen pro Bildpixel
break;
}