/**
	This method is used to read the state parameters from a file
*/
void SimBiConState::readState(FILE* f, int offset){
	if (f == NULL)
		throwError("File pointer is NULL - cannot read gain coefficients!!");

	//have a temporary buffer used to read the file line by line...
	char buffer[200];
	Trajectory* tempTraj;


	//this is where it happens.
	while (!feof(f)){
		//get a line from the file...
		fgets(buffer, 200, f);
		if (strlen(buffer)>195)
			throwError("The input file contains a line that is longer than ~200 characters - not allowed");
		char *line = lTrim(buffer);
		int lineType = getConLineType(line);
		switch (lineType) {
			case CON_STATE_END:
				//we're done...
				return;
				break;
			case CON_NEXT_STATE:
				if (sscanf(line, "%d", &this->nextStateIndex) != 1)
					throwError("An index must be specified when using the \'nextState\' keyword");
				this->nextStateIndex += offset;
				break;
			case CON_STATE_DESCRIPTION:
				strcpy(this->description, trim(line));
				break;
			case CON_STATE_TIME:
				if (sscanf(line, "%lf", &stateTime)!=1)
					throwError("The time that is expected to be spent in this state needs to be provided.");
				break;
			case CON_STATE_STANCE:
				reverseStance = false;
				keepStance = false;
				if (strncmp(trim(line), "left",4) == 0)
					stateStance = LEFT_STANCE;
				else
					if (strncmp(trim(line), "right", 5) == 0)
						stateStance = RIGHT_STANCE;
					else
						if (strncmp(trim(line), "reverse", 7) == 0)
							reverseStance = true;
						else if (strncmp(trim(line), "same", 4) == 0)
								keepStance = true;
							else
								throwError("When using the \'stateStance\' keyword, \'left\', \'right\' or \'reverse\' must be specified.");
				break;
			case CON_TRANSITION_ON:
				transitionOnFootContact = false;
				if (strncmp(trim(line), "footDown", 8) == 0)
					transitionOnFootContact = true;
				else
					if (strncmp(trim(line), "timeUp", 6) == 0)
						//nothn' to do, since this is the default
						;
					else
						throwError("When using the \'transitionOn\' keyword, \'footDown\' or \'timeUp\' must be specified.");
				break;
			case CON_TRAJECTORY_START:
				//create a new trajectory, and read its information from the file
				tempTraj = new Trajectory();
				strcpy(tempTraj->jName, trim(line));
				tempTraj->readTrajectory(f);
				this->sTraj.push_back(tempTraj);
				break;

			case CON_D_TRAJX_START:
				if( dTrajX != NULL )
					throwError( "Two dTrajX trajectory, this is illegal!" );
				dTrajX = new Trajectory1D();
				readTrajectory1D( f, *dTrajX, CON_D_TRAJX_END );
				break;

			case CON_D_TRAJZ_START:
				if( dTrajZ != NULL )
					throwError( "Two dTrajZ trajectory, this is illegal!" );
				dTrajZ = new Trajectory1D();
				readTrajectory1D( f, *dTrajZ, CON_D_TRAJZ_END );
				break;

			case CON_V_TRAJX_START:
				if( vTrajX != NULL )
					throwError( "Two vTrajX trajectory, this is illegal!" );
				vTrajX = new Trajectory1D();
				readTrajectory1D( f, *vTrajX, CON_V_TRAJX_END );
				break;

			case CON_V_TRAJZ_START:
				if( vTrajZ != NULL )
					throwError( "Two vTrajZ trajectory, this is illegal!" );
				vTrajZ = new Trajectory1D();
				readTrajectory1D( f, *vTrajZ, CON_V_TRAJZ_END );
				break;

			case CON_COMMENT:
				break;


			case CON_NOT_IMPORTANT:
				tprintf("Ignoring input line: \'%s\'\n", line);
				break;


			default:
				throwError("Incorrect SIMBICON input file: \'%s\' - unexpected line.", buffer);
		}
	}
	throwError("Incorrect SIMBICON input file: No \'/State\' found", buffer);
}
/**
	This method is used to read the state parameters from a file
*/
void SimBiConState::readState(FILE* f, int offset){

    //have a temporary buffer used to read the file line by line...
    char buffer[200];
    Trajectory* tempTraj;


    //this is where it happens.
    while (!feof(f)){
        //get a line from the file...
        fgets(buffer, 200, f);

        char *line = lTrim(buffer);
        int lineType = getConLineType(line);
        switch (lineType) {
        case CON_STATE_END:
            //we're done...
            return;
            break;
        case CON_NEXT_STATE:
            if (sscanf(line, "%d", &this->nextStateIndex) != 1)
                return;
            this->nextStateIndex += offset;
            break;
        case CON_STATE_DESCRIPTION:
            strcpy(this->description, trim(line));
            break;
        case CON_STATE_TIME:
            if (sscanf(line, "%lf", &stateTime)!=1)
                return;
            break;
        case CON_STATE_STANCE:
            reverseStance = false;
            keepStance = false;
            if (strncmp(trim(line), "left",4) == 0)
                stateStance = LEFT_STANCE;
            else
                if (strncmp(trim(line), "right", 5) == 0)
                    stateStance = RIGHT_STANCE;
                else
                    if (strncmp(trim(line), "reverse", 7) == 0)
                        reverseStance = true;
                    else if (strncmp(trim(line), "same", 4) == 0)
                        keepStance = true;
                    else
                        return;
                    break;
        case CON_TRANSITION_ON:
            transitionOnFootContact = false;
            if (strncmp(trim(line), "footDown", 8) == 0)
                transitionOnFootContact = true;
            else
                if (strncmp(trim(line), "timeUp", 6) == 0)
                    //nothn' to do, since this is the default
                        ;
                else
                    return;
            break;
        case CON_TRAJECTORY_START:
            //create a new trajectory, and read its information from the file
            tempTraj = new Trajectory();
            strcpy(tempTraj->jName, trim(line));
            tempTraj->readTrajectory(f);
            this->sTraj.push_back(tempTraj);
            break;

        case CON_D_TRAJX_START:
            if( dTrajX != NULL )
                return;
            dTrajX = new Trajectory1D();
            readTrajectory1D( f, *dTrajX, CON_D_TRAJX_END );
            break;

        case CON_D_TRAJZ_START:
            if( dTrajZ != NULL )
                return;
            dTrajZ = new Trajectory1D();
            readTrajectory1D( f, *dTrajZ, CON_D_TRAJZ_END );
            break;

        case CON_V_TRAJX_START:
            if( vTrajX != NULL )
                return;
            vTrajX = new Trajectory1D();
            readTrajectory1D( f, *vTrajX, CON_V_TRAJX_END );
            break;

        case CON_V_TRAJZ_START:
            if( vTrajZ != NULL )
                return;
            vTrajZ = new Trajectory1D();
            readTrajectory1D( f, *vTrajZ, CON_V_TRAJZ_END );
            break;

        case CON_COMMENT:
            break;


        case CON_NOT_IMPORTANT:

            break;


        default:
            return;
        }
    }
    return;
}