From 46f8ba5f4bed3a5c29a7416a10e332bb0ca0b3bc Mon Sep 17 00:00:00 2001 From: Dirk Engling Date: Sun, 2 Feb 2014 08:54:04 +0100 Subject: Add a coordinate file extractor --- src/Makefile | 9 ++++++--- src/mapcoords.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 3 deletions(-) create mode 100644 src/mapcoords.c (limited to 'src') diff --git a/src/Makefile b/src/Makefile index 8d1a66d..0ccb1f9 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,7 +1,10 @@ -all: decompress extractblocks +all: decompress extractblocks mapcoords decompress: decompress.c mystdlib.c - gcc -O2 -o ../bin/decompress decompress.c mystdlib.c -lz + $(CC) -O2 -o ../bin/decompress decompress.c mystdlib.c -lz extractblocks: extractblocks_new.c mystdlib.c - gcc -o ../bin/extractblocks extractblocks_new.c mystdlib.c + $(CC) -o ../bin/extractblocks extractblocks_new.c mystdlib.c + +mapcoords: mapcoords.c mystdlib.c + $(CC) -o ../bin/mapcoords mapcoords.c mystdlib.c diff --git a/src/mapcoords.c b/src/mapcoords.c new file mode 100644 index 0000000..b7d19f6 --- /dev/null +++ b/src/mapcoords.c @@ -0,0 +1,50 @@ +#include "mystdlib.h" +#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 ); +} + +int qsort_cmp( const void *a, const void *b ) +{ + return strcmp( *(char**)a, *(char**)b ); +} + +int main( int argc, char ** args ) +{ + MAP coords = map_file( args[1], 1 ); + int i, l, lines; + char *p, **offsets, *input = malloc(1024); + size_t input_length = 1024; + + if( !coords ) exit( 111 ); + p = (char *)coords->addr; + for ( i=0, lines=0; isize; ++i ) + if( p[i] == 0x0a ) + { + ++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] ) + offsets[l++] = p+i+1; + + l--; qsort(offsets, l, sizeof(char*), qsort_cmp ); + + while( getline( &input, &input_length, stdin ) >= 0 ) + { + char **coord_line = bsearch( input, offsets, l, sizeof(char*), find_offset ); + printf( "%s\n", *coord_line + strlen( input ) ); + } + + return 0; +} -- cgit v1.2.3