-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSectionEntry.cs
86 lines (78 loc) · 2.55 KB
/
SectionEntry.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/* ***********************************************
* Copyright (c) 2009-2010 luoshasha. All rights reserved";
* CLR version: 4.0.30319.296"
* File name: SectionEntry.cs"
* Date: 12/5/2012 11:51:59 AM
* Author : luoshasha(sand)
* Email : [email protected]
* Description:
* History: created by luoshasha(sand) 12/5/2012 11:51:59 AM
* ***********************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SandLib.Util
{
/// <summary>
/// a section entry holds kv entry and empty lines, comments lines etc.
/// </summary>
public class SectionEntry : Entry
{
private List<Entry> _subentries = new List<Entry>();
private string _sec_name;
private Dictionary<string, KVEntry> _kvs = new Dictionary<string, KVEntry>();
public SectionEntry(string secname)
: base(EntryType.Section)
{
_sec_name = secname;
}
public string SectionName
{
get { return _sec_name; }
}
public void AddEntry(Entry entry)
{
if (entry.Type == EntryType.Section)
throw new InvalidOperationException("section entry can't contain another section!");
_subentries.Add(entry);
}
public void SetKeyValue(string key, string value)
{
if (_kvs.ContainsKey(key))
{
_kvs[key].Value = value;
}
else
{
//make a new entry
KVEntry kv = new KVEntry(key, value);
this.AddEntry(kv);
_kvs[key] = kv;
}
}
public KVEntry GetKVEntry(string key)
{
KVEntry kv = null;
_kvs.TryGetValue(key, out kv);
//return an empty entry avoiding null reference exception.
return kv ?? KVEntry.EmptyEntry;
}
/// <summary>
/// build entry lines
/// </summary>
/// <returns></returns>
public override string GetEntryLines()
{
StringBuilder sb = new StringBuilder();
sb.AppendFormat("[{0}]", _sec_name);
sb.Append("\r\n");
for (int i = 0; i < _subentries.Count; ++i)
{
string sublines = _subentries[i].GetEntryLines();
sb.AppendLine(sublines);
}
return sb.ToString();
}
}
}