Update IniFile.cpp

This commit is contained in:
FusixGit 2014-12-04 17:47:37 +03:00
parent bc84ee3ac4
commit 17102ec1cd

View File

@ -76,7 +76,7 @@ bool INI_FILE::CreateStringsMap()
StringsCount = 1; StringsCount = 1;
for(DWORD i = 9; i < FileSize; i++) for(DWORD i = 0; i < FileSize; i++)
{ {
if(FileRaw[i] == '\r' && FileRaw[i+1] == '\n') if(FileRaw[i] == '\r' && FileRaw[i+1] == '\n')
{ {
@ -88,40 +88,32 @@ bool INI_FILE::CreateStringsMap()
return true; return true;
} }
int INI_FILE::StrTrim(char* Str, STRING_TRIM_TYPE Type) int INI_FILE::StrTrim(char* Str)
{ {
int StrLn = strlen(Str) + 1; int i = 0, j;
if ((StrLn == 0) || (Type < TRIM_LEFT) || (Type > TRIM_BOTH)) { while((Str[i]==' ')||(Str[i]=='\t'))
return 0; {
} i++;
char *NewStr = new char[StrLn]; }
int IdxSrc = -1, IdxDest = 0; if(i>0)
if ((Type == TRIM_LEFT) || (Type == TRIM_BOTH)) { {
bool InText = false; for(j=0; j < strlen(Str); j++)
while(Str[++IdxSrc]) { {
if (!InText && (Str[IdxSrc] != ' ') && (Str[IdxSrc] != '\n') && (Str[IdxSrc] != '\t')) { Str[j]=Str[j+i];
InText = true; }
} Str[j]='\0';
if (InText) { }
NewStr[IdxDest++] = Str[IdxSrc];
} i = strlen(Str)-1;
} while((Str[i] == ' ')||(Str[i]=='\t'))
NewStr[IdxDest] = '\0'; {
} else { i--;
IdxDest = StrLn - 1; }
strcpy_s(NewStr, StrLn, Str); if(i < (strlen(Str)-1))
} {
if ((Type == TRIM_RIGHT) || (Type == TRIM_BOTH)) { Str[i+1] = '\0';
while(--IdxDest > 0) { }
if ((NewStr[IdxDest] != ' ') && (NewStr[IdxDest] != '\n') && (NewStr[IdxDest] != '\t')) { return 0;
break;
}
}
NewStr[IdxDest] = '\0';
}
strcpy_s(Str, StrLn, NewStr);
delete NewStr;
return IdxDest;
} }
DWORD INI_FILE::GetFileStringFromNum(DWORD StringNumber, char *RetString, DWORD Size) DWORD INI_FILE::GetFileStringFromNum(DWORD StringNumber, char *RetString, DWORD Size)
@ -173,8 +165,8 @@ bool INI_FILE::FillVariable(INI_SECTION_VARIABLE *Variable, char *Str, DWORD Str
{ {
memcpy(Variable->VariableName, Str, i); memcpy(Variable->VariableName, Str, i);
memcpy(Variable->VariableValue, &(Str[i+1]), StrSize-(i-1)); memcpy(Variable->VariableValue, &(Str[i+1]), StrSize-(i-1));
//StrTrim(Variable->VariableName, TRIM_BOTH); StrTrim(Variable->VariableName);
//StrTrim(Variable->VariableValue, TRIM_BOTH); StrTrim(Variable->VariableValue);
break; break;
} }
} }
@ -193,14 +185,14 @@ bool INI_FILE::Parse()
DWORD CurrentSectionNum = -1; DWORD CurrentSectionNum = -1;
DWORD CurrentVariableNum = -1; DWORD CurrentVariableNum = -1;
// Calculate section count // Calculate sections count
for(DWORD CurrentStringNum = 0; CurrentStringNum < FileStringsCount; CurrentStringNum++) for(DWORD CurrentStringNum = 0; CurrentStringNum < FileStringsCount; CurrentStringNum++)
{ {
CurrentStringSize = GetFileStringFromNum(CurrentStringNum, CurrentString, 512); CurrentStringSize = GetFileStringFromNum(CurrentStringNum, CurrentString, 512);
if(CurrentString[0] == ';') continue; // It's a comment if(CurrentString[0] == ';') continue; // It's comment
if(CurrentString[0] == '[' && CurrentString[CurrentStringSize-1] == ']') // It's section declaration if(CurrentString[0] == '[' && CurrentString[CurrentStringSize-1] == ']') // It's section diclarate
{ {
SectionsCount++; SectionsCount++;
continue; continue;
@ -214,10 +206,10 @@ bool INI_FILE::Parse()
{ {
CurrentStringSize = GetFileStringFromNum(CurrentStringNum, CurrentString, 512); CurrentStringSize = GetFileStringFromNum(CurrentStringNum, CurrentString, 512);
if(CurrentString[0] == ';') continue; // It's a comment if(CurrentString[0] == ';') continue; // It's comment
if(CurrentString[0] == '[' && CurrentString[CurrentStringSize-1] == ']') // It's section declaration if(CurrentString[0] == '[' && CurrentString[CurrentStringSize-1] == ']') // It's section diclarate
{ {
CurrentSectionNum++; CurrentSectionNum++;
continue; continue;
@ -250,7 +242,7 @@ bool INI_FILE::Parse()
{ {
CurrentStringSize = GetFileStringFromNum(CurrentStringNum, CurrentString, 512); CurrentStringSize = GetFileStringFromNum(CurrentStringNum, CurrentString, 512);
if(CurrentString[0] == ';') // It's a comment if(CurrentString[0] == ';') // It's comment
{ {
continue; continue;
} }
@ -286,7 +278,7 @@ bool INI_FILE::SectionExists(char *SectionName)
return false; return false;
} }
bool INI_FILE::GetVariableInSection(char *SectionName, char *VariableName, INI_VAR_STRING *RetVariable) bool INI_FILE::GetVariableInSectionPrivate(char *SectionName, char *VariableName, INI_SECTION_VARIABLE *RetVariable)
{ {
INI_SECTION *Section = NULL; INI_SECTION *Section = NULL;
INI_SECTION_VARIABLE *Variable = NULL; INI_SECTION_VARIABLE *Variable = NULL;
@ -305,6 +297,7 @@ bool INI_FILE::GetVariableInSection(char *SectionName, char *VariableName, INI_V
SetLastError(318); // This region is not found SetLastError(318); // This region is not found
return false; return false;
} }
// Find variable // Find variable
for(DWORD i = 0; i < Section->VariablesCount; i++) for(DWORD i = 0; i < Section->VariablesCount; i++)
{ {
@ -321,106 +314,66 @@ bool INI_FILE::GetVariableInSection(char *SectionName, char *VariableName, INI_V
} }
memset(RetVariable, 0x00, sizeof(*RetVariable)); memset(RetVariable, 0x00, sizeof(*RetVariable));
memcpy(RetVariable->Name, Variable->VariableName, strlen(Variable->VariableName)); memcpy(RetVariable, Variable, sizeof(*Variable));
memcpy(RetVariable->Value, Variable->VariableValue, strlen(Variable->VariableValue));
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);
if(!Status) return Status;
memset(RetVariable, 0x00, sizeof(*RetVariable));
memcpy(RetVariable->Name, Variable.VariableName, strlen(Variable.VariableName));
memcpy(RetVariable->Value, Variable.VariableValue, strlen(Variable.VariableValue));
return true; return true;
} }
bool INI_FILE::GetVariableInSection(char *SectionName, char *VariableName, INI_VAR_DWORD *RetVariable) bool INI_FILE::GetVariableInSection(char *SectionName, char *VariableName, INI_VAR_DWORD *RetVariable)
{ {
INI_SECTION *Section = NULL; bool Status = false;
INI_SECTION_VARIABLE *Variable = NULL; INI_SECTION_VARIABLE Variable = {};
// Find section Status = GetVariableInSectionPrivate(SectionName, VariableName, &Variable);
for(DWORD i = 0; i < IniData.SectionCount; i++) if(!Status) return Status;
{
if(memcmp(IniData.Section[i].SectionName, SectionName, strlen(SectionName)) == 0)
{
Section = &(IniData.Section[i]);
break;
}
}
if(Section == NULL)
{
SetLastError(318); // This region is not found
return false;
}
// Find variable
for(DWORD i = 0; i < Section->VariablesCount; i++)
{
if(memcmp(Section->Variables[i].VariableName, VariableName, strlen(VariableName)) == 0)
{
Variable = &(Section->Variables[i]);
break;
}
}
if(Variable == NULL)
{
SetLastError(1898); // Member of the group is not found
return false;
}
memset(RetVariable, 0x00, sizeof(*RetVariable)); memset(RetVariable, 0x00, sizeof(*RetVariable));
memcpy(RetVariable->Name, Variable->VariableName, strlen(Variable->VariableName)); memcpy(RetVariable->Name, Variable.VariableName, strlen(Variable.VariableName));
#ifndef _WIN64 #ifndef _WIN64
RetVariable->ValueDec = strtol(Variable->VariableValue, NULL, 10); RetVariable->ValueDec = strtol(Variable.VariableValue, NULL, 10);
RetVariable->ValueHex = strtol(Variable->VariableValue, NULL, 16); RetVariable->ValueHex = strtol(Variable.VariableValue, NULL, 16);
#else #else
RetVariable->ValueDec = _strtoi64(Variable->VariableValue, NULL, 10); RetVariable->ValueDec = _strtoi64(Variable.VariableValue, NULL, 10);
RetVariable->ValueHex = _strtoi64(Variable->VariableValue, NULL, 16); RetVariable->ValueHex = _strtoi64(Variable.VariableValue, NULL, 16);
#endif #endif
return true; return true;
} }
bool INI_FILE::GetVariableInSection(char *SectionName, char *VariableName, INI_VAR_BYTEARRAY *RetVariable) bool INI_FILE::GetVariableInSection(char *SectionName, char *VariableName, INI_VAR_BYTEARRAY *RetVariable)
{ {
INI_SECTION *Section = NULL; bool Status = false;
INI_SECTION_VARIABLE *Variable = NULL; INI_SECTION_VARIABLE Variable = {};
// Find section Status = GetVariableInSectionPrivate(SectionName, VariableName, &Variable);
for(DWORD i = 0; i < IniData.SectionCount; i++) if(!Status) return Status;
{
if(memcmp(IniData.Section[i].SectionName, SectionName, strlen(SectionName)) == 0)
{
Section = &(IniData.Section[i]);
break;
}
}
if(Section == NULL)
{
SetLastError(318); // This region is not found
return false;
}
// Find variable
for(DWORD i = 0; i < Section->VariablesCount; i++)
{
if(memcmp(Section->Variables[i].VariableName, VariableName, strlen(VariableName)) == 0)
{
Variable = &(Section->Variables[i]);
break;
}
}
if(Variable == NULL)
{
SetLastError(1898); // Member of the group is not found
return false;
}
DWORD ValueLen = strlen(Variable->VariableName); DWORD ValueLen = strlen(Variable.VariableValue);
if((ValueLen % 2) != 0) return false; if((ValueLen % 2) != 0) return false;
memset(RetVariable, 0x00, sizeof(*RetVariable)); memset(RetVariable, 0x00, sizeof(*RetVariable));
memcpy(RetVariable->Name, Variable->VariableName, ValueLen); memcpy(RetVariable->Name, Variable.VariableName, ValueLen);
char Mask[15] = {};
for(DWORD i = 0; i <= ValueLen; i++) for(DWORD i = 0; i <= ValueLen; i++)
{ {
if((i % 2) != 0) continue; if((i % 2) != 0) continue;
switch(Variable->VariableValue[i]) switch(Variable.VariableValue[i])
{ {
case '0': break; case '0': break;
case '1': RetVariable->Value[(i/2)] += (1 << 4); break; case '1': RetVariable->Value[(i/2)] += (1 << 4); break;
@ -440,9 +393,9 @@ bool INI_FILE::GetVariableInSection(char *SectionName, char *VariableName, INI_V
case 'F': RetVariable->Value[(i/2)] += (15 << 4); break; case 'F': RetVariable->Value[(i/2)] += (15 << 4); break;
} }
switch(Variable->VariableValue[i+1]) switch(Variable.VariableValue[i+1])
{ {
case '0': break; case '0': break;
case '1': RetVariable->Value[(i/2)] += 1; break; case '1': RetVariable->Value[(i/2)] += 1; break;
case '2': RetVariable->Value[(i/2)] += 2; break; case '2': RetVariable->Value[(i/2)] += 2; break;
case '3': RetVariable->Value[(i/2)] += 3; break; case '3': RetVariable->Value[(i/2)] += 3; break;
@ -466,39 +419,13 @@ bool INI_FILE::GetVariableInSection(char *SectionName, char *VariableName, INI_V
bool INI_FILE::GetVariableInSection(char *SectionName, char *VariableName, INI_VAR_BOOL *RetVariable) bool INI_FILE::GetVariableInSection(char *SectionName, char *VariableName, INI_VAR_BOOL *RetVariable)
{ {
INI_SECTION *Section = NULL; bool Status = false;
INI_SECTION_VARIABLE *Variable = NULL; INI_SECTION_VARIABLE Variable = {};
// Find section Status = GetVariableInSectionPrivate(SectionName, VariableName, &Variable);
for(DWORD i = 0; i < IniData.SectionCount; i++) if(!Status) return Status;
{
if(memcmp(IniData.Section[i].SectionName, SectionName, strlen(SectionName)) == 0)
{
Section = &(IniData.Section[i]);
break;
}
}
if(Section == NULL)
{
SetLastError(318); // This region is not found
return false;
}
// Find variable
for(DWORD i = 0; i < Section->VariablesCount; i++)
{
if(memcmp(Section->Variables[i].VariableName, VariableName, strlen(VariableName)) == 0)
{
Variable = &(Section->Variables[i]);
break;
}
}
if(Variable == NULL)
{
SetLastError(1898); // Member of the group is not found
return false;
}
memset(RetVariable, 0x00, sizeof(*RetVariable)); memset(RetVariable, 0x00, sizeof(*RetVariable));
RetVariable->Value = (bool)strtol(Variable->VariableValue, NULL, 10); RetVariable->Value = (bool)strtol(Variable.VariableValue, NULL, 10);
return true; return true;
} }