#include <stdio.h>
#include <string.h>

#include "user.h"

user_store* read_from_stdin(user_store *us){
#define BUF_SIZE 100
  char buf[BUF_SIZE];
  char fname[BUF_SIZE];
  char gname[BUF_SIZE];
  while(1) {
    char *tmp = fgets(buf,BUF_SIZE,stdin);
    /*     printf ("buf: '%s' =>  (%lu) '%c'\n", 
             buf, strlen(buf), 
             buf[strlen(buf)-1]); */
    if (tmp == NULL) { break ; }
    if (buf[strlen(buf)-1]=='\n') {
      sscanf(buf, "%s , %s", fname, gname);
      /*      fprintf(stdout, "Adding: '%s' '%s'\n",
              fname, gname);*/
      us = add_user(us, fname, gname);
    } else {
      fprintf(stderr, "Too long name\nSeems spooky... bailing out\n");
      return us;
    }
  }
  return us;
}


int main() {

  user_store *us = new_user_store();

  /* us = add_user(us, "Diego", "Maradona"); */
  /* us = add_user(us, "Mario", "Kempes"); */

  read_from_stdin(us);

  print_user_store(us);
    
  free_user_store(us);
}
