/* state.c */

#include "state.h"
#include <stdio.h>


/*
 * Initialize state with name etc.
 * *state           - pointer to state to be initialized
 * *stateName       - pointer to char array representing the name of the state
 */
void initState(State *state, char *stateName) {
  state->nofStates = 0;
  strcpy(state->name, stateName);
}



/*
 * Connect the fromState with the toState. Direction is from .. to.
 * If connections are made to several states, connections are numbered
 * starting with 0.
 * *fromState       - pointer to state from where to start when traversing
 * *toState         - pointer to state to where to arrive when traversing
 *
 * return value:      TRUE, if connection could be established, else FALSE
 */
boolean connectStates(State *fromState, State *toState) {
  if (fromState->nofStates == MAX_STATES) {
    return FALSE;
  }

  fromState->arr[fromState->nofStates] = toState;
  fromState->nofStates++;
}



/*
 * Print the name of the state.
 * *state           - pointer to state
 */
void printStateName(State *state) {
  printf("%s\n", state->name);
}



/*
 * Print the name of the state between stars.
 * *state           - pointer to state
 */
void printStateNameBetweenStars(State *state) {
  printf("*%s*\n", state->name);
}



/*
 * Visit several states starting from firstState proceeding to next state
 * according the steering array stateIndicesArray. In arriving at a state
 * (except firstState), the action is performed which *outFctPtr is poining
 * to. Repeat for all array indices.
 * *firstState      - pointer to state where to start visiting (traversing)
 *                    from
 * *stateIndicesArray - array containing indices. Each index steers which
 *                    connection in a state to choose next. Order (and index)
 *                    of connections are determined when connecting states.
 * arrSize          - size of the array stateIndicesArray
 * (*outFctPtr)()   - pointer to function to (output) action when visiting
 *                    a state
 */
void visitStates(State *firstState, int *stateIndicesArr, int arrSize, void (*outFctPtr)()) {
  int i;
  State *nextState;

  nextState = firstState;
  for (i = 0; i < arrSize; ++i) {
    nextState = nextState->arr[stateIndicesArr[i]];
    (* outFctPtr)(nextState);
  }
}
