Beispiel #1
0
    /// <summary>
    /// 将形如#1(int)的数据定义解析为要输出的字符串
    /// </summary>
    private static string _GetDataInIndexType(DataInIndexDefine define, string oneDataString, out string errorString)
    {
        // 一组数据中的子元素用英文逗号分隔
        string[] allElementString = oneDataString.Trim().Split(new char[] { ',' }, System.StringSplitOptions.RemoveEmptyEntries);
        // 检查是否存在指定序号的数据
        if (allElementString.Length < define.DataIndex)
        {
            errorString = string.Format("解析#{0}({1})类型的数据错误,输入的数据中只有{2}个子元素", define.DataIndex, define.DataType.ToString(), allElementString.Length);
            return(null);
        }
        // 检查是否为指定类型的合法数据
        string inputString = allElementString[define.DataIndex - 1];

        if (define.DataType != DataType.String)
        {
            inputString = inputString.Trim();
        }

        string value = _GetDataStringInTableString(inputString, define.DataType, out errorString);

        if (errorString != null)
        {
            errorString = string.Format("解析#{0}({1})类型的数据错误,", define.DataIndex, define.DataType.ToString()) + errorString;
            return(null);
        }
        else
        {
            return(value);
        }
    }
    /// <summary>
    /// 将形如#1(int)的格式定义字符串转为DataInIndexDefine定义
    /// </summary>
    private static DataInIndexDefine _GetDataInIndexDefine(string defineString, out string errorString)
    {
        DataInIndexDefine dataInIndexDefine = new DataInIndexDefine();

        // 检查#后是否跟合法数字,且数字后面用括号注明类型
        defineString = defineString.Substring(1).Trim();
        // 检查类型是否合法
        int leftBracketIndex  = defineString.IndexOf('(');
        int rightBracketIndex = defineString.LastIndexOf(')');

        if (leftBracketIndex == -1 || rightBracketIndex != defineString.Length - 1)
        {
            errorString = "未注明格式类型,需要形如:#1(int)";
            return(dataInIndexDefine);
        }
        else
        {
            string dataTypeString = defineString.Substring(leftBracketIndex + 1, rightBracketIndex - leftBracketIndex - 1).Trim();
            dataInIndexDefine.DataType = _AnalyzeDataType(dataTypeString);
            if (!(dataInIndexDefine.DataType == DataType.Int || dataInIndexDefine.DataType == DataType.Float || dataInIndexDefine.DataType == DataType.Bool || dataInIndexDefine.DataType == DataType.String || dataInIndexDefine.DataType == DataType.Lang))
            {
                errorString = "格式类型非法,只支持int、float、bool、string、lang这几种类型";
                return(dataInIndexDefine);
            }
        }
        // 检查数据索引值是否合法
        string dataIndexString = defineString.Substring(0, leftBracketIndex).Trim();
        int    dataIndex;

        if (int.TryParse(dataIndexString, out dataIndex))
        {
            if (dataIndex > 0)
            {
                dataInIndexDefine.DataIndex = dataIndex;
            }
            else
            {
                errorString = "数据索引值编号最小要从1开始";
                return(dataInIndexDefine);
            }
        }
        else
        {
            errorString = string.Format("数据索引值不是合法数字,你的输入值为:{0}", dataIndexString);
            return(dataInIndexDefine);
        }

        errorString = null;
        return(dataInIndexDefine);
    }