/* restore_imgbm.c < file2 > file3 Program to read NRESTORED imgbm points from ngdc_combined file which was restored from tape (file name is argument 2) into memory, and then read from standard input another imgbm file (ngdc_combined which has bad data in it) and write to standard output each point in memory corresponding to a point in the standard input. A quick and dirty fix for a problem with backup tapes of the ngdc data. WHFS and DTS, 24 Sep 1996 The parameter NRESTORED is defined below and must be set to the number of elements in the restored file, that is, number of bytes / 8. If you find that this file has to be "imgbm_combine"d, then you have to reset this value and recompile. This array size is hard wired with this parameter to save having to link with the memory allocation stuff. To compile: cc -g restore_imgbm.c -lm -o restore_imgbm */ #include #include #define NRESTORED 2550943 struct DATAPAIR { int index; int depth; } a[NRESTORED]; main(argc, argv) int argc; char **argv; { int i, last_b_index; FILE *fp; struct DATAPAIR b; if (argc != 2 || (fp = fopen(argv[1], "r")) == NULL) { fprintf(stderr,"usage: restore_imgbm < file_of_pixels_to_keep > output_file\n"); exit(-1); } /* Get here when file opened ok. Read and close it. */ if ( (fread((char*)a, sizeof(struct DATAPAIR), NRESTORED, fp)) != NRESTORED) { fprintf(stderr,"restore_imgbm: Error reading %d pairs from %s\n", NRESTORED, argv[1]); exit(-1); } fclose(fp); /* Just to be sure we are really dealing with sorted input, double check: */ for (i = 1; i < NRESTORED; i++) { if (a[i].index <= a[i-1].index) { fprintf(stderr,"restore_imgbm: Error. The input in %s is not sorted. Run imgbm_combine and recompile with new size parameter.\n"); exit(-1); } } /* Read and compare and write out while we can read from stdin. Use last_b_index to make sure that b input is also sorted: */ i = 0; last_b_index = -1; while ( (fread((char*)&b, sizeof(struct DATAPAIR), 1, stdin)) == 1 ) { if (b.index <= last_b_index) { fprintf(stderr,"restore_imgbm: Error. The standard input is not sorted. Run imgbm_combine.\n"); exit(-1); } last_b_index = b.index; /* If a's index is less than b's, increment the counter: */ while( (a[i].index < b.index) && (i < (NRESTORED-1) ) ) i++; /* If a's index now equals b's, write out a: */ if (a[i].index == b.index) fwrite((char *)&a[i], sizeof(struct DATAPAIR), 1, stdout); } exit(0); }