<commit_before>#include <string>
#include <stdlib.h>
#include <sstream>

#include "../USB.h"
#include "../configuration.h"

extern HINSTANCE hInst;
std::wstring IniDir;
std::wstring LogDir;

void CALLBACK USBsetSettingsDir( const char* dir )
{
	OSDebugOut(L"USBsetSettingsDir: %S\n", dir);
	wchar_t dst[4096] = {0};
	size_t num = 0;
	mbstowcs_s(&num, dst, dir, ARRAYSIZE(dst));
	IniDir = dst;
}

void CALLBACK USBsetLogDir( const char* dir )
{
	OSDebugOut(L"USBsetLogDir: %S\n", dir);
	wchar_t dst[4096] = {0};
	size_t num = 0;
	mbstowcs_s(&num, dst, dir, ARRAYSIZE(dst));
	LogDir = dst;
}

//TODO Use \\?\ to go past 260 char limit
void GetIniFile(std::wstring &iniFile)
{
	iniFile.clear();
	if(!IniDir.length()) {
		WCHAR tmp[MAX_PATH] = {0};
		GetModuleFileName(GetModuleHandle((LPWSTR)hInst), tmp, MAX_PATH);

		std::wstring path(tmp);
		unsigned last = path.find_last_of(L"\\");
		iniFile = path.substr(0, last);
		iniFile.append(L"\\inis");
		CreateDirectory(iniFile.c_str(), NULL);
		iniFile.append(L"\\USBqemu-wheel.ini");
	} else {
		iniFile.append(IniDir);
		iniFile.append(L"USBqemu-wheel.ini");
	}
}


template<typename T>
bool LoadSettingValue(const std::wstring& ini, const std::wstring& section, const wchar_t* param, T& value);
template<typename T>
bool SaveSettingValue(const std::wstring& ini, const std::wstring& section, const wchar_t* param, T& value);

template<>
bool LoadSettingValue(const std::wstring& ini, const std::wstring& section, const wchar_t* param, std::string& value)
{
	wchar_t tmp[4096] = { 0 };
	if (!GetPrivateProfileStringW(section.c_str(), param, NULL, tmp, sizeof(tmp) / sizeof(*tmp), ini.c_str()))
		return false;
	if (GetLastError() == ERROR_FILE_NOT_FOUND)
		return false;

	char tmpA[4096] = { 0 };
	size_t num = 0;
	wcstombs_s(&num, tmpA, tmp, sizeof(tmpA)); //TODO error-check
	value = tmpA;
	return true;
}

template<>
bool LoadSettingValue(const std::wstring& ini, const std::wstring& section, const wchar_t* param, std::wstring& value)
{
	wchar_t tmp[4096] = { 0 };
	if (!GetPrivateProfileStringW(section.c_str(), param, NULL, tmp, sizeof(tmp) / sizeof(*tmp), ini.c_str()))
		return false;
	if (GetLastError() == ERROR_FILE_NOT_FOUND)
		return false;
	value = tmp;
	return true;
}

template<>
bool LoadSettingValue(const std::wstring& ini, const std::wstring& section, const wchar_t* param, int64_t& value)
{
	//value = GetPrivateProfileIntW(section.c_str(), param, 0, ini.c_str());
	wchar_t tmp[4096] = { 0 };
	if (!GetPrivateProfileStringW(section.c_str(), param, NULL, tmp, sizeof(tmp) / sizeof(*tmp), ini.c_str()))
		return false;
	if (GetLastError() == ERROR_FILE_NOT_FOUND)
		return false;
	value = wcstoul(tmp, NULL, 10);
	return true;
}

template<>
bool SaveSettingValue(const std::wstring& ini, const std::wstring& section, const wchar_t* param, std::string& value)
{
	std::wstring wstr;
	wstr.assign(value.begin(), value.end());
	return !!WritePrivateProfileStringW(section.c_str(), param, wstr.c_str(), ini.c_str());
}

template<>
bool SaveSettingValue(const std::wstring& ini, const std::wstring& section, const wchar_t* param, std::wstring& value)
{
	return !!WritePrivateProfileStringW(section.c_str(), param, value.c_str(), ini.c_str());
}

template<>
bool SaveSettingValue(const std::wstring& ini, const std::wstring& section, const wchar_t* param, int64_t& value)
{
	wchar_t tmp[32] = { 0 };
	swprintf_s(tmp, L"%I64u", value);
	return !!WritePrivateProfileStringW(section.c_str(), param, tmp, ini.c_str());
}
<commit_msg>win32 config: use int32_t instead<commit_after>#include <string>
#include <stdlib.h>
#include <sstream>

#include "../USB.h"
#include "../configuration.h"

