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
| #include <stdio.h>
#include <stdlib.h>
#include <string.h>
int parse(const char *filename, char *png, double width, double height, double altitude_max, double tolerance){
FILE *f;
char buf[BUFSIZ];
char *endptr1,*endptr2, *endptr3, *endptr4, *s;
double val1, val2, val3, val4;
f = fopen(filename,"r");
if(f == NULL)
{
fprintf(stderr,"erreur de lecture du fichier %s\n",filename);
return 1;
}
int tour = 1;
while(fgets(buf, BUFSIZ, f) != NULL)
{
switch(tour)
{
case 1 :
png = (char*)malloc((strlen(buf)+1)*sizeof(char));
if(png == NULL)
{
fprintf(stderr,"erreur d'allocation memoire\n");
return 2;
}
strcpy(png,buf);
break;
case 2 :
val1 = strtod(buf,&endptr1);
if(*endptr1 != '\0')
{
fprintf(stderr,"nombre incorrect\n");
return 2;
}
width = val1;
*s = *endptr1;
s++;
while(*s == ' ')
*s++;
val2 = strtod(buf,&endptr2);
if(*endptr2 != '\0')
{
fprintf(stderr,"nombre incorrect\n");
return 3;
}
height = val2;
break;
case 3:
val3 = strtod(buf, &endptr3);
if(*endptr3 != '\0')
{
fprintf(stderr,"nombre incorrect\n");
return 7;
}
altitude_max = val3;
break;
case 4:
val4 = strtod(buf, &endptr4);
if(*endptr4 != '\0')
{
fprintf(stderr,"nombre incorrect\n");
return 7;
}
tolerance = val4;
break;
}
tour++;
}
return 0;
}
int main(int argc, char *argv[])
{
char *png;
double width, height, altitude_max, tolerance;
if(parse(argv[0], png, width, height, altitude_max, tolerance) != 0)
{
fprintf(stderr,"erreur\n");
exit(2);
}
printf("image : %s\nlargeur : %lf\nhauteur : %lf\naltitude_max : %lf\ntolerance : %lf\n", png,width, height, altitude_max, tolerance);
return 0;
} |
Partager