From 1f15b520ad6af093d196f624e0078d9a118ff02f Mon Sep 17 00:00:00 2001 From: Dirk Engling Date: Tue, 22 Jan 2019 22:51:11 +0100 Subject: Add code that looks up Branchen names by codes --- src/export/map_branches.c | 67 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 src/export/map_branches.c (limited to 'src') diff --git a/src/export/map_branches.c b/src/export/map_branches.c new file mode 100644 index 0000000..16bf923 --- /dev/null +++ b/src/export/map_branches.c @@ -0,0 +1,67 @@ +#define _WITH_GETLINE +#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); + 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; + for (int 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); + } +} -- cgit v1.2.3