CSC8501_Advanced_Programming_For_Games / MazeGeneration / FileSystem.cpp
FileSystem.cpp
Raw
#include "FileSystem.h"

bool FileSystem::LoadFile(std::string from, char** &maze, int mazeHeight, int mazeWidth) {
	std::ifstream file;
	std::string temp;

	std::cout << "Loading maze from " << from << "\n";

	file.exceptions(std::ifstream::badbit); 

	try {
		file.open(from.c_str());
		if (!file.is_open()) {
			std::cout << "File does not exist" << "\n";
			return false;
		}
		while (!file.eof()) {
			getline(file, temp);
			if (!temp.empty()) {
				mazeHeight++;
				(temp[0]) ? mazeWidth = temp.size() : mazeWidth = temp.size();
			}
		}

		file.clear();
		file.seekg(0);

		int mH = 0;
		while (!file.eof()) {
			getline(file, temp);
			if (!temp.empty()) {
				for (int i = 0; i < mazeWidth; i++) {
					maze[mH][i] = temp.at(i);
				}
				mH++;
			}
		}
	}
	catch (const std::ifstream::failure& e)	{
		std::cout << "Exception opening/reading file in \"FileSystem::LoadMazeFile\"";
	}

	file.close();
	std::cout << "Loaded maze file" << "\n" << "\n";
	return true;
}

void FileSystem::LoadFile(std::string from, int &mazeHeight, int &mazeWidth){
	std::ifstream file;
	file.exceptions(std::ifstream::badbit);
	std::string temp;

	mazeHeight = 0;

	try {
		file.open(from.c_str());
		if (!file.is_open()) {
			std::cout << "File does not exist" << "\n";
		}
		while (!file.eof()) 	{
			getline(file, temp);
			if (!temp.empty()) {
				mazeHeight++;
				(temp[0]) ? mazeWidth = temp.size() : mazeWidth = temp.size();
			}
		}
	}
	catch (const std::ifstream::failure& e) {
		std::cout << "Exception opening/reading file in \"FileSystem::LoadMazeFileHeightWidth\"";
	}
	file.close();
}

bool FileSystem::SaveMazeFile(std::string filename, char** &maze, uint32_t mazeHeight, uint32_t mazeWidth) {
	std::ofstream file;
	
	if (std::ifstream(filename)) {
		std::cout << "file already exists" << std::endl;
		return false;
	}

	if (!file) {
		std::cout << "file could not be created" << std::endl;
		return false;
	}

	file.open(filename);
	for (int y = 0; y < mazeHeight; y++) {
		for (int x = 0; x < mazeWidth; x++) {
			file << maze[y][x];
			file << ((x == (mazeWidth - 1)) ? "\n" : "");
		}
	}
	file.close();
	std::cout << "Saved maze file" << "\n" << "\n";
	return true;
}

bool FileSystem::IsMazeSolved(char** maze, uint32_t mazeHeight, uint32_t mazeWidth) {
	for (int i = 0; i < mazeHeight; i++) {
		for (int j = 0; j < mazeWidth; j++) {
			if (maze[i][j] == 'o') {
				return true;
			}
		}
	}
	return false;
}

bool FileSystem::LoadFile(std::string from, std::string& to) {
	std::ifstream file;
	std::string temp;

	std::cout << "Loading from text " << from << "\n";

	file.exceptions(std::ifstream::badbit);
	try {
		file.open(from.c_str());
		if (!file.is_open()) {
			std::cout << "File does not exist" << "\n";
			return false;
		}
		while (!file.eof()) {
			getline(file, temp);
			if (!temp.empty())
				to += temp + "\n";
		}

		std::cout << "Loaded file" << "\n" << "\n";
	}
	catch (const std::ifstream::failure& e) {
		std::cout << "Exception opening/reading file in \"FileSystem::LoadFile\"";
	}
	
	file.close();
	return true;
}