// File: /workspace/github_c_makefile_star2_O3/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 $0x38, %rsp
xorl %eax, %eax
movq %rax, 0x8(%rsp)
movq %rax, 0x30(%rsp)
cmpl $0x3, %edi
jne 0x14ea
movq %rsi, %r14
movq 0x10(%rsi), %rdi
callq 0x21a2
testq %rax, %rax
jne 0x1505
addq $0x10, %r14
movq %r14, %rdi
callq 0x1365
jmp 0x14ef
callq 0x126c
movl $0x1, %ebx
movl %ebx, %eax
addq $0x38, %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
movq %rbx, 0x10(%rsp)
leaq 0x30(%rsp), %rsi
movl $0x1, %ebx
movq %rax, %rdi
movl $0x1, %edx
callq 0x18b2
testl %eax, %eax
jg 0x155b
movq %r14, %rdi
callq 0x131c
jmp 0x14f4
movl %eax, %eax
movq 0x30(%rsp), %rbp
movq %rax, 0x18(%rsp)
imulq $0x100c, %rax, %rbx     # imm = 0x100C
xorl %eax, %eax
movq $0x1, (%rbp,%rax)
movl $0x0, 0x8(%rbp,%rax)
addq $0x100c, %rax           # imm = 0x100C
cmpq %rax, %rbx
jne 0x1570
movq %r15, %rdi
callq 0x1130
testl %eax, %eax
movq %r15, 0x20(%rsp)
jne 0x166d
movl $0x1, %r14d
leaq 0x8(%rsp), %r13
leaq 0x2aca(%rip), %r12      # 0x407d
incl %r14d
movq %r15, %rdi
movq %r13, %rsi
movl %r14d, %edx
callq 0x18b2
testl %eax, %eax
je 0x163d
cmpl 0x18(%rsp), %eax
jne 0x1654
movq 0x8(%rsp), %rax
xorl %ecx, %ecx
movl 0x4(%rax,%rcx), %edx
movl 0x4(%rbp,%rcx), %esi
cmpl %esi, %edx
cmovgl %edx, %esi
movl %esi, 0x4(%rbp,%rcx)
movl (%rax,%rcx), %edx
cmpl $0x2, %edx
je 0x1600
testl %edx, %edx
jne 0x1631
movl $0x0, (%rbp,%rcx)
jmp 0x1631
movl (%rbp,%rcx), %edx
cmpl $0x2, %edx
je 0x1620
cmpl $0x1, %edx
jne 0x1631
movl $0x2, (%rbp,%rcx)
movl 0x8(%rax,%rcx), %edx
movl %edx, 0x8(%rbp,%rcx)
jmp 0x1631
movl 0x8(%rax,%rcx), %edx
movl 0x8(%rbp,%rcx), %esi
cmpl %esi, %edx
cmovgl %edx, %esi
movl %esi, 0x8(%rbp,%rcx)
addq $0x100c, %rcx           # imm = 0x100C
cmpq %rcx, %rbx
jne 0x15d9
movq 0x20(%rsp), %r15
movq %r15, %rdi
callq 0x1130
testl %eax, %eax
je 0x15b3
jmp 0x166d
movq 0x499d(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
movq %r12, %rsi
movl %r14d, %edx
xorl %eax, %eax
callq 0x1120
jmp 0x163d
leaq 0x4(%rbp), %rax
movabsq $0x100000000, %rcx      # imm = 0x100000000
movq 0x18(%rsp), %rdx
movq 0x10(%rsp), %rbx
cmpl $0x0, (%rax)
jne 0x168e
movq %rcx, -0x4(%rax)
addq $0x100c, %rax           # imm = 0x100C
decq %rdx
jne 0x1685
leaq 0xc(%rbp), %r15
leaq 0x2a08(%rip), %r12      # 0x40ac
xorl %r13d, %r13d
movl -0xc(%r15), %edx
movl -0x8(%r15), %ecx
movl -0x4(%r15), %r8d
movq %rbx, %rdi
movq %r15, %rsi
callq 0x23bd
cmpl $-0x1, %eax
je 0x16d6
incq %r13
addq $0x100c, %r15           # imm = 0x100C
cmpq %r13, 0x18(%rsp)
jne 0x16a7
jmp 0x16ef
movq 0x491b(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
movq %r12, %rsi
movl %r13d, %edx
xorl %eax, %eax
callq 0x1120
jmp 0x16c3
movq 0x20(%rsp), %r14
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x1170
movq %r14, %rdi
callq 0x11e0
cmpb $0xa, %al
jne 0x1700
movq %r14, %rdi
callq 0x1130
movl $0x0, 0x4(%rsp)
movl $0x0, %r12d
testl %eax, %eax
jne 0x1859
leaq 0x8(%rsp), %r15
xorl %ebx, %ebx
xorl %r12d, %r12d
movl $0x0, 0x4(%rsp)
leal 0x1(%rbx), %r13d
movq %r14, %rdi
movq %r15, %rsi
movl %r13d, %edx
callq 0x18b2
cmpl 0x18(%rsp), %eax
jne 0x1846
movl %r13d, 0x2c(%rsp)
incl 0x4(%rsp)
movq 0x8(%rsp), %r15
addq $0xc, %r15
movq %rbp, %r14
xorl %r13d, %r13d
cmpb $0x0, (%r15)
je 0x17e1
cmpl $0x2, 0x4997(%rip)      # 0x6114
je 0x181d
incl %r12d
movl (%r14), %eax
testl %eax, %eax
je 0x17b2
cmpl $0x2, %eax
je 0x17c6
cmpl $0x1, %eax
jne 0x17fc
movq %r15, %rdi
callq 0x11b0
movq 0x10(%rsp), %rdi
movl %ebx, %esi
movl %r13d, %edx
movl %eax, %ecx
callq 0x2d6f
jmp 0x17dd
movq 0x10(%rsp), %rdi
movl %ebx, %esi
movl %r13d, %edx
movq %r15, %rcx
callq 0x2d82
jmp 0x17dd
movq %r15, %rdi
callq 0x10a0
movq 0x10(%rsp), %rdi
movl %ebx, %esi
movl %r13d, %edx
callq 0x2afe
testl %eax, %eax
je 0x17fc
incq %r13
movl $0x100c, %eax           # imm = 0x100C
addq %rax, %r14
addq %rax, %r15
cmpq %r13, 0x18(%rsp)
jne 0x1770
jmp 0x1837
movq 0x47f5(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
leaq 0x28c6(%rip), %rsi      # 0x40d3
movl %r13d, %edx
movl 0x4(%rsp), %ecx
xorl %eax, %eax
callq 0x1120
jmp 0x17e1
leaq -0xc(%r15), %rcx
leaq 0x28a7(%rip), %rdi      # 0x40cf
movl %ebx, %esi
movl %r13d, %edx
callq 0x1210
jmp 0x1783
movq 0x20(%rsp), %r14
leaq 0x8(%rsp), %r15
movl 0x2c(%rsp), %r13d
movq %r14, %rdi
callq 0x1130
movl %r13d, %ebx
testl %eax, %eax
je 0x173c
movq 0x10(%rsp), %r14
movq %r14, %rdi
callq 0x2885
testl %eax, %eax
jle 0x1872
movq %r14, %rdi
callq 0x1288
movq 0x477f(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
leaq 0x2883(%rip), %rsi      # 0x4106
xorl %ebx, %ebx
movl 0x4(%rsp), %edx
movl %r12d, %ecx
xorl %eax, %eax
callq 0x1120
movq 0x8(%rsp), %rdi
callq 0x1030
movq %rbp, %rdi
callq 0x1030
movq %r14, %rdi
callq 0x2117
jmp 0x14f4