summaryrefslogtreecommitdiff
path: root/src/splitfiles.c
blob: d49971b7e3671619870a4d68e17dc825fa16f0d6 (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "mystdlib.h"

#define NUMFIELDS 12
#define PREFIX "teiln/"

#define STREETFIELD 3
#define HOUSENUMFIELD 4
#define ZIPCODEFIELD 6
#define TOWNFIELD 7

char **generate_index( MAP toindex )
{
  off_t  fileidx = 0, idxidx = 0;
  char **index;

  while( fileidx < toindex->size )
    if( !toindex->addr[ fileidx++ ] ) idxidx++;
  index = (char**)malloc( sizeof( char *) * idxidx );

  fileidx = idxidx = 0;
  if( index )
  {
    while( fileidx < toindex->size )
    {
      index[ idxidx++ ] = toindex->addr + fileidx;
      while( (fileidx < toindex->size) && (toindex->addr[ fileidx++]));
    }
  }
  return index;
}

static int g_coordidxsize;

int coordcmp( const void *s1, const void *s2 )
{
  return strncmp( s1, s2, g_coordidxsize );
}

int main( )
{
  int i = 0, j, k, leave = 0;
  off_t in1, in2, in3;
  char fname[32], **streetnames = NULL;
  MAP streets = NULL, vnames, nnames, tables, coords = NULL;

  if( !(coords = map_file( "coords/list", true ))) goto cleanup_main;
  if( !(streets = map_file( "streets/list",true ))) goto cleanup_main;
  if( !(streetnames = generate_index( streets ))) goto cleanup_main;

  for( i=0; !leave; i+=3 )
  {
    int  fof[ NUMFIELDS ];
    char coordidx[160], *coordline;
    vnames = nnames = tables = NULL;

    snprintf( fname, sizeof( fname ), PREFIX "%05d", i );
    if( !( tables = map_file( fname, true ) ) ) goto cleanup_loop;
    snprintf( fname, sizeof( fname ), PREFIX "%05d", i+1 );
    if( !( nnames = map_file( fname, true ) ) ) goto cleanup_loop;
    snprintf( fname, sizeof( fname ), PREFIX "%05d", i+2 );
    if( !( vnames = map_file( fname, true ) ) ) goto cleanup_loop;

    in1 = 0x34; in2 = 0x34; in3 = 0;

    for( j = 0; j < NUMFIELDS; ++j)
    {
      fof[ j ] = in1;
      for( k = 0; k < 3000; ++k )
        while( tables->addr[ in1++ ]);
    }

    for( j = 0; j < 3000; ++j )
    {
      char *stringend;
      int v = strtoul( tables->addr + fof[ STREETFIELD ], &stringend, 16 );
      char *currentstreet = ( stringend == (char*)(tables->addr + fof[ STREETFIELD ])) ? "" : streetnames[ v ];
      int xco= 0, yco= 0;

      printf( "%s\t%s", nnames->addr+in2, vnames->addr+in3 );
      while( nnames->addr[ in2++ ] );
      while( vnames->addr[ in3++ ] );

      g_coordidxsize = snprintf( coordidx, sizeof( coordidx ), "%s;%s;%s;%s;",
        tables->addr + fof [ ZIPCODEFIELD ],
        tables->addr + fof [ TOWNFIELD ],
        currentstreet,
        tables->addr + fof [ HOUSENUMFIELD ] );

      if( !(coordline = (char*)bsearch( coordidx, coords->addr, coords->size / 90, 90, coordcmp ))) coordline = ";;;;;;";
      v = 5; while( v--) { while( *coordline && *coordline++ != ';'); }
      sscanf( coordline, "%d;%d", &xco, &yco );

      for( k = 0; k < NUMFIELDS; ++k )
      {
        if( k == STREETFIELD )
        {
          printf( "\t%s", currentstreet);
        }
        else
          printf( "\t%s", tables->addr + fof[ k ] );
        {
        }
        while(          tables->addr [ fof[ k ]++] );
      }

      printf( "\t%d\t%d", xco, yco );
      putchar( 10 );
    }

    leave ^= 1;

    cleanup_loop:
    leave ^= 1;

    unmap_file( &vnames ); 
    unmap_file( &nnames ); 
    unmap_file( &tables ); 
  }

  cleanup_main:
  if( streetnames ) free( streetnames );
  unmap_file( &streets );
  unmap_file( &coords );

  return 0;
}