/******************************************************************************
*	File		-	Version.cpp
*	Author		-	Joey Pollack
*	Date		-	2020/03/06 (y/m/d)
*	Mod Date	-	2021/08/31 (y/m/d)
*	Description	-	Defines the current version of the engine and provides
*					methods to work with the Version struct. Using Semantic
*					Versioning https://semver.org/
*
******************************************************************************/

#include "version.h"
#include <sstream>
#include <LunariumConfig.h>

namespace lunarium
{
	Version Version::GetVersion()
	{
		return { Lunarium_VERSION_MAJOR, Lunarium_VERSION_MINOR, Lunarium_VERSION_PATCH };
	}

	OpRes Version::StringToVersion(const char * str, Version & v)
	{
		std::string part = "";
		const char* pos = str;

		// Major version
		while (*pos != '.')
		{
			if (*pos == 0)
			{
				return OpRes::Fail("StringToVersion -- str was malformed");
			}

			part += *pos;
			pos++;
		}

		v.Major = atoi(part.c_str());


		// Minor version
		part = "";
		pos++;
		while (*pos != '.')
		{
			if (*pos == 0)
			{
				return OpRes::Fail("StringToVersion -- str was malformed");
			}

			part += *pos;
			pos++;
		}

		v.Minor = atoi(part.c_str());

		// Patch version
		part = "";
		pos++;
		while (*pos != 0)
		{
			part += *pos;
			pos++;
		}

		v.Patch = atoi(part.c_str());

		return OpRes::OK();
	}

	std::string Version::ToString()
	{
		std::ostringstream oss;
		oss << Major << "." << Minor << "." << Patch;
		return oss.str();
	}
}