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

/*
char * bad_read_string() {
	// DON'T USE THIS
	char name[MAX_BUFFER_SIZE];
	fgets(name, sizeof(name), stdin);
	return name;
}
*/

void read_string(char * destination, int num_chars_to_read) {
	fgets(destination, num_chars_to_read, stdin);

	// Strip the final new line character
	destination[strcspn(destination, "\n")] = 0;
}

/*
 * This function returns -1 if it fails
 */
char read_char() {
	char return_value;

	char buffer[MAX_BUFFER_SIZE];
	read_string(buffer, MAX_BUFFER_SIZE);

	if (1 != sscanf(buffer, "%c", &return_value)) {
		// couldn't read an integer
		return_value = ERROR;
    	}

	return return_value;
}

/*
 * This function returns -1 if it fails
 */
int read_integer() {
	int return_value;

	char buffer[MAX_BUFFER_SIZE];
	read_string(buffer, MAX_BUFFER_SIZE);
	char garbage[MAX_BUFFER_SIZE]; // this is to collect any garbage AFTER the integer

	if (1 != sscanf(buffer, "%d%s", &return_value, garbage)) {
		// couldn't read an integer
		return_value = ERROR;
    	}
	// printf("Garbage: %s", garbage);

	return return_value;
}
