diff options
| author | Dirk Engling <erdgeist@erdgeist.org> | 2014-02-02 08:54:04 +0100 |
|---|---|---|
| committer | Dirk Engling <erdgeist@erdgeist.org> | 2014-02-02 08:54:04 +0100 |
| commit | 46f8ba5f4bed3a5c29a7416a10e332bb0ca0b3bc (patch) | |
| tree | 1219a29c3bcb937e7e828fb5101d2af831410ad2 /src | |
| parent | 8df5dd9620dfa1462b6973d393f6891b612ae256 (diff) | |
Add a coordinate file extractor
Diffstat (limited to 'src')
| -rw-r--r-- | src/Makefile | 9 | ||||
| -rw-r--r-- | src/mapcoords.c | 50 |
2 files changed, 56 insertions, 3 deletions
diff --git a/src/Makefile b/src/Makefile index 8d1a66d..0ccb1f9 100644 --- a/src/Makefile +++ b/src/Makefile | |||
| @@ -1,7 +1,10 @@ | |||
| 1 | all: decompress extractblocks | 1 | all: decompress extractblocks mapcoords |
| 2 | 2 | ||
| 3 | decompress: decompress.c mystdlib.c | 3 | decompress: decompress.c mystdlib.c |
| 4 | gcc -O2 -o ../bin/decompress decompress.c mystdlib.c -lz | 4 | $(CC) -O2 -o ../bin/decompress decompress.c mystdlib.c -lz |
| 5 | 5 | ||
| 6 | extractblocks: extractblocks_new.c mystdlib.c | 6 | extractblocks: extractblocks_new.c mystdlib.c |
| 7 | gcc -o ../bin/extractblocks extractblocks_new.c mystdlib.c | 7 | $(CC) -o ../bin/extractblocks extractblocks_new.c mystdlib.c |
| 8 | |||
| 9 | mapcoords: mapcoords.c mystdlib.c | ||
| 10 | $(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 @@ | |||
| 1 | #include "mystdlib.h" | ||
| 2 | #include <stdlib.h> | ||
| 3 | #include <stdio.h> | ||
| 4 | #include <string.h> | ||
| 5 | |||
| 6 | int find_offset( const void *key, const void *line ) | ||
| 7 | { | ||
| 8 | size_t l = strlen( *(char**)key ); | ||
| 9 | return strncmp( *(char**)key, *(char**)line, l ); | ||
| 10 | } | ||
| 11 | |||
| 12 | int qsort_cmp( const void *a, const void *b ) | ||
| 13 | { | ||
| 14 | return strcmp( *(char**)a, *(char**)b ); | ||
| 15 | } | ||
| 16 | |||
| 17 | int main( int argc, char ** args ) | ||
| 18 | { | ||
| 19 | MAP coords = map_file( args[1], 1 ); | ||
| 20 | int i, l, lines; | ||
| 21 | char *p, **offsets, *input = malloc(1024); | ||
| 22 | size_t input_length = 1024; | ||
| 23 | |||
| 24 | if( !coords ) exit( 111 ); | ||
| 25 | p = (char *)coords->addr; | ||
| 26 | for ( i=0, lines=0; i<coords->size; ++i ) | ||
| 27 | if( p[i] == 0x0a ) | ||
| 28 | { | ||
| 29 | ++lines; | ||
| 30 | p[i] = 0; | ||
| 31 | } | ||
| 32 | |||
| 33 | offsets = malloc( lines * sizeof(char*)); | ||
| 34 | if( !offsets ) exit( 111 ); | ||
| 35 | |||
| 36 | offsets[0] = p; l = 1; | ||
| 37 | for ( i=0, lines=0; i<coords->size; ++i ) | ||
| 38 | if( !p[i] ) | ||
| 39 | offsets[l++] = p+i+1; | ||
| 40 | |||
| 41 | l--; qsort(offsets, l, sizeof(char*), qsort_cmp ); | ||
| 42 | |||
| 43 | while( getline( &input, &input_length, stdin ) >= 0 ) | ||
| 44 | { | ||
| 45 | char **coord_line = bsearch( input, offsets, l, sizeof(char*), find_offset ); | ||
| 46 | printf( "%s\n", *coord_line + strlen( input ) ); | ||
| 47 | } | ||
| 48 | |||
| 49 | return 0; | ||
| 50 | } | ||
