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