more yaml read/write flexibility: strings
This commit is contained in:
@@ -21,40 +21,48 @@ namespace OpenRa.FileFormats
|
|||||||
Nodes = nodes;
|
Nodes = nodes;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Dictionary<string, MiniYaml> FromFile( string path )
|
static Dictionary<string, MiniYaml> FromLines(string[] lines)
|
||||||
{
|
{
|
||||||
var lines = File.ReadAllLines( path );
|
|
||||||
|
|
||||||
var levels = new List<Dictionary<string, MiniYaml>>();
|
var levels = new List<Dictionary<string, MiniYaml>>();
|
||||||
levels.Add( new Dictionary<string, MiniYaml>() );
|
levels.Add(new Dictionary<string, MiniYaml>());
|
||||||
|
|
||||||
foreach( var line in lines )
|
foreach (var line in lines)
|
||||||
{
|
{
|
||||||
var t = line.TrimStart( ' ', '\t' );
|
var t = line.TrimStart(' ', '\t');
|
||||||
if( t.Length == 0 || t[ 0 ] == '#' )
|
if (t.Length == 0 || t[0] == '#')
|
||||||
continue;
|
continue;
|
||||||
var level = line.Length - t.Length;
|
var level = line.Length - t.Length;
|
||||||
|
|
||||||
if( levels.Count <= level )
|
if (levels.Count <= level)
|
||||||
throw new InvalidOperationException( "Bad indent in miniyaml" );
|
throw new InvalidOperationException("Bad indent in miniyaml");
|
||||||
while( levels.Count > level + 1 )
|
while (levels.Count > level + 1)
|
||||||
levels.RemoveAt( levels.Count - 1 );
|
levels.RemoveAt(levels.Count - 1);
|
||||||
|
|
||||||
var colon = t.IndexOf( ':' );
|
var colon = t.IndexOf(':');
|
||||||
var d = new Dictionary<string, MiniYaml>();
|
var d = new Dictionary<string, MiniYaml>();
|
||||||
|
|
||||||
if( colon == -1 )
|
if (colon == -1)
|
||||||
levels[ level ].Add( t.Trim(), new MiniYaml( null, d ) );
|
levels[level].Add(t.Trim(), new MiniYaml(null, d));
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
var value = t.Substring( colon + 1 ).Trim();
|
var value = t.Substring(colon + 1).Trim();
|
||||||
if( value.Length == 0 )
|
if (value.Length == 0)
|
||||||
value = null;
|
value = null;
|
||||||
levels[ level ].Add( t.Substring( 0, colon ).Trim(), new MiniYaml( value, d ) );
|
levels[level].Add(t.Substring(0, colon).Trim(), new MiniYaml(value, d));
|
||||||
}
|
}
|
||||||
levels.Add( d );
|
levels.Add(d);
|
||||||
}
|
}
|
||||||
return levels[ 0 ];
|
return levels[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Dictionary<string, MiniYaml> FromFile( string path )
|
||||||
|
{
|
||||||
|
return FromLines(File.ReadAllLines( path ));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Dictionary<string, MiniYaml> FromString(string text)
|
||||||
|
{
|
||||||
|
return FromLines(text.Split('\n'));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Dictionary<string, MiniYaml> Merge( Dictionary<string, MiniYaml> a, Dictionary<string, MiniYaml> b )
|
public static Dictionary<string, MiniYaml> Merge( Dictionary<string, MiniYaml> a, Dictionary<string, MiniYaml> b )
|
||||||
@@ -116,6 +124,11 @@ namespace OpenRa.FileFormats
|
|||||||
File.WriteAllLines(filename, y.ToLines(true).Select(x => x.TrimEnd()).ToArray());
|
File.WriteAllLines(filename, y.ToLines(true).Select(x => x.TrimEnd()).ToArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static string WriteToString(this MiniYamlNodes y)
|
||||||
|
{
|
||||||
|
return string.Join("\n", y.ToLines(true).Select(x => x.TrimEnd()).ToArray());
|
||||||
|
}
|
||||||
|
|
||||||
public static IEnumerable<string> ToLines(this MiniYamlNodes y, bool lowest)
|
public static IEnumerable<string> ToLines(this MiniYamlNodes y, bool lowest)
|
||||||
{
|
{
|
||||||
foreach (var kv in y)
|
foreach (var kv in y)
|
||||||
|
|||||||
Reference in New Issue
Block a user