// File: /workspace/github_cpp_makefile_star2_O2/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 $0x28, %rsp
movq %rdi, %rbx
leaq 0x3fdb(%rip), %rdi      # 0x10925
callq 0x77b0
movl $0x140, %edi            # imm = 0x140
callq 0x74d0
movq %rax, %r14
movq %rax, %rdi
movq %rbx, %rsi
callq 0x7710
movq %r14, 0x20(%rsp)
movq 0x128(%rbx), %rsi
cmpq $0x0, 0xd0(%r14)
je 0xc9a7
leaq 0x34a0(%rip), %rdi      # 0xfe24
xorl %eax, %eax
callq 0x7060
leaq 0x4088(%rip), %rdi      # 0x10a1a
callq 0x77b0
movq (%r14), %rax
movq %r14, %rdi
callq *0x8(%rax)
pushq $0x1
jmp 0xccef
movq %rsi, 0xd0(%r14)
movq 0x110(%rbx), %rdi
xorl %edx, %edx
callq 0x7540
testl %eax, %eax
je 0xc9dc
movq 0x128(%rbx), %rsi
leaq 0x347e(%rip), %rdi      # 0xfe4c
xorl %eax, %eax
callq 0x7060
pushq $0x2
jmp 0xccef
decq 0x120(%rbx)
movq %rsp, %rax
andq $0x0, (%rax)
leaq 0x18(%rsp), %rax
andq $0x0, (%rax)
leaq 0x130(%rbx), %r13
movb $0x1, %al
movl %eax, 0xc(%rsp)
pushq $0x5
popq %rbp
xorl %r12d, %r12d
movq 0x110(%rbx), %rdi
callq 0x77c0
testl %eax, %eax
jne 0xcca0
cmpl $0x1, %r12d
jg 0xcca0
movq 0x110(%rbx), %rdi
callq 0x7030
movq %rax, 0x128(%rbx)
movq (%rsp), %rdi
movq 0x18(%rsp), %rdx
xorl %esi, %esi
callq 0x72a0
movq 0x110(%rbx), %rdx
leaq 0x18(%rsp), %rdi
movq %rsp, %rsi
callq 0x7250
testl %eax, %eax
js 0xccdd
incq 0x120(%rbx)
movq (%rsp), %rdi
callq 0x7050
movq 0x18(%rsp), %rax
movq (%rsp), %r15
xorl %ecx, %ecx
cmpq %rcx, %rax
je 0xca8f
cmpb $0xa, (%r15,%rcx)
je 0xca98
incq %rcx
jmp 0xca7e
cmpb $0xa, (%r15,%rax)
je 0xca9b
jmp 0xcaa4
movq %rcx, %rax
movb $0x0, (%r15,%rax)
movq (%rsp), %r15
movq %r15, %rsi
callq 0x7580
testl %eax, %eax
jle 0xcba1
cmpl $0x64, %eax
jne 0xcb31
leaq 0x3eee(%rip), %rdi      # 0x109ae
callq 0x77b0
movq (%rsp), %r15
movq %r15, %rdi
callq 0x71f0
movb $0xa, (%r15,%rax)
movq (%rsp), %r15
movq 0x120(%rbx), %rax
movq %rax, 0x10(%rsp)
movq %r13, %rdi
leaq 0x10(%rsp), %rsi
callq 0x77a0
movq %rax, %rdi
movq %r15, %rsi
callq 0x7990
movq 0x120(%rbx), %rsi
leaq 0x33a9(%rip), %rdi      # 0xfeb5
xorl %eax, %eax
callq 0x7060
movq 0x120(%rbx), %rsi
movq (%rsp), %rdx
leaq 0x32c9(%rip), %rdi      # 0xfdee
xorl %eax, %eax
callq 0x7060
jmp 0xca06
leaq 0x3e10(%rip), %rdi      # 0x10948
callq 0x77b0
testl %r12d, %r12d
jne 0xcb5f
movq (%rsp), %rax
movb $0x20, %cl
movb %cl, (%rax)
movq (%rsp), %rax
movb %cl, 0x1(%rax)
movq (%rsp), %rax
movb %cl, 0x2(%rax)
movq (%rsp), %rax
movb %cl, 0x3(%rax)
movq (%rsp), %r15
movq %r15, %rdi
callq 0x71f0
cmpq $0x8, %rax
jb 0xcb8a
movq %r15, %rdi
leaq 0x2719(%rip), %rsi      # 0xf294
pushq $0x8
popq %rdx
callq 0x7650
cmpl $0x1, %eax
sbbl $-0x1, %r12d
cmpl $0x2, %r12d
jl 0xcba1
leaq 0x3de5(%rip), %rdi      # 0x1097c
callq 0x77b0
jmp 0xca06
testb $0x1, 0xc(%rsp)
jne 0xcbc0
movq %r14, %rdi
movq %r15, %rsi
callq 0x7420
movl $0x0, 0xc(%rsp)
jmp 0xca06
leaq 0x3e13(%rip), %rdi      # 0x109da
callq 0x77b0
movq (%rsp), %rsi
movq %r14, %rdi
callq 0x75f0
testl %eax, %eax
js 0xcc30
leaq 0x32f9(%rip), %rdi      # 0xfedc
movl %eax, %esi
xorl %eax, %eax
callq 0x7060
movq (%rsp), %r15
movq %r15, %rdi
callq 0x71f0
movb $0xa, (%r15,%rax)
movq (%rsp), %r15
movq 0x120(%rbx), %rax
movq %rax, 0x10(%rsp)
movq %r13, %rdi
leaq 0x10(%rsp), %rsi
callq 0x77a0
movq %rax, %rdi
movq %r15, %rsi
callq 0x7990
movb $0x1, %al
movl %eax, 0xc(%rsp)
jmp 0xca06
cmpl $-0x64, %eax
jne 0xcd1e
movq 0x120(%rbx), %rsi
leaq 0x32ae(%rip), %rdi      # 0xfef5
xorl %eax, %eax
callq 0x7060
movq 0x110(%rbx), %rdi
movq 0x128(%rbx), %rsi
xorl %edx, %edx
callq 0x7540
testl %eax, %eax
jne 0xcd37
decq 0x120(%rbx)
movq 0x128(%rbx), %rsi
movq %r14, %rdi
callq 0x7660
movq 0x20(%rsp), %r14
movq %r14, %rdi
callq 0x7280
movl $0x0, 0xc(%rsp)
testl %eax, %eax
je 0xca06
jmp 0xccf0
movq (%rsp), %rdi
callq 0x75c0
movq 0x110(%rbx), %rdi
movq 0x128(%rbx), %rsi
xorl %ebp, %ebp
xorl %edx, %edx
callq 0x7540
testl %eax, %eax
je 0xcd01
movq 0x128(%rbx), %rsi
leaq 0x327f(%rip), %rdi      # 0xff51
xorl %eax, %eax
callq 0x7060
pushq $0x3
jmp 0xccef
leaq 0x319b(%rip), %rdi      # 0xfe7f
movl %eax, %esi
xorl %eax, %eax
callq 0x7060
pushq $-0x2
popq %rbp
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
decq 0x120(%rbx)
addq $0x178, %rbx            # imm = 0x178
leaq 0x20(%rsp), %rsi
movq %rbx, %rdi
callq 0x7140
jmp 0xccf0
leaq 0x3cd5(%rip), %rdi      # 0x109fa
callq 0x77b0
movq (%rsp), %rdi
callq 0x75c0
pushq $0x4
jmp 0xccef
movq 0x128(%rbx), %rsi
leaq 0x31e0(%rip), %rdi      # 0xff25
xorl %eax, %eax
callq 0x7060
movq (%rsp), %rdi
callq 0x75c0
jmp 0xccd9
movq %rax, %rbx
movl $0x140, %esi            # imm = 0x140
movq %r14, %rdi
callq 0x74e0
movq %rbx, %rdi
callq 0x78d0
nop