Exemple #1
0
        public static INIReader Open(string path, BlankCharacterOptions options = BlankCharacterOptions.OnlyDeleteLast)
        {
            if (!File.Exists(path))
            {
                return(null);
            }

            var values = InitializeFile(path, options);

            if (values == null)
            {
                return(null);
            }

            return(new INIReader()
            {
                values = values
            });
        }
Exemple #2
0
        private static string ProcessValue(string value, BlankCharacterOptions options)
        {
            string result = value;

            switch (options)
            {
            case BlankCharacterOptions.None:
            {
                break;
            }

            case BlankCharacterOptions.Delete:
            {
                result = value.Trim(null);
                break;
            }

            case BlankCharacterOptions.OnlyDeleteFirst:
            {
                result = value.TrimStart(null);
                break;
            }

            case BlankCharacterOptions.OnlyDeleteLast:
            {
                result = value.TrimEnd(null);
                break;
            }

            case BlankCharacterOptions.DeleteAll:
            {
                result = value.Replace(" ", string.Empty);
                break;
            }
            }

            return(result);
        }
Exemple #3
0
        private static Dictionary <string, Dictionary <string, string> > InitializeFile(string path, BlankCharacterOptions options)
        {
            FileStream   stream = null;
            StreamReader reader = null;

            try
            {
                stream = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.None);
                reader = new StreamReader(stream);

                Dictionary <string, Dictionary <string, string> > sections = new Dictionary <string, Dictionary <string, string> >();

                Dictionary <string, string> kvs = null;

                while (true)
                {
                    string line = reader.ReadLine();

                    if (line == null)
                    {
                        break;
                    }

                    if (line == string.Empty)
                    {
                        continue;
                    }

                    if (line.StartsWith("[") && line.EndsWith("]"))
                    {
                        // 是一个section
                        kvs = new Dictionary <string, string>();
                        string section = line.Substring(1, line.Length - 2);
                        sections[section] = kvs;
                    }
                    else
                    {
                        string[] items = line.Split(Splitter, StringSplitOptions.RemoveEmptyEntries);
                        if (items.Length == 0)
                        {
                            // 格式不正确
                            continue;
                        }

                        if (items.Length > 2)
                        {
                            // 有多个等于号,合并value
                            string key            = items[0];
                            int    separatorIndex = line.IndexOf('=');
                            string value          = line.Substring(separatorIndex + 1);
                            kvs[key] = ProcessValue(value, options);
                            continue;
                        }

                        if (items.Length == 2)
                        {
                            string key   = items[0];
                            string value = items[1];
                            kvs[key] = ProcessValue(value, options);
                            continue;
                        }
                    }
                }

                return(sections);
            }
            catch (Exception ex)
            {
                logger.ErrorFormat("打开INI文件异常, {0}, {1}", path, ex);
                return(null);
            }
            finally
            {
                if (stream != null)
                {
                    stream.Close();
                    stream.Dispose();
                }

                if (reader != null)
                {
                    reader.Close();
                    reader.Dispose();
                }
            }
        }