#include <Arduino.h>

#define DEBUG 0

#if DEBUG == 1
  #define debug(x) Serial.print(x)
  #define debugln(x) Serial.println(x)
#else
#define debug(x)
#define debugln(x)  
#endif

char colonIndex[10][50];
char commaIndex[10][50];
char lbrIndex[10][50];
char rbrIndex[10][50];


void SetAction() {

  }


char* Parser(char* tempInput, char d, int* ndxptr, int* nxt) {
    // Stores the state of string
    static char* input = NULL;
    // Initialize the input string
    if (tempInput != NULL){
        debugln("tempInput");
        input = tempInput;
        }
    // Case for final token
    if (input == NULL) {
        debugln("input");
        return "\0";
      }
        // Stores the extracted string
        char* result = new char[strlen(input) + 1];
      int i = 0;
    
        // Start extracting string and
        // store it in array
        // If not at the end of the array
        // Keep looking through
      for (; input[i] != '\0'; i++) {
        // If current character is not
        // equal to the delimiting char
        // assign to result
        if ( input[i] != d ) {
          debugln("if");
          result[i] = input[i];
          *ndxptr = *ndxptr + 1;
          *nxt+= 1;
          }
        // Else store the string formed
        // and set t
        else {
          debugln("else");
          result[i] = '\0';
          debugln(input);
          // Moves the pointer of the first
          // char to the next delimiter + 1
          *ndxptr = *ndxptr + 1;
          *nxt = *nxt + 1;
          input = input + i + 1;
          debugln(input);
          return result;
        }
      }
      debugln("end");
      result[i] = '\0';
      input = NULL;
      return result;
    } 
/////
int CompChar(char *a, const char *b) { //Looks for b in a  Found = 1, Not found = 0
    //Searches string "a" for char "b"
    // Returns  int
      // 1 if found
      // 0 if not found
                      debug("CC1");
    static char * input = NULL;
    int result = NULL;

    if(*a != NULL)
    {  
                    debug("CC2");
      input = a;
    }
                    debug("CC3");
    printf("end of input = %c\n", input[3]);
    if(input[2] == NULL) 
    {
      Serial.println(" Yes NULL Terminated.");  
    }
    else if(input[2] == '\0') 
    {
      Serial.println(" Yes, \\0 terminated.");
    }

    int i = 0;
    for(; input[i] != '\0'; i++)
                    debug("CC4");
    {
      if(input != b) 
      {
                    debug("CC5");
        printf("input = %s\n", input);
        input = input + 1;
      }
      else if(input[i] == *b)
      {
                  debug("CC6");
        result = i;
        return result;
      }
      else 
      {
                  debug("CC7");
        return 0;
      }
                  debug("CC8");
    }
                  debug("CC9");
  }
  
  void ReadSerial()
  {
    byte b = 100;
    char ui[b];
    char *d = " ";

    while(Serial.available() > 0)
    {
      size_t b = Serial.readBytesUntil('\n', ui,  strlen(ui) - 1);
      ui[strlen(ui)] = '\0';

      int loc = CompChar(ui, d);
      printf("loc = %i\n", loc);

      ui[0] = '\0';
      printf("ui after clearing: %s\n", ui);
    }
  }

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);

}

void loop() {
  // put your main code here, to run repeatedly:
  delay(10); // this speeds up the simulation

    ReadSerial();
  
  }