C#下的Config类, 支持Get, Set, GetList

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

namespace  YakaLib
{
    
public   class  Config
    {
        
private  Dictionary < string string >  _KeyValue  =   new  Dictionary < string string > ();
        
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 < string string >  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 ;
            }
        }
    }
}

 

转载于:https://www.cnblogs.com/yakashop/archive/2010/11/02/1867280.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值