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