开发者

Using Key like "[]Server" into sortedDictionary, but why?

I am trying to understand and probably reuse part of DevExpress Demo code to save simple settings into ini file. I know I can use .NET System.Configuration doing what I want. Just for a smallish project, simple save it as a text file seem more flexible and light-weighted, at least that is what it seems.

Anyway, while I am playing with it, I am trying to understand why the code I am reading trying to add key as "[]Server" and "[]DBFormat" as key name. They do that for a reason I can not understand yet, I could probably use some help here.

Here is the Code I think relevant:

 public class IniFile {
        SortedDictionary<string, string> data = new SortedDictionary<string, string>();
        ...
        public void Load(string path) {
            using(StreamReader sr = new StreamReader(path)) {
                string folder = "[]";
                while(!sr.EndOfStream) {
                    string s = sr.ReadLine().Trim();
                    if(s.Length == 0 || s[0] == ';') continue;
                    if(s[0] == '[') {
                        folder = s;
                        continue;
                    }
                    string key, value;
                    int delim = s.IndexOf('=');
                    if(delim < 0) {
                        key = folder + s.Replace("[", string.Empty).Replace("]", string.Empty);
                        value = string.Empty;
                    } else {
                        key = folder + s.Remove(delim).TrimEnd().Replace("[", string.Empty).Replace("]", string.Empty);
                        value = s.Substring(delim + 1).TrimStart();
                    }
                    if(!data.ContainsKey(key)) data.Add(key, value);
                    else data[key] = value;
                }
            }
        }
        ...
        public void Save(string path) {
            using(StreamWriter sw = new StreamWriter(path)) {
                string folder = "[]";
                foreach(string key in data.Keys) {
                    int delim = key.IndexOf(']');
                    string keyFolder = key.Remove(delim + 1);
                    string keyName = key.Substring(delim + 1);
                    if(keyFolder != folder) {
                        folder = keyFolder;
                        sw.WriteLine(folder);
                    }
                    sw.WriteLine(keyName + " = " + data[key]);
                }
            }
        }

        void AddRawValue(string key, string value) {
            key = key.Trim();
            value = value.T开发者_运维百科rim();
            int folderBegin = key.IndexOf('[');
            int folderEnd = key.IndexOf(']');
            if(folderBegin != 0 || folderEnd < 0) throw new ArgumentException("key");
            data.Add(key, value);
        }

And here is part of the ini file itself:

DBFormat = "Mdb"
Login = "admin"
Password = ""
Server = "(localhost)"

Obvious, they go though the trouble to add [] into keyname for some reason, but end up not using it in the demo data. I am think they are using string inside [] to group settings?


I only skimmed it, but I'd imagine the intent might have been "[section]setting" so "[]setting" would represent a setting not in a section. Look how easy it is to get values, just a single string will do! (There seems to be confusion between a "section" and a "folder" -- which might be an above-par variable name for that code...)

Then again, I could be way off as I only invested about 10 seconds of time on that tripe >:)

Happy coding.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