// File: 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:
subq $0xb8, %rsp
movq %rdi, 0xa8(%rsp)
movq 0xa8(%rsp), %rax
movq %rax, 0x38(%rsp)
leaq 0xa52b(%rip), %rdi      # 0x296ae
xorl %eax, %eax
callq 0x13070
movl $0x140, %edi            # imm = 0x140
callq 0x13fb0
movq 0x38(%rsp), %rsi
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x40(%rsp)
callq 0x146b0
jmp 0x1f1ab
movq 0x40(%rsp), %rax
movq %rax, 0xa0(%rsp)
cmpq $0x0, 0xa0(%rsp)
jne 0x1f209
leaq 0xa508(%rip), %rdi      # 0x296d2
movb $0x0, %al
callq 0x13070
movl $0xffffffff, 0xb4(%rsp) # imm = 0xFFFFFFFF
jmp 0x1f8e2
movq 0x40(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x98(%rsp)
movl %eax, 0x94(%rsp)
movl $0x140, %esi            # imm = 0x140
callq 0x13ff0
jmp 0x1f8f1
movq 0x38(%rsp), %rax
movq 0xa0(%rsp), %rdi
movq 0x128(%rax), %rsi
callq 0x14ed0
movl %eax, 0x90(%rsp)
cmpl $0x0, 0x90(%rsp)
je 0x1f289
movq 0x38(%rsp), %rax
movq 0x128(%rax), %rsi
leaq 0xa4aa(%rip), %rdi      # 0x296f0
movb $0x0, %al
callq 0x13070
leaq 0xa4c4(%rip), %rdi      # 0x29718
movb $0x0, %al
callq 0x13070
movq 0xa0(%rsp), %rax
movq %rax, 0x30(%rsp)
cmpq $0x0, %rax
je 0x1f279
movq 0x30(%rsp), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
movl $0x1, 0xb4(%rsp)
jmp 0x1f8e2
movq 0x38(%rsp), %rax
movq 0x110(%rax), %rdi
movq 0x128(%rax), %rsi
xorl %edx, %edx
callq 0x141f0
movl %eax, 0x90(%rsp)
cmpl $0x0, 0x90(%rsp)
je 0x1f2de
movq 0x38(%rsp), %rax
movq 0x128(%rax), %rsi
leaq 0xa48b(%rip), %rdi      # 0x29752
movb $0x0, %al
callq 0x13070
movl $0x2, 0xb4(%rsp)
jmp 0x1f8e2
movq 0x38(%rsp), %rax
movq 0x120(%rax), %rcx
addq $-0x1, %rcx
movq %rcx, 0x120(%rax)
movq $0x0, 0x88(%rsp)
movq $0x0, 0x80(%rsp)
movl $0x0, 0x7c(%rsp)
movl $0x0, 0x78(%rsp)
movq 0x38(%rsp), %rax
movq 0x110(%rax), %rdi
callq 0x14990
movl %eax, %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, 0x2f(%rsp)
jne 0x1f347
cmpl $0x1, 0x7c(%rsp)
setle %al
movb %al, 0x2f(%rsp)
movb 0x2f(%rsp), %al
testb $0x1, %al
jne 0x1f354
jmp 0x1f84d
movq 0x38(%rsp), %rax
movq 0x110(%rax), %rdi
callq 0x13030
movq %rax, %rcx
movq 0x38(%rsp), %rax
movq %rcx, 0x128(%rax)
movq 0x88(%rsp), %rdi
movq 0x80(%rsp), %rdx
xorl %esi, %esi
callq 0x13770
movq 0x38(%rsp), %rax
movq 0x110(%rax), %rdx
leaq 0x80(%rsp), %rdi
leaq 0x88(%rsp), %rsi
callq 0x136b0
movl %eax, 0x74(%rsp)
cmpl $0x0, 0x74(%rsp)
jge 0x1f3d9
movl 0x74(%rsp), %esi
leaq 0xa3c3(%rip), %rdi      # 0x29785
movb $0x0, %al
callq 0x13070
movl $0xfffffffe, 0xb4(%rsp) # imm = 0xFFFFFFFE
jmp 0x1f8e2
movq 0x38(%rsp), %rax
movq 0x120(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x120(%rax)
movq 0x88(%rsp), %rdi
callq 0x13060
movq $0x0, 0x68(%rsp)
movq 0x68(%rsp), %rcx
xorl %eax, %eax
cmpq 0x80(%rsp), %rcx
movb %al, 0x2e(%rsp)
jae 0x1f436
movq 0x88(%rsp), %rax
movq 0x68(%rsp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0xa, %eax
setne %al
movb %al, 0x2e(%rsp)
movb 0x2e(%rsp), %al
testb $0x1, %al
jne 0x1f440
jmp 0x1f450
movq 0x68(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x68(%rsp)
jmp 0x1f406
movq 0x88(%rsp), %rax
movq 0x68(%rsp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0xa, %eax
jne 0x1f477
movq 0x88(%rsp), %rax
movq 0x68(%rsp), %rcx
movb $0x0, (%rax,%rcx)
movq 0x38(%rsp), %rdi
movl $0x1, 0x64(%rsp)
movq 0x88(%rsp), %rsi
callq 0x142b0
movl %eax, 0x90(%rsp)
cmpl $0x0, 0x90(%rsp)
jle 0x1f615
cmpl $0x64, 0x90(%rsp)
jne 0x1f570
leaq 0xa300(%rip), %rdi      # 0x297bb
movb $0x0, %al
callq 0x13070
movl $0x0, 0x64(%rsp)
movq 0x88(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x88(%rsp), %rdi
callq 0x13540
movq 0x18(%rsp), %rcx
movq %rax, %rdx
movq 0x38(%rsp), %rax
movb $0xa, (%rcx,%rdx)
movq 0x88(%rsp), %rcx
movq %rcx, 0x20(%rsp)
movq %rax, %rdi
addq $0x130, %rdi            # imm = 0x130
movq 0x120(%rax), %rax
movq %rax, 0x58(%rsp)
leaq 0x58(%rsp), %rsi
callq 0x148b0
movq 0x20(%rsp), %rsi
movq %rax, %rdi
callq 0x146a0
movq 0x38(%rsp), %rax
movq 0x120(%rax), %rsi
leaq 0xa2a6(%rip), %rdi      # 0x297e8
movb $0x0, %al
callq 0x13070
movq 0x38(%rsp), %rax
movq 0x120(%rax), %rsi
movq 0x88(%rsp), %rdx
leaq 0x9e51(%rip), %rdi      # 0x293b5
movb $0x0, %al
callq 0x13070
jmp 0x1f613
leaq 0xa298(%rip), %rdi      # 0x2980f
movb $0x0, %al
callq 0x13070
cmpl $0x0, 0x7c(%rsp)
jne 0x1f5b4
movq 0x88(%rsp), %rax
movb $0x20, (%rax)
movq 0x88(%rsp), %rax
movb $0x20, 0x1(%rax)
movq 0x88(%rsp), %rax
movb $0x20, 0x2(%rax)
movq 0x88(%rsp), %rax
movb $0x20, 0x3(%rax)
movq 0x88(%rsp), %rdi
callq 0x13540
cmpq $0x8, %rax
jb 0x1f5f4
movq 0x88(%rsp), %rdi
leaq 0x8b85(%rip), %rsi      # 0x2815b
movl $0x8, %edx
callq 0x144c0
cmpl $0x0, %eax
jne 0x1f5e7
jmp 0x1f5f2
movl 0x7c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x7c(%rsp)
jmp 0x1f5f4
cmpl $0x1, 0x7c(%rsp)
jle 0x1f611
movl $0x0, 0x64(%rsp)
leaq 0xa23a(%rip), %rdi      # 0x29844
movb $0x0, %al
callq 0x13070
jmp 0x1f613
jmp 0x1f61d
movl $0x1, 0x64(%rsp)
cmpl $0x1, 0x64(%rsp)
jne 0x1f848
cmpl $0x0, 0x78(%rsp)
jne 0x1f7f3
leaq 0xa23d(%rip), %rdi      # 0x29877
movb $0x0, %al
callq 0x13070
movq 0xa0(%rsp), %rdi
movq 0x88(%rsp), %rsi
callq 0x14400
movl %eax, 0x54(%rsp)
cmpl $0x0, 0x54(%rsp)
jl 0x1f6d8
movl 0x54(%rsp), %esi
leaq 0xa22c(%rip), %rdi      # 0x29898
movb $0x0, %al
callq 0x13070
movq 0x88(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x88(%rsp), %rdi
callq 0x13540
movq 0x8(%rsp), %rcx
movq %rax, %rdx
movq 0x38(%rsp), %rax
movb $0xa, (%rcx,%rdx)
movq 0x88(%rsp), %rcx
movq %rcx, 0x10(%rsp)
movq %rax, %rdi
addq $0x130, %rdi            # imm = 0x130
movq 0x120(%rax), %rax
movq %rax, 0x48(%rsp)
leaq 0x48(%rsp), %rsi
callq 0x148b0
movq 0x10(%rsp), %rsi
movq %rax, %rdi
callq 0x146a0
cmpl $-0x64, 0x54(%rsp)
jne 0x1f7bd
movq 0x38(%rsp), %rax
movl $0x1, 0x78(%rsp)
movq 0x120(%rax), %rsi
leaq 0xa1b3(%rip), %rdi      # 0x298b1
movb $0x0, %al
callq 0x13070
movq 0x38(%rsp), %rax
movq 0x110(%rax), %rdi
movq 0x128(%rax), %rsi
xorl %edx, %edx
callq 0x141f0
movl %eax, 0x90(%rsp)
cmpl $0x0, 0x90(%rsp)
je 0x1f767
movq 0x38(%rsp), %rax
movq 0x128(%rax), %rsi
leaq 0xa19e(%rip), %rdi      # 0x298e1
movb $0x0, %al
callq 0x13070
movq 0x88(%rsp), %rdi
callq 0x14380
movl $0x3, 0xb4(%rsp)
jmp 0x1f8e2
movq 0x38(%rsp), %rax
movq 0x120(%rax), %rcx
addq $-0x1, %rcx
movq %rcx, 0x120(%rax)
movq 0xa0(%rsp), %rdi
movq 0x128(%rax), %rsi
callq 0x144d0
movl %eax, 0x90(%rsp)
movq 0xa0(%rsp), %rdi
callq 0x136e0
cmpl $0x0, %eax
je 0x1f7bb
movl $0x5, 0xb4(%rsp)
jmp 0x1f8e2
jmp 0x1f7f1
cmpl $0x0, 0x54(%rsp)
jge 0x1f7ef
leaq 0xa142(%rip), %rdi      # 0x2990d
movb $0x0, %al
callq 0x13070
movq 0x88(%rsp), %rdi
callq 0x14380
movl $0x4, 0xb4(%rsp)
jmp 0x1f8e2
jmp 0x1f7f1
jmp 0x1f846
movq 0xa0(%rsp), %rdi
movq 0x88(%rsp), %rsi
callq 0x13d60
movl %eax, 0x90(%rsp)
cmpl $0x0, 0x90(%rsp)
jge 0x1f844
leaq 0xa10e(%rip), %rdi      # 0x2992e
movb $0x0, %al
callq 0x13070
movq 0x88(%rsp), %rdi
callq 0x14380
movl $0x5, 0xb4(%rsp)
jmp 0x1f8e2
jmp 0x1f846
jmp 0x1f848
jmp 0x1f31d
movq 0x88(%rsp), %rdi
callq 0x14380
movq 0x38(%rsp), %rax
movq 0x110(%rax), %rdi
movq 0x128(%rax), %rsi
xorl %edx, %edx
callq 0x141f0
movl %eax, 0x90(%rsp)
cmpl $0x0, 0x90(%rsp)
je 0x1f8ac
movq 0x38(%rsp), %rax
movq 0x128(%rax), %rsi
leaq 0xa0b3(%rip), %rdi      # 0x2994b
movb $0x0, %al
callq 0x13070
movl $0x3, 0xb4(%rsp)
jmp 0x1f8e2
movq 0x38(%rsp), %rdi
movq 0x120(%rdi), %rax
addq $-0x1, %rax
movq %rax, 0x120(%rdi)
addq $0x178, %rdi            # imm = 0x178
leaq 0xa0(%rsp), %rsi
callq 0x13390
movl $0x0, 0xb4(%rsp)
movl 0xb4(%rsp), %eax
addq $0xb8, %rsp
retq
movq 0x98(%rsp), %rdi
callq 0x14d20
nop