<commit_before>#define _CRT_SECURE_NO_WARNINGS

#include "UserActions.h"
#include <fstream>
using std::ifstream;

#include <cstring>

const int MAX_CHAR_COMMAND = 128;
const int MAX_CHARS_PER_LINE = 134; // 128 + 6
const int MAX_TOKENS_PER_LINE = 4;
const char* const DELIMITER = ";";

UserActions::UserActions() : m_mMappings({}) {}

void UserActions::add(std::string name, Command c) {
	if (!m_mMappings.count(name)) {
		std::vector<Command> tempCommands;
		m_mMappings[name] = tempCommands;
	}

	m_mMappings[name].push_back(c);
}

int UserActions::getActionState(std::string name) {
	if (!m_mMappings.count(name)) {
		return 0;
	}

	std::vector<Command> commands = m_mMappings[name];
	int ret = 0;
	InputHandler* handlerInstance = InputHandler::Instance();
	for (unsigned int c = 0; c < commands.size(); c++) {
		switch (commands[c].type) {
			case CONTROLLER_BUTTON:
				ret = !handlerInstance->joysticksInitialised() ? 0 :
					handlerInstance->getButtonState(0, commands[c].buttonId);
				break;
			case CONTROLLER_STICK:
				if (handlerInstance->joysticksInitialised()) {
					int stickValue = handlerInstance->stickValue(
						0, commands[c].stickAxis
					);
					int stickDirection = commands[c].stickDirection;
					if (stickDirection == 0 || stickValue == stickDirection) {
						ret = stickValue;
					}
				}
				break;
			case KEYBOARD_KEY:
				ret = handlerInstance->getKeyState(commands[c].key);
				break;
		}

		if (ret != 0) {
			break;
		}
	}

	return ret;
}

void UserActions::resetActionState(std::string name) {
	if (!m_mMappings.count(name)) {
		return;
	}

	std::vector<Command> commands = m_mMappings[name];
	InputHandler* handlerInstance = InputHandler::Instance();
	for (unsigned int c = 0; c < commands.size(); c++) {
		switch (commands[c].type) {
			case CONTROLLER_BUTTON:
				if (handlerInstance->joysticksInitialised()) {
					handlerInstance->setButtonState(
						0, commands[c].buttonId, false
					);
				}
				break;
			case KEYBOARD_KEY:
				handlerInstance->setKeyState(commands[c].key, false);
				break;
		}
	}
}

int UserActions::setActionsFromFile(const char* mappingFile) {
	// create a file-reading object
	ifstream fin;
	fin.open(mappingFile); // open a file
	if (!fin.good()) {
		return NO_FILE_FOUND; // exit if file not found
	}

	// read each line of the file
	while (!fin.eof()) {
		// read an entire line into memory
		char buf[MAX_CHARS_PER_LINE];
		// @check too long lines
		fin.getline(buf, MAX_CHARS_PER_LINE);

		if (buf[0] == '\0' || buf[0] == '#') {
			continue;
		}

		char commandName[MAX_CHAR_COMMAND];
		char* token;
		InputType type;
		int value, direction;

		// @TODO Check buffer overflow
		token = strtok(buf, DELIMITER);
		strncpy(commandName, token, MAX_CHAR_COMMAND);

		// @TODO check missing values
		token = strtok(0, DELIMITER);
		if (token == 0) {
			return NO_TYPE_FOUND;
		}

		type = (InputType) atoi(token);
		token = strtok(0, DELIMITER);
		if (token == 0) {
			return NO_VALUE_FOUND;
		}

		value = atoi(token);
		token = strtok(0, DELIMITER);

		if (token != 0) {
			direction = atoi(token);
		}

		Command c;
		c.type = type;
		switch (type) {
			case KEYBOARD_KEY:
				c.key = (SDL_Scancode) value;
				break;
			case CONTROLLER_BUTTON:
				c.buttonId = value;
				break;
			case CONTROLLER_STICK:
				c.stickAxis = (JoystickControl) value;
				c.stickDirection = direction;
				break;
			default:
				return INVALID_TYPE;
		}
		add(commandName, c);
	}
}<commit_msg>return 0 in case the parsing went well<commit_after>#define _CRT_SECURE_NO_WARNINGS

#include "UserActions.h"
#include <fstream>
using std::ifstream;

