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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
| #include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <errno.h>
int getInt(char * const buff, const size_t N) {
int rc = -1;
if (fgets(buff, N, stdin)) {
long res = 0L;
char * endp = NULL;
size_t lg = strlen(buff);
buff[lg-1] = '\0';
res = strtol(buff, &endp, 10);
if (endp && *endp != '\0') {
fprintf(stderr, "Invalid number %s\n", endp);
}
else if (errno == ERANGE) {
fprintf(stderr, "Number isn't in the correct range\n");
}
else if (res > INT_MAX || res < INT_MIN) {
fprintf(stderr, "Number isn't in the correct range (INT_MIN < x < INT_MAX)\n");
}
else {
rc = res;
}
}
return rc;
}
void badLine(unsigned int num) {
fprintf(stderr, "%u\t: bad line !!\n", num);
}
int main(void)
{
FILE * f = NULL;
char buffStdin[10+1] = {0};
char buffLine[100+1] = { '\0' };
int x, y, found = 0;
unsigned int cnt = 0;
#ifdef WITH_SSCANF
int mapId, xx, yy;
#endif /* WITH_SSCANF*/
printf("Enter the X coordinate :\n");
x = getInt(buffStdin, sizeof(buffStdin)/sizeof(*buffStdin));
printf("Enter the Y coordinate :\n");
y = getInt(buffStdin, sizeof(buffStdin)/sizeof(*buffStdin));
f = fopen("data.txt", "r");
if (!f) {
fprintf(stderr, "Cannot open file data.txt\n");
exit(EXIT_FAILURE);
}
while (fgets(buffLine, sizeof(buffLine)/sizeof(*buffLine), f)) {
#ifdef WITH_SSCANF
++cnt;
rc = sscanf(buffLine, "./BD-ORTHO-%*d/%*d/%*d-%*d-%d/%*d-%d-%d-%*d.txt\n", &mapId, &xx, &yy);
if (rc == 3 && x == xx && y == yy) {
printf("OK, line %u matches: for x=%d and y=%d the map number is %d\n", cnt, x, y, mapId);
}
else if (rc != 3) {
badLine(cnt); continue;
}
#else
char const * lastSlash = strrchr(buffLine, '/');
++cnt;
if (lastSlash) {
int xFromFile, yFromFile;
char * dashOne, * dashTwo, * dashThree;
dashOne = strchr(lastSlash, '-');
if (dashOne) {
dashTwo = strchr(dashOne+1, '-');
if (dashTwo) {
dashThree = strchr(dashTwo+1, '-');
if (dashThree) {
char coordinateValue[4+1] = { '\0' };
strncpy(coordinateValue, dashOne+1, dashTwo-dashOne-1); coordinateValue[4] = '\0';
xFromFile = strtol(coordinateValue, NULL, 10);
strncpy(coordinateValue, dashTwo+1, dashThree-dashTwo-1); coordinateValue[4] = '\0';
yFromFile = strtol(coordinateValue, NULL, 10);
if (x == xFromFile && y == yFromFile) {
char * dashBeforeMapNum = NULL;
char mapNumStr[3+1] = { '\0' }; int mapNum = 0;
printf("OK, line %u matches, now looking for the map id.\n", cnt);
buffLine[lastSlash-buffLine] = '\0';
dashBeforeMapNum = strrchr(buffLine, '-');
if (dashBeforeMapNum) {
strncpy(mapNumStr, dashBeforeMapNum+1, 3);
mapNumStr[3] = '\0';
mapNum = strtol(mapNumStr, NULL, 10);
printf("OK, for x:%d and y:%d, the map number is %d\n", x, y, mapNum);
found = 1;
break;
}
else {
badLine(cnt); continue;
}
}
else {
continue;
}
}
else {
badLine(cnt); continue;
}
}
else {
badLine(cnt); continue;
}
}
else {
badLine(cnt); continue;
}
}
else {
badLine(cnt); continue;
}
#endif /* WITH_SSCANF*/
}
fclose(f);
if (!found) {
printf("Sorry, no matching entry found\n");
}
return 0;
} |
Partager