From 215346b166e6cedf94dfd315f3c9745b64cd668f Mon Sep 17 00:00:00 2001 From: User Erdgeist Date: Mon, 3 Feb 2014 05:57:22 +0000 Subject: Lots of debugging. Now it works for the new format --- src/mapcoords.c | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/mapcoords.c b/src/mapcoords.c index b7d19f6..b46f1cf 100644 --- a/src/mapcoords.c +++ b/src/mapcoords.c @@ -1,12 +1,14 @@ +#define _WITH_GETLINE #include "mystdlib.h" #include #include #include +#include int find_offset( const void *key, const void *line ) { - size_t l = strlen( *(char**)key ); - return strncmp( *(char**)key, *(char**)line, l ); + size_t l = strlen( (char*)key ); + return strncmp( (char*)key, *(char**)line, l ); } int qsort_cmp( const void *a, const void *b ) @@ -19,31 +21,41 @@ int main( int argc, char ** args ) MAP coords = map_file( args[1], 1 ); int i, l, lines; char *p, **offsets, *input = malloc(1024); + ssize_t ll; size_t input_length = 1024; if( !coords ) exit( 111 ); p = (char *)coords->addr; for ( i=0, lines=0; isize; ++i ) - if( p[i] == 0x0a ) - { + if( p[i] == 0x00 ) ++lines; - p[i] = 0; - } offsets = malloc( lines * sizeof(char*)); if( !offsets ) exit( 111 ); offsets[0] = p; l = 1; - for ( i=0, lines=0; isize; ++i ) - if( !p[i] ) + for ( i=0; isize; ++i ) + if( p[i] == 0x00 ) offsets[l++] = p+i+1; l--; qsort(offsets, l, sizeof(char*), qsort_cmp ); - while( getline( &input, &input_length, stdin ) >= 0 ) + while( ( ll = getline( &input, &input_length, stdin ) ) >= 0 ) { - char **coord_line = bsearch( input, offsets, l, sizeof(char*), find_offset ); - printf( "%s\n", *coord_line + strlen( input ) ); + char **coord_line; + input[ll-1]='\t'; + coord_line = bsearch( input, offsets, l, sizeof(char*), find_offset ); + if( !coord_line && ll > 2 && isalpha( input[ll-2] ) ) + { + input[ll-2] = '\t'; input[ll-1]=0; + ll--; + coord_line = bsearch( input, offsets, l, sizeof(char*), find_offset ); + } + + if( coord_line ) + printf( "%s\n", *coord_line + ll ); + else + puts( "\t" ); } return 0; -- cgit v1.2.3