diff options
author | Dirk Engling <erdgeist@erdgeist.org> | 2014-02-23 05:43:36 +0100 |
---|---|---|
committer | Dirk Engling <erdgeist@erdgeist.org> | 2014-02-23 05:43:36 +0100 |
commit | 1ed87df0b05ea96092286dfe7ed05d5f2304c3dc (patch) | |
tree | 4cb98192246684e63740e4ac676d28105326910f /src | |
parent | 7663bfdd80c1ea8f4e65d72b2780e893ed0282b2 (diff) |
Export entries into their files
Diffstat (limited to 'src')
-rw-r--r-- | src/export/extract_version_1.c | 62 |
1 files changed, 57 insertions, 5 deletions
diff --git a/src/export/extract_version_1.c b/src/export/extract_version_1.c index a739116..d81abd3 100644 --- a/src/export/extract_version_1.c +++ b/src/export/extract_version_1.c | |||
@@ -2,6 +2,9 @@ | |||
2 | #include <stdint.h> | 2 | #include <stdint.h> |
3 | #include <stdio.h> | 3 | #include <stdio.h> |
4 | #include <string.h> | 4 | #include <string.h> |
5 | #include <unistd.h> | ||
6 | #include <fcntl.h> | ||
7 | |||
5 | #include "mystdlib.h" | 8 | #include "mystdlib.h" |
6 | 9 | ||
7 | static char xlat_table[] = { | 10 | static char xlat_table[] = { |
@@ -32,6 +35,13 @@ static char cp437_to_iso8859_1_table[] = { | |||
32 | 0x2e, 0xb1, 0x2e, 0x2e, 0x2e, 0x2e, 0xf7, 0x2e, 0xb0, 0x2e, 0xb7, 0x2e, 0x2e, 0xb2, 0x2e, 0xa0 | 35 | 0x2e, 0xb1, 0x2e, 0x2e, 0x2e, 0x2e, 0xf7, 0x2e, 0xb0, 0x2e, 0xb7, 0x2e, 0x2e, 0xb2, 0x2e, 0xa0 |
33 | }; | 36 | }; |
34 | 37 | ||
38 | static struct { | ||
39 | int outfiles[13]; | ||
40 | char * vorwahl; | ||
41 | char * ort; | ||
42 | char * zip; | ||
43 | } g_state; | ||
44 | |||
35 | void decode_7bit_string( uint8_t const *source, uint8_t *dest ) | 45 | void decode_7bit_string( uint8_t const *source, uint8_t *dest ) |
36 | { | 46 | { |
37 | uint16_t acc = 0, acc_bits = 0; | 47 | uint16_t acc = 0, acc_bits = 0; |
@@ -45,6 +55,29 @@ again: | |||
45 | } | 55 | } |
46 | } | 56 | } |
47 | 57 | ||
58 | void split_to_files( uint8_t *entries, int num_entries ) | ||
59 | { | ||
60 | int entry = 0, column; | ||
61 | char *e = (char*)entries; | ||
62 | |||
63 | for( entry = 0; entry < num_entries; ++entry ) | ||
64 | { | ||
65 | /* mimic flags from telefonbuch v3 */ | ||
66 | write( g_state.outfiles[0], entry ? "02\n" : ( num_entries > 1 ? "01\n" : "00\n" ), 3 ); | ||
67 | |||
68 | for( column = 0; column < 11; ++column ) | ||
69 | { | ||
70 | char * end = strchr( e, '\t' ); | ||
71 | if( end ) { | ||
72 | *end = '\n'; | ||
73 | write( g_state.outfiles[column+1], e, end - e + 1); | ||
74 | e = end + 1; | ||
75 | } else | ||
76 | dprintf( g_state.outfiles[column+1], "%s\n", e); | ||
77 | } | ||
78 | } | ||
79 | } | ||
80 | |||
48 | void act_on_record( uint8_t *file, int flag, uint8_t *page, uint16_t record_off ) | 81 | void act_on_record( uint8_t *file, int flag, uint8_t *page, uint16_t record_off ) |
49 | { | 82 | { |
50 | uint8_t outbuf[8192], *out_dest = outbuf, *record; | 83 | uint8_t outbuf[8192], *out_dest = outbuf, *record; |
@@ -75,8 +108,7 @@ void act_on_record( uint8_t *file, int flag, uint8_t *page, uint16_t record_off | |||
75 | } | 108 | } |
76 | 109 | ||
77 | decode_7bit_string( record, out_dest ); | 110 | decode_7bit_string( record, out_dest ); |
78 | /* XXX TODO: Split record in 12 chunks each, mark continuation, if num_entries > 1 */ | 111 | split_to_files( outbuf, num_entries ); |
79 | puts( (char*)outbuf ); | ||
80 | } | 112 | } |
81 | 113 | ||
82 | /* Page is always 0x2000 */ | 114 | /* Page is always 0x2000 */ |
@@ -107,16 +139,36 @@ void act_on_file( uint8_t *file, size_t len ) | |||
107 | for(i=0; ort[i]; ++i ) ort_conv[i] = cp437_to_iso8859_1_table[((uint8_t*)ort)[i]]; ort_conv[i] = 0; | 139 | for(i=0; ort[i]; ++i ) ort_conv[i] = cp437_to_iso8859_1_table[((uint8_t*)ort)[i]]; ort_conv[i] = 0; |
108 | /* printf( "Working on a %04d page and %06d records file, city: %4s %-32s with prefix %s\n", num_pages, num_records, zip, ort_conv, vorwahl ); */ | 140 | /* printf( "Working on a %04d page and %06d records file, city: %4s %-32s with prefix %s\n", num_pages, num_records, zip, ort_conv, vorwahl ); */ |
109 | 141 | ||
142 | g_state.ort = ort_conv; | ||
143 | g_state.zip = zip; | ||
144 | g_state.vorwahl = vorwahl; | ||
145 | |||
110 | for( page = 0; page < num_pages; ++page ) | 146 | for( page = 0; page < num_pages; ++page ) |
111 | act_on_page( file, file + 0x800 + 0x2000 * page, page ); | 147 | act_on_page( file, file + 0x800 + 0x2000 * page, page ); |
112 | } | 148 | } |
113 | 149 | ||
114 | int main( int args, char **argv ) | 150 | int main( int args, char **argv ) |
115 | { | 151 | { |
116 | MAP f = map_file( argv[1], 1 ); | 152 | char filename[1024]; |
153 | MAP f; | ||
154 | int i; | ||
155 | |||
156 | for( i=0; i<12; ++i ) | ||
157 | { | ||
158 | sprintf( filename, "%02d_unknown", i+1 ); | ||
159 | g_state.outfiles[i] = open( filename, O_WRONLY | O_APPEND | O_CREAT, 0644 ); | ||
160 | } | ||
161 | |||
162 | while( fgets( filename, sizeof(filename), stdin ) ) { | ||
163 | filename[strlen(filename)-1] = 0; /* fgets sucks */ | ||
164 | f = map_file( filename, 1 ); | ||
165 | |||
166 | act_on_file( f->addr, f->size ); | ||
167 | unmap_file( &f ); | ||
168 | } | ||
117 | 169 | ||
118 | act_on_file( f->addr, f->size ); | 170 | for( i=0; i<13; ++i ) |
171 | close( g_state.outfiles[i] ); | ||
119 | 172 | ||
120 | unmap_file( &f ); | ||
121 | return 0; | 173 | return 0; |
122 | } | 174 | } |