_ParamParser.cs
上传用户:horngjaan
上传日期:2009-12-12
资源大小:2882k
文件大小:3k
- using System;
- using System.Collections;
- namespace LumiSoft.MailServer
- {
- #region class _Parameter
- internal class _Parameter
- {
- private string m_ParamName = "";
- private string m_ParamValue = "";
- /// <summary>
- /// Default constructor.
- /// </summary>
- /// <param name="paramName"></param>
- /// <param name="paramValue"></param>
- public _Parameter(string paramName,string paramValue)
- {
- m_ParamName = paramName;
- m_ParamValue = paramValue;
- }
- #region Properties implementation
- /// <summary>
- ///
- /// </summary>
- public string ParamName
- {
- get{ return m_ParamName; }
- }
- /// <summary>
- ///
- /// </summary>
- public string ParamValue
- {
- get{ return m_ParamValue; }
- }
- #endregion
- }
- #endregion
- /// <summary>
- /// Summary description for _ParamParser.
- /// </summary>
- internal class _ParamParser
- {
- // public _ParamParser()
- // {
- // }
-
- #region function Parse
- /// <summary>
- /// Parses parameters.
- /// </summary>
- /// <param name="argsText">Arguments text.</param>
- /// <param name="paramNames">Array of wanted parameters.</param>
- /// <param name="paramValueSeparators">Array of possible separators(name/value).</param>
- /// <returns>Returns array of parsed parametes.</returns>
- public static _Parameter[] Parse(string argsText,string[] paramNames,string[] paramValueSeparators)
- {
- //--- Remove continues spaces ---------//
- // eg, aaa= 5 would be aaa=5
- while(true){
- if(argsText.IndexOf(" ") == -1){
- break;
- }
- argsText = argsText.Replace(" "," ");
- }
- //--------------------------------------//
- //--- Remove spaces between parameter and value ---//
- // eg, FROM : aa would be FROM:a
- foreach(string sep in paramValueSeparators){
- int iSepPos = 0;
- while(argsText.IndexOf(sep,iSepPos) > -1){
- iSepPos = argsText.IndexOf(sep,iSepPos);
- //1) check left side of separator
- //2) check right side of separator
- if(iSepPos > 1){
- // if left side char is ' ', remove it.
- if(argsText.Substring(iSepPos-1,1) == " "){
- argsText = argsText.Remove(iSepPos-1,1);
- iSepPos--;
- }
- }
- // if right side char is ' ', remove it.
- if(iSepPos < argsText.Length-1 && argsText.Substring(iSepPos+1,1) == " "){
- argsText = argsText.Remove(iSepPos+1,1);
- }
- iSepPos++;
- }
- }
- //-------------------------------------------------//
- ArrayList paramList = new ArrayList();
- string[] param = argsText.Split(new char[]{' '});
- foreach(string parameterInfo in param){
- string name = parameterInfo;
- string val = "";
- // Try to split paramName and paramValue
- foreach(string paramName in paramNames){
- if(parameterInfo.ToUpper().StartsWith(paramName)){
- name = paramName;
- // Gets parameter value.
- if(parameterInfo.Length > paramName.Length){
- val = parameterInfo.Substring(paramName.Length);
- }
-
- break;
- }
- }
- paramList.Add(new _Parameter(name,val));
- }
- _Parameter[] retVal = new _Parameter[paramList.Count];
- paramList.CopyTo(retVal);
- return retVal;
- }
- #endregion
- }
- }