summaryrefslogtreecommitdiff
path: root/src/export/split_version_3.c
blob: 8170a3ac4a467cb6743f939332e6bc6a8cee13a4 (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
51
52
53
54
55
56
57
58
59
60
61
62
#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];
  int  outfiles[64], i, base = 0, fixed_columns = 0;
  ssize_t temp = 0;
  uint32_t *p = (uint32_t*)f;

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

  /* No output file yet valid */
  for( i=0; i<64; ++i ) outfiles[i] = -1;

  while( fgets( table, sizeof(table), stdin ) ) {
    int file, strnr, columns, count;

    table[strlen(table)-1] = 0;

    {
      int f_in = open( table, O_RDONLY );
      if( f_in < 0  || read( f_in, f, sizeof(f)) <= 0 ) {
        fprintf( stderr, "Can not read file %s\n", table );
        exit(1);
      }
      close(f_in);
    }

    count = (int)*p,
    columns = fixed_columns ? fixed_columns : (int)p[1] / 4 - 1;

    for( file=0; file<columns; ++file ) {
      uint32_t off;

      /* 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 ) {
          fprintf( stderr, "Can not create output file %s\n", table );
          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'; }
      temp += write( outfiles[file], f + p[file+1], off - p[file+1] );
    }
  }

  for( i=0; i<64; ++i ) close( outfiles[i] );
  if( !temp )
    exit(1);

  return 0;
}