#include "Stations.h"

#include "stdio.h"


struct Stations *InitStations(){
 struct Stations *s;
  s = (struct Stations*) malloc (sizeof(struct Stations));
  s -> c = 0;
  s -> items = 0;
 return s;
}



int GetStationID(char *s, struct Stations *stat){
 int i;
  for (i=0; i < stat -> c; i++)
   if (strcmp(s, stat -> items[i]) == 0)
     return i;
  return -1;
}


void AddStation(char *s, struct Stations *stat){
 if (GetStationID(s, stat) == -1)
  {
   stat -> c ++;
   stat -> items = (char**) realloc(stat -> items, sizeof(char*)*stat->c);
   stat -> items[stat -> c-1] = (char*) calloc(strlen(s)+3 , sizeof(char));
   strcpy(stat -> items[stat->c-1], s);
   s[strlen(s)] = 0;
  }
}


int ReadStations(char *s, struct Stations *stat){
FILE *f;
int i;
char str1[256], str2[256];

if ((f = fopen(s,"rt")) == NULL) return -1;

 while (!feof(f)) {
  fscanf(f, "%s%s%d",  &str1, &str2, &i);
  AddStation(str1,stat);
  AddStation(str2,stat);
 }

fclose(f);
return 1;
}

void DestStations(struct Stations *stat){
  int i;

   for (i=0; i < stat->c; i++)
     free(stat->items[i]);

   free(stat->items);
  free(stat);   
 }



int TestStations(struct Stations *stat){
  int i;
   for (i=0; i < stat->c; i++)
    if ( stat->items[i][ strlen(stat->items[i]) +2 ] == 0 )
     return 0;
 // printw("    All stations!    ");
 return 1;
 }


void WasStation(struct Stations *stat, int n){
 stat->items[n][ strlen(stat->items[n])+2] = 1;
}

void EscStation(struct Stations *stat, int n){
 stat->items[n][ strlen(stat->items[n])+2 ] = 0;
}

void ClearWasEscStations(struct Stations *stat){
  int i;
   for (i=0; i < stat->c; i++)
    stat->items[i][ strlen(stat->items[i])+2 ] = 0;
}

void PrintWasEscStations(struct Stations *stat){
  int i;
   printw(" [STATIONS:");
   for (i=0; i < stat->c; i++)
    printw("%s%d ", stat->items[i], stat->items[i][ strlen(stat->items[i])+2 ]);
   printw("] ");
}



