//_______________________________________________ // COINCOMP // Program used to compare 2 lists of coins // Usage: coincomp // Lists most be in TAB delimited text format // 1st column is the country name in English // 2nd column is the KM/Y# // // Ver 1.00 // 2004-2006 By Enrique Waizel //_______________________________________________ #define STRICT #include #include #include #include #include #include #include #include #include #include #include #define VERSION 213 #define MAX_LIN_CFG 1000 //________________________________________________________________________ int main( int argc, char **argv ) { FILE *mifd; FILE *res; char resfile[50]; char buf[MAX_LIN_CFG]; struct col { char country[50]; char km[10]; } mycol[3000], his; char c3[40]; char c; int myi=0; int i=0; int j,k,l,m,n,o; int hisline = 0; char lastyhisc[100] = ""; char lastymyc[100] = ""; if (argc < 3) { printf("usage: coincomp \n"); getchar(); exit (1); } mifd = fopen(argv[1], "r"); if (mifd == NULL) { printf("Cannot open file %s\n",argv[1]); //getchar(); return (-1); } else { while (fgets(buf, MAX_LIN_CFG, mifd) != NULL) { n = strlen(buf); for (m=0; m= 'a') && (buf[m] <= 'z')) buf[m] -= ('a'-'A'); } mycol[i].country[0]=0; mycol[i].km[0]=0; c3[0]=0; j = 0; while(buf[j] != '\t') j++; strncpy(mycol[i].country, buf, j); mycol[i].country[j] = 0; j++; k=j; while(buf[j] != '\t') j++; strncpy(mycol[i].km, &buf[k], j-k); mycol[i].km[j-k] = 0; // avoid strict km comparision ; only main number l = 0; while ((mycol[i].km[l] >= '0') && (mycol[i].km[l] <= '9')) l++; mycol[i].km[l] = 0; printf("*%s* *%s*\n", mycol[i].country, mycol[i].km); i++; } //while fclose (mifd); myi = i; mifd = fopen(argv[2], "r"); if (mifd == NULL) { printf("Cannot Open file %s\n", argv[2]); //getchar(); return (-1); } else { strcpy(resfile, argv[2]); resfile[ strlen (resfile) - 4 ] = 0; strcat(resfile, argv[1]); res = fopen(resfile, "w"); while (fgets(buf, MAX_LIN_CFG, mifd) != NULL) { hisline++; n = strlen(buf); for (m=0; m= 'a') && (buf[m] <= 'z')) buf[m] -= ('a'-'A'); } his.country[0]=0; his.km[0]=0; c3[0]=0; j = 0; while(buf[j] != '\t') j++; strncpy(his.country, buf, j); his.country[j] = 0; j++; k=j; while(buf[j] != '\t') j++; strncpy(his.km, &buf[k], j-k); his.km[j-k] = 0; // avoid strict km comparision ; only main number l = 0; while ((his.km[l] >= '0') && (his.km[l] <= '9')) l++; his.km[l] = 0; //printf("*%s* *%s*\n", mycol[i].country, mycol[i].km); //remove spaces at the end o = strlen(his.country); while ((o > 0) & (his.country[o-1] == ' ')) { his.country[o-1] = 0; o--; } if ( strlen(his.country) > 1) // lets find a match { j=0; while ((strcmp(his.country, mycol[j].country) > 0) & (j < myi)) j++; if ( strcmp(his.country, mycol[j].country) == 0 ) { // found country //printf("%s match\n", his.country ); while ( ( strcmp(his.country, mycol[j].country) == 0 ) && ( strcmp(his.km, mycol[j].km) != 0 ) && ( j < myi ) ) j++; if (strcmp(his.km, mycol[j].km) != 0) fprintf(res,"%s", buf); // don't have that Km //else // fprintf(res,"YES %s", buf); } else // Can we assume this country as..... // Same as before? if ((strcmp(lastymyc, mycol[j].country) == 0) && (strcmp(lastyhisc, his.country) == 0)) { strcpy (his.country, mycol[j].country); } else { { printf("Want to assume %s as %s (y/n)\n",his.country, mycol[j].country ); c = getch(); if ((c == 'y') || (c == 'Y')) // Close match to country name { strcpy(lastymyc, mycol[j].country); strcpy(lastyhisc, his.country); strcpy (his.country, mycol[j].country); while ( ( strcmp(his.country, mycol[j].country) == 0 ) && ( strcmp(his.km, mycol[j].km) != 0 ) && ( j < myi ) ) j++; if (strcmp(his.km, mycol[j].km) != 0) fprintf(res,"%s", buf); // don't have that Km //else // fprintf(res,"YES %s", buf); } else { // backward search printf("Want to assume %s as %s (y/n)\n",his.country, mycol[j-1].country ); c = getch(); if ((c == 'y') || (c == 'Y')) // Close match to country name { strcpy(lastymyc, mycol[j].country); strcpy(lastyhisc, his.country); strcpy (his.country, mycol[j-1].country); j--; while ( ( strcmp(his.country, mycol[j].country) == 0 ) && ( strcmp(his.km, mycol[j].km) != 0 ) && ( j > 0 ) ) j--; if (strcmp(his.km, mycol[j].km) != 0) fprintf(res,"%s", buf); // don't have that Km //else // fprintf(res,"YES %s", buf); } else fprintf(res,"%s", buf); // don't even have that Country } } } } i++; } //while fclose (mifd); } //else fclose(res); return (1); } //else }