// File: /workspace/github_c_makefile_star2_O1/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 $0x48, %rsp
xorl %eax, %eax
movq %rax, 0x18(%rsp)
movq %rax, 0x8(%rsp)
cmpl $0x3, %edi
jne 0x14ea
movq %rsi, %r14
movq 0x10(%rsi), %rdi
callq 0x2207
testq %rax, %rax
jne 0x1505
addq $0x10, %r14
movq %r14, %rdi
callq 0x1365
jmp 0x14ef
callq 0x126c
movl $0x1, %ebp
movl %ebp, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq 0x8(%r14), %rdi
addq $0x8, %r14
leaq 0x2c77(%rip), %rsi      # 0x418e
callq 0x11a0
testq %rax, %rax
jne 0x152e
movq %r14, %rdi
movq %rbx, %rsi
callq 0x133a
jmp 0x14ef
movq %rax, %r15
leaq 0x8(%rsp), %rsi
movl $0x1, %ebp
movq %rax, %rdi
movl $0x1, %edx
callq 0x18ee
testl %eax, %eax
jg 0x1556
movq %r14, %rdi
callq 0x131c
jmp 0x14f4
movq %rbx, 0x20(%rsp)
movq 0x8(%rsp), %rsi
movl %eax, 0x14(%rsp)
movl %eax, %r14d
imulq $0x100c, %r14, %rcx     # imm = 0x100C
xorl %edx, %edx
movq $0x1, (%rsi,%rdx)
movl $0x0, 0x8(%rsi,%rdx)
addq $0x100c, %rdx           # imm = 0x100C
cmpq %rdx, %rcx
jne 0x1570
movq %r15, %rdi
callq 0x1130
testl %eax, %eax
movq %r15, 0x30(%rsp)
jne 0x166e
movq 0x8(%rsp), %rbx
imulq $0x100c, %r14, %r12     # imm = 0x100C
movl $0x1, %ebp
leaq 0x18(%rsp), %r14
leaq 0x2abf(%rip), %r13      # 0x407d
incl %ebp
movq %r15, %rdi
movq %r14, %rsi
movl %ebp, %edx
callq 0x18ee
testl %eax, %eax
je 0x163f
cmpl 0x14(%rsp), %eax
jne 0x1656
movq 0x18(%rsp), %rax
xorl %ecx, %ecx
movl 0x4(%rax,%rcx), %edx
movl 0x4(%rbx,%rcx), %esi
cmpl %esi, %edx
cmovgl %edx, %esi
movl %esi, 0x4(%rbx,%rcx)
movl (%rax,%rcx), %edx
cmpl $0x2, %edx
je 0x1604
testl %edx, %edx
jne 0x1633
movl $0x0, (%rbx,%rcx)
jmp 0x1633
movl (%rbx,%rcx), %edx
cmpl $0x2, %edx
je 0x1622
cmpl $0x1, %edx
jne 0x1633
movl $0x2, (%rbx,%rcx)
movl 0x8(%rax,%rcx), %edx
movl %edx, 0x8(%rbx,%rcx)
jmp 0x1633
movl 0x8(%rax,%rcx), %edx
movl 0x8(%rbx,%rcx), %esi
cmpl %esi, %edx
cmovgl %edx, %esi
movl %esi, 0x8(%rbx,%rcx)
addq $0x100c, %rcx           # imm = 0x100C
cmpq %rcx, %r12
jne 0x15de
movq 0x30(%rsp), %r15
movq %r15, %rdi
callq 0x1130
testl %eax, %eax
je 0x15be
jmp 0x166e
movq 0x499b(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
movq %r13, %rsi
movl %ebp, %edx
xorl %eax, %eax
callq 0x1120
jmp 0x163f
movl 0x14(%rsp), %ebp
cmpl $0x2, %ebp
movl $0x1, %ebx
cmovgel %ebp, %ebx
movq 0x8(%rsp), %rax
imulq $0x100c, %rbx, %rcx     # imm = 0x100C
xorl %edx, %edx
movabsq $0x100000000, %rsi      # imm = 0x100000000
movq 0x20(%rsp), %r13
cmpl $0x0, 0x4(%rax,%rdx)
jne 0x16a5
movq %rsi, (%rax,%rdx)
addq $0x100c, %rdx           # imm = 0x100C
cmpq %rdx, %rcx
jne 0x169a
movq 0x8(%rsp), %r14
addq $0xc, %r14
leaq 0x29eb(%rip), %r15      # 0x40ac
xorl %r12d, %r12d
movl -0xc(%r14), %edx
movl -0x8(%r14), %ecx
movl -0x4(%r14), %r8d
movq %r13, %rdi
movq %r14, %rsi
callq 0x244b
cmpl $-0x1, %eax
je 0x16f1
incq %r12
addq $0x100c, %r14           # imm = 0x100C
cmpq %r12, %rbx
jne 0x16c4
jmp 0x170a
movq 0x4900(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
movq %r15, %rsi
movl %r12d, %edx
xorl %eax, %eax
callq 0x1120
jmp 0x16e0
movq 0x30(%rsp), %r14
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x1170
movq %r14, %rdi
callq 0x11e0
cmpb $0xa, %al
jne 0x171b
movq %r14, %rdi
callq 0x1130
movl $0x0, 0x4(%rsp)
movl $0x0, %r12d
testl %eax, %eax
jne 0x1893
cmpl $0x2, %ebp
movl $0x1, %r13d
cmovgel %ebp, %r13d
movq 0x8(%rsp), %rax
movq %rax, 0x40(%rsp)
leaq 0x18(%rsp), %r15
movl $0x100c, %ebx           # imm = 0x100C
xorl %ecx, %ecx
xorl %r12d, %r12d
movl $0x0, 0x4(%rsp)
movq %rcx, 0x28(%rsp)
leal 0x1(%rcx), %edx
movq %r14, %rdi
movq %r15, %rsi
movl %edx, 0x3c(%rsp)
callq 0x18ee
cmpl %ebp, %eax
jne 0x187f
incl 0x4(%rsp)
movq 0x18(%rsp), %r15
addq $0xc, %r15
movq 0x40(%rsp), %rbp
xorl %r14d, %r14d
cmpb $0x0, (%r15)
je 0x181f
cmpl $0x2, 0x4960(%rip)      # 0x6114
je 0x1833
movl (%rbp), %ecx
testl %ecx, %ecx
je 0x1803
cmpl $0x2, %ecx
je 0x17e7
xorl %eax, %eax
cmpl $0x1, %ecx
jne 0x1818
movq %r15, %rdi
callq 0x11b0
movq 0x20(%rsp), %rdi
movq 0x28(%rsp), %rsi
movl %r14d, %edx
movl %eax, %ecx
callq 0x2de7
jmp 0x1818
movq %r15, %rdi
callq 0x10a0
movq 0x20(%rsp), %rdi
movq 0x28(%rsp), %rsi
movl %r14d, %edx
callq 0x2b7a
jmp 0x1818
movq 0x20(%rsp), %rdi
movq 0x28(%rsp), %rsi
movl %r14d, %edx
movq %r15, %rcx
callq 0x2dfa
incl %r12d
testl %eax, %eax
je 0x1850
incq %r14
addq %rbx, %rbp
addq %rbx, %r15
cmpq %r14, %r13
jne 0x17a7
jmp 0x1871
leaq -0xc(%r15), %rcx
leaq 0x2891(%rip), %rdi      # 0x40cf
movq 0x28(%rsp), %rsi
movl %r14d, %edx
callq 0x1210
jmp 0x17b6
movq 0x47a1(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
leaq 0x2872(%rip), %rsi      # 0x40d3
movl %r14d, %edx
movl 0x4(%rsp), %ecx
xorl %eax, %eax
callq 0x1120
jmp 0x181f
movq 0x30(%rsp), %r14
movl 0x14(%rsp), %ebp
leaq 0x18(%rsp), %r15
movq %r14, %rdi
callq 0x1130
movl 0x3c(%rsp), %ecx
testl %eax, %eax
je 0x1773
movq 0x20(%rsp), %rbx
movq %rbx, %rdi
callq 0x2905
testl %eax, %eax
jle 0x18ac
movq %rbx, %rdi
callq 0x1288
movq 0x4745(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
leaq 0x2849(%rip), %rsi      # 0x4106
xorl %ebp, %ebp
movl 0x4(%rsp), %edx
movl %r12d, %ecx
xorl %eax, %eax
callq 0x1120
movq 0x18(%rsp), %rdi
callq 0x1030
movq 0x8(%rsp), %rdi
callq 0x1030
movq %rbx, %rdi
callq 0x217c
jmp 0x14f4