From 1ed87df0b05ea96092286dfe7ed05d5f2304c3dc Mon Sep 17 00:00:00 2001 From: Dirk Engling Date: Sun, 23 Feb 2014 05:43:36 +0100 Subject: Export entries into their files --- src/export/extract_version_1.c | 62 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 57 insertions(+), 5 deletions(-) (limited to 'src') 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 @@ #include #include #include +#include +#include + #include "mystdlib.h" static char xlat_table[] = { @@ -32,6 +35,13 @@ static char cp437_to_iso8859_1_table[] = { 0x2e, 0xb1, 0x2e, 0x2e, 0x2e, 0x2e, 0xf7, 0x2e, 0xb0, 0x2e, 0xb7, 0x2e, 0x2e, 0xb2, 0x2e, 0xa0 }; +static struct { + int outfiles[13]; + char * vorwahl; + char * ort; + char * zip; +} g_state; + void decode_7bit_string( uint8_t const *source, uint8_t *dest ) { uint16_t acc = 0, acc_bits = 0; @@ -45,6 +55,29 @@ again: } } +void split_to_files( uint8_t *entries, int num_entries ) +{ + int entry = 0, column; + char *e = (char*)entries; + + for( entry = 0; entry < num_entries; ++entry ) + { + /* mimic flags from telefonbuch v3 */ + write( g_state.outfiles[0], entry ? "02\n" : ( num_entries > 1 ? "01\n" : "00\n" ), 3 ); + + for( column = 0; column < 11; ++column ) + { + char * end = strchr( e, '\t' ); + if( end ) { + *end = '\n'; + write( g_state.outfiles[column+1], e, end - e + 1); + e = end + 1; + } else + dprintf( g_state.outfiles[column+1], "%s\n", e); + } + } +} + void act_on_record( uint8_t *file, int flag, uint8_t *page, uint16_t record_off ) { 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 } decode_7bit_string( record, out_dest ); - /* XXX TODO: Split record in 12 chunks each, mark continuation, if num_entries > 1 */ - puts( (char*)outbuf ); + split_to_files( outbuf, num_entries ); } /* Page is always 0x2000 */ @@ -107,16 +139,36 @@ void act_on_file( uint8_t *file, size_t len ) for(i=0; ort[i]; ++i ) ort_conv[i] = cp437_to_iso8859_1_table[((uint8_t*)ort)[i]]; ort_conv[i] = 0; /* 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 ); */ + g_state.ort = ort_conv; + g_state.zip = zip; + g_state.vorwahl = vorwahl; + for( page = 0; page < num_pages; ++page ) act_on_page( file, file + 0x800 + 0x2000 * page, page ); } int main( int args, char **argv ) { - MAP f = map_file( argv[1], 1 ); + char filename[1024]; + MAP f; + int i; + + for( i=0; i<12; ++i ) + { + sprintf( filename, "%02d_unknown", i+1 ); + g_state.outfiles[i] = open( filename, O_WRONLY | O_APPEND | O_CREAT, 0644 ); + } + + while( fgets( filename, sizeof(filename), stdin ) ) { + filename[strlen(filename)-1] = 0; /* fgets sucks */ + f = map_file( filename, 1 ); + + act_on_file( f->addr, f->size ); + unmap_file( &f ); + } - act_on_file( f->addr, f->size ); + for( i=0; i<13; ++i ) + close( g_state.outfiles[i] ); - unmap_file( &f ); return 0; } -- cgit v1.2.3