// File: /workspace/github_c_makefile_star2_O2/bmottershead[P]DBF2TSV/tsv2dbf.c
// Function: main
int main( int argc, char ** argv ) {
  FILE*     tsv_file = NULL;
  DBFHandle dbf_file = NULL;
  int       num_columns, i, j, rows=0, values=0;
  column    *columns = NULL;
  column    *fields = NULL;
  char      c;

  // Check that there are two arguments, the input TSV file and
  // the output DBF file.
  if (argc!=3) {
    fprintf(stderr, "Usage: tsv2dbf tsv-file dbf-file\n");
    return EXIT_FAILURE;
  }
  
  // Open DBF file
  dbf_file = DBFCreate(argv[2]);
  if (dbf_file == NULL) {
    fprintf(stderr, "%s file cannot be created\n", argv[2]);
    return EXIT_FAILURE;
  }

  // Open TSV file.
  tsv_file = fopen(argv[1],"r");
  if (tsv_file == NULL) {
    fprintf(stderr, "%s cannot be opened\n", argv[1]);
    DBFClose(dbf_file);
    return EXIT_FAILURE;
  }

  // Read  header row of TSV file for titles.
  num_columns = get_columns(tsv_file, &fields, 1);
  if (num_columns <= 0) {
    fprintf(stderr, "%s can't be read or is not a DBF file\n", argv[1]);
    return EXIT_FAILURE;
  }

  // Initialize type, widths in header array
  for (j=0; j<num_columns; j++) {
    fields[j].type=FTInteger;
    fields[j].width=0;
    fields[j].decimals=0;
  }

  // Loop through TSV data rows, determining the 
  // most restrictive data type for each column which will
  // permit all the actual TSV values to be loaded.
  for (i=1; !feof(tsv_file); i++) {
    int num_field_columns = get_columns(tsv_file, &columns, i+1);

    if (num_field_columns==0)
      continue;
    if (num_field_columns != num_columns) {
      fprintf(stderr, "Wrong number of fields at row %d. Row ignored.", i+1);
      continue;
    } 
    for (j=0; j<num_columns; j++) {
      fields[j].width=max(columns[j].width, fields[j].width);
      switch(columns[j].type) {
      case FTString:
        fields[j].type=FTString;
        break;
      case FTDouble:
        if (fields[j].type==FTInteger) {
          fields[j].type=FTDouble;
          fields[j].decimals=columns[j].decimals;
        } else if (fields[j].type==FTDouble) {
          fields[j].decimals=max(columns[j].decimals, fields[j].decimals);
        } 
        break;
      default:
        break;
      }
    }
  }

  // Make columns that had all empty values into strings.
  for (j=0; j<num_columns; j++) {
    if (fields[j].width==0) {
      fields[j].type=FTString;
      fields[j].width=1;
    }
  }
  
  // Define the fields of the  DBF file.
  for (i=0; i<num_columns; i++) {
    int ret=DBFAddField(dbf_file, fields[i].value, fields[i].type, 
                fields[i].width, fields[i].decimals);
    if (ret==-1) 
      fprintf(stderr,"Error adding field %d to DBF file\n",i);
  }

  // Reset TSV file and skip over header row.
  fseek(tsv_file, 0, SEEK_SET);
  while((c = getc(tsv_file)) != RS) { /* empty loop */}

  // Read the data rows and write the column values into the DBF file.
  for (i=0; !feof(tsv_file); i++) {
    int num_field_columns = get_columns(tsv_file, &columns, i+1);

    if (num_field_columns != num_columns)
      continue;

    rows++;
    for (j=0; j<num_columns; j++) {
      int ret=0;

      if (columns[j].value[0]==0)
        continue;

      if (debug==2)
        dump_column("row", i, j, &columns[j]);

      values++;
      switch (fields[j].type) {
      case FTInteger:
        ret=DBFWriteIntegerAttribute(dbf_file, i, j, atoi(columns[j].value));
        break;
      case FTDouble:
        ret=DBFWriteDoubleAttribute(dbf_file, i, j, atof(columns[j].value));
        break;
      case FTString:
        ret=DBFWriteStringAttribute(dbf_file, i, j, columns[j].value);
        break;
      // These won't occur, but are mentioned to avoid a compiler warning.
      case FTLogical:
      case FTInvalid:
        break;
      }
      if (ret==0) 
        fprintf(stderr,"Error writing column %d of row %d\n",j,rows);
    }
  }


  // Some information on stderr
  for (i=0; i<DBFGetFieldCount(dbf_file); i++) {
    int width, decimals; 
    char title[12];
    DBFFieldType type = DBFGetFieldInfo(dbf_file, i, title, &width, &decimals);
    fprintf(stderr, "%d %s %s %d.%d\n", i, title, type_to_str(type), width, decimals);
  }
  fprintf(stderr, "Data rows: %d, Non-null values: %d\n", rows, values);

  // Finish
  free(columns);
  free(fields);
  DBFClose(dbf_file);
  return EXIT_SUCCESS;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
andq $0x0, 0x18(%rsp)
andq $0x0, 0x40(%rsp)
pushq $0x1
popq %rbx
cmpl $0x3, %edi
jne 0x1350
movq %rsi, %r14
movq 0x10(%rsi), %rdi
callq 0x2081
testq %rax, %rax
jne 0x1371
movq 0x5cc2(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
movq 0x10(%r14), %rdx
leaq 0x2ce2(%rip), %rsi      # 0x4026
xorl %eax, %eax
callq 0x1120
jmp 0x1749
movq 0x5ca1(%rip), %rax      # 0x6ff8
movq (%rax), %rcx
leaq 0x2ca3(%rip), %rdi      # 0x4004
pushq $0x21
popq %rsi
pushq $0x1
popq %rdx
callq 0x11d0
jmp 0x1749
movq %rax, %r12
movq 0x8(%r14), %rdi
leaq 0x2e0f(%rip), %rsi      # 0x418e
callq 0x11a0
testq %rax, %rax
jne 0x13aa
movq 0x5c68(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
movq 0x8(%r14), %rdx
leaq 0x2ca3(%rip), %rsi      # 0x4041
xorl %eax, %eax
callq 0x1120
jmp 0x1741
movq %rax, %r15
movq %r12, 0x20(%rsp)
leaq 0x40(%rsp), %rsi
pushq $0x1
popq %rbp
movq %rax, %rdi
movl %ebp, %edx
callq 0x175a
testl %eax, %eax
jg 0x13ec
movq 0x5c29(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
movq 0x8(%r14), %rdx
leaq 0x2c79(%rip), %rsi      # 0x4056
xorl %eax, %eax
callq 0x1120
pushq $0x1
popq %rbx
jmp 0x1749
movl %eax, %eax
movq 0x40(%rsp), %r14
movq %rax, 0x30(%rsp)
imulq $0x100c, %rax, %r13     # imm = 0x100C
xorl %eax, %eax
cmpq %rax, %r13
je 0x141c
movq $0x1, (%r14,%rax)
andl $0x0, 0x8(%r14,%rax)
addq $0x100c, %rax           # imm = 0x100C
jmp 0x1401
leaq 0x18(%rsp), %rbx
leaq 0x2c55(%rip), %r12      # 0x407d
movq %r15, %rdi
callq 0x1130
testl %eax, %eax
jne 0x14db
incl %ebp
movq %r15, %rdi
movq %rbx, %rsi
movl %ebp, %edx
callq 0x175a
testl %eax, %eax
je 0x1428
cmpl 0x30(%rsp), %eax
jne 0x14c0
movq 0x18(%rsp), %rax
xorl %ecx, %ecx
cmpq %rcx, %r13
je 0x1428
movl 0x4(%rax,%rcx), %edx
movl 0x4(%r14,%rcx), %esi
cmpl %esi, %edx
cmovgl %edx, %esi
movl %esi, 0x4(%r14,%rcx)
movl (%rax,%rcx), %edx
cmpl $0x2, %edx
je 0x1483
testl %edx, %edx
jne 0x14b7
andl $0x0, (%r14,%rcx)
jmp 0x14b7
movl (%r14,%rcx), %edx
cmpl $0x2, %edx
je 0x14a4
cmpl $0x1, %edx
jne 0x14b7
movl $0x2, (%r14,%rcx)
movl 0x8(%rax,%rcx), %edx
movl %edx, 0x8(%r14,%rcx)
jmp 0x14b7
movl 0x8(%rax,%rcx), %edx
movl 0x8(%r14,%rcx), %esi
cmpl %esi, %edx
cmovgl %edx, %esi
movl %esi, 0x8(%r14,%rcx)
addq $0x100c, %rcx           # imm = 0x100C
jmp 0x1458
movq 0x5b31(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
movq %r12, %rsi
movl %ebp, %edx
xorl %eax, %eax
callq 0x1120
jmp 0x1428
xorl %eax, %eax
movabsq $0x100000000, %rcx      # imm = 0x100000000
movq 0x20(%rsp), %rbp
cmpq %rax, %r13
je 0x1505
cmpl $0x0, 0x4(%r14,%rax)
jne 0x14fd
movq %rcx, (%r14,%rax)
addq $0x100c, %rax           # imm = 0x100C
jmp 0x14ec
leaq 0xc(%r14), %rbx
leaq 0x2b9c(%rip), %r12      # 0x40ac
xorl %r13d, %r13d
cmpq %r13, 0x30(%rsp)
je 0x1559
movl -0xc(%rbx), %edx
movl -0x8(%rbx), %ecx
movl -0x4(%rbx), %r8d
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x228f
cmpl $-0x1, %eax
je 0x1540
incq %r13
addq $0x100c, %rbx           # imm = 0x100C
jmp 0x1513
movq 0x5ab1(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
movq %r12, %rsi
movl %r13d, %edx
xorl %eax, %eax
callq 0x1120
jmp 0x1534
movq %r15, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x1170
movq %r15, %rdi
callq 0x11e0
cmpb $0xa, %al
jne 0x1565
leaq 0x18(%rsp), %rbx
movl $0x0, 0x14(%rsp)
movl $0x0, 0x2c(%rsp)
xorl %r12d, %r12d
movq %r15, %rdi
callq 0x1130
testl %eax, %eax
jne 0x16a3
incl %r12d
movq %r15, %rdi
movq %rbx, %rsi
movl %r12d, %edx
callq 0x175a
cmpl 0x30(%rsp), %eax
jne 0x1589
incl 0x14(%rsp)
movq 0x18(%rsp), %r13
leal -0x1(%r12), %eax
movl %eax, 0x10(%rsp)
addq $0xc, %r13
movq %r14, %rbx
xorl %ebp, %ebp
cmpq %rbp, 0x30(%rsp)
je 0x1694
cmpb $0x0, (%r13)
je 0x1661
cmpl $0x2, 0x5b2c(%rip)      # 0x7114
jne 0x1600
leaq -0xc(%r13), %rcx
leaq 0x2ada(%rip), %rdi      # 0x40cf
movl 0x10(%rsp), %esi
movl %ebp, %edx
callq 0x1900
incl 0x2c(%rsp)
movl (%rbx), %eax
testl %eax, %eax
je 0x1630
cmpl $0x2, %eax
je 0x1645
cmpl $0x1, %eax
jne 0x1674
movq %r13, %rdi
callq 0x11b0
movq 0x20(%rsp), %rdi
movl 0x10(%rsp), %esi
movl %ebp, %edx
movl %eax, %ecx
callq 0x2bf0
jmp 0x165d
movq 0x20(%rsp), %rdi
movl 0x10(%rsp), %esi
movl %ebp, %edx
movq %r13, %rcx
callq 0x2c03
jmp 0x165d
movq %r13, %rdi
callq 0x10a0
movq 0x20(%rsp), %rdi
movl 0x10(%rsp), %esi
movl %ebp, %edx
callq 0x29a0
testl %eax, %eax
je 0x1674
incq %rbp
movl $0x100c, %eax           # imm = 0x100C
addq %rax, %rbx
addq %rax, %r13
jmp 0x15cb
movq 0x597d(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
leaq 0x2a4e(%rip), %rsi      # 0x40d3
movl %ebp, %edx
movl 0x14(%rsp), %ecx
xorl %eax, %eax
callq 0x1120
jmp 0x1661
movq 0x20(%rsp), %rbp
leaq 0x18(%rsp), %rbx
jmp 0x1589
leaq 0x4c(%rsp), %rbx
leaq 0x2a47(%rip), %r13      # 0x40f6
movq %rbp, %r12
xorl %ebp, %ebp
movq %r12, %rdi
callq 0x2736
cmpl %eax, %ebp
jge 0x170d
movq %r12, %rdi
movl %ebp, %esi
movq %rbx, %rdx
leaq 0x3c(%rsp), %rcx
leaq 0x38(%rsp), %r8
callq 0x273e
movq 0x591a(%rip), %rcx      # 0x6ff8
movq (%rcx), %r15
movl %eax, %edi
callq 0x1963
movl 0x3c(%rsp), %r9d
movl 0x38(%rsp), %ecx
movl %ecx, (%rsp)
movq %r15, %rdi
movq %r13, %rsi
movl %ebp, %edx
movq %rbx, %rcx
movq %rax, %r8
xorl %eax, %eax
callq 0x1120
incl %ebp
jmp 0x16b4
movq 0x58e4(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
leaq 0x29e8(%rip), %rsi      # 0x4106
xorl %ebx, %ebx
movl 0x14(%rsp), %edx
movl 0x2c(%rsp), %ecx
xorl %eax, %eax
callq 0x1120
movq 0x18(%rsp), %rdi
callq 0x1030
movq %r14, %rdi
callq 0x1030
movq %r12, %rdi
callq 0x1ff6
movl %ebx, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq