예제 #1
0
        void ProcessSwitch(XElement node, bool output = true)
        {
            string name     = node.GetAttribute("name").Capitalize();
            string type     = propertyMap[name];
            var    typeInfo = TypeTable.GetType(type);

            unpack.AppendLine("switch({0})", name);
            unpack.OpenBlock();
            pack.AppendLine("switch({0})", name);
            pack.OpenBlock();
            foreach (XElement child in node.Elements())
            {
                if (child.Name == "case")
                {
                    string value  = child.GetAttribute("value");
                    var    values = value.Split('|');

                    foreach (var splitValue in values)
                    {
                        string caseValue = splitValue.Trim();
                        if (typeInfo.IsEnum)
                        {
                            EnumInfo enumInfo = (EnumInfo)typeInfo;
                            caseValue = type + "." + enumInfo.ValueMap.FirstOrDefault(x => x.Value == caseValue).Key;
                        }
                        switch (type)
                        {
                        case "string":
                            unpack.AppendLine("case \"{0}\":", caseValue);
                            pack.AppendLine("case \"{0}\":", caseValue);
                            break;

                        default:
                            unpack.AppendLine("case {0}:", caseValue);
                            pack.AppendLine("case {0}:", caseValue);
                            break;
                        }
                    }
                    unpack.Indent();
                    pack.Indent();
                    ProcessChildElements(child, output);
                    unpack.Unindent();
                    unpack.AppendLine("break;");
                    pack.Unindent();
                    pack.AppendLine("break;");
                }
            }
            unpack.CloseBlock();
            pack.CloseBlock();
        }
예제 #2
0
        void ProcessSubfield(string supername, string supertype, XElement node, bool output = true)
        {
            string   type           = node.GetAttribute("type");
            string   name           = node.GetAttribute("name").Capitalize();
            TypeInfo typeInfo       = TypeTable.GetType(type);
            bool     propertyExists = propertyMap.ContainsKey(name);

            if (!propertyExists)
            {
                propertyMap.Add(name, type);
                string text = node.GetAttribute("text", false);
                if (!String.IsNullOrWhiteSpace(text))
                {
                    properties.AppendSummary(text);
                }
            }
            if (!output)
            {
                name = "Unused" + name;
            }
            if (!propertyExists)
            {
                properties.AppendReadWriteProperty(type, name);
                properties.AppendLine();
            }
            string and   = node.GetAttribute("and", false);
            string shift = node.GetAttribute("shift", false);

            if (!String.IsNullOrEmpty(and) && !String.IsNullOrEmpty(shift))
            {
                if (typeInfo.IsSignedType || supertype != type)
                {
                    unpack.AppendLine("{0} = ({4})({1} >> {2} & {3});", name, supername, shift, and, type);
                    pack.AppendLine("{0} |= ({4})({1} & {2} << {3});", supername, name, and, shift, supertype);
                }
                else
                {
                    unpack.AppendLine("{0} = ({4})({1} >> {2} & {3});", name, supername, shift, and, type);
                    pack.AppendLine("{0} |= ({4})({1} & {2} << {3});", supername, name, and, shift, supertype);
                }
            }
            else if (!String.IsNullOrEmpty(shift))
            {
                if (typeInfo.IsSignedType || supertype != type)
                {
                    unpack.AppendLine("{0} = ({3})({1} >> {2});", name, supername, shift, type);
                    pack.AppendLine("{0} |= ({3})({1} << {2});", supername, name, shift, supertype);
                }
                else
                {
                    unpack.AppendLine("{0} = ({3})({1} >> {2});", name, supername, shift, type);
                    pack.AppendLine("{0} |= ({3})({1} << {2});", supername, name, shift, supertype);
                }
            }
            else if (!String.IsNullOrEmpty(and))
            {
                if (typeInfo.IsSignedType)
                {
                    var      andInt = Convert.ToInt32(and, 16);
                    BitArray ba     = new BitArray(new int[] { andInt });
                    int      pos;
                    for (pos = 0; pos < ba.Length; pos++)
                    {
                        if (!ba[pos])
                        {
                            break;
                        }
                    }
                    uint   negCheck      = (uint)Math.Pow(2, pos - 1);
                    string negativeCheck = String.Format("0x{0:X}", negCheck);
                    int    size          = negativeCheck.Replace("0x", "").Length;
                    int    sizediff      = Util.GetHexLength(type) - size;
                    string negativeOr    = "0x" + new string('F', sizediff) + new string('0', size);
                    unpack.AppendLine("if(({0} & {1}) != 0) // Is the subvalue negative?", supername, negativeCheck);
                    unpack.Indent();
                    unpack.AppendLine("{0} = ({4})({1} & {2} | {3});", name, supername, and, negativeOr, type);
                    unpack.Unindent();
                    unpack.AppendLine("else");
                    unpack.Indent();
                    unpack.AppendLine("{0} = ({3})({1} & {2});", name, supername, and, type);
                    unpack.Unindent();
                }
                else
                {
                    unpack.AppendLine("{0} = ({3})({1} & {2});", name, supername, and, type);
                    pack.AppendLine("{0} |= ({3})({1} & {2});", supername, name, and, supertype);
                }
            }
        }