summaryrefslogtreecommitdiff
path: root/src/postprocess/sort_plz.c
blob: 022e90a5736a7c2d935329406aee04e27b52e86b (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
  target is in current directory:
    entries_single/<PLZ> entries_multi/<PLZ>
    <PLZ> is either [0-9_]{5} or _____ or brken
  opens files in source directory:
    01_Flags 02_Nachname 03_Vorname 04_Zusaetze 07_Strasse 08_Hausnummer 09_Verweise 10_Postleitzahl
    11_Ort 12_Vorwahl 13_Rufnummer 14_15_Email_Webadresse 16_Koordinaten
  appends to all of the above dirs plus
    00_Jahr
*/

#include <sys/stat.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <errno.h>
#include <unistd.h>
#include <err.h>

enum { F_00, F_01, F_10, F_02, F_03, F_04, F_07, F_08, F_09, F_11, F_12, F_13, F_14, F_15, F_16, F_COUNT };

static char *g_filenames[] = {
 "00_Jahr", "01_Flags", "10_Postleitzahl", "02_Nachname", "03_Vorname", "04_Zusaetze", "07_Strasse", "08_Hausnummer", "09_Verweise", "11_Ort", "12_Vorwahl", "13_Rufnummer", "14_Webadresse", "15_Email", "16_Koordinaten" };

typedef struct {
  char plz[8];
  FILE * file;
} outhandle;

static outhandle g_outhandles[32*1024];
static int g_outhandle_count;

FILE * fopen_prefix(char *prefix, int file_id, int readonly) {
  char filename[1024];
  snprintf( filename, sizeof(filename), "%s/%s", prefix, g_filenames[file_id]);
  return fopen(filename, readonly ? "r" : "a");
}

/* This function gives us a binary search that returns a pointer, even if
   no exact match is found. In that case it sets exactmatch 0 and gives
   calling functions the chance to insert data
*/
void *binary_search( const void * const key, const void * base, const size_t member_count, const size_t member_size,
                     size_t compare_size, int *exactmatch ) {
  size_t interval = member_count;

  while( interval ) {
    uint8_t *lookat = ((uint8_t*)base) + member_size * ( interval / 2 );
    int cmp = memcmp( lookat, key, compare_size );
    if(cmp == 0 ) {
      base = lookat;
      break;
    }
    if(cmp < 0) {
      base = lookat + member_size;
      interval--;
    }
    interval /= 2;
  }

  *exactmatch = interval;
  return (void*)base;
}

FILE * get_file_for_postleitzahl(char *plz) {
  int exactmatch = 0;
  outhandle * oh = (outhandle *)binary_search(plz, g_outhandles, g_outhandle_count, sizeof(outhandle), 5, &exactmatch);
  if (!exactmatch) {
    size_t s = (g_outhandles + g_outhandle_count) - oh;
    memmove(oh + 1, oh, s * sizeof(outhandle));
    oh->file = fopen(plz, "a");
    if (!oh->file) errx( 1, "Couldn't open file %s for writing\n", plz);
    memcpy(oh->plz, plz, 5);
    g_outhandle_count++;
  }
  return oh->file;
}

int main(int argc, char **args) {
  FILE * in_handles[F_COUNT] = { NULL };
  FILE * out_handle = NULL;
  char flags[4], outfile[6];
  int i, in_multi = 0;
  char *input = malloc(1024);
  size_t input_size = 1024;

  if (argc != 1) exit(1);

  /* First open all input files */
  for (i=F_01; i<F_COUNT; ++i) {
    in_handles[i] = fopen_prefix(args[1], i, 1);
    if (!in_handles[i])
      errx( 1, "Couldn't open file %s\n", g_filenames[i]);
  }

  mkdir("output", 0755);
  chdir("output");

  /* Get Flags to check if we're processing a continuation */
  while (fgets(flags, 4, in_handles[F_01])) {
    ssize_t linelen;
    char flag = strtoul(flags, 0, 16);

    /* If we're in multiline mode, we just copy lines as long as we see continuations */
    if (in_multi) {
      if (flag & 0x2) {
        fputs(args[1], out_handle);      // write Jahr
        fputc(10, out_handle);
        fwrite(flags, 3, 1, out_handle); // copy Flags verbatim
        for (i=F_10; i<F_COUNT; ++i) {          // process the rest of entries
          ssize_t linelen = getline(&input, &input_size, in_handles[i]);
          fwrite(input, linelen, 1, out_handle);
        }
        continue;
      }
      in_multi = 0;
    }

    if (flag & 0x1)
      in_multi = 1;

    /* Read Postleitzahl to get destination */
    linelen = getline(&input, &input_size, in_handles[F_10]);
    if (linelen && input[linelen - 1] == 10) { // chomp
      input[linelen - 1] = 0;
      --linelen;
    }

    if (linelen == 0) // empty PLZ
      strcpy(outfile, "_____");
    else if (linelen == 5) { // potentially normal
      int broken = 0;
      for (i=0; i<5; ++i) {
        if ( (input[i] < '0' || input[i] > '9') && input[i] != '.') {
          broken = 1;
          break;
        }
        outfile[i] = input[i];
        if (outfile[i] == '.') outfile[i] = '_';
      }
      outfile[5] = 0;
      if (broken)
        strcpy(outfile, "brken");
    } else
      strcpy(outfile, "brken");

    out_handle = get_file_for_postleitzahl(outfile);

    fputs(args[1], out_handle);            // write Jahr
    fputc(10, out_handle);
    fwrite(flags, 3, 1, out_handle);       // copy Flags verbatim
    fputs(input, out_handle); // copy Postleitzahl verbatim
    fputc(10, out_handle);

    for (i=F_02; i<F_COUNT; ++i) {                // process the rest of entries
      ssize_t linelen = getline(&input, &input_size, in_handles[i]);
      fwrite(input, linelen, 1, out_handle);
    }
  }

  for (i=0; i<g_outhandle_count; ++i)
    fclose(g_outhandles[i].file);

  return 0;
}