summaryrefslogtreecommitdiff
path: root/src/mapcoords.c
blob: b7d19f6bc4ca3fdfe2af0144de08eb0315721b31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "mystdlib.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

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; i<coords->size; ++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; i<coords->size; ++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;
}