summaryrefslogtreecommitdiff
path: root/src/export/map_branches_v4.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/export/map_branches_v4.c')
-rw-r--r--src/export/map_branches_v4.c71
1 files changed, 71 insertions, 0 deletions
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 @@
1#define _WITH_GETLINE
2#define _GNU_SOURCE
3#include <stdlib.h>
4#include <stdint.h>
5#include <stdio.h>
6#include <string.h>
7#include <ctype.h>
8
9typedef struct {
10 long code;
11 char *name;
12} branchen_code;
13
14enum { MAX_CODES = 128 * 1024 };
15branchen_code g_codes[MAX_CODES];
16long g_code_count;
17
18static int find_code( const void *key, const void *bc)
19{
20 return (long)key - ((branchen_code*)bc)->code;
21}
22
23static int qsort_cmp( const void *a, const void *b )
24{
25 return ((branchen_code*)a)->code - ((branchen_code*)b)->code;
26}
27
28int main( int argc, char ** args )
29{
30 FILE * map_file;
31 char *end_p, *input = malloc(1024);
32 size_t input_length = 1024;
33 ssize_t ll;
34
35 if( argc != 2 ) { fprintf( stderr, "Syntax: %s <branchcodes> < <branches_files>\n", args[0] ); exit(111); }
36
37 map_file = fopen( args[1], "r" );
38 if (!map_file || !input) { fprintf( stderr, "Error allocating resources\n" ); exit( 111 ); }
39
40 /* Fill array with maps */
41 while ( (ll = getline( &input, &input_length, map_file ) ) >= 0 ) {
42 char * r = strchr(input, 10);
43 if (r) *r = 0;
44 g_codes[g_code_count].code = strtoul(input, &end_p, 10);
45 asprintf(&g_codes[g_code_count].name, "%s", end_p + 1) ;
46 // printf( "%ld: %s\n", g_codes[g_code_count].code, g_codes[g_code_count].name);
47 g_code_count++;
48 }
49
50 qsort(g_codes, g_code_count, sizeof(branchen_code), qsort_cmp );
51
52 /* Now scan lines from 09_Verweise for semicolon separated branchen codes */
53 while ( (ll = getline( &input, &input_length, stdin ) ) >= 0 ) {
54 char *codes = input;
55 branchen_code *bc;
56 int multiple;
57 for (multiple = 0;; ++multiple) {
58 long code = strtoul(codes, &end_p, 10);
59 if (codes == end_p) break;
60 bc = (branchen_code*)bsearch((void *)(uintptr_t)code, g_codes, g_code_count, sizeof(branchen_code), find_code);
61 if (bc) {
62 if (multiple) putchar(';');
63 printf("%s", bc->name);
64 }
65 if (*end_p != ';') break;
66 codes = end_p + 1;
67 }
68 putchar(10);
69 }
70 return 0;
71}