zoukankan      html  css  js  c++  java
  • C#下的Config类, 支持Get, Set, GetList

    代码
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;

    namespace YakaLib
    {
        
    public class Config
        {
            
    private Dictionary<stringstring> _KeyValue = new Dictionary<stringstring>();
            
    private Dictionary<string, List<string>> _KeyList = new Dictionary<string, List<string>>();
            
    private Dictionary<string, List<string>> _KeyRichList = new Dictionary<string, List<string>>();
            
    private string configDir;
            
    public Config(string strConfigfile)
            {
                configDir 
    = strConfigfile;
                StreamReader sr 
    = new StreamReader(strConfigfile);
                
    while (!sr.EndOfStream)
                {
                    
    string strLine = sr.ReadLine();
                    strLine 
    = strLine.Replace(" """);
                    
    if (strLine.Contains('='))
                    {
                        
    int nIndex = strLine.IndexOf('=');
                        
    if (strLine.StartsWith("#"))
                        {
                            
    continue;
                        }

                        
    if (nIndex > 0 && strLine.Length > nIndex + 1)
                        {
                            _KeyValue[strLine.Substring(
    0, nIndex)] = strLine.Substring(nIndex + 1);
                        }
                    }
                    
    else if ((!strLine.StartsWith("#")) && strLine.Contains('{'))
                    {
                        List
    <string> list = new List<string>();
                        
    int nIndex = strLine.IndexOf('{');
                        
    if (strLine.StartsWith("#"))
                        {
                            
    continue;
                        }
                        
    if (nIndex > 0 && strLine.Length > nIndex)
                        {
                            _KeyList[strLine.Substring(
    0, nIndex)] = list;
                        }

                        
    string item = "";
                        
    while (!sr.EndOfStream)
                        {
                            item 
    = sr.ReadLine();
                            item 
    = item.Replace(" """);
                            
    if (item.StartsWith("#"|| item.Length == 0)
                            {
                                
    continue;
                            }
                            
    if ((!item.StartsWith("#")) && item.Contains('}'))
                            {
                                
    int itemsEnd = item.IndexOf('}');
                                item 
    = item.Substring(0, itemsEnd);
                                
    if (item.Length != 0)
                                {
                                    list.Add(item);
                                }
                                
    break;
                            }
                            list.Add(item);
                        }
                    }
                }
                sr.Close();

                FillRichDict();
            }
            
    public Config(List<string> strConfigList)
            {
                
    foreach(string str in strConfigList)
                {
                    
    string strLine = str.Replace(" """);
                    
    if (strLine.Contains('='))
                    {
                        
    int nIndex = strLine.IndexOf('=');
                        
    if (strLine.StartsWith("#"))
                        {
                            
    continue;
                        }

                        
    if (nIndex > 0 && strLine.Length > nIndex + 1)
                        {
                            _KeyValue[strLine.Substring(
    0, nIndex)] = strLine.Substring(nIndex + 1);
                        }
                    }
                }
                    
            }
            
    public override string ToString()
            {
                
    string str = "";
                StreamReader sr 
    = new StreamReader(configDir);
                
    while (!sr.EndOfStream)
                {
                    str 
    += sr.ReadLine();
                    str 
    += "\r\n";
                }
                
    return str;
            }

            
    public string Get(string key)
            {
                
    if (_KeyValue.ContainsKey(key))
                {
                    
    return _KeyValue[key];
                }
                
    else
                {
                    
    return "";
                }
            }

            
    public int Set(string key, string value)
            {
                
    int errorCode = -1;
                
    if (_KeyValue.ContainsKey(key))
                {
                    _KeyValue[key] 
    = value;
                    RefreshKeyToFile(key);
                    errorCode 
    = 0;
                }
                
    else
                {
                    errorCode 
    = -1;
                }

                
    return errorCode;
            }

            
    public void RefreshDictToFile()
            {
                
            }

            
    public void RefreshKeyToFile(string key)
            {
                
    if (_KeyValue.ContainsKey(key))
                {
                    List
    <string> fileStream = new List<string>();
                    
    //int KeyLineNumber = 0;
                    StreamReader sr = new StreamReader(configDir);
                    
    while (!sr.EndOfStream)
                    {
                        
    string strLine = sr.ReadLine();
                        
    if (strLine.Contains(key))
                        {
                            
    string[] strSplitedArr = strLine.Split('=');
                            
    if (strSplitedArr.Length == 2)
                            {
                                strSplitedArr[
    1= " " + _KeyValue[key];
                                strLine 
    = strSplitedArr[0+ "=" + strSplitedArr[1];
                            }
                        }
                        fileStream.Add(strLine);
                    }
                    sr.Close();

                    StreamWriter sw 
    = new StreamWriter(configDir);
                    
    foreach (string item in fileStream)
                    {
                        sw.WriteLine(item);
                        sw.Flush();
                    }
                    sw.Close();
                }
            }

            
    public List<string> GetList(string key)
            {
                
    if (_KeyList.ContainsKey(key))
                {
                    
    return _KeyList[key];
                }
                
    else
                {
                    
    return new List<string>();
                }
            }

            
    private void FillRichDict()
            {
                StreamReader sr 
    = new StreamReader(configDir);
                
    while (!sr.EndOfStream)
                {
                    
    string strLine = sr.ReadLine();
                    strLine 
    = strLine.Replace(" """);
                    
    if (strLine.Contains('='))
                    {
                        
    int nIndex = strLine.IndexOf('=');
                        
    if (strLine.StartsWith("#"))
                        {
                            
    continue;
                        }

                        
    if (nIndex > 0 && strLine.Length > nIndex + 1)
                        {
                            _KeyValue[strLine.Substring(
    0, nIndex)] = strLine.Substring(nIndex + 1);
                        }
                    }
                    
    else if ((!strLine.StartsWith("#")) && strLine.Contains('{'))
                    {
                        List
    <string> list = new List<string>();
                        
    int nIndex = strLine.IndexOf('{');
                        
    //if (strLine.StartsWith("#") && strLine.Contains("{"))
                        
    //{
                        
    //    continue;
                        
    //}
                        if (nIndex > 0 && strLine.Length > nIndex)
                        {
                            _KeyRichList[strLine.Substring(
    0, nIndex)] = list;
                        }

                        
    string item = "";
                        
    while (!sr.EndOfStream)
                        {
                            item 
    = sr.ReadLine();
                            
    //item = item.Replace(" ", "");
                            
    //if (item.StartsWith("#") || item.Length == 0)
                            
    //{
                            
    //    continue;
                            
    //}
                            if ((!item.StartsWith("#")) && item.Contains('}'))
                            {
                                
    int itemsEnd = item.IndexOf('}');
                                item 
    = item.Substring(0, itemsEnd);
                                
    if (item.Length != 0)
                                {
                                    list.Add(item);
                                }
                                
    break;
                            }
                            list.Add(item);
                        }
                    }
                }
                sr.Close();
            }

            
    public Dictionary<string, List<string>> GetKeyRichListDict()
            {
                
    return _KeyRichList;
            }

            
    public Dictionary<stringstring> GetKeyValueDict()
            {
                
    return _KeyValue;
            }

            
    public Dictionary<string, List<string>> GetKeyListDict()
            {
                
    return _KeyList;
            }

            
    public bool CheckLexical(ref string error)
            {
                StreamReader sr 
    = new StreamReader(configDir);
                UInt32 linenumber 
    = 0;
                
    int symbol = 0;
                
    while (!sr.EndOfStream)
                {
                    linenumber
    ++;
                    
    string str = sr.ReadLine();
                    
    if (str.StartsWith("#"))
                    {
                        
    continue;
                    }
                    
    if (str.Contains('{'))
                    {
                        symbol
    ++;
                    }
                    
    else if (str.Contains("}"))
                    {
                        
    if (symbol == 1)
                        {
                            symbol
    --;
                        }
                        
    else if (symbol == 0)
                        {
                            error 
    = "Lost a { to match } in line " + linenumber;
                            
    return false;
                        }
                    }
                }
                
    if (symbol == 0)
                {
                    
    return true;
                }
                
    else
                {
                    error 
    = "miss { or }";
                    
    return false;
                }
            }

            
    public bool CheckLexical()
            {
                StreamReader sr 
    = new StreamReader(configDir);
                UInt32 linenumber 
    = 0;
                
    int symbol = 0;
                
    while (!sr.EndOfStream)
                {
                    linenumber
    ++;
                    
    string str = sr.ReadLine();
                    
    if (str.StartsWith("#"))
                    {
                        
    continue;
                    }
                    
    if (str.Contains('{'))
                    {
                        symbol
    ++;
                    }
                    
    else if (str.Contains("}"))
                    {
                        
    if (symbol == 1)
                        {
                            symbol
    --;
                        }
                        
    else if (symbol == 0)
                        {
                            
    //error = "Lost a { to match } in line " + linenumber;
                            return false;
                        }
                    }
                }
                
    if (symbol == 0)
                {
                    
    return true;
                }
                
    else
                {
                    
    //error = "miss { or }";
                    return false;
                }
            }
        }
    }
  • 相关阅读:
    Appium [安装包] Appium 国内下载地址 (百度云盘,已更新至 AppiumDesktop_1.7.1)(转载)
    PLSQL 触发器
    Java解析Json数据的两种方式
    easyui combobox 动态加载数组数据
    js控制easyui datagrid列的显示和隐藏
    Js中for循环的阻塞机制
    数据库中的视图
    严重:Error configuring application listener of class org.springframework.web.util.IntrospectorCleanupListener
    eclipse背景设置什么颜色缓解眼睛疲劳之一
    Maven详解
  • 原文地址:https://www.cnblogs.com/yakashop/p/1867280.html
Copyright © 2011-2022 走看看