/* this program parses input in set format and tells what the elements 
 * and subsets are.
 *
 * The idea is that every thing in a set is an object - sets are objects, 
 * elements are objects, and subsets are merely other sets.  This allows 
 * great versatility in implementing more complex functions, such as 
 * computing the union or intersection of two or more sets.
 * 
 * Element and subset objects are stored in arrays in a set.
 */

#include "sets.h"
using namespace mine;

Set *get_elements(char *, char[], Set *, int, int);   

int main(int argc, char *argv[]) {
  char input[MAX_ELEMENTS];
  Set *set;
  
  /* copy argv into input */
  strcpy(input, argv[1]);
  
  cout <<"input (in main): " << input <<endl;
  /* get the elements */
    set = get_elements("", input, set, 0, 0);
    /*
  cout <<"elements: \n";
  for (int x = 0; x < set->num_elements; x++) {
    cout <<set->elements[x].characters <<endl;
  }
    */
  return 0;
}

/******************************************************
 * get_element gets the elements recursively from the input string.
 * It then parses the string, looking for integers separated by comments
 * and bound in brackets.  If it finds any that fit the criteria, it creates
 * a new Element and puts it in the Element array.  get_element is then called
 * again to parse the next element.
 *
 * char *current is the current string, set is the set we are currently 
 * working on, and brace_count is the number of open braces there are.
 */

Set *get_elements(char *current, char input[], Set *set, int input_counter, int brace_count) {
  char next_char; // the next character in input
  /* get the next character */
  cout <<"input " <<input_counter <<": " <<input[input_counter] <<endl;
  //cout <<"current "<<current<<endl;
  next_char = input[input_counter];
  input_counter++;
  
  /* check to see if there's only one open brace */
  if (next_char == ',') {
    cout <<"next_char is ','\t current is " << current <<"\n";
    Element *element;

    element->copy(current);
    cout <<"Element copied\n";
    set->elements[set->num_elements] = *element;
    set->num_elements++;
    cout <<"element set\n";
    get_elements("", input, set, input_counter, brace_count);
  }
  if (next_char == '{') {
    cout <<"next_char is '{'\n";
    brace_count++;
    get_elements(current, input, set, input_counter, brace_count);
  }
  if (next_char == '}') {
    cout <<"next_char is '}'\n";
    brace_count--;
    Element *element;
    
    element->copy(current);
    cout <<"Element copied.  elements: "<<set->num_elements<<"\n";
    set->elements[set->num_elements] = *element;
    set->num_elements++;
    cout <<"elements: " <<set->num_elements-1<<endl;
    /* when there are no more open braces, get out of the loop */
    if (brace_count == 0) return set;
    else get_elements("", input, set, input_counter, brace_count);
  }
  cout <<"about to concatonate stuff\n";
  /*cout <<"concatonating "<<next_char<<" to " <<current <<" .\n";*/
  if (current == "") {
    current = &next_char;
    cout <<"current: "<<*current<<endl;
    get_elements(current, input, set, input_counter, brace_count);
    }
  else {
    strcat(current, &next_char);
    cout <<next_char <<" concatonated to "<< *current <<"\n";
    get_elements(current, input, set, input_counter, brace_count);
    }
return set;
}