extern HINSTANCE hInst;
std::wstring IniDir;
std::wstring LogDir;

void CALLBACK USBsetSettingsDir( const char* dir )
{
	OSDebugOut(L"USBsetSettingsDir: %S\n", dir);
	wchar_t dst[4096] = {0};
	size_t num = 0;
	mbstowcs_s(&num, dst, dir, ARRAYSIZE(dst));
	IniDir = dst;
}

void CALLBACK USBsetLogDir( const char* dir )
{
	OSDebugOut(L"USBsetLogDir: %S\n", dir);
	wchar_t dst[4096] = {0};
	size_t num = 0;
	mbstowcs_s(&num, dst, dir, ARRAYSIZE(dst));
	LogDir = dst;
}

//TODO Use \\?\ to go past 260 char limit
void GetIniFile(std::wstring &iniFile)
{
	iniFile.clear();
	if(!IniDir.length()) {
		WCHAR tmp[MAX_PATH] = {0};
		GetModuleFileName(GetModuleHandle((LPWSTR)hInst), tmp, MAX_PATH);

		std::wstring path(tmp);
		unsigned last = path.find_last_of(L"\\");
		iniFile = path.substr(0, last);
		iniFile.append(L"\\inis");
		CreateDirectory(iniFile.c_str(), NULL);
		iniFile.append(L"\\USBqemu-wheel.ini");
	} else {
		iniFile.append(IniDir);
		iniFile.append(L"USBqemu-wheel.ini");
	}
}


template<typename T>
bool LoadSettingValue(const std::wstring& ini, const std::wstring& section, const wchar_t* param, T& value);
template<typename T>
bool SaveSettingValue(const std::wstring& ini, const std::wstring& section, const wchar_t* param, T& value);

template<>
bool LoadSettingValue(const std::wstring& ini, const std::wstring& section, const wchar_t* param, std::string& value)
{
	wchar_t tmp[4096] = { 0 };
	if (!GetPrivateProfileStringW(section.c_str(), param, NULL, tmp, sizeof(tmp) / sizeof(*tmp), ini.c_str()))
		return false;
	if (GetLastError() == ERROR_FILE_NOT_FOUND)
		return false;

	char tmpA[4096] = { 0 };
	size_t num = 0;
	wcstombs_s(&num, tmpA, tmp, sizeof(tmpA)); //TODO error-check
	value = tmpA;
	return true;
}

template<>
bool LoadSettingValue(const std::wstring& ini, const std::wstring& section, const wchar_t* param, std::wstring& value)
{
	wchar_t tmp[4096] = { 0 };
	if (!GetPrivateProfileStringW(section.c_str(), param, NULL, tmp, sizeof(tmp) / sizeof(*tmp), ini.c_str()))
		return false;
	if (GetLastError() == ERROR_FILE_NOT_FOUND)
		return false;
	value = tmp;
	return true;
}

template<>
bool LoadSettingValue(const std::wstring& ini, const std::wstring& section, const wchar_t* param, int32_t& value)
{
	//value = GetPrivateProfileIntW(section.c_str(), param, 0, ini.c_str());
	wchar_t tmp[4096] = { 0 };
	if (!GetPrivateProfileStringW(section.c_str(), param, NULL, tmp, sizeof(tmp) / sizeof(*tmp), ini.c_str()))
		return false;
	if (GetLastError() == ERROR_FILE_NOT_FOUND)
		return false;
	value = wcstoul(tmp, NULL, 10);
	return true;
}

template<>
bool SaveSettingValue(const std::wstring& ini, const std::wstring& section, const wchar_t* param, std::string& value)
{
	std::wstring wstr;
	wstr.assign(value.begin(), value.end());
	return !!WritePrivateProfileStringW(section.c_str(), param, wstr.c_str(), ini.c_str());
}

template<>
bool SaveSettingValue(const std::wstring& ini, const std::wstring& section, const wchar_t* param, std::wstring& value)
{
	return !!WritePrivateProfileStringW(section.c_str(), param, value.c_str(), ini.c_str());
}

template<>
bool SaveSettingValue(const std::wstring& ini, const std::wstring& section, const wchar_t* param, int32_t& value)
{
	wchar_t tmp[32] = { 0 };
	swprintf_s(tmp, L"%I64u", value);
	return !!WritePrivateProfileStringW(section.c_str(), param, tmp, ini.c_str());
}

{'fraction_non_alphanumeric': 0.16385714285714287, 'fraction_numerical': 0.014857142857142857, 'mean_word_length': 5.725264169068204, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, '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': '3058701', 'n_tokens_mistral': 2774, 'n_tokens_neox': 2639, 'n_words': 622}