#include <cstring>

const int MAX_CHAR_COMMAND = 128;
const int MAX_CHARS_PER_LINE = 134; // 128 + 6
const int MAX_TOKENS_PER_LINE = 4;
const char* const DELIMITER = ";";

UserActions::UserActions() : m_mMappings({}) {}

void UserActions::add(std::string name, Command c) {
	if (!m_mMappings.count(name)) {
		std::vector<Command> tempCommands;
		m_mMappings[name] = tempCommands;
	}

	m_mMappings[name].push_back(c);
}

int UserActions::getActionState(std::string name) {
	if (!m_mMappings.count(name)) {
		return 0;
	}

	std::vector<Command> commands = m_mMappings[name];
	int ret = 0;
	InputHandler* handlerInstance = InputHandler::Instance();
	for (unsigned int c = 0; c < commands.size(); c++) {
		switch (commands[c].type) {
			case CONTROLLER_BUTTON:
				ret = !handlerInstance->joysticksInitialised() ? 0 :
					handlerInstance->getButtonState(0, commands[c].buttonId);
				break;
			case CONTROLLER_STICK:
				if (handlerInstance->joysticksInitialised()) {
					int stickValue = handlerInstance->stickValue(
						0, commands[c].stickAxis
					);
					int stickDirection = commands[c].stickDirection;
					if (stickDirection == 0 || stickValue == stickDirection) {
						ret = stickValue;
					}
				}
				break;
			case KEYBOARD_KEY:
				ret = handlerInstance->getKeyState(commands[c].key);
				break;
		}

		if (ret != 0) {
			break;
		}
	}

	return ret;
}

void UserActions::resetActionState(std::string name) {
	if (!m_mMappings.count(name)) {
		return;
	}

	std::vector<Command> commands = m_mMappings[name];
	InputHandler* handlerInstance = InputHandler::Instance();
	for (unsigned int c = 0; c < commands.size(); c++) {
		switch (commands[c].type) {
			case CONTROLLER_BUTTON:
				if (handlerInstance->joysticksInitialised()) {
					handlerInstance->setButtonState(
						0, commands[c].buttonId, false
					);
				}
				break;
			case KEYBOARD_KEY:
				handlerInstance->setKeyState(commands[c].key, false);
				break;
		}
	}
}

int UserActions::setActionsFromFile(const char* mappingFile) {
	// create a file-reading object
	ifstream fin;
	fin.open(mappingFile); // open a file
	if (!fin.good()) {
		return NO_FILE_FOUND; // exit if file not found
	}

	// read each line of the file
	while (!fin.eof()) {
		// read an entire line into memory
		char buf[MAX_CHARS_PER_LINE];
		// @check too long lines
		fin.getline(buf, MAX_CHARS_PER_LINE);

		if (buf[0] == '\0' || buf[0] == '#') {
			continue;
		}

		char commandName[MAX_CHAR_COMMAND];
		char* token;
		InputType type;
		int value, direction;

		// @TODO Check buffer overflow
		token = strtok(buf, DELIMITER);
		strncpy(commandName, token, MAX_CHAR_COMMAND);

		// @TODO check missing values
		token = strtok(0, DELIMITER);
		if (token == 0) {
			return NO_TYPE_FOUND;
		}

		type = (InputType) atoi(token);
		token = strtok(0, DELIMITER);
		if (token == 0) {
			return NO_VALUE_FOUND;
		}

		value = atoi(token);
		token = strtok(0, DELIMITER);

		if (token != 0) {
			direction = atoi(token);
		}

		Command c;
		c.type = type;
		switch (type) {
			case KEYBOARD_KEY:
				c.key = (SDL_Scancode) value;
				break;
			case CONTROLLER_BUTTON:
				c.buttonId = value;
				break;
			case CONTROLLER_STICK:
				c.stickAxis = (JoystickControl) value;
				c.stickDirection = direction;
				break;
			default:
				return INVALID_TYPE;
		}
		add(commandName, c);
	}

	return 0;
}
{'fraction_non_alphanumeric': 0.13487836476176768, 'fraction_numerical': 0.0089247157046207, 'mean_word_length': 3.6693548387096775, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6051052', 'n_tokens_mistral': 2903, 'n_tokens_neox': 2449, 'n_words': 559}