2014-12-03 18:58:56 +00:00
|
|
|
/*
|
2014-12-09 22:00:20 +00:00
|
|
|
Copyright 2014 Stas'M Corp.
|
2014-12-03 18:58:56 +00:00
|
|
|
|
2014-12-09 22:00:20 +00:00
|
|
|
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
|
2014-12-03 18:58:56 +00:00
|
|
|
|
2014-12-09 22:00:20 +00:00
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
2014-12-03 18:58:56 +00:00
|
|
|
|
2014-12-09 22:00:20 +00:00
|
|
|
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.
|
2014-12-03 18:58:56 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "stdafx.h"
|
|
|
|
#include <Windows.h>
|
2014-12-09 13:00:11 +00:00
|
|
|
#include <stdlib.h>
|
2014-12-03 18:58:56 +00:00
|
|
|
#include "IniFile.h"
|
|
|
|
|
|
|
|
INI_FILE::INI_FILE(wchar_t *FilePath)
|
|
|
|
{
|
|
|
|
DWORD Status = 0;
|
|
|
|
DWORD NumberOfBytesRead = 0;
|
|
|
|
|
2014-12-10 16:11:36 +00:00
|
|
|
HANDLE hFile = CreateFile(FilePath, GENERIC_READ, FILE_SHARE_WRITE|FILE_SHARE_READ,
|
2014-12-09 22:00:20 +00:00
|
|
|
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
2014-12-03 18:58:56 +00:00
|
|
|
|
2014-12-09 22:00:20 +00:00
|
|
|
if (hFile == INVALID_HANDLE_VALUE)
|
2014-12-03 18:58:56 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileSize = GetFileSize(hFile, NULL);
|
2014-12-09 22:00:20 +00:00
|
|
|
if (FileSize == INVALID_FILE_SIZE)
|
2014-12-03 18:58:56 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileRaw = new char[FileSize];
|
2014-12-09 22:00:20 +00:00
|
|
|
Status = ReadFile(hFile, FileRaw, FileSize, &NumberOfBytesRead, NULL);
|
|
|
|
if (!Status)
|
2014-12-03 18:58:56 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
CreateStringsMap();
|
|
|
|
Parse();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
INI_FILE::~INI_FILE()
|
|
|
|
{
|
2014-12-09 22:00:20 +00:00
|
|
|
for (DWORD i = 0; i < IniData.SectionCount; i++)
|
2014-12-03 18:58:56 +00:00
|
|
|
{
|
|
|
|
delete[] IniData.Section[i].Variables;
|
|
|
|
}
|
|
|
|
delete[] IniData.Section;
|
|
|
|
delete[] FileStringsMap;
|
|
|
|
delete FileRaw;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool INI_FILE::CreateStringsMap()
|
|
|
|
{
|
|
|
|
DWORD StringsCount = 1;
|
|
|
|
|
2014-12-09 22:00:20 +00:00
|
|
|
for (DWORD i = 0; i < FileSize; i++)
|
2014-12-03 18:58:56 +00:00
|
|
|
{
|
2014-12-09 22:00:20 +00:00
|
|
|
if (FileRaw[i] == '\r' && FileRaw[i + 1] == '\n') StringsCount++;
|
2014-12-03 18:58:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FileStringsCount = StringsCount;
|
|
|
|
|
|
|
|
FileStringsMap = new DWORD[StringsCount];
|
|
|
|
FileStringsMap[0] = 0;
|
|
|
|
|
|
|
|
StringsCount = 1;
|
|
|
|
|
2014-12-09 22:00:20 +00:00
|
|
|
for (DWORD i = 0; i < FileSize; i++)
|
2014-12-03 18:58:56 +00:00
|
|
|
{
|
2014-12-09 22:00:20 +00:00
|
|
|
if (FileRaw[i] == '\r' && FileRaw[i + 1] == '\n')
|
2014-12-03 18:58:56 +00:00
|
|
|
{
|
2014-12-09 22:00:20 +00:00
|
|
|
FileStringsMap[StringsCount] = i + 2;
|
2014-12-03 18:58:56 +00:00
|
|
|
StringsCount++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-12-04 18:22:57 +00:00
|
|
|
int INI_FILE::StrTrim(char* Str)
|
2014-12-03 18:58:56 +00:00
|
|
|
{
|
2014-12-04 14:47:37 +00:00
|
|
|
int i = 0, j;
|
2014-12-09 22:00:20 +00:00
|
|
|
while ((Str[i] == ' ') || (Str[i] == '\t'))
|
2014-12-04 14:47:37 +00:00
|
|
|
{
|
|
|
|
i++;
|
|
|
|
}
|
2014-12-09 22:00:20 +00:00
|
|
|
if (i>0)
|
2014-12-04 14:47:37 +00:00
|
|
|
{
|
2014-12-09 22:00:20 +00:00
|
|
|
for (j = 0; j < strlen(Str); j++)
|
2014-12-04 14:47:37 +00:00
|
|
|
{
|
2014-12-09 22:00:20 +00:00
|
|
|
Str[j] = Str[j + i];
|
2014-12-04 14:47:37 +00:00
|
|
|
}
|
2014-12-09 22:00:20 +00:00
|
|
|
Str[j] = '\0';
|
2014-12-04 18:22:57 +00:00
|
|
|
}
|
|
|
|
|
2014-12-09 22:00:20 +00:00
|
|
|
i = strlen(Str) - 1;
|
|
|
|
while ((Str[i] == ' ') || (Str[i] == '\t'))
|
2014-12-04 14:47:37 +00:00
|
|
|
{
|
|
|
|
i--;
|
|
|
|
}
|
2014-12-09 22:00:20 +00:00
|
|
|
if (i < (strlen(Str) - 1))
|
2014-12-04 14:47:37 +00:00
|
|
|
{
|
2014-12-09 22:00:20 +00:00
|
|
|
Str[i + 1] = '\0';
|
2014-12-04 14:47:37 +00:00
|
|
|
}
|
|
|
|
return 0;
|
2014-12-03 18:58:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DWORD INI_FILE::GetFileStringFromNum(DWORD StringNumber, char *RetString, DWORD Size)
|
|
|
|
{
|
|
|
|
DWORD CurrentStringNum = 0;
|
|
|
|
DWORD EndStringPos = 0;
|
|
|
|
DWORD StringSize = 0;
|
|
|
|
|
2014-12-09 23:59:13 +00:00
|
|
|
if (StringNumber > FileStringsCount) return 0;
|
2014-12-03 18:58:56 +00:00
|
|
|
|
2014-12-09 22:00:20 +00:00
|
|
|
for (DWORD i = FileStringsMap[StringNumber]; i < FileSize; i++)
|
2014-12-03 18:58:56 +00:00
|
|
|
{
|
2014-12-09 22:00:20 +00:00
|
|
|
if ((FileRaw[i] == '\r' && FileRaw[i + 1] == '\n') || i == (FileSize - 1))
|
2014-12-03 18:58:56 +00:00
|
|
|
{
|
|
|
|
EndStringPos = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-09 22:00:20 +00:00
|
|
|
StringSize = EndStringPos - FileStringsMap[StringNumber];
|
2014-12-03 18:58:56 +00:00
|
|
|
|
2014-12-09 23:59:13 +00:00
|
|
|
if (Size < StringSize) return 0;
|
2014-12-03 18:58:56 +00:00
|
|
|
|
|
|
|
memset(RetString, 0x00, Size);
|
|
|
|
memcpy(RetString, &(FileRaw[FileStringsMap[StringNumber]]), StringSize);
|
|
|
|
return StringSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool INI_FILE::IsVariable(char *Str, DWORD StrSize)
|
|
|
|
{
|
|
|
|
bool Quotes = false;
|
|
|
|
|
2014-12-09 22:00:20 +00:00
|
|
|
for (DWORD i = 0; i < StrSize; i++)
|
2014-12-03 18:58:56 +00:00
|
|
|
{
|
2014-12-09 22:00:20 +00:00
|
|
|
if (Str[i] == '"' || Str[i] == '\'') Quotes = !Quotes;
|
|
|
|
if (Str[i] == '=' && !Quotes) return true;
|
2014-12-03 18:58:56 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool INI_FILE::FillVariable(INI_SECTION_VARIABLE *Variable, char *Str, DWORD StrSize)
|
|
|
|
{
|
|
|
|
bool Quotes = false;
|
|
|
|
|
2014-12-09 22:00:20 +00:00
|
|
|
for (DWORD i = 0; i < StrSize; i++)
|
2014-12-03 18:58:56 +00:00
|
|
|
{
|
2014-12-09 22:00:20 +00:00
|
|
|
if (Str[i] == '"' || Str[i] == '\'') Quotes = !Quotes;
|
|
|
|
if (Str[i] == '=' && !Quotes)
|
2014-12-03 18:58:56 +00:00
|
|
|
{
|
|
|
|
memcpy(Variable->VariableName, Str, i);
|
2014-12-09 22:00:20 +00:00
|
|
|
memcpy(Variable->VariableValue, &(Str[i + 1]), StrSize - (i - 1));
|
2014-12-04 14:47:37 +00:00
|
|
|
StrTrim(Variable->VariableName);
|
|
|
|
StrTrim(Variable->VariableValue);
|
2014-12-03 18:58:56 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool INI_FILE::Parse()
|
|
|
|
{
|
|
|
|
DWORD CurrentStringNum = 0;
|
|
|
|
char CurrentString[512];
|
|
|
|
DWORD CurrentStringSize = 0;
|
|
|
|
|
|
|
|
DWORD SectionsCount = 0;
|
|
|
|
DWORD VariablesCount = 0;
|
|
|
|
|
|
|
|
DWORD CurrentSectionNum = -1;
|
|
|
|
DWORD CurrentVariableNum = -1;
|
|
|
|
|
2014-12-04 14:47:37 +00:00
|
|
|
// Calculate sections count
|
2014-12-09 22:00:20 +00:00
|
|
|
for (DWORD CurrentStringNum = 0; CurrentStringNum < FileStringsCount; CurrentStringNum++)
|
2014-12-03 18:58:56 +00:00
|
|
|
{
|
|
|
|
CurrentStringSize = GetFileStringFromNum(CurrentStringNum, CurrentString, 512);
|
|
|
|
|
2014-12-09 22:00:20 +00:00
|
|
|
if (CurrentString[0] == ';') continue; // It's a comment
|
|
|
|
|
|
|
|
if (CurrentString[0] == '[' && CurrentString[CurrentStringSize - 1] == ']') // It's section declaration
|
2014-12-03 18:58:56 +00:00
|
|
|
{
|
|
|
|
SectionsCount++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DWORD *SectionVariableCount = new DWORD[SectionsCount];
|
|
|
|
memset(SectionVariableCount, 0x00, sizeof(DWORD)*SectionsCount);
|
|
|
|
|
2014-12-09 22:00:20 +00:00
|
|
|
for (DWORD CurrentStringNum = 0; CurrentStringNum < FileStringsCount; CurrentStringNum++)
|
2014-12-03 18:58:56 +00:00
|
|
|
{
|
|
|
|
CurrentStringSize = GetFileStringFromNum(CurrentStringNum, CurrentString, 512);
|
|
|
|
|
2014-12-09 22:00:20 +00:00
|
|
|
if (CurrentString[0] == ';') continue; // It's a comment
|
|
|
|
|
2014-12-03 18:58:56 +00:00
|
|
|
|
2014-12-09 22:00:20 +00:00
|
|
|
if (CurrentString[0] == '[' && CurrentString[CurrentStringSize - 1] == ']') // It's section declaration
|
2014-12-03 18:58:56 +00:00
|
|
|
{
|
|
|
|
CurrentSectionNum++;
|
|
|
|
continue;
|
|
|
|
}
|
2014-12-09 22:00:20 +00:00
|
|
|
if (IsVariable(CurrentString, CurrentStringSize))
|
2014-12-03 18:58:56 +00:00
|
|
|
{
|
|
|
|
VariablesCount++;
|
|
|
|
SectionVariableCount[CurrentSectionNum]++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
IniData.SectionCount = SectionsCount;
|
|
|
|
IniData.Section = new INI_SECTION[SectionsCount];
|
|
|
|
memset(IniData.Section, 0x00, sizeof(PINI_SECTION)*SectionsCount);
|
|
|
|
|
2014-12-09 22:00:20 +00:00
|
|
|
for (DWORD i = 0; i < SectionsCount; i++)
|
2014-12-03 18:58:56 +00:00
|
|
|
{
|
|
|
|
IniData.Section[i].VariablesCount = SectionVariableCount[i];
|
|
|
|
IniData.Section[i].Variables = new INI_SECTION_VARIABLE[SectionVariableCount[i]];
|
|
|
|
memset(IniData.Section[i].Variables, 0x00, sizeof(INI_SECTION_VARIABLE)*SectionVariableCount[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
delete[] SectionVariableCount;
|
|
|
|
|
|
|
|
CurrentSectionNum = -1;
|
|
|
|
CurrentVariableNum = -1;
|
|
|
|
|
2014-12-09 22:00:20 +00:00
|
|
|
for (DWORD CurrentStringNum = 0; CurrentStringNum < FileStringsCount; CurrentStringNum++)
|
2014-12-03 18:58:56 +00:00
|
|
|
{
|
|
|
|
CurrentStringSize = GetFileStringFromNum(CurrentStringNum, CurrentString, 512);
|
|
|
|
|
2014-12-09 22:00:20 +00:00
|
|
|
if (CurrentString[0] == ';') // It's a comment
|
2014-12-03 18:58:56 +00:00
|
|
|
{
|
2014-12-04 18:22:57 +00:00
|
|
|
continue;
|
2014-12-03 18:58:56 +00:00
|
|
|
}
|
|
|
|
|
2014-12-09 22:00:20 +00:00
|
|
|
if (CurrentString[0] == '[' && CurrentString[CurrentStringSize - 1] == ']')
|
2014-12-03 18:58:56 +00:00
|
|
|
{
|
|
|
|
CurrentSectionNum++;
|
|
|
|
CurrentVariableNum = 0;
|
2014-12-09 22:00:20 +00:00
|
|
|
memcpy(IniData.Section[CurrentSectionNum].SectionName, &(CurrentString[1]), (CurrentStringSize - 2));
|
2014-12-03 18:58:56 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-12-09 22:00:20 +00:00
|
|
|
if (IsVariable(CurrentString, CurrentStringSize))
|
2014-12-03 18:58:56 +00:00
|
|
|
{
|
|
|
|
FillVariable(&(IniData.Section[CurrentSectionNum].Variables[CurrentVariableNum]), CurrentString, CurrentStringSize);
|
|
|
|
CurrentVariableNum++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-12-04 20:01:30 +00:00
|
|
|
PINI_SECTION INI_FILE::GetSection(char *SectionName)
|
2014-12-03 18:58:56 +00:00
|
|
|
{
|
2014-12-09 22:00:20 +00:00
|
|
|
for (DWORD i = 0; i < IniData.SectionCount; i++)
|
2014-12-03 18:58:56 +00:00
|
|
|
{
|
2014-12-09 22:00:20 +00:00
|
|
|
if (memcmp(IniData.Section[i].SectionName, SectionName, strlen(SectionName)) == 0)
|
2014-12-03 18:58:56 +00:00
|
|
|
{
|
2014-12-04 20:01:30 +00:00
|
|
|
return &IniData.Section[i];
|
2014-12-03 18:58:56 +00:00
|
|
|
}
|
|
|
|
}
|
2014-12-04 20:01:30 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool INI_FILE::SectionExists(char *SectionName)
|
|
|
|
{
|
2014-12-09 22:00:20 +00:00
|
|
|
if (GetSection(SectionName) == NULL) return false;
|
2014-12-04 20:01:30 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool INI_FILE::VariableExists(char *SectionName, char *VariableName)
|
2014-12-09 22:00:20 +00:00
|
|
|
{
|
|
|
|
INI_SECTION_VARIABLE Variable = { 0 };
|
2014-12-04 20:01:30 +00:00
|
|
|
return GetVariableInSectionPrivate(SectionName, VariableName, &Variable);
|
2014-12-03 18:58:56 +00:00
|
|
|
}
|
|
|
|
|
2014-12-04 14:47:37 +00:00
|
|
|
bool INI_FILE::GetVariableInSectionPrivate(char *SectionName, char *VariableName, INI_SECTION_VARIABLE *RetVariable)
|
2014-12-03 18:58:56 +00:00
|
|
|
{
|
|
|
|
INI_SECTION *Section = NULL;
|
|
|
|
INI_SECTION_VARIABLE *Variable = NULL;
|
|
|
|
|
|
|
|
// Find section
|
2014-12-04 20:01:30 +00:00
|
|
|
Section = GetSection(SectionName);
|
2014-12-09 22:00:20 +00:00
|
|
|
if (Section == NULL)
|
2014-12-03 18:58:56 +00:00
|
|
|
{
|
|
|
|
SetLastError(318); // This region is not found
|
|
|
|
return false;
|
|
|
|
}
|
2014-12-04 14:47:37 +00:00
|
|
|
|
2014-12-03 18:58:56 +00:00
|
|
|
// Find variable
|
2014-12-09 22:00:20 +00:00
|
|
|
for (DWORD i = 0; i < Section->VariablesCount; i++)
|
2014-12-03 18:58:56 +00:00
|
|
|
{
|
2014-12-09 22:00:20 +00:00
|
|
|
if (memcmp(Section->Variables[i].VariableName, VariableName, strlen(VariableName)) == 0)
|
2014-12-03 18:58:56 +00:00
|
|
|
{
|
|
|
|
Variable = &(Section->Variables[i]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-12-09 22:00:20 +00:00
|
|
|
if (Variable == NULL)
|
2014-12-03 18:58:56 +00:00
|
|
|
{
|
|
|
|
SetLastError(1898); // Member of the group is not found
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(RetVariable, 0x00, sizeof(*RetVariable));
|
2014-12-04 14:47:37 +00:00
|
|
|
memcpy(RetVariable, Variable, sizeof(*Variable));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool INI_FILE::GetVariableInSection(char *SectionName, char *VariableName, INI_VAR_STRING *RetVariable)
|
|
|
|
{
|
|
|
|
bool Status = false;
|
|
|
|
INI_SECTION_VARIABLE Variable = {};
|
|
|
|
|
|
|
|
Status = GetVariableInSectionPrivate(SectionName, VariableName, &Variable);
|
2014-12-09 22:00:20 +00:00
|
|
|
if (!Status) return Status;
|
2014-12-04 14:47:37 +00:00
|
|
|
|
|
|
|
memset(RetVariable, 0x00, sizeof(*RetVariable));
|
|
|
|
memcpy(RetVariable->Name, Variable.VariableName, strlen(Variable.VariableName));
|
|
|
|
memcpy(RetVariable->Value, Variable.VariableValue, strlen(Variable.VariableValue));
|
2014-12-04 18:22:57 +00:00
|
|
|
|
2014-12-03 18:58:56 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool INI_FILE::GetVariableInSection(char *SectionName, char *VariableName, INI_VAR_DWORD *RetVariable)
|
|
|
|
{
|
2014-12-04 14:47:37 +00:00
|
|
|
bool Status = false;
|
|
|
|
INI_SECTION_VARIABLE Variable = {};
|
2014-12-03 18:58:56 +00:00
|
|
|
|
2014-12-04 14:47:37 +00:00
|
|
|
Status = GetVariableInSectionPrivate(SectionName, VariableName, &Variable);
|
2014-12-09 22:00:20 +00:00
|
|
|
if (!Status) return Status;
|
2014-12-04 18:22:57 +00:00
|
|
|
|
2014-12-03 18:58:56 +00:00
|
|
|
memset(RetVariable, 0x00, sizeof(*RetVariable));
|
2014-12-04 14:47:37 +00:00
|
|
|
memcpy(RetVariable->Name, Variable.VariableName, strlen(Variable.VariableName));
|
2014-12-03 18:58:56 +00:00
|
|
|
|
|
|
|
#ifndef _WIN64
|
2014-12-04 14:47:37 +00:00
|
|
|
RetVariable->ValueDec = strtol(Variable.VariableValue, NULL, 10);
|
|
|
|
RetVariable->ValueHex = strtol(Variable.VariableValue, NULL, 16);
|
2014-12-03 18:58:56 +00:00
|
|
|
#else
|
2014-12-04 14:47:37 +00:00
|
|
|
RetVariable->ValueDec = _strtoi64(Variable.VariableValue, NULL, 10);
|
|
|
|
RetVariable->ValueHex = _strtoi64(Variable.VariableValue, NULL, 16);
|
2014-12-03 18:58:56 +00:00
|
|
|
#endif
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool INI_FILE::GetVariableInSection(char *SectionName, char *VariableName, INI_VAR_BYTEARRAY *RetVariable)
|
|
|
|
{
|
2014-12-04 14:47:37 +00:00
|
|
|
bool Status = false;
|
|
|
|
INI_SECTION_VARIABLE Variable = {};
|
2014-12-03 18:58:56 +00:00
|
|
|
|
2014-12-04 14:47:37 +00:00
|
|
|
Status = GetVariableInSectionPrivate(SectionName, VariableName, &Variable);
|
2014-12-09 22:00:20 +00:00
|
|
|
if (!Status) return Status;
|
2014-12-04 18:22:57 +00:00
|
|
|
|
2014-12-04 14:47:37 +00:00
|
|
|
DWORD ValueLen = strlen(Variable.VariableValue);
|
2014-12-09 22:00:20 +00:00
|
|
|
if ((ValueLen % 2) != 0) return false;
|
|
|
|
|
2014-12-09 13:09:54 +00:00
|
|
|
// for security reasons not more than 16 bytes
|
|
|
|
if (ValueLen > 32) ValueLen = 32; // 32 hex digits
|
2014-12-03 18:58:56 +00:00
|
|
|
|
|
|
|
memset(RetVariable, 0x00, sizeof(*RetVariable));
|
2014-12-10 16:11:36 +00:00
|
|
|
memcpy(RetVariable->Name, Variable.VariableName, strlen(Variable.VariableName));
|
2014-12-03 18:58:56 +00:00
|
|
|
|
2014-12-09 22:00:20 +00:00
|
|
|
for (DWORD i = 0; i <= ValueLen; i++)
|
2014-12-03 18:58:56 +00:00
|
|
|
{
|
2014-12-09 22:00:20 +00:00
|
|
|
if ((i % 2) != 0) continue;
|
|
|
|
|
|
|
|
switch (Variable.VariableValue[i])
|
2014-12-03 18:58:56 +00:00
|
|
|
{
|
2014-12-09 22:00:20 +00:00
|
|
|
case '0': break;
|
|
|
|
case '1': RetVariable->Value[(i / 2)] += (1 << 4); break;
|
|
|
|
case '2': RetVariable->Value[(i / 2)] += (2 << 4); break;
|
|
|
|
case '3': RetVariable->Value[(i / 2)] += (3 << 4); break;
|
|
|
|
case '4': RetVariable->Value[(i / 2)] += (4 << 4); break;
|
|
|
|
case '5': RetVariable->Value[(i / 2)] += (5 << 4); break;
|
|
|
|
case '6': RetVariable->Value[(i / 2)] += (6 << 4); break;
|
|
|
|
case '7': RetVariable->Value[(i / 2)] += (7 << 4); break;
|
|
|
|
case '8': RetVariable->Value[(i / 2)] += (8 << 4); break;
|
|
|
|
case '9': RetVariable->Value[(i / 2)] += (9 << 4); break;
|
|
|
|
case 'A': RetVariable->Value[(i / 2)] += (10 << 4); break;
|
|
|
|
case 'B': RetVariable->Value[(i / 2)] += (11 << 4); break;
|
|
|
|
case 'C': RetVariable->Value[(i / 2)] += (12 << 4); break;
|
|
|
|
case 'D': RetVariable->Value[(i / 2)] += (13 << 4); break;
|
|
|
|
case 'E': RetVariable->Value[(i / 2)] += (14 << 4); break;
|
|
|
|
case 'F': RetVariable->Value[(i / 2)] += (15 << 4); break;
|
2014-12-04 18:22:57 +00:00
|
|
|
}
|
2014-12-03 18:58:56 +00:00
|
|
|
|
2014-12-09 22:00:20 +00:00
|
|
|
switch (Variable.VariableValue[i + 1])
|
2014-12-03 18:58:56 +00:00
|
|
|
{
|
2014-12-09 22:00:20 +00:00
|
|
|
case '0': break;
|
|
|
|
case '1': RetVariable->Value[(i / 2)] += 1; break;
|
|
|
|
case '2': RetVariable->Value[(i / 2)] += 2; break;
|
|
|
|
case '3': RetVariable->Value[(i / 2)] += 3; break;
|
|
|
|
case '4': RetVariable->Value[(i / 2)] += 4; break;
|
|
|
|
case '5': RetVariable->Value[(i / 2)] += 5; break;
|
|
|
|
case '6': RetVariable->Value[(i / 2)] += 6; break;
|
|
|
|
case '7': RetVariable->Value[(i / 2)] += 7; break;
|
|
|
|
case '8': RetVariable->Value[(i / 2)] += 8; break;
|
|
|
|
case '9': RetVariable->Value[(i / 2)] += 9; break;
|
|
|
|
case 'A': RetVariable->Value[(i / 2)] += 10; break;
|
|
|
|
case 'B': RetVariable->Value[(i / 2)] += 11; break;
|
|
|
|
case 'C': RetVariable->Value[(i / 2)] += 12; break;
|
|
|
|
case 'D': RetVariable->Value[(i / 2)] += 13; break;
|
|
|
|
case 'E': RetVariable->Value[(i / 2)] += 14; break;
|
|
|
|
case 'F': RetVariable->Value[(i / 2)] += 15; break;
|
2014-12-03 18:58:56 +00:00
|
|
|
}
|
|
|
|
}
|
2014-12-09 22:00:20 +00:00
|
|
|
RetVariable->ArraySize = ValueLen / 2;
|
2014-12-03 18:58:56 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-12-04 20:01:30 +00:00
|
|
|
bool INI_FILE::GetVariableInSection(char *SectionName, char *VariableName, bool *RetVariable)
|
2014-12-03 18:58:56 +00:00
|
|
|
{
|
2014-12-04 14:47:37 +00:00
|
|
|
bool Status = false;
|
|
|
|
INI_SECTION_VARIABLE Variable = {};
|
|
|
|
|
|
|
|
Status = GetVariableInSectionPrivate(SectionName, VariableName, &Variable);
|
2014-12-09 22:00:20 +00:00
|
|
|
if (!Status) return Status;
|
2014-12-03 18:58:56 +00:00
|
|
|
|
2014-12-04 20:01:30 +00:00
|
|
|
*RetVariable = (bool)strtol(Variable.VariableValue, NULL, 10);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool INI_FILE::GetSectionVariablesList(char *SectionName, INI_SECTION_VARLIST *VariablesList)
|
|
|
|
{
|
|
|
|
INI_SECTION *Section = NULL;
|
|
|
|
|
|
|
|
Section = GetSection(SectionName);
|
2014-12-09 22:00:20 +00:00
|
|
|
if (Section == NULL)
|
2014-12-04 20:01:30 +00:00
|
|
|
{
|
|
|
|
SetLastError(318); // This region is not found
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
VariablesList->EntriesCount = Section->VariablesCount;
|
|
|
|
|
|
|
|
VariablesList->NamesEntries = new INI_SECTION_VARLIST_ENTRY[VariablesList->EntriesCount];
|
2014-12-09 22:00:20 +00:00
|
|
|
memset(VariablesList->NamesEntries, 0x00, sizeof(INI_SECTION_VARLIST_ENTRY)*VariablesList->EntriesCount);
|
|
|
|
|
2014-12-04 20:01:30 +00:00
|
|
|
VariablesList->ValuesEntries = new INI_SECTION_VARLIST_ENTRY[VariablesList->EntriesCount];
|
|
|
|
memset(VariablesList->ValuesEntries, 0x00, sizeof(INI_SECTION_VARLIST_ENTRY)*VariablesList->EntriesCount);
|
|
|
|
|
2014-12-09 22:00:20 +00:00
|
|
|
for (DWORD i = 0; i < Section->VariablesCount; i++)
|
2014-12-04 20:01:30 +00:00
|
|
|
{
|
2014-12-09 22:00:20 +00:00
|
|
|
memcpy(VariablesList->NamesEntries[i].String, Section->Variables[i].VariableName,
|
|
|
|
strlen(Section->Variables[i].VariableName));
|
|
|
|
|
|
|
|
memcpy(VariablesList->ValuesEntries[i].String, Section->Variables[i].VariableValue,
|
2014-12-04 20:01:30 +00:00
|
|
|
strlen(Section->Variables[i].VariableValue));
|
|
|
|
}
|
|
|
|
|
2014-12-03 18:58:56 +00:00
|
|
|
return true;
|
|
|
|
}
|
2014-12-09 13:00:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
// ---------------------------- WCHAR_T BLOCK ----------------------------------------------
|
|
|
|
|
|
|
|
bool INI_FILE::SectionExists(wchar_t *SectionName)
|
|
|
|
{
|
2014-12-09 22:00:20 +00:00
|
|
|
char cSectionName[MAX_STRING_LEN] = { 0x00 };
|
2014-12-09 13:00:11 +00:00
|
|
|
|
|
|
|
wcstombs(cSectionName, SectionName, MAX_STRING_LEN);
|
|
|
|
|
|
|
|
return GetSection(cSectionName);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool INI_FILE::VariableExists(wchar_t *SectionName, wchar_t *VariableName)
|
2014-12-09 22:00:20 +00:00
|
|
|
{
|
|
|
|
INI_SECTION_VARIABLE Variable = { 0 };
|
2014-12-09 13:00:11 +00:00
|
|
|
|
2014-12-09 22:00:20 +00:00
|
|
|
char cSectionName[MAX_STRING_LEN] = { 0x00 };
|
|
|
|
char cVariableName[MAX_STRING_LEN] = { 0x00 };
|
2014-12-09 13:00:11 +00:00
|
|
|
|
|
|
|
wcstombs(cSectionName, SectionName, MAX_STRING_LEN);
|
|
|
|
wcstombs(cVariableName, VariableName, MAX_STRING_LEN);
|
|
|
|
|
|
|
|
return GetVariableInSectionPrivate(cSectionName, cVariableName, &Variable);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool INI_FILE::GetVariableInSection(wchar_t *SectionName, wchar_t *VariableName, INI_VAR_STRING *RetVariable)
|
|
|
|
{
|
2014-12-09 22:00:20 +00:00
|
|
|
char cSectionName[MAX_STRING_LEN] = { 0x00 };
|
|
|
|
char cVariableName[MAX_STRING_LEN] = { 0x00 };
|
2014-12-09 13:00:11 +00:00
|
|
|
|
|
|
|
wcstombs(cSectionName, SectionName, MAX_STRING_LEN);
|
|
|
|
wcstombs(cVariableName, VariableName, MAX_STRING_LEN);
|
|
|
|
|
|
|
|
return GetVariableInSection(cSectionName, cVariableName, RetVariable);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool INI_FILE::GetVariableInSection(wchar_t *SectionName, wchar_t *VariableName, INI_VAR_DWORD *RetVariable)
|
|
|
|
{
|
2014-12-09 22:00:20 +00:00
|
|
|
char cSectionName[MAX_STRING_LEN] = { 0x00 };
|
|
|
|
char cVariableName[MAX_STRING_LEN] = { 0x00 };
|
2014-12-09 13:00:11 +00:00
|
|
|
|
|
|
|
wcstombs(cSectionName, SectionName, MAX_STRING_LEN);
|
|
|
|
wcstombs(cVariableName, VariableName, MAX_STRING_LEN);
|
|
|
|
|
|
|
|
return GetVariableInSection(cSectionName, cVariableName, RetVariable);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool INI_FILE::GetVariableInSection(wchar_t *SectionName, wchar_t *VariableName, INI_VAR_BYTEARRAY *RetVariable)
|
|
|
|
{
|
2014-12-09 22:00:20 +00:00
|
|
|
char cSectionName[MAX_STRING_LEN] = { 0x00 };
|
|
|
|
char cVariableName[MAX_STRING_LEN] = { 0x00 };
|
2014-12-09 13:00:11 +00:00
|
|
|
|
|
|
|
wcstombs(cSectionName, SectionName, MAX_STRING_LEN);
|
|
|
|
wcstombs(cVariableName, VariableName, MAX_STRING_LEN);
|
|
|
|
|
|
|
|
return GetVariableInSection(cSectionName, cVariableName, RetVariable);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool INI_FILE::GetVariableInSection(wchar_t *SectionName, wchar_t *VariableName, bool *RetVariable)
|
|
|
|
{
|
2014-12-09 22:00:20 +00:00
|
|
|
char cSectionName[MAX_STRING_LEN] = { 0x00 };
|
|
|
|
char cVariableName[MAX_STRING_LEN] = { 0x00 };
|
2014-12-09 13:00:11 +00:00
|
|
|
|
|
|
|
wcstombs(cSectionName, SectionName, MAX_STRING_LEN);
|
|
|
|
wcstombs(cVariableName, VariableName, MAX_STRING_LEN);
|
|
|
|
|
|
|
|
return GetVariableInSection(cSectionName, cVariableName, RetVariable);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool INI_FILE::GetSectionVariablesList(wchar_t *SectionName, INI_SECTION_VARLIST *VariablesList)
|
|
|
|
{
|
2014-12-09 22:00:20 +00:00
|
|
|
char cSectionName[MAX_STRING_LEN] = { 0x00 };
|
2014-12-09 13:00:11 +00:00
|
|
|
|
|
|
|
wcstombs(cSectionName, SectionName, MAX_STRING_LEN);
|
|
|
|
|
|
|
|
return GetSectionVariablesList(cSectionName, VariablesList);
|
2014-12-10 16:11:36 +00:00
|
|
|
}
|