Created
January 4, 2016 10:26
-
-
Save McTwist/a44eff14f185d76c076f to your computer and use it in GitHub Desktop.
Parsing INI in TorqueScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ================ | |
// INI Parser | |
// ================ | |
// Author: McTwist | |
// Version: 1.0 | |
// Description: Parses ini code in an easy way | |
// License: Free to use | |
// ================ | |
// INI reading | |
// Creating the object | |
function INI::onAdd(%this) | |
{ | |
if (!isObject(%this)) | |
return; | |
// Default properties | |
%this.useSections = 1; | |
%this.Reset(); | |
} | |
// Removing the object | |
function INI::onRemove(%this) | |
{ | |
%this.data.delete(); | |
} | |
// Reset | |
function INI::Reset(%this) | |
{ | |
if (isObject(%this.data)) | |
%this.data.delete(); | |
%this.data = new ScriptObject(); | |
%this.currentSection = ""; | |
%this.currentVar = ""; | |
} | |
// Reads a file | |
function INI::ReadFile(%this, %file) | |
{ | |
%this.Reset(); | |
%stream = new FileObject(); | |
%stream.openForRead(%file); | |
// Read everything | |
while (!%stream.isEOF()) | |
%this.ParseNextLine(%stream.readLine()); | |
%stream.close(); | |
%stream.delete(); | |
%this.currentSection = ""; | |
%this.currentVar = ""; | |
} | |
// Reads a string | |
function INI::ReadString(%this, %string) | |
{ | |
%this.Reset(); | |
while (%string !$= "") | |
{ | |
%string = nextToken(%string, "line", "\n"); | |
%this.ParseNextLine(%line); | |
} | |
%this.currentSection = ""; | |
%this.currentVar = ""; | |
} | |
// Read line | |
function INI::ParseNextLine(%this, %line) | |
{ | |
if (!%this.quote) | |
{ | |
// Handle empty lines, even if specification tells not to | |
if (%line $= "") | |
return; | |
// Comments | |
if (getSubStr(%line, 0, 1) $= ";") | |
return; | |
// Section | |
if (getSubStr(%line, 0, 1) $= "[") | |
{ | |
%section = strpos(%line, "]"); | |
%this.currentSection = getSubStr(%line, 1, %section - 1); | |
return; | |
} | |
%assign = strpos(%line, "="); | |
// Invalid line | |
if (%assign == -1) | |
return; | |
%var = trim(getSubStr(%line, 0, %assign)); | |
// Array (PHP specification) | |
if ((%array = strpos(%var, "[")) != -1 && getSubStr(%var, %array, 2) $= "[]") | |
{ | |
%var = getSubStr(%var, 0, strlen(%var)-2); | |
%i = %this.Get(%this.currentSection, %var); | |
if (%i $= "") | |
%i = 0; | |
%this.Set(%this.currentSection, %var, %i+1); | |
%var = %var @ "_" @ %i; | |
} | |
%this.currentVar = %var; | |
%this.ParseNextValue(getSubStr(%line, %assign+1, strlen(%line))); | |
} | |
// Continue on the value | |
else | |
{ | |
%this.ParseNextValue(%line); | |
} | |
} | |
// Read a value | |
function INI::ParseNextValue(%this, %value, %i) | |
{ | |
%quote = strpos(%value, "\"", %assign); | |
// Quoted | |
if (%quote != -1 && !%this.quote) | |
{ | |
%this.quote = 1; | |
%value = getSubStr(%value, %quote+1, strlen(%value)); | |
} | |
// No quotes | |
else | |
{ | |
%value = (%this.quote) ? %value : trim(%value); | |
} | |
// Find end quote | |
if (%this.quote) | |
{ | |
%quote2 = 0; | |
while ((%quote2 = strpos(%value, "\"", %quote2)) != -1) | |
{ | |
if (%this.IsEscaped(%value, %quote2)) | |
{ | |
%quote2++; | |
continue; | |
} | |
break; | |
} | |
if (%quote2 == -1) | |
%quote2 = strlen(%value); | |
else | |
%this.quote = ""; | |
%value = getSubStr(%value, 0, %quote2); | |
if (%this.Get(%this.currentSection, %this.currentVar) !$= "") | |
%value = %this.Get(%this.currentSection, %this.currentVar) NL %value; | |
} | |
// Finish up by replacing escaped characters | |
%value = strreplace(%value, "\\\\", "\\"); | |
%value = strreplace(%value, "\\t", "\t"); | |
%value = strreplace(%value, "\\r\\n", "\n"); | |
%value = strreplace(%value, "\\n", "\n"); | |
%value = strreplace(%value, "\\r", "\n"); | |
%value = strreplace(%value, "\\;", ";"); | |
%value = strreplace(%value, "\\#", "#"); | |
%value = strreplace(%value, "\\=", "="); | |
%value = strreplace(%value, "\\:", ":"); | |
%this.Set(%this.currentSection, %this.currentVar, %value); | |
} | |
// Check if a string is escaped | |
function INI::IsEscaped(%this, %string, %pos) | |
{ | |
%escaped = 0; | |
for (%it = %pos-1; %it > 0 && getSubStr(%string, %it, 1) $= "\\"; %it--) | |
%escaped = !%escaped; | |
return %escaped; | |
} | |
// Get values | |
function INI::Get(%this, %section, %var, %i) | |
{ | |
if (%i !$= "") | |
%var = %var @ "_" @ %i; | |
if (%this.useSections) | |
return %this.data.var_[%section, %var]; | |
else | |
return %this.data.var_[%var]; | |
} | |
// Set values(Private) | |
function INI::Set(%this, %section, %var, %i, %data) | |
{ | |
if (%data $= "") | |
%data = %i; | |
else if (%i !$= "") | |
%var = %var @ "_" @ %i; | |
if (%this.useSections) | |
%this.data.var_[%section, %var] = %data; | |
else | |
%this.data.var_[%var] = %data; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment