using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using static System.Collections.Specialized.BitVector32;
namespace Apewer.Models
{
/// INI 节。
[Serializable]
public sealed class IniSection : ICollection, IEnumerable, IToJson
{
private List _fields = new List();
/// 节的名称(不含方括号)。
public string Name { get; set; }
///
public override string ToString() => $"Name = \"{Name}\", Count = {_fields.Count}";
#region ICollection
///
public int Count { get => _fields.Count; }
/// 集合是只读的。
/// 此值始终为 FALSE。
public bool IsReadOnly { get => false; }
/// 添加字段。
///
public void Add(TextField item)
{
if (item == null) throw new ArgumentNullException(nameof(item));
_fields.Add(item);
}
/// 清空字段。
public void Clear() => _fields.Clear();
/// 检查此集合是否包含指定的字段。
public bool Contains(TextField item) => _fields.Contains(item);
/// 复制字段到数组中。
///
///
///
public void CopyTo(TextField[] array, int arrayIndex)
{
if (array == null) throw new ArgumentNullException(nameof(array));
if (arrayIndex < 0) throw new ArgumentOutOfRangeException(nameof(arrayIndex));
if (array.Length - arrayIndex < _fields.Count) throw new ArgumentException("目标数组长度不足。");
_fields.CopyTo(array, arrayIndex);
}
/// 移除字段。
public bool Remove(TextField item)
{
var removed = false;
while (_fields.Contains(item))
{
_fields.Remove(item);
removed = true;
}
return removed;
}
#endregion
#region Enumerable
///
public IEnumerator GetEnumerator() => new Enumerator(i => i < _fields.Count ? new Class(_fields[i]) : null);
///
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
#endregion
#region IToJson
///
public Json ToJson()
{
var fields = Json.NewObject();
foreach (var field in _fields)
{
fields.SetProperty(field.Name, field.Value);
}
var json = Json.NewObject();
json.SetProperty("name", Name);
json.SetProperty("fields", fields);
return json;
}
#endregion
/// 检查此集合是否包含指定的字段。
/// 字段名称。
public bool Contains(string name)
{
var count = _fields.Count;
for (var i = 0; i < count; i++)
{
if (_fields[0].Name == name) return true;
}
return false;
}
/// 获取字段。
/// "
public TextField GetField(int index)
{
if (index < 0 || index >= _fields.Count) throw new ArgumentOutOfRangeException(nameof(index));
return _fields[index];
}
///
public string GetValue(string name)
{
if (name.IsEmpty()) throw new ArgumentNullException(nameof(name));
foreach (var field in _fields)
{
if (field.Name == name) return field.Value;
}
return null;
}
///
public void SetValue(string name, string value)
{
if (name.IsEmpty()) throw new ArgumentNullException(nameof(name));
var count = _fields.Count;
var setted = false;
for (var i = 0; i < count; i++)
{
if (_fields[i].Name == name)
{
_fields[i].Value = value;
setted = true;
}
}
if (!setted)
{
var field = new TextField(name, value);
_fields.Add(field);
}
}
/// 获取或设置字段。
public string this[string name] { get => GetValue(name); set => SetValue(name, value); }
///
public IniSection() { }
///
public IniSection(string name) : this()
{
this.Name = name;
}
///
///
public IniSection(string name, IEnumerable fields) : this(name)
{
if (fields == null) throw new ArgumentNullException(nameof(fields));
_fields.AddRange(fields.Map(x => x == null ? null : new TextField(x.Name, x.Value)).FindAll(x => x != null));
}
///
///
public IniSection(IEnumerable fields) : this(null, fields) { }
///
///
public IniSection(string name, IEnumerable> fields) : this(name)
{
if (fields == null) throw new ArgumentNullException(nameof(fields));
_fields.AddRange(fields.Map(x => new TextField(x)));
}
///
///
public IniSection(IEnumerable> fields) : this(null, fields) { }
}
}