summaryrefslogtreecommitdiff
path: root/src/backen.c
blob: b93c72147610bb90ddc2acec3c84947dc1b42313 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <string.h>
#include "mystdlib.h"

int mystrcmp( void *a, void *b ) {
  return strncmp( a, *(char**)b, strlen( a ));
}

int mystrcmp2( void *a, void *b ) {
  return strcmp( *(char**)a, *(char**)b);
}

int main( int argc, char **argv ) {
  char path[32]; /* files/file_XXXXX */
  int streetnum = 0, geomapnum = 0, geomahnum = 0, d = 0, i, j, k, run = 1;
  char *geomahs[15000000];
  char *streets[400000];
  MAP maps[11]; int mapoffs[11];
  MAP streetf = map_file( "lists/streetlist", 1);
  MAP geomahf = map_file( "lists/geohnrlist", 1);
  unsigned char lookup[1024], **geokord; /* PLZ, Streetname, Hnr */

  streets[streetnum++] = streetf->addr;
  for( i=0; i<streetf->size; ++i )
    if( streetf->addr[i] == 0 ) streets[streetnum++] = streetf->addr+i+1;

  geomahs[geomahnum++] = geomahf->addr;
  for( i=0; i<geomahf->size; ++i )
    if( geomahf->addr[i] == 0 ) geomahs[geomahnum++] = geomahf->addr+i+1;

  qsort( geomahs, geomahnum, sizeof(char*), mystrcmp2 );

  i = 0;
  while( run ) {
    for(j=0; j<11; ++j) {
      sprintf( path, "files/file_%05X", i++);
      maps[j] = map_file( path, 1 ); mapoffs[j] = 0;
    }

    if( !maps[0] ) goto out;

    for(j=0;j<maps[0]->size;++j) {
      char ch;

      for( k=0; k<11; ++k) {
        unsigned char *end;
        int sid,x ;
        switch(k) {
           case 0:
             printf( "%02X\t", maps[k]->addr[mapoffs[k]++]);
             break;
           case 5:
             sid = strtol( maps[k]->addr+mapoffs[k], &end, 10);
             if( end > maps[k]->addr+mapoffs[k] ) {
               // no hnr, end+1 points to "" afterwards
               if( !*end ) end--;

               printf( "%s\t%s\t", streets[sid], end+1 );
               end = lookup + 6 + sprintf( lookup+6, "%s\t%s", streets[sid], end + 1 );
               while( isalpha( (int)end[-1] )) *--end = 0;
               *end++ = '\t'; *end = 0;
             } else {
               printf( "\t\t" ); lookup[6] = lookup[7] = '\t'; lookup[8] = 0;
             }
             while( maps[k]->addr[mapoffs[k]++]);
             break;
           case 10:
             if( !maps[k]->addr[mapoffs[k]] )
               printf( "\t\t" );
             else if( !strchr(maps[k]->addr+mapoffs[k], 9))
               printf( "%s\t\t", maps[k]->addr+mapoffs[k] );
             else
               printf( "%s\t", maps[k]->addr+mapoffs[k] );
             while( maps[k]->addr[mapoffs[k]++]);
             break;
           case 7:
             for( x=0; (x<5) && (maps[k]->addr+mapoffs[k])[x]; ++x )
               lookup[x] = (maps[k]->addr+mapoffs[k])[x];
             for( ;     x<5; ++x )
               lookup[x] = '.';
             lookup[x] = '\t';
           default:
             while( maps[k]->addr[mapoffs[k]] == '\t' ) mapoffs[k]++;
             while( (ch = maps[k]->addr[mapoffs[k]++] ))
               if( ch < 32 )
                 putchar( 32 );
               else
                 putchar( ch );
             putchar( '\t' );
           break;
        }
      }
      /* postprocessing */

      if( (geokord = (char**)bsearch( lookup, geomahs, geomahnum, sizeof(char *), mystrcmp ) ) ) {
        printf( "%s", *geokord+strlen(lookup)+1 );
      } else {
        putchar('\t'); putchar('\t');
      }
      putchar( 10 );
    }
    for(j=0; j<11; ++j) unmap_file( &maps[j] );
  }

out:
  unmap_file( &streetf );
  unmap_file( &geomahf );
  return 0;
}