diff options
Diffstat (limited to 'src/export/map_coords.c')
| -rw-r--r-- | src/export/map_coords.c | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/src/export/map_coords.c b/src/export/map_coords.c new file mode 100644 index 0000000..b46f1cf --- /dev/null +++ b/src/export/map_coords.c | |||
| @@ -0,0 +1,62 @@ | |||
| 1 | #define _WITH_GETLINE | ||
| 2 | #include "mystdlib.h" | ||
| 3 | #include <stdlib.h> | ||
| 4 | #include <stdio.h> | ||
| 5 | #include <string.h> | ||
| 6 | #include <ctype.h> | ||
| 7 | |||
| 8 | int find_offset( const void *key, const void *line ) | ||
| 9 | { | ||
| 10 | size_t l = strlen( (char*)key ); | ||
| 11 | return strncmp( (char*)key, *(char**)line, l ); | ||
| 12 | } | ||
| 13 | |||
| 14 | int qsort_cmp( const void *a, const void *b ) | ||
| 15 | { | ||
| 16 | return strcmp( *(char**)a, *(char**)b ); | ||
| 17 | } | ||
| 18 | |||
| 19 | int main( int argc, char ** args ) | ||
| 20 | { | ||
| 21 | MAP coords = map_file( args[1], 1 ); | ||
| 22 | int i, l, lines; | ||
| 23 | char *p, **offsets, *input = malloc(1024); | ||
| 24 | ssize_t ll; | ||
| 25 | size_t input_length = 1024; | ||
| 26 | |||
| 27 | if( !coords ) exit( 111 ); | ||
| 28 | p = (char *)coords->addr; | ||
| 29 | for ( i=0, lines=0; i<coords->size; ++i ) | ||
| 30 | if( p[i] == 0x00 ) | ||
| 31 | ++lines; | ||
| 32 | |||
| 33 | offsets = malloc( lines * sizeof(char*)); | ||
| 34 | if( !offsets ) exit( 111 ); | ||
| 35 | |||
| 36 | offsets[0] = p; l = 1; | ||
| 37 | for ( i=0; i<coords->size; ++i ) | ||
| 38 | if( p[i] == 0x00 ) | ||
| 39 | offsets[l++] = p+i+1; | ||
| 40 | |||
| 41 | l--; qsort(offsets, l, sizeof(char*), qsort_cmp ); | ||
| 42 | |||
| 43 | while( ( ll = getline( &input, &input_length, stdin ) ) >= 0 ) | ||
| 44 | { | ||
| 45 | char **coord_line; | ||
| 46 | input[ll-1]='\t'; | ||
| 47 | coord_line = bsearch( input, offsets, l, sizeof(char*), find_offset ); | ||
| 48 | if( !coord_line && ll > 2 && isalpha( input[ll-2] ) ) | ||
| 49 | { | ||
| 50 | input[ll-2] = '\t'; input[ll-1]=0; | ||
| 51 | ll--; | ||
| 52 | coord_line = bsearch( input, offsets, l, sizeof(char*), find_offset ); | ||
| 53 | } | ||
| 54 | |||
| 55 | if( coord_line ) | ||
| 56 | printf( "%s\n", *coord_line + ll ); | ||
| 57 | else | ||
| 58 | puts( "\t" ); | ||
| 59 | } | ||
| 60 | |||
| 61 | return 0; | ||
| 62 | } | ||
