StyleCop clean IniFile
This commit is contained in:
@@ -20,93 +20,93 @@ namespace OpenRA.FileFormats
|
|||||||
{
|
{
|
||||||
Dictionary<string, IniSection> sections = new Dictionary<string, IniSection>();
|
Dictionary<string, IniSection> sections = new Dictionary<string, IniSection>();
|
||||||
|
|
||||||
public IniFile( Stream s )
|
public IniFile(Stream s)
|
||||||
{
|
{
|
||||||
Load( s );
|
Load(s);
|
||||||
}
|
}
|
||||||
|
|
||||||
public IniFile( params Stream[] streams )
|
public IniFile(params Stream[] streams)
|
||||||
{
|
{
|
||||||
foreach( var s in streams )
|
foreach (var s in streams)
|
||||||
Load( s );
|
Load(s);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Load( Stream s )
|
public void Load(Stream s)
|
||||||
{
|
{
|
||||||
StreamReader reader = new StreamReader( s );
|
StreamReader reader = new StreamReader(s);
|
||||||
IniSection currentSection = null;
|
IniSection currentSection = null;
|
||||||
|
|
||||||
while( !reader.EndOfStream )
|
while (!reader.EndOfStream)
|
||||||
{
|
{
|
||||||
string line = reader.ReadLine();
|
var line = reader.ReadLine();
|
||||||
|
|
||||||
if( line.Length == 0 ) continue;
|
if (line.Length == 0) continue;
|
||||||
|
|
||||||
switch( line[ 0 ] )
|
switch (line[0])
|
||||||
{
|
{
|
||||||
case ';': break;
|
case ';': break;
|
||||||
case '[': currentSection = ProcessSection( line ); break;
|
case '[': currentSection = ProcessSection(line); break;
|
||||||
default: ProcessEntry( line, currentSection ); break;
|
default: ProcessEntry(line, currentSection); break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Regex sectionPattern = new Regex( @"^\[([^]]*)\]" );
|
Regex sectionPattern = new Regex(@"^\[([^]]*)\]");
|
||||||
|
|
||||||
IniSection ProcessSection( string line )
|
IniSection ProcessSection(string line)
|
||||||
{
|
{
|
||||||
Match m = sectionPattern.Match( line );
|
Match m = sectionPattern.Match(line);
|
||||||
if( m == null || !m.Success )
|
if (m == null || !m.Success)
|
||||||
return null;
|
return null;
|
||||||
string sectionName = m.Groups[ 1 ].Value.ToLowerInvariant();
|
string sectionName = m.Groups[1].Value.ToLowerInvariant();
|
||||||
|
|
||||||
IniSection ret;
|
IniSection ret;
|
||||||
if( !sections.TryGetValue( sectionName, out ret ) )
|
if (!sections.TryGetValue(sectionName, out ret))
|
||||||
sections.Add( sectionName, ret = new IniSection( sectionName ) );
|
sections.Add(sectionName, ret = new IniSection(sectionName));
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ProcessEntry( string line, IniSection currentSection )
|
bool ProcessEntry(string line, IniSection currentSection)
|
||||||
{
|
{
|
||||||
int comment = line.IndexOf( ';' );
|
var comment = line.IndexOf(';');
|
||||||
if( comment >= 0 )
|
if (comment >= 0)
|
||||||
line = line.Substring( 0, comment );
|
line = line.Substring(0, comment);
|
||||||
|
|
||||||
line = line.Trim();
|
line = line.Trim();
|
||||||
if( line.Length == 0 )
|
if (line.Length == 0)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
var key = line;
|
var key = line;
|
||||||
var value = "";
|
var value = "";
|
||||||
int eq = line.IndexOf( '=' );
|
int eq = line.IndexOf('=');
|
||||||
if( eq >= 0 )
|
if (eq >= 0)
|
||||||
{
|
{
|
||||||
key = line.Substring( 0, eq );
|
key = line.Substring(0, eq);
|
||||||
value = line.Substring( eq + 1, line.Length - eq - 1 );
|
value = line.Substring(eq + 1, line.Length - eq - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
if( currentSection == null )
|
if (currentSection == null)
|
||||||
throw new InvalidOperationException( "No current INI section" );
|
throw new InvalidOperationException("No current INI section");
|
||||||
|
|
||||||
if( !currentSection.Contains( key ) )
|
if (!currentSection.Contains(key))
|
||||||
currentSection.Add( key, value );
|
currentSection.Add(key, value);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public IniSection GetSection( string s )
|
public IniSection GetSection(string s)
|
||||||
{
|
{
|
||||||
return GetSection( s, false );
|
return GetSection(s, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public IniSection GetSection( string s, bool allowFail )
|
public IniSection GetSection(string s, bool allowFail)
|
||||||
{
|
{
|
||||||
IniSection section;
|
IniSection section;
|
||||||
if( sections.TryGetValue( s.ToLowerInvariant(), out section ) )
|
if (sections.TryGetValue(s.ToLowerInvariant(), out section))
|
||||||
return section;
|
return section;
|
||||||
|
|
||||||
if( allowFail )
|
if (allowFail)
|
||||||
return new IniSection( s );
|
return new IniSection(s);
|
||||||
throw new InvalidOperationException( "Section does not exist in map or rules: " + s );
|
throw new InvalidOperationException("Section does not exist in map or rules: " + s);
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<IniSection> Sections { get { return sections.Values; } }
|
public IEnumerable<IniSection> Sections { get { return sections.Values; } }
|
||||||
@@ -117,25 +117,25 @@ namespace OpenRA.FileFormats
|
|||||||
public string Name { get; private set; }
|
public string Name { get; private set; }
|
||||||
Dictionary<string, string> values = new Dictionary<string, string>();
|
Dictionary<string, string> values = new Dictionary<string, string>();
|
||||||
|
|
||||||
public IniSection( string name )
|
public IniSection(string name)
|
||||||
{
|
{
|
||||||
Name = name;
|
Name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Add( string key, string value )
|
public void Add(string key, string value)
|
||||||
{
|
{
|
||||||
values[key] = value;
|
values[key] = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool Contains( string key )
|
public bool Contains(string key)
|
||||||
{
|
{
|
||||||
return values.ContainsKey( key );
|
return values.ContainsKey(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
public string GetValue( string key, string defaultValue )
|
public string GetValue(string key, string defaultValue)
|
||||||
{
|
{
|
||||||
string s;
|
string s;
|
||||||
return values.TryGetValue( key, out s ) ? s : defaultValue;
|
return values.TryGetValue(key, out s) ? s : defaultValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerator<KeyValuePair<string, string>> GetEnumerator()
|
public IEnumerator<KeyValuePair<string, string>> GetEnumerator()
|
||||||
|
|||||||
Reference in New Issue
Block a user