// File: /workspace/github_cpp_makefile_star2_O3/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, %r14
leaq 0x47f3(%rip), %rdi      # 0xf925
callq 0x5570
movl $0x140, %edi            # imm = 0x140
callq 0x5370
movq %rax, %rbx
movq %rax, %rdi
movq %r14, %rsi
callq 0x5500
movq %rbx, 0x30(%rsp)
movq 0x128(%r14), %rsi
cmpq $0x0, 0xd0(%rbx)
je 0xb192
leaq 0x3cb8(%rip), %rdi      # 0xee24
xorl %eax, %eax
callq 0x5060
leaq 0x48a0(%rip), %rdi      # 0xfa1a
callq 0x5570
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x8(%rax)
movl $0x1, %ebp
jmp 0xb5b6
movq %rsi, 0xd0(%rbx)
movq 0x110(%r14), %rdi
xorl %edx, %edx
callq 0x53c0
testl %eax, %eax
je 0xb1ca
movq 0x128(%r14), %rsi
leaq 0x3c93(%rip), %rdi      # 0xee4c
xorl %eax, %eax
callq 0x5060
movl $0x2, %ebp
jmp 0xb5b6
decq 0x120(%r14)
xorl %eax, %eax
movq %rax, 0x8(%rsp)
movq %rax, 0x28(%rsp)
movq 0x110(%r14), %rdi
callq 0x5590
testl %eax, %eax
je 0xb233
movq 0x8(%rsp), %rdi
callq 0x5420
movq 0x110(%r14), %rdi
movq 0x128(%r14), %rsi
xorl %edx, %edx
callq 0x53c0
testl %eax, %eax
je 0xb565
movq 0x128(%r14), %rsi
leaq 0x3d2f(%rip), %rdi      # 0xef51
xorl %eax, %eax
callq 0x5060
movl $0x3, %ebp
jmp 0xb5b6
leaq 0x130(%r14), %r15
movq $0x0, 0x18(%rsp)
movl $0x0, 0x14(%rsp)
movq 0x110(%r14), %rdi
callq 0x5030
movq %rax, 0x128(%r14)
movq 0x8(%rsp), %rdi
movq 0x28(%rsp), %rdx
xorl %esi, %esi
callq 0x51e0
movq 0x110(%r14), %rdx
leaq 0x28(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x5190
testl %eax, %eax
js 0xb589
incq 0x120(%r14)
movq 0x8(%rsp), %rdi
callq 0x5050
movq 0x28(%rsp), %rcx
movq 0x8(%rsp), %rbp
xorl %eax, %eax
testq %rcx, %rcx
je 0xb2c1
cmpb $0xa, (%rbp,%rax)
je 0xb2c1
incq %rax
cmpq %rax, %rcx
jne 0xb2af
movq %rcx, %rax
cmpb $0xa, (%rbp,%rax)
jne 0xb2d2
movb $0x0, (%rbp,%rax)
movq 0x8(%rsp), %rbp
movq %rbp, %rsi
callq 0x53f0
testl %eax, %eax
jle 0xb409
cmpl $0x64, %eax
jne 0xb391
leaq 0x46bc(%rip), %rdi      # 0xf9ae
callq 0x5570
movq 0x8(%rsp), %rbp
movq %rbp, %rdi
callq 0x5150
movb $0xa, (%rbp,%rax)
movq 0x8(%rsp), %rbp
movq 0x120(%r14), %rax
movq %rax, 0x20(%rsp)
movq %r15, %rdi
leaq 0x20(%rsp), %rsi
callq 0x5560
movq %rax, %r12
movq 0x8(%rax), %r13
movq %rbp, %rdi
callq 0x5150
movq %r12, %rdi
xorl %esi, %esi
movq %r13, %rdx
movq %rbp, %rcx
movq %rax, %r8
callq 0x5580
movq 0x120(%r14), %rsi
leaq 0x3b5e(%rip), %rdi      # 0xeeb5
xorl %eax, %eax
callq 0x5060
movq 0x120(%r14), %rsi
movq 0x8(%rsp), %rdx
leaq 0x3a7d(%rip), %rdi      # 0xedee
xorl %eax, %eax
callq 0x5060
movq 0x110(%r14), %rdi
callq 0x5590
testl %eax, %eax
je 0xb24b
jmp 0xb1ed
leaq 0x45b0(%rip), %rdi      # 0xf948
callq 0x5570
cmpl $0x0, 0x14(%rsp)
jne 0xb3c5
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), %rbp
movq %rbp, %rdi
callq 0x5150
cmpq $0x8, %rax
jb 0xb3f8
movl $0x8, %edx
movq %rbp, %rdi
leaq 0x2ead(%rip), %rsi      # 0xe294
callq 0x5490
cmpl $0x1, %eax
movl 0x14(%rsp), %eax
sbbl $-0x1, %eax
jmp 0xb3fc
movl 0x14(%rsp), %eax
movl %eax, 0x14(%rsp)
cmpl $0x2, %eax
jge 0xb5c7
cmpl $0x0, 0x18(%rsp)
je 0xb42a
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x5300
movl $0x1, %eax
movq %rax, 0x18(%rsp)
jmp 0xb4b6
leaq 0x45a9(%rip), %rdi      # 0xf9da
callq 0x5570
movq 0x8(%rsp), %rsi
movq %rbx, %rdi
callq 0x5450
testl %eax, %eax
js 0xb4da
movq $0x0, 0x18(%rsp)
leaq 0x3a81(%rip), %rdi      # 0xeedc
movl %eax, %esi
xorl %eax, %eax
callq 0x5060
movq 0x8(%rsp), %r12
movq %r12, %rdi
callq 0x5150
movb $0xa, (%r12,%rax)
movq 0x8(%rsp), %r12
movq 0x120(%r14), %rax
movq %rax, 0x20(%rsp)
movq %r15, %rdi
leaq 0x20(%rsp), %rsi
callq 0x5560
movq %rax, %r13
movq 0x8(%rax), %rbp
movq %r12, %rdi
callq 0x5150
movq %r13, %rdi
xorl %esi, %esi
movq %rbp, %rdx
movq %r12, %rcx
movq %rax, %r8
callq 0x5580
movq 0x110(%r14), %rdi
callq 0x5590
testl %eax, %eax
jne 0xb1ed
cmpl $0x2, 0x14(%rsp)
jl 0xb24b
jmp 0xb1ed
cmpl $-0x64, %eax
jne 0xb5e4
movq 0x120(%r14), %rsi
leaq 0x3a04(%rip), %rdi      # 0xeef5
xorl %eax, %eax
callq 0x5060
movq 0x110(%r14), %rdi
movq 0x128(%r14), %rsi
xorl %edx, %edx
callq 0x53c0
testl %eax, %eax
jne 0xb601
decq 0x120(%r14)
cmpq $0x0, 0xd8(%rbx)
jne 0xb542
movq 0x128(%r14), %rsi
movq %rsi, 0xd8(%rbx)
leaq 0x2d92(%rip), %rdi      # 0xe2cd
xorl %eax, %eax
callq 0x5060
movl $0x5, %ebp
cmpl $0x0, 0x8(%rbx)
jne 0xb5b6
movq %rbx, %rdi
callq 0x54a0
testl %eax, %eax
jne 0xb5b6
movl $0x1, 0x8(%rbx)
jmp 0xb41b
decq 0x120(%r14)
movq 0x180(%r14), %rsi
cmpq 0x188(%r14), %rsi
je 0xb5a0
movq %rbx, (%rsi)
addq $0x8, 0x180(%r14)
jmp 0xb5b4
leaq 0x38ef(%rip), %rdi      # 0xee7f
movl %eax, %esi
xorl %eax, %eax
callq 0x5060
movl $0xfffffffe, %ebp       # imm = 0xFFFFFFFE
jmp 0xb5b6
addq $0x178, %r14            # imm = 0x178
leaq 0x30(%rsp), %rdx
movq %r14, %rdi
callq 0x51a0
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x43ae(%rip), %rdi      # 0xf97c
callq 0x5570
movq 0x110(%r14), %rdi
callq 0x5590
jmp 0xb1ed
leaq 0x440f(%rip), %rdi      # 0xf9fa
callq 0x5570
movq 0x8(%rsp), %rdi
callq 0x5420
movl $0x4, %ebp
jmp 0xb5b6
movq 0x128(%r14), %rsi
leaq 0x3916(%rip), %rdi      # 0xef25
xorl %eax, %eax
callq 0x5060
movq 0x8(%rsp), %rdi
callq 0x5420
jmp 0xb229
movq %rax, %r14
movl $0x140, %esi            # imm = 0x140
movq %rbx, %rdi
callq 0x5380
movq %r14, %rdi
callq 0x5640
nop