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
| #include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void get_ints(char* str) {
long int int01, int04, int07;
if (str != NULL) {
if ((str[0] == 'f') && (str[1] == ' ')) {
char * substr = strtok ((str + 2), " ");
if (substr != NULL) {
char c;
c = substr[0];
if ((c >= '0') && (c <= '9')) {
int01 = strtol(substr, NULL, 10);
if ((int01 != LONG_MAX) && (int01 != LONG_MIN)) {
substr = strtok (NULL, " ");
if (substr != NULL) {
c = substr[0];
if ((c >= '0') && (c <= '9')) {
int04 = strtol(substr, NULL, 10);
if ((int04 != LONG_MAX) && (int04 != LONG_MIN)) {
substr = strtok (NULL, " ");
if (substr != NULL) {
c = substr[0];
if ((c >= '0') && (c <= '9')) {
int07 = strtol(substr, NULL, 10);
if ((int07 != LONG_MAX) && (int07 != LONG_MIN)) {
substr = strtok (NULL, " ");
printf("get_ints :\n*) int01: %lu\n*) int04: %lu\n*) int07: %lu\n\n", int01, int04, int07);
} else {
int01 = int04 = int07 = LONG_MAX;
printf("get_ints (error) - int07 conversion failed\n\n");
}
} else {
int01 = int04 = int07 = LONG_MAX;
printf("get_ints (error) - int07 is not a number\n\n");
}
} else {
int01 = int04 = int07 = LONG_MAX;
printf("get_ints (error) - int07 substr failed\n\n");
}
} else {
int01 = int04 = int07 = LONG_MAX;
printf("get_ints (error) - int04 conversion failed\n\n");
}
} else {
int01 = int04 = int07 = LONG_MAX;
printf("get_ints (error) - int04 is not a number\n\n");
}
} else {
int01 = int04 = int07 = LONG_MAX;
printf("get_ints (error) - int04 substr failed\n\n");
}
} else {
int01 = int04 = int07 = LONG_MAX;
printf("get_ints (error) - int01 conversion failed\n\n");
}
} else {
int01 = int04 = int07 = LONG_MAX;
printf("get_ints (error) - int01 is not a number\n\n");
}
} else {
int01 = int04 = int07 = LONG_MAX;
printf("get_ints (error) - int01 substr failed (initialization)\n\n");
}
} else {
int01 = int04 = int07 = LONG_MAX;
printf("get_ints (error) - str doesn't start with f\n\n");
}
} else {
int01 = int04 = int07 = LONG_MAX;
printf("get_ints (error) - empty parameter\n\n");
}
}
/*****************************************************************************/
/*********************************** Main **********************************/
/*****************************************************************************/
int main(int argc, char** argv)
{
char example01[] = "f 1\\\\3 7 9\\0";
char example02[] = "f 1 1 10\\\\11";
char example03[] = "f 1\\8\\9 3\\\\5 7";
char error01[] = "a 1\\8\\9 3\\\\5 7";
char error02[] = "1\\8\\9 3\\\\5 7";
char error03[] = "f 1 1 \\\\11";
char error04[] = "f 1 9\\\\11";
char error05[] = "f 1 a 9";
get_ints(example01);
get_ints(example02);
get_ints(example03);
get_ints(NULL); // empty parameter
get_ints(error01); // str doesn't start with f
get_ints(error02); // str doesn't start with f
get_ints(error03); // int07 is not a number
get_ints(error04); // int07 substr failed
get_ints(error05); // int04 is not a number
return EXIT_SUCCESS;
} |
Partager