<commit_before>/*
Copyright 2014 <NAME>.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#include "stdafx.h"
#include <Windows.h>
#include <stdlib.h>
#include "IniFile.h"

INI_FILE::INI_FILE(wchar_t *FilePath)
{
	DWORD Status = 0;
	DWORD NumberOfBytesRead = 0;

	HANDLE hFile = CreateFile(FilePath, GENERIC_ALL, FILE_SHARE_READ | FILE_SHARE_WRITE,
		NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

	if (hFile == INVALID_HANDLE_VALUE)
	{
		return;
	}

	FileSize = GetFileSize(hFile, NULL);
	if (FileSize == INVALID_FILE_SIZE)
	{
		return;
	}

	FileRaw = new char[FileSize];
	Status = ReadFile(hFile, FileRaw, FileSize, &NumberOfBytesRead, NULL);
	if (!Status)
	{
		return;
	}

	CreateStringsMap();
	Parse();
}


INI_FILE::~INI_FILE()
{
	for (DWORD i = 0; i < IniData.SectionCount; i++)
	{
		delete[] IniData.Section[i].Variables;
	}
	delete[] IniData.Section;
	delete[] FileStringsMap;
	delete FileRaw;
}

bool INI_FILE::CreateStringsMap()
{
	DWORD StringsCount = 1;

	for (DWORD i = 0; i < FileSize; i++)
	{
		if (FileRaw[i] == '\r' && FileRaw[i + 1] == '\n') StringsCount++;
	}

	FileStringsCount = StringsCount;

	FileStringsMap = new DWORD[StringsCount];
	FileStringsMap[0] = 0;

	StringsCount = 1;

	for (DWORD i = 0; i < FileSize; i++)
	{
		if (FileRaw[i] == '\r' && FileRaw[i + 1] == '\n')
		{
			FileStringsMap[StringsCount] = i + 2;
			StringsCount++;
		}
	}

	return true;
}

int INI_FILE::StrTrim(char* Str)
{
	int i = 0, j;
	while ((Str[i] == ' ') || (Str[i] == '\t'))
	{
		i++;
	}
	if (i>0)
	{
		for (j = 0; j < strlen(Str); j++)
		{
			Str[j] = Str[j + i];
		}
		Str[j] = '\0';
	}

	i = strlen(Str) - 1;
	while ((Str[i] == ' ') || (Str[i] == '\t'))
	{
		i--;
	}
	if (i < (strlen(Str) - 1))
	{
		Str[i + 1] = '\0';
	}
	return 0;
}

DWORD INI_FILE::GetFileStringFromNum(DWORD StringNumber, char *RetString, DWORD Size)
{
	DWORD CurrentStringNum = 0;
	DWORD EndStringPos = 0;
	DWORD StringSize = 0;

	if (StringNumber > FileStringsCount) return -1;

	for (DWORD i = FileStringsMap[StringNumber]; i < FileSize; i++)
	{
		if ((FileRaw[i] == '\r' && FileRaw[i + 1] == '\n') || i == (FileSize - 1))
		{
			EndStringPos = i;
			break;
		}
	}

	StringSize = EndStringPos - FileStringsMap[StringNumber];

	if (Size < StringSize) return -1;

	memset(RetString, 0x00, Size);
	memcpy(RetString, &(FileRaw[FileStringsMap[StringNumber]]), StringSize);
	return StringSize;
}

bool INI_FILE::IsVariable(char *Str, DWORD StrSize)
{
	bool Quotes = false;

	for (DWORD i = 0; i < StrSize; i++)
	{
		if (Str[i] == '"' || Str[i] == '\'') Quotes = !Quotes;
		if (Str[i] == '=' && !Quotes) return true;
	}
	return false;
}

bool INI_FILE::FillVariable(INI_SECTION_VARIABLE *Variable, char *Str, DWORD StrSize)
{
	bool Quotes = false;

	for (DWORD i = 0; i < StrSize; i++)
	{
		if (Str[i] == '"' || Str[i] == '\'') Quotes = !Quotes;
		if (Str[i] == '=' && !Quotes)
		{
			memcpy(Variable->VariableName, Str, i);<commit_msg>More bug fixes<commit_after>/*
Copyright 2014 Stas'M Corp.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#include "stdafx.h"
#include <Windows.h>
#include <stdlib.h>
#include "IniFile.h"

INI_FILE::INI_FILE(wchar_t *FilePath)
{
	DWORD Status = 0;
	DWORD NumberOfBytesRead = 0;

	HANDLE hFile = CreateFile(FilePath, GENERIC_ALL, FILE_SHARE_READ | FILE_SHARE_WRITE,
		NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

	if (hFile == INVALID_HANDLE_VALUE)
	{
		return;
	}

	FileSize = GetFileSize(hFile, NULL);
	if (FileSize == INVALID_FILE_SIZE)
	{
		return;
	}

	FileRaw = new char[FileSize];
	Status = ReadFile(hFile, FileRaw, FileSize, &NumberOfBytesRead, NULL);
	if (!Status)
	{
		return;
	}

	CreateStringsMap();
	Parse();
}


INI_FILE::~INI_FILE()
{
	for (DWORD i = 0; i < IniData.SectionCount; i++)
	{
		delete[] IniData.Section[i].Variables;
	}
	delete[] IniData.Section;
	delete[] FileStringsMap;
	delete FileRaw;
}

bool INI_FILE::CreateStringsMap()
{
	DWORD StringsCount = 1;

	for (DWORD i = 0; i < FileSize; i++)
	{
		if (FileRaw[i] == '\r' && FileRaw[i + 1] == '\n') StringsCount++;
	}

	FileStringsCount = StringsCount;

	FileStringsMap = new DWORD[StringsCount];
	FileStringsMap[0] = 0;

	StringsCount = 1;

	for (DWORD i = 0; i < FileSize; i++)
	{
		if (FileRaw[i] == '\r' && FileRaw[i + 1] == '\n')
		{
			FileStringsMap[StringsCount] = i + 2;
			StringsCount++;
		}
	}

	return true;
}

int INI_FILE::StrTrim(char* Str)
{
	int i = 0, j;
	while ((Str[i] == ' ') || (Str[i] == '\t'))
	{
		i++;
	}
	if (i>0)
	{
		for (j = 0; j < strlen(Str); j++)
		{
			Str[j] = Str[j + i];
		}
		Str[j] = '\0';
	}

	i = strlen(Str) - 1;
	while ((Str[i] == ' ') || (Str[i] == '\t'))
	{
		i--;
	}
	if (i < (strlen(Str) - 1))
	{
		Str[i + 1] = '\0';
	}
	return 0;
}

DWORD INI_FILE::GetFileStringFromNum(DWORD StringNumber, char *RetString, DWORD Size)
{
	DWORD CurrentStringNum = 0;
	DWORD EndStringPos = 0;
	DWORD StringSize = 0;

	if (StringNumber > FileStringsCount) return 0;

	for (DWORD i = FileStringsMap[StringNumber]; i < FileSize; i++)
	{
		if ((FileRaw[i] == '\r' && FileRaw[i + 1] == '\n') || i == (FileSize - 1))
		{
			EndStringPos = i;
			break;
		}
	}

	StringSize = EndStringPos - FileStringsMap[StringNumber];

	if (Size < StringSize) return 0;

	memset(RetString, 0x00, Size);
	memcpy(RetString, &(FileRaw[FileStringsMap[StringNumber]]), StringSize);
	return StringSize;
}

bool INI_FILE::IsVariable(char *Str, DWORD StrSize)
{
	bool Quotes = false;

	for (DWORD i = 0; i < StrSize; i++)
	{
		if (Str[i] == '"' || Str[i] == '\'') Quotes = !Quotes;
		if (Str[i] == '=' && !Quotes) return true;
	}
	return false;
}

bool INI_FILE::FillVariable(INI_SECTION_VARIABLE *Variable, char *Str, DWORD StrSize)
{
	bool Quotes = false;

	for (DWORD i = 0; i < StrSize; i++)
	{
		if (Str[i] == '"' || Str[i] == '\'') Quotes = !Quotes;
		if (Str[i] == '=' && !Quotes)
		{
			memcpy(Variable->VariableName, Str, i);
{'fraction_non_alphanumeric': 0.16720683559222158, 'fraction_numerical': 0.012080141426045963, 'mean_word_length': 3.856223175965665, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5083325', 'n_tokens_mistral': 2834, 'n_tokens_neox': 2643, 'n_words': 700}