From a187241f4e4cf8a592e0a3cc0b61f949e6184a9e Mon Sep 17 00:00:00 2001 From: Dirk Engling Date: Wed, 30 Jan 2019 18:12:18 +0100 Subject: Add branch name mapper code for v3 --- src/export/map_branches.c | 71 --------------------------------------- src/export/map_branches_v3.c | 79 ++++++++++++++++++++++++++++++++++++++++++++ src/export/map_branches_v4.c | 71 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 150 insertions(+), 71 deletions(-) delete mode 100644 src/export/map_branches.c create mode 100644 src/export/map_branches_v3.c create mode 100644 src/export/map_branches_v4.c (limited to 'src') diff --git a/src/export/map_branches.c b/src/export/map_branches.c deleted file mode 100644 index 160945d..0000000 --- a/src/export/map_branches.c +++ /dev/null @@ -1,71 +0,0 @@ -#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); - 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; -} diff --git a/src/export/map_branches_v3.c b/src/export/map_branches_v3.c new file mode 100644 index 0000000..22d0036 --- /dev/null +++ b/src/export/map_branches_v3.c @@ -0,0 +1,79 @@ +#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; +} diff --git a/src/export/map_branches_v4.c b/src/export/map_branches_v4.c new file mode 100644 index 0000000..160945d --- /dev/null +++ b/src/export/map_branches_v4.c @@ -0,0 +1,71 @@ +#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); + 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; +} -- cgit v1.2.3