summaryrefslogtreecommitdiff
path: root/src/splitold.c
blob: bd8577526c4797dd4db8d0f9e981f35351aacf50 (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
#include <stdint.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>

int main( int argc, char **args ) {
  char table[64], f[1024*1024*16];
  int  outfiles[64], i, off, base = 0;
  uint32_t fixed_columns = 0;

  if( argc > 1 ) base = atol( args[1] );
  if( argc > 2 ) fixed_columns = atol( args[2] );

  for( i=0; i<64; ++i ) outfiles[i] = -1;
  while( fgets( table, sizeof(table), stdin ) ) {
    int off = ( table[strlen(table)-1] = 0 ); /* fgets sucks */
    int f_in = open( table, O_RDONLY );
    size_t s_in = read( f_in, f, sizeof(f));
    uint32_t *p = (uint32_t*)f;
    uint32_t count = p[0], columns = fixed_columns ? fixed_columns : p[1] / 4 - 1;
    unsigned int file, strnr;

    close(f_in);

    for( file=0; file<columns; ++file ) {
      /* Create outfile, if it is not yet there */
      if( outfiles[file] == -1 ) {
        sprintf( table, "%02d_unknown", file+base );
        outfiles[file] = open( table, O_WRONLY | O_APPEND | O_CREAT, 0644 );
        if ( outfiles[file] == -1 ) exit(1);
      }
      off = p[file+1];
      /* Look for end of this chunk, which is <count> strings long */
      for( strnr=0; strnr < count; ++strnr ) { while( f[off++] ) {}; f[off-1] = '\n'; }
      write( outfiles[file], f + p[file+1], off - p[file+1] );
    }
  }
  for( i=0; i<64; ++i ) close( outfiles[i] );
  return 0;
}