// File: nompelis[P]INTecplotLib/intecplot.cpp
// Function: inTec_Zone::HandleKeyword_Varlocation(char const*)
int inTec_Zone::HandleKeyword_Varlocation( const char *string )
{
#ifdef _DEBUG_
   printf(" i Inside HandleKeyword_Varlocation() \n");
#endif
   int iret=1;

   // make a duplicate buffer
   size_t is=0;
   while( string[is] != '\0' && string[is] != '\n' ) ++is;
   char *buf = (char *) malloc(is*sizeof(char));
   if( buf == NULL ) return(-1);

   // shift passed spaces
   memcpy( buf, string, is );
   char *s = buf;
   while( s[0] == ' ' ) s++;
   is = 0;
   while( s[is] != '\0' && s[is] != '\n' ) ++is;

   // the case where all variables are nodal, which is the default
   if( iret != 0 && is >= 5 ) {
      if( strncasecmp( s, "(NODAL)", 7 ) == 0 ) {
         // perhaps do something to mark all variables as nodal
         std::map< int, int > :: iterator it;
         for( it= ivar_loc.begin(); it != ivar_loc.end(); ++it ) {
            (*it).second = 1;
         }
         iret = 0;
#ifdef _DEBUG_
         printf(" --- All variables are nodal \n");
#endif
      }
   }

   // the case where variables are individualluy separated
   int ierror=0;
   if( iret != 0 && is >= 15 ) {    // the check may be different...

      std::map< int, int > itmp_loc;
      std::map< int, int > :: iterator it;
      for(int n=1;n<=num_var;++n) itmp_loc[n] = 1;

#ifdef _DEBUG_
     printf(" --- Parsing variable locations (nodal/cell-centered) \n");
     printf(" --- Number of variables in zone( from file): %d \n", num_var);
#endif
      char *s,*sr;
      char *token;
      const char *delim;

      // first pass is the parentheses and we assume it will not be seen again
#ifdef _DEBUG3_
     printf(" --- Searching for parentheses\n");
#endif
      delim = (const char *) "()";
      token = strtok_r( buf, delim, &sr );
      if( token == NULL ) {
         // this is in error
         printf(" e Malformed varlocation line!\n");
         ierror = 1;
      }
#ifdef _DEBUG3_
      printf(" --- Token in parentheses --->|%s|<---\n",token);
#endif

      int k;
      for( k=0,s=token; ierror==0 ; s=NULL,++k ) {
#ifdef _DEBUG3_
         printf("LOOP START: ");
         printf("S  -->|%s|<-- \n",s);
#endif
         // look for first delimiter (a group before an equal sign)
         delim = (const char *) "= ";
      // printf("DELIM1 -->|%s|<-- \n",delim);
         token = strtok_r( s, delim, &sr );
      // printf("TOKEN1 -->|%s|<-- \n",token);
         // this is the proper termination condition
         if( token == NULL ) break;

         // look for variable numbers
         char *s2=token,*sr2;
         token = strtok_r( s2, "[]", &sr2 );
      // printf("TOKEN+ -->|%s|<-- \n",token);
         int kk;
         for( kk=0,s2=token; ierror==0 ; s2=NULL,++kk ) {
            token = strtok_r( s2, ",", &sr2 );
            if( token == NULL ) break;
      //    printf("TOKEN++ -->|%s|<-- \n", token);

            // look for a minus sign and take action
            int isign=0;
            is=0;
            while( token[is] != '\0' ) {
               if( token[is] == '-' ) {
                  isign = 1;         // indicate this is a range group
                  token[is] = ' ';   // turn sign to a space for easy parsing
               }
               ++is;
            }
      //    printf("TOKEN++ changed as range/number -->|%s|<-- \n", token);

            // parse the number range or variable number
            if( isign == 1 ) {
               int i1=-1,i2=-1;
               is = sscanf( token, "%d %d", &i1,&i2);
#ifdef _DEBUG2_
               printf(" --- Variable range: %d - %d \n", i1,i2);
#endif
               if( is != 2 ) {
#ifdef _DEBUG2_
                  printf(" --- Failed parsing range! \n");
#endif
                  ierror = 2;
                  break;
               } else {
                  if( i1 > num_var || i2 > num_var ||
                      i1 == 0      || i2 == 0 ) {
#ifdef _DEBUG_
                     printf(" --- Variables out of range! \n");
#endif
                     ierror = 20;
                     break;
                  } else {
                     for(int n=1;n<=num_var;++n)
                        if( i1 <= n && n <= i2 ) itmp_loc[n] = 99;
                  }
               }
            } else {
               int i0=-1;
               is = sscanf( token, "%d", &i0);
#ifdef _DEBUG2_
               printf(" --- Variable: %d \n", i0);
#endif
               if( is != 1 ) {
#ifdef _DEBUG2_
                  printf(" --- Failed parsing variable slot! \n");
#endif
                  ierror = 2;
                  break;
               } else {
                  if( i0 > num_var || i0 == 0 ) {
#ifdef _DEBUG_
                     printf(" --- Variable out of range! \n");
#endif
                     ierror = 20;
                     break;
                  } else {
                     itmp_loc[i0] = 99;
                  }
               }
            }
#ifdef _DEBUG3_
            printf(" --- Inner cycling... n");
#endif
         }
         if( ierror != 0 ) break;

         // look for second delimiter (a group before a comma)
         s = NULL;
         delim = (const char *) ",";
      // printf("DELIM2 -->|%s|<-- \n",delim);
         token = strtok_r( s, delim, &sr );
         if( token == NULL ) {
            ierror = 2;
            break;
         }
      // printf("TOKEN2 -->|%s|<-- \n",token);

         // skip the first few spaces if any
         while( token[0] == ' ' || token[0] == '=' ) token++;
      // printf("CLEAN TOKEN2 -->|%s|<-- \n",token);

         // determine the "variable location" based on the second string
         int iloc=0;    // this would eventually result in an error...
         if( strlen( token ) >= 5 ) {
            if( strncasecmp( token, "NODAL", 5 ) == 0 ) iloc = 1;
         }
         if( strlen( token ) >= 12 ) {
            if( strncasecmp( token, "CELLCENTERED", 12 ) == 0 ) iloc = 2;
         }
         for( it= itmp_loc.begin(); it != itmp_loc.end(); ++it ) {
            if( (*it).second == 99 ) (*it).second = iloc;
         }

#ifdef _DEBUG3_
         printf(" --- Outer cycling... n");
#endif
      }

#ifdef _DEBUG3_
      printf(" --- Variable locations (temporary) \n");
#endif
      for( it= itmp_loc.begin(); it != itmp_loc.end(); ++it ) {
#ifdef _DEBUG3_
         printf("      ==> var. %d location %d \n", (*it).first, (*it).second );
#endif
         ivar_loc[ (*it).first ] = (*it).second;
      }

      // drop contents of this array (althought it would have been cleaned)
      itmp_loc.clear();

      // report on errors trapped
      if( ierror == 0 ) {
         iret = 0;
      } else {
         // provide some output
      }

#ifdef _DEBUG2_
      printf(" --- Variable locations \n");
      for( it= ivar_loc.begin(); it != ivar_loc.end(); ++it ) {
         printf("      ==> var. %d location %d \n", (*it).first, (*it).second );
      }
#endif
   }

#ifdef _DEBUG_
   if( iret != 0 ) printf(" --- Problematic VARLOCATION line \n");
#endif

   // drop the buffer
   free( buf );

#ifdef _DEBUG_
   printf(" i Exiting HandleKeyword_Varlocation() \n");
#endif
   return( iret );
}

// Assembly:
subq $0x178, %rsp            # imm = 0x178
movq %rdi, 0x168(%rsp)
movq %rsi, 0x160(%rsp)
movq 0x168(%rsp), %rax
movq %rax, 0x50(%rsp)
leaq 0xfaed(%rip), %rdi      # 0x286a8
movb $0x0, %al
callq 0x13070
movl $0x1, 0x15c(%rsp)
movq $0x0, 0x150(%rsp)
movq 0x160(%rsp), %rax
movq 0x150(%rsp), %rcx
movsbl (%rax,%rcx), %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, 0x4f(%rsp)
je 0x18c16
movq 0x160(%rsp), %rax
movq 0x150(%rsp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0xa, %eax
setne %al
movb %al, 0x4f(%rsp)
movb 0x4f(%rsp), %al
testb $0x1, %al
jne 0x18c20
jmp 0x18c36
movq 0x150(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x150(%rsp)
jmp 0x18bd9
movq 0x150(%rsp), %rdi
shlq $0x0, %rdi
callq 0x145f0
movq %rax, 0x148(%rsp)
cmpq $0x0, 0x148(%rsp)
jne 0x18c6a
movl $0xffffffff, 0x174(%rsp) # imm = 0xFFFFFFFF
jmp 0x195e2
movq 0x148(%rsp), %rdi
movq 0x160(%rsp), %rsi
movq 0x150(%rsp), %rdx
callq 0x13c70
movq 0x148(%rsp), %rax
movq %rax, 0x140(%rsp)
movq 0x140(%rsp), %rax
movsbl (%rax), %eax
cmpl $0x20, %eax
jne 0x18cbd
movq 0x140(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x140(%rsp)
jmp 0x18c97
movq $0x0, 0x150(%rsp)
movq 0x140(%rsp), %rax
movq 0x150(%rsp), %rcx
movsbl (%rax,%rcx), %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, 0x4e(%rsp)
je 0x18d06
movq 0x140(%rsp), %rax
movq 0x150(%rsp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0xa, %eax
setne %al
movb %al, 0x4e(%rsp)
movb 0x4e(%rsp), %al
testb $0x1, %al
jne 0x18d10
jmp 0x18d26
movq 0x150(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x150(%rsp)
jmp 0x18cc9
cmpl $0x0, 0x15c(%rsp)
je 0x18e07
cmpq $0x5, 0x150(%rsp)
jb 0x18e07
movq 0x140(%rsp), %rdi
leaq 0xf42e(%rip), %rsi      # 0x28180
movl $0x7, %edx
callq 0x144c0
cmpl $0x0, %eax
jne 0x18e05
leaq 0x138(%rsp), %rdi
callq 0x14e30
movq 0x50(%rsp), %rdi
addq $0x58, %rdi
callq 0x14e00
movq %rax, 0x130(%rsp)
movq 0x130(%rsp), %rax
movq %rax, 0x138(%rsp)
movq 0x50(%rsp), %rdi
addq $0x58, %rdi
callq 0x141e0
movq %rax, 0x128(%rsp)
leaq 0x138(%rsp), %rdi
leaq 0x128(%rsp), %rsi
callq 0x14950
testb $0x1, %al
jne 0x18dc9
jmp 0x18dec
leaq 0x138(%rsp), %rdi
callq 0x14290
movl $0x1, 0x4(%rax)
leaq 0x138(%rsp), %rdi
callq 0x13940
jmp 0x18d98
movl $0x0, 0x15c(%rsp)
leaq 0xf8d2(%rip), %rdi      # 0x286d0
movb $0x0, %al
callq 0x13070
jmp 0x18e07
movl $0x0, 0x124(%rsp)
cmpl $0x0, 0x15c(%rsp)
je 0x195a1
cmpq $0xf, 0x150(%rsp)
jb 0x195a1
leaq 0xf0(%rsp), %rdi
callq 0x14610
leaq 0xe8(%rsp), %rdi
callq 0x14e30
movl $0x1, 0xe4(%rsp)
movq 0x50(%rsp), %rcx
movl 0xe4(%rsp), %eax
cmpl 0x20(%rcx), %eax
jg 0x18ec5
leaq 0xf0(%rsp), %rdi
leaq 0xe4(%rsp), %rsi
callq 0x138d0
movq %rax, 0x40(%rsp)
jmp 0x18e81
movq 0x40(%rsp), %rax
movl $0x1, (%rax)
movl 0xe4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xe4(%rsp)
jmp 0x18e54
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xd8(%rsp)
movl %eax, 0xd4(%rsp)
leaq 0xf0(%rsp), %rdi
callq 0x14c80
jmp 0x195f1
leaq 0xf823(%rip), %rdi      # 0x286ef
xorl %eax, %eax
callq 0x13070
jmp 0x18ed5
movq 0x50(%rsp), %rax
movl 0x20(%rax), %esi
leaq 0xf843(%rip), %rdi      # 0x28727
xorl %eax, %eax
callq 0x13070
jmp 0x18eed
leaq 0xf866(%rip), %rax      # 0x2875a
movq %rax, 0xb0(%rsp)
movq 0x148(%rsp), %rdi
movq 0xb0(%rsp), %rsi
leaq 0xc0(%rsp), %rdx
callq 0x14de0
movq %rax, 0xb8(%rsp)
cmpq $0x0, 0xb8(%rsp)
jne 0x18f47
leaq 0xf82a(%rip), %rdi      # 0x2875d
xorl %eax, %eax
callq 0x13070
jmp 0x18f3c
movl $0x1, 0x124(%rsp)
movl $0x0, 0xac(%rsp)
movq 0xb8(%rsp), %rax
movq %rax, 0xc8(%rsp)
cmpl $0x0, 0x124(%rsp)
jne 0x194c0
leaq 0xfebb(%rip), %rax      # 0x28e32
movq %rax, 0xb0(%rsp)
movq 0xc8(%rsp), %rdi
movq 0xb0(%rsp), %rsi
leaq 0xc0(%rsp), %rdx
callq 0x14de0
movq %rax, 0xb8(%rsp)
cmpq $0x0, 0xb8(%rsp)
jne 0x18fb4
jmp 0x194c0
movq 0xb8(%rsp), %rax
movq %rax, 0xa0(%rsp)
movq 0xa0(%rsp), %rdi
leaq 0xf7aa(%rip), %rsi      # 0x2877d
leaq 0x98(%rsp), %rdx
callq 0x14de0
movq %rax, 0xb8(%rsp)
movl $0x0, 0x94(%rsp)
movq 0xb8(%rsp), %rax
movq %rax, 0xa0(%rsp)
cmpl $0x0, 0x124(%rsp)
jne 0x192de
movq 0xa0(%rsp), %rdi
leaq 0xfcfd(%rip), %rsi      # 0x28d1d
leaq 0x98(%rsp), %rdx
callq 0x14de0
movq %rax, 0xb8(%rsp)
cmpq $0x0, 0xb8(%rsp)
jne 0x19045
jmp 0x192de
movl $0x0, 0x90(%rsp)
movq $0x0, 0x150(%rsp)
movq 0xb8(%rsp), %rax
movq 0x150(%rsp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x0, %eax
je 0x190c3
movq 0xb8(%rsp), %rax
movq 0x150(%rsp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x2d, %eax
jne 0x190ad
movl $0x1, 0x90(%rsp)
movq 0xb8(%rsp), %rax
movq 0x150(%rsp), %rcx
movb $0x20, (%rax,%rcx)
movq 0x150(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x150(%rsp)
jmp 0x1905c
cmpl $0x1, 0x90(%rsp)
jne 0x19209
movl $0xffffffff, 0x8c(%rsp) # imm = 0xFFFFFFFF
movl $0xffffffff, 0x88(%rsp) # imm = 0xFFFFFFFF
movq 0xb8(%rsp), %rdi
leaq 0xf68a(%rip), %rsi      # 0x28780
leaq 0x8c(%rsp), %rdx
leaq 0x88(%rsp), %rcx
movb $0x0, %al
callq 0x13f40
cltq
movq %rax, 0x150(%rsp)
cmpq $0x2, 0x150(%rsp)
je 0x19132
movl $0x2, 0x124(%rsp)
jmp 0x192de
movq 0x50(%rsp), %rcx
movl 0x8c(%rsp), %eax
cmpl 0x20(%rcx), %eax
jg 0x19168
movq 0x50(%rsp), %rcx
movl 0x88(%rsp), %eax
cmpl 0x20(%rcx), %eax
jg 0x19168
cmpl $0x0, 0x8c(%rsp)
je 0x19168
cmpl $0x0, 0x88(%rsp)
jne 0x19188
leaq 0xf617(%rip), %rdi      # 0x28786
xorl %eax, %eax
callq 0x13070
jmp 0x19178
movl $0x14, 0x124(%rsp)
jmp 0x192de
movl $0x1, 0x84(%rsp)
movq 0x50(%rsp), %rcx
movl 0x84(%rsp), %eax
cmpl 0x20(%rcx), %eax
jg 0x19200
movl 0x8c(%rsp), %eax
cmpl 0x84(%rsp), %eax
jg 0x191eb
movl 0x84(%rsp), %eax
cmpl 0x88(%rsp), %eax
jg 0x191eb
leaq 0xf0(%rsp), %rdi
leaq 0x84(%rsp), %rsi
callq 0x138d0
movq %rax, 0x38(%rsp)
jmp 0x191e0
movq 0x38(%rsp), %rax
movl $0x63, (%rax)
jmp 0x191ed
movl 0x84(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x84(%rsp)
jmp 0x19193
jmp 0x19202
jmp 0x19204
jmp 0x192ba
movl $0xffffffff, 0x80(%rsp) # imm = 0xFFFFFFFF
movq 0xb8(%rsp), %rdi
leaq 0xf560(%rip), %rsi      # 0x28783
leaq 0x80(%rsp), %rdx
movb $0x0, %al
callq 0x13f40
cltq
movq %rax, 0x150(%rsp)
cmpq $0x1, 0x150(%rsp)
je 0x19257
movl $0x2, 0x124(%rsp)
jmp 0x192de
movq 0x50(%rsp), %rcx
movl 0x80(%rsp), %eax
cmpl 0x20(%rcx), %eax
jg 0x19272
cmpl $0x0, 0x80(%rsp)
jne 0x1928f
leaq 0xf52c(%rip), %rdi      # 0x287a5
xorl %eax, %eax
callq 0x13070
jmp 0x19282
movl $0x14, 0x124(%rsp)
jmp 0x192de
leaq 0xf0(%rsp), %rdi
leaq 0x80(%rsp), %rsi
callq 0x138d0
movq %rax, 0x30(%rsp)
jmp 0x192ab
movq 0x30(%rsp), %rax
movl $0x63, (%rax)
jmp 0x192b8
jmp 0x192ba
jmp 0x192bc
movq $0x0, 0xa0(%rsp)
movl 0x94(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x94(%rsp)
jmp 0x19003
cmpl $0x0, 0x124(%rsp)
je 0x192ed
jmp 0x194c0
movq $0x0, 0xc8(%rsp)
leaq 0xfa1d(%rip), %rax      # 0x28d1d
movq %rax, 0xb0(%rsp)
movq 0xc8(%rsp), %rdi
movq 0xb0(%rsp), %rsi
leaq 0xc0(%rsp), %rdx
callq 0x14de0
movq %rax, 0xb8(%rsp)
cmpq $0x0, 0xb8(%rsp)
jne 0x19348
movl $0x2, 0x124(%rsp)
jmp 0x194c0
jmp 0x1934a
movq 0xb8(%rsp), %rax
movsbl (%rax), %ecx
movb $0x1, %al
cmpl $0x20, %ecx
movb %al, 0x2f(%rsp)
je 0x19375
movq 0xb8(%rsp), %rax
movsbl (%rax), %eax
cmpl $0x3d, %eax
sete %al
movb %al, 0x2f(%rsp)
movb 0x2f(%rsp), %al
testb $0x1, %al
jne 0x1937f
jmp 0x19395
movq 0xb8(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xb8(%rsp)
jmp 0x1934a
movl $0x0, 0x7c(%rsp)
movq 0xb8(%rsp), %rdi
callq 0x13540
cmpq $0x5, %rax
jb 0x193d8
movq 0xb8(%rsp), %rdi
leaq 0xf404(%rip), %rsi      # 0x287c3
movl $0x5, %edx
callq 0x144c0
cmpl $0x0, %eax
jne 0x193d6
movl $0x1, 0x7c(%rsp)
jmp 0x193d8
movq 0xb8(%rsp), %rdi
callq 0x13540
cmpq $0xc, %rax
jb 0x19413
movq 0xb8(%rsp), %rdi
leaq 0xf3cf(%rip), %rsi      # 0x287c9
movl $0xc, %edx
callq 0x144c0
cmpl $0x0, %eax
jne 0x19411
movl $0x2, 0x7c(%rsp)
jmp 0x19413
leaq 0xf0(%rsp), %rdi
callq 0x14e00
movq %rax, 0x70(%rsp)
movq 0x70(%rsp), %rax
movq %rax, 0xe8(%rsp)
leaq 0xf0(%rsp), %rdi
callq 0x141e0
movq %rax, 0x68(%rsp)
leaq 0xe8(%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0x14950
testb $0x1, %al
jne 0x1945c
jmp 0x1949c
leaq 0xe8(%rsp), %rdi
callq 0x14290
cmpl $0x63, 0x4(%rax)
jne 0x1948b
movl 0x7c(%rsp), %eax
movl %eax, 0x28(%rsp)
leaq 0xe8(%rsp), %rdi
callq 0x14290
movl 0x28(%rsp), %ecx
movl %ecx, 0x4(%rax)
jmp 0x1948d
leaq 0xe8(%rsp), %rdi
callq 0x13940
jmp 0x19432
jmp 0x1949e
movq $0x0, 0xc8(%rsp)
movl 0xac(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xac(%rsp)
jmp 0x18f62
leaq 0xf0(%rsp), %rdi
callq 0x14e00
movq %rax, 0x60(%rsp)
movq 0x60(%rsp), %rax
movq %rax, 0xe8(%rsp)
leaq 0xf0(%rsp), %rdi
callq 0x141e0
movq %rax, 0x58(%rsp)
leaq 0xe8(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0x14950
testb $0x1, %al
jne 0x19509
jmp 0x1956e
leaq 0xe8(%rsp), %rdi
movq %rdi, 0x8(%rsp)
callq 0x14290
movq 0x8(%rsp), %rdi
movq %rax, %rcx
movq 0x50(%rsp), %rax
movl 0x4(%rcx), %ecx
movl %ecx, 0x14(%rsp)
addq $0x58, %rax
movq %rax, 0x18(%rsp)
callq 0x14290
movq 0x18(%rsp), %rdi
movq %rax, %rsi
callq 0x138d0
movq %rax, 0x20(%rsp)
jmp 0x19551
movq 0x20(%rsp), %rax
movl 0x14(%rsp), %ecx
movl %ecx, (%rax)
leaq 0xe8(%rsp), %rdi
callq 0x13940
jmp 0x194df
leaq 0xf0(%rsp), %rdi
callq 0x132b0
cmpl $0x0, 0x124(%rsp)
jne 0x19592
movl $0x0, 0x15c(%rsp)
jmp 0x19594
jmp 0x19594
leaq 0xf0(%rsp), %rdi
callq 0x14c80
cmpl $0x0, 0x15c(%rsp)
je 0x195b9
leaq 0xf224(%rip), %rdi      # 0x287d6
movb $0x0, %al
callq 0x13070
movq 0x148(%rsp), %rdi
callq 0x14380
leaq 0xf22d(%rip), %rdi      # 0x287fa
movb $0x0, %al
callq 0x13070
movl 0x15c(%rsp), %eax
movl %eax, 0x174(%rsp)
movl 0x174(%rsp), %eax
addq $0x178, %rsp            # imm = 0x178
retq
movq 0xd8(%rsp), %rdi
callq 0x14d20
nop