// File: /workspace/github_cpp_makefile_star2_O1/nompelis[P]INTecplotLib/intecplot.cpp
// Function: inTec_File::ParseComponent_Zone()
int inTec_File::ParseComponent_Zone()
{
#ifdef _DEBUG_
   printf(" i Inside \"ParseComponent_Zone()\" \n");
#endif

   // instantiate a zone object
   inTec_Zone *zone = new inTec_Zone( this );
   // check for allocation error
   if( zone == NULL ) {
      printf(" e Could not allocate zone! \n");
      return(-1);
   }

   // set the position in the file for this zone
   int iret = zone->NotifyPositionInFile( ipos );
   if( iret != 0 ) {
      printf(" e Error setting zone's position: %ld \n", ipos);
      printf("   This should never happen (zone rejects only when new)\n");
      delete zone;
      return(1);
   }

   // seek back to the begining of the line that got us here so we can re-parse
   iret = fseek( fp, ipos, SEEK_SET );
   if( iret != 0 ) {
      printf(" e Error seeking to proper parsing position: %ld \n", ipos);
      return(2);
   }
   // retract line-number as the line will be re-read
   --iline;

   // Looking to parse multiple keywords and over multiple lines
   // T= <string>
   // I=
   // J=
   // K=
   // N=  NODES=
   // E=  ELEMENTS=

   // The idea here is to keep ingesting in lines until we find a new component
   // We ingest comments that can interrupt the geometry component
   // Once a new component is detected, we wrap up the process

   char *buf=NULL;
   size_t ibuf_size=0;

   int idone_zone = 0;   // flag for present zone component termination
   int igot_head = 0;    // flag for whether we have a description of the zone
   while( !feof( fp ) && idone_zone <= 1 ) {
      int ic;

      // information about the position in the file
      ipos = ftell( fp );
#ifdef _DEBUG2_
      printf(" i Position in file at %ld bytes, line %ld \n", ipos, iline+1 );
#endif

      // prepare the buffer
      memset( buf, '\0', ibuf_size );
      // read a line of data; get count of bytes read
      ic = inUtils_ReadTextline( &ibuf_size, &buf, fp );
      // check error code
      if( ic < 0 ) {
         printf(" e There was an external library error (inUtils): %d\n", ic);
         return(-2);    // this error code may have to change
      }

      ++iline;

      // sanitize the line
      inUtils_TrimLeadingSpaceTextline( buf );
      size_t isize=0;
      while( isize < ibuf_size && buf[isize] != '\n' ) ++isize;
      if( buf[isize] == '\n' ) buf[isize] = '\0';
#ifdef _DEBUG3_
      printf("STRING (%p, %ld):%s\n", buf, isize, buf );
#endif

      // attempt to identify this line as a new component to terminate parsing
      int iparse_line=1;    // default is to parse the line
      iret = IdentifyComponent( buf );
#ifdef _DEBUG3_
printf("FOUND iret=%d idone_zone=%d \n", iret,idone_zone);
#endif
      // check if we have jumped to a new component under certain conditions...
      if( iret > 0 ) {             // we found a component

         // only allow "stray" comment components to pass through
         if( iret == Comment ) {
#ifdef _DEBUG_
            printf(" --- Found comment (inside Zone component) \n");
#endif
            // we will not be parsing this line
            iparse_line = 0;
            // store comment in strings
            buf[ strlen(buf) ] = '\n';
            strings[ iline ] = buf;
#ifdef _DEBUG_
            printf(" i Added comment to strings line %ld \n", iline );
#endif
            // show the line
            printf(" [%ld]  %s", iline, buf );
         } else {
#ifdef _DEBUG_
            printf(" --- Found component (at _or_ after Zone component)\n");
#endif
            // over-write the "zone" keyword marker if this is the first pass
            if( idone_zone == 0 ) {
               buf[0] = ' '; buf[1] = ' '; buf[2] = ' '; buf[3] = ' ';
            }

            // possibly deal with a "false positive" of a zone...
// 
//  This fails when finding leading "ZONETYPE" for example!!!
//  What I need to do is to find a better trap for this
// 
            if( strlen( buf ) >= 8 ) {
               if( strncasecmp( buf, "ZONETYPE", 8 ) == 0 ) {

               } else {
                  // increment termination condition variable
                  ++idone_zone;
               }
            }

            // will not parse new components
            if( idone_zone > 1) {
               iparse_line = 0;
#ifdef _DEBUG_
               printf(" --- New zone encountered; terminating this zone \n");
#endif
            }
         }
      } else {
#ifdef _DEBUG2_
         printf(" --- The line is part of the Zone component \n");
#endif
         iparse_line = 1;
      }

      // parse the line only if we are still parsing the current zone field
      if( iparse_line == 1 ) {
         if( igot_head == 0 ) {
#ifdef _DEBUG_
            printf(" --- Looking for Zone keywords \n");
#endif
            int np = zone->ParseKeywords( buf );
            if( np >= 0 ) {
#ifdef _DEBUG_
               printf(" --- Parsed %d item(s) \n",np);
#endif
               buf[ strlen(buf) ] = '\n';
               strings[ iline ] = buf;
#ifdef _DEBUG2_
               printf(" i Added to strings line %ld \n", iline );
#endif
            }
            if( np == -100 ) {     // special return value!
               // we have parsed through possibly multiple "zone" lines...
               // ...and encountered data
               igot_head = 1;
#ifdef _DEBUG_
               printf(" i Finished reading zone header (at line: %ld)\n",iline);
#endif

               // rewind this line
               iret = fseek( fp, ipos, SEEK_SET );
               if( iret != 0 ) {
                  printf(" e Error seeking to parsing position: %ld \n", ipos);
                  free( buf );
                  return(3);
               }
               // retract line-number as the line will be re-read
               --iline;

               // set the data position for this zone
               iret = zone->NotifyDataPositionInFile( ipos );
               // (here we must put the zone in data-reading particular state)
               if( zone->SetState_Reading() != 0 ) {
                  // respond to the possibility of the zone being mis-used...
                  return(5);   // this error code may change
               }

            } else if( np < 0 ) {
               // an error in parsing keywords was trapped
               // (This section will have to be changed based on how we want to
               // handle bad keywords, etc.)
               printf(" e Error parsing zone keywords \n");
               free( buf );
               return(4);
            }
         } else { // we have parsed the header completely; now parsing data
#ifdef _DEBUG2_
            printf(" --- Collecting zone data \n");
#endif
            // push the buffer to the zone for data extraction
            // (we must have set the zone to a data-reading state earlier)
            iret = zone->ParseNumericData( buf );
            // possibly respond to errors returned by the zone...
            if( iret < 0 ) {
               printf(" e Error parsing zone data \n");
               free( buf );
               return(5);
            }

         }

      } // (end of iparse_line=1)
   }

   // drop the buffer
   free( buf );

   // rewind this line
   iret = fseek( fp, ipos, SEEK_SET );
   if( iret != 0 ) {
      printf(" e Error seeking to parsing position at end: %ld \n", ipos);
      return(3);
   }
   --iline;


#ifdef _DROP_ZONE_
//printf("GOT HERE (zone istate=%d) \n",zone->GetState());
  zone->Dump( (const char *) "crap.dat" );
#ifdef _DEBUG_
// temporarily drop the zone object
  printf("HACK dropping the zone object\n");
  delete zone;
#endif
#else
  // add zone pointer to the file's zones container
  zones.push_back( zone );
#endif

#ifdef _DEBUG_
// printf(" i *** Skipping parsing of ZONE component *** \n");
   // we do this by returning control to the main parsing loop
#endif

   return(0);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, %rbx
leaq 0x45f1(%rip), %rdi      # 0xf88d
callq 0x5580
movl $0x140, %edi            # imm = 0x140
callq 0x5380
movq %rax, %r14
movq %rax, %rdi
movq %rbx, %rsi
callq 0x5510
movq %r14, 0x18(%rsp)
movq 0x128(%rbx), %rsi
cmpq $0x0, 0xd0(%r14)
je 0xb300
leaq 0x3ab6(%rip), %rdi      # 0xed8c
xorl %eax, %eax
callq 0x5060
leaq 0x469e(%rip), %rdi      # 0xf982
callq 0x5580
movq 0x18(%rsp), %rdi
movl $0x1, %ebp
testq %rdi, %rdi
je 0xb336
movq (%rdi), %rax
callq *0x8(%rax)
jmp 0xb336
movq %rsi, 0xd0(%r14)
movq 0x110(%rbx), %rdi
xorl %r14d, %r14d
xorl %edx, %edx
callq 0x53d0
testl %eax, %eax
je 0xb347
movq 0x128(%rbx), %rsi
leaq 0x3a8a(%rip), %rdi      # 0xedb4
xorl %eax, %eax
callq 0x5060
movl $0x2, %ebp
movl %ebp, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
decq 0x120(%rbx)
leaq 0x8(%rsp), %r12
movq %r14, (%r12)
leaq 0x20(%rsp), %rax
movq %r14, (%rax)
movq %rax, %r14
leaq 0x130(%rbx), %rax
movq %rax, 0x30(%rsp)
xorl %r13d, %r13d
movl $0x0, 0x14(%rsp)
movq 0x110(%rbx), %rdi
callq 0x55a0
testl %eax, %eax
jne 0xb769
cmpl $0x1, %r13d
jg 0xb769
movq 0x110(%rbx), %rdi
callq 0x5030
movq %rax, 0x128(%rbx)
movq 0x8(%rsp), %rdi
movq 0x20(%rsp), %rdx
xorl %esi, %esi
callq 0x51e0
movq 0x110(%rbx), %rdx
movq %r14, %rdi
movq %r12, %rsi
callq 0x5190
testl %eax, %eax
js 0xb406
incq 0x120(%rbx)
movq 0x8(%rsp), %rdi
callq 0x5050
movq 0x20(%rsp), %rcx
testq %rcx, %rcx
je 0xb423
movq 0x8(%rsp), %rdx
xorl %eax, %eax
cmpb $0xa, (%rdx,%rax)
je 0xb425
incq %rax
cmpq %rax, %rcx
jne 0xb3f3
movq %rcx, %rax
jmp 0xb425
xorl %r15d, %r15d
leaq 0x39d7(%rip), %rdi      # 0xede7
movl %eax, %esi
xorl %eax, %eax
callq 0x5060
movl $0xfffffffe, %ebp       # imm = 0xFFFFFFFE
jmp 0xb74e
xorl %eax, %eax
movq 0x8(%rsp), %rcx
cmpb $0xa, (%rcx,%rax)
jne 0xb434
movb $0x0, (%rcx,%rax)
movq 0x8(%rsp), %rsi
callq 0x5400
testl %eax, %eax
jle 0xb581
cmpl $0x64, %eax
jne 0xb4ff
leaq 0x44c0(%rip), %rdi      # 0xf916
callq 0x5580
movl %ebp, 0x10(%rsp)
movl %r13d, %ebp
movq 0x8(%rsp), %r13
movq %r13, %rdi
callq 0x5150
movb $0xa, (%r13,%rax)
movq 0x8(%rsp), %r13
movq 0x120(%rbx), %rax
movq %rax, 0x28(%rsp)
movq 0x30(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0x5570
movq %rax, %r12
movq 0x8(%rax), %r14
movq %r13, %rdi
callq 0x5150
xorl %r15d, %r15d
movq %r12, %rdi
leaq 0x8(%rsp), %r12
xorl %esi, %esi
movq %r14, %rdx
leaq 0x20(%rsp), %r14
movq %r13, %rcx
movl %ebp, %r13d
movl 0x10(%rsp), %ebp
movq %rax, %r8
callq 0x5590
movq 0x120(%rbx), %rsi
leaq 0x3944(%rip), %rdi      # 0xee1d
xorl %eax, %eax
callq 0x5060
movq 0x120(%rbx), %rsi
movq 0x8(%rsp), %rdx
leaq 0x3863(%rip), %rdi      # 0xed56
xorl %eax, %eax
callq 0x5060
jmp 0xb584
leaq 0x43aa(%rip), %rdi      # 0xf8b0
callq 0x5580
movl %r13d, %r15d
testl %r13d, %r13d
jne 0xb534
movq 0x8(%rsp), %rax
movb $0x20, %cl
movb %cl, (%rax)
movq 0x8(%rsp), %rax
movb %cl, 0x1(%rax)
movq 0x8(%rsp), %rax
movb %cl, 0x2(%rax)
movq 0x8(%rsp), %rax
movb %cl, 0x3(%rax)
movq 0x8(%rsp), %r13
movq %r13, %rdi
callq 0x5150
cmpq $0x8, %rax
jb 0xb567
movl $0x8, %edx
movq %r13, %rdi
leaq 0x2ca6(%rip), %rsi      # 0xe1fc
callq 0x54a0
cmpl $0x1, %eax
movl %r15d, %r13d
sbbl $-0x1, %r13d
jmp 0xb56a
movl %r15d, %r13d
cmpl $0x2, %r13d
jl 0xb581
leaq 0x436d(%rip), %rdi      # 0xf8e4
callq 0x5580
xorl %r15d, %r15d
jmp 0xb584
movb $0x1, %r15b
testb %r15b, %r15b
je 0xb5af
cmpl $0x0, 0x14(%rsp)
movb $0x1, %r15b
je 0xb5b7
movq 0x18(%rsp), %rdi
movq 0x8(%rsp), %rsi
callq 0x5310
movl $0x1, 0x14(%rsp)
jmp 0xb74e
movb $0x1, %r15b
jmp 0xb74e
movl %r13d, 0x10(%rsp)
leaq 0x437f(%rip), %rdi      # 0xf942
callq 0x5580
movq 0x18(%rsp), %rdi
movq 0x8(%rsp), %rsi
callq 0x5460
movl %eax, %r13d
testl %eax, %eax
js 0xb650
leaq 0x385f(%rip), %rdi      # 0xee44
movl %r13d, %esi
xorl %eax, %eax
callq 0x5060
movq 0x8(%rsp), %r14
movq %r14, %rdi
callq 0x5150
movb $0xa, (%r14,%rax)
movq 0x8(%rsp), %r14
movq 0x120(%rbx), %rax
movq %rax, 0x28(%rsp)
movq 0x30(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0x5570
movq %rax, %r12
movq 0x8(%rax), %r15
movq %r14, %rdi
callq 0x5150
movq %r12, %rdi
leaq 0x8(%rsp), %r12
xorl %esi, %esi
movq %r15, %rdx
movb $0x1, %r15b
movq %r14, %rcx
leaq 0x20(%rsp), %r14
movq %rax, %r8
callq 0x5590
cmpl $-0x64, %r13d
jne 0xb6b0
movq 0x120(%rbx), %rsi
leaq 0x37f9(%rip), %rdi      # 0xee5d
xorl %eax, %eax
callq 0x5060
movq 0x110(%rbx), %rdi
movq 0x128(%rbx), %rsi
xorl %edx, %edx
callq 0x53d0
testl %eax, %eax
je 0xb6e9
movq 0x128(%rbx), %rsi
xorl %r15d, %r15d
leaq 0x37f8(%rip), %rdi      # 0xee8d
xorl %eax, %eax
callq 0x5060
movq 0x8(%rsp), %rdi
callq 0x5430
movl $0x3, %ebp
jmp 0xb741
movl $0x0, 0x14(%rsp)
testl %r13d, %r13d
jns 0xb749
leaq 0x429a(%rip), %rdi      # 0xf962
callq 0x5580
movq 0x8(%rsp), %rdi
callq 0x5430
movl $0x4, %ebp
movl $0x0, 0x14(%rsp)
xorl %r15d, %r15d
jmp 0xb749
decq 0x120(%rbx)
movq 0x18(%rsp), %rax
cmpq $0x0, 0xd8(%rax)
jne 0xb71b
movq 0x128(%rbx), %rsi
movq %rsi, 0xd8(%rax)
leaq 0x2b21(%rip), %rdi      # 0xe235
xorl %eax, %eax
callq 0x5060
movq 0x18(%rsp), %r13
cmpl $0x0, 0x8(%r13)
jne 0xb733
movq %r13, %rdi
callq 0x54b0
testl %eax, %eax
je 0xb75c
xorl %r15d, %r15d
testb %r15b, %r15b
movl $0x5, %eax
cmovel %eax, %ebp
movl $0x1, 0x14(%rsp)
movl 0x10(%rsp), %r13d
testb %r15b, %r15b
jne 0xb379
jmp 0xb336
movl $0x1, 0x8(%r13)
movb $0x1, %r15b
jmp 0xb736
movq 0x8(%rsp), %rdi
callq 0x5430
movq 0x110(%rbx), %rdi
movq 0x128(%rbx), %rsi
xorl %edx, %edx
callq 0x53d0
testl %eax, %eax
je 0xb7ab
movq 0x128(%rbx), %rsi
leaq 0x371f(%rip), %rdi      # 0xeeb9
xorl %eax, %eax
callq 0x5060
movl $0x3, %ebp
jmp 0xb336
decq 0x120(%rbx)
movq 0x180(%rbx), %rsi
cmpq 0x188(%rbx), %rsi
je 0xb7d4
movq 0x18(%rsp), %rax
movq %rax, (%rsi)
addq $0x8, 0x180(%rbx)
jmp 0xb7e8
addq $0x178, %rbx            # imm = 0x178
leaq 0x18(%rsp), %rdx
movq %rbx, %rdi
callq 0x51a0
xorl %ebp, %ebp
jmp 0xb336
movq %rax, %rbx
movl $0x140, %esi            # imm = 0x140
movq %r14, %rdi
callq 0x5390
movq %rbx, %rdi
callq 0x5650
nop