mirror of
https://github.com/simtactics/mysimulation.git
synced 2025-07-08 15:40:33 -04:00
Added FSO.Files for use with the API server
Don't ask me. FreeSO is the prime example of dependency hell.
This commit is contained in:
parent
4b5e584eeb
commit
8fec258215
104 changed files with 14653 additions and 163 deletions
59
server/tso.files/IniFile.cs
Executable file
59
server/tso.files/IniFile.cs
Executable file
|
@ -0,0 +1,59 @@
|
|||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
|
||||
namespace FSO.Files
|
||||
{
|
||||
public class IniFile : Dictionary<string, IniSection>
|
||||
{
|
||||
public static IniFile Read(string path){
|
||||
using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read)){
|
||||
var result = new IniFile();
|
||||
result.Decode(stream);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void Decode(Stream stream)
|
||||
{
|
||||
IniSection currentSection = null;
|
||||
|
||||
using (var reader = new StreamReader(stream)){
|
||||
string line = null;
|
||||
|
||||
while((line = reader.ReadLine()) != null){
|
||||
line = line.TrimStart();
|
||||
|
||||
if (line.StartsWith(";")){
|
||||
//Comment
|
||||
}else if (line.StartsWith("[")){
|
||||
//Section
|
||||
currentSection = new IniSection();
|
||||
currentSection.Name = line.Substring(1);
|
||||
currentSection.Name = currentSection.Name.Substring(0, currentSection.Name.LastIndexOf("]"));
|
||||
this.Add(currentSection.Name, currentSection);
|
||||
}
|
||||
else
|
||||
{
|
||||
//Could be a key / value
|
||||
var split = line.IndexOf("=");
|
||||
if(split != -1 && currentSection != null && split+1 < line.Length){
|
||||
var key = line.Substring(0, split).Trim();
|
||||
var value = line.Substring(split + 1).TrimStart();
|
||||
|
||||
if(key.Length > 0)
|
||||
{
|
||||
currentSection[key] = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class IniSection : Dictionary<string, string>
|
||||
{
|
||||
public string Name { get; set; }
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue