diff options
Diffstat (limited to 'src/export/split_version_3.c')
| -rw-r--r-- | src/export/split_version_3.c | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/src/export/split_version_3.c b/src/export/split_version_3.c new file mode 100644 index 0000000..2019762 --- /dev/null +++ b/src/export/split_version_3.c | |||
| @@ -0,0 +1,58 @@ | |||
| 1 | #include <stdint.h> | ||
| 2 | #include <stdio.h> | ||
| 3 | #include <unistd.h> | ||
| 4 | #include <fcntl.h> | ||
| 5 | #include <stdlib.h> | ||
| 6 | #include <string.h> | ||
| 7 | |||
| 8 | int main( int argc, char **args ) { | ||
| 9 | char table[64], f[1024*1024]; | ||
| 10 | int outfiles[64], i, base = 0, fixed_columns = 0; | ||
| 11 | uint32_t *p = (uint32_t*)f; | ||
| 12 | |||
| 13 | if( argc > 1 ) base = atoi( args[1] ); | ||
| 14 | if( argc > 2 ) fixed_columns = atoi( args[2] ); | ||
| 15 | |||
| 16 | /* No output file yet valid */ | ||
| 17 | for( i=0; i<64; ++i ) outfiles[i] = -1; | ||
| 18 | |||
| 19 | while( fgets( table, sizeof(table), stdin ) ) { | ||
| 20 | int file, strnr, columns, count; | ||
| 21 | |||
| 22 | table[strlen(table)-1] = 0; | ||
| 23 | |||
| 24 | { | ||
| 25 | int f_in = open( table, O_RDONLY ); | ||
| 26 | if( f_in < 0 || read( f_in, f, sizeof(f)) <= 0 ) { | ||
| 27 | fprintf( stderr, "Can not read file %s\n", table ); | ||
| 28 | exit(1); | ||
| 29 | } | ||
| 30 | close(f_in); | ||
| 31 | } | ||
| 32 | |||
| 33 | count = (int)*p, | ||
| 34 | columns = fixed_columns ? fixed_columns : (int)p[1] / 4 - 1; | ||
| 35 | |||
| 36 | for( file=0; file<columns; ++file ) { | ||
| 37 | uint32_t off; | ||
| 38 | |||
| 39 | /* Create outfile, if it is not yet there */ | ||
| 40 | if( outfiles[file] == -1 ) { | ||
| 41 | sprintf( table, "%02d_unknown", file + base ); | ||
| 42 | outfiles[file] = open( table, O_WRONLY | O_APPEND | O_CREAT, 0644 ); | ||
| 43 | if ( outfiles[file] == -1 ) { | ||
| 44 | fprintf( stderr, "Can not create output file %s\n", table ); | ||
| 45 | exit(1); | ||
| 46 | } | ||
| 47 | } | ||
| 48 | |||
| 49 | off = p[file+1]; | ||
| 50 | /* Look for end of this chunk, which is <count> strings long */ | ||
| 51 | for( strnr=0; strnr < count; ++strnr ) { while( f[off++] ) {}; f[off-1] = '\n'; } | ||
| 52 | write( outfiles[file], f + p[file+1], off - p[file+1] ); | ||
| 53 | } | ||
| 54 | } | ||
| 55 | |||
| 56 | for( i=0; i<64; ++i ) close( outfiles[i] ); | ||
| 57 | return 0; | ||
| 58 | } | ||
