#define _WITH_GETLINE #define _GNU_SOURCE #include #include #include #include #include typedef struct { long code; char *name; } branchen_code; enum { MAX_CODES = 128 * 1024 }; branchen_code g_codes[MAX_CODES]; long g_code_count; static int find_code( const void *key, const void *bc) { return (long)key - ((branchen_code*)bc)->code; } static int qsort_cmp( const void *a, const void *b ) { return ((branchen_code*)a)->code - ((branchen_code*)b)->code; } int main( int argc, char ** args ) { FILE * map_file; char *end_p, *input = malloc(1024); size_t input_length = 1024; ssize_t ll; if( argc != 2 ) { fprintf( stderr, "Syntax: %s < \n", args[0] ); exit(111); } map_file = fopen( args[1], "r" ); if (!map_file || !input) { fprintf( stderr, "Error allocating resources\n" ); exit( 111 ); } /* Fill array with maps */ while ( (ll = getline( &input, &input_length, map_file ) ) >= 0 ) { char * r = strchr(input, 10); if (r) *r = 0; g_codes[g_code_count].code = strtoul(input, &end_p, 10); if (input == end_p) break; if (*end_p != ';') { fprintf( stderr, "Input error, line: %s\n", input); exit(1); } r = strchr(end_p + 1, ';'); if (!r) { fprintf( stderr, "Input error, line: %s\n", input); exit(1); } *r = 0; asprintf(&g_codes[g_code_count].name, "%s", end_p + 1) ; // printf( "%ld: %s\n", g_codes[g_code_count].code, g_codes[g_code_count].name); g_code_count++; } qsort(g_codes, g_code_count, sizeof(branchen_code), qsort_cmp ); /* Now scan lines from 09_Verweise for semicolon separated branchen codes */ while ( (ll = getline( &input, &input_length, stdin ) ) >= 0 ) { char *codes = input; branchen_code *bc; int multiple; for (multiple = 0;; ++multiple) { long code = strtoul(codes, &end_p, 10); if (codes == end_p) break; bc = (branchen_code*)bsearch((void *)(uintptr_t)code, g_codes, g_code_count, sizeof(branchen_code), find_code); if (bc) { if (multiple) putchar(';'); printf("%s", bc->name); } if (*end_p != ';') break; codes = end_p + 1; } putchar(10); } return 0; }