Exemplo n.º 1
0
        public static XmlElement GetPathElement(XmlElement tag_element, ref List <Limits> limits, PathComponent path, ConstantLookup constants)
        {
            XmlElement elem = tag_element;

            if (path.Parent != null)
            {
                elem = GetPathElement(tag_element, ref limits, path.Parent, constants);
                if (elem == null)
                {
                    return(null);
                }
            }
            if (path is MemberComponent member_path)
            {
                if (!(elem.SelectSingleNode("./if:Member[@Name='" + member_path.Name + "'] | ./if:Sections/if:Section/if:Member[@Name='" + member_path.Name + "']", XMLUtil.nameSpaces) is XmlElement child_elem))
                {
                    throw new Exception("Unable to find path " + member_path + " in element " + (elem?.GetAttribute("Name") ?? "<Unknown>"));
                }
                DataType type = DataTypeParser.Parse(child_elem.GetAttribute("Datatype"), out string _);
                if (type is ARRAY array)
                {
                    limits.AddRange(array.Limits.Select(x => new Limits()
                    {
                        Low = x.LowLimit.ResolveInt(constants), High = x.HighLimit.ResolveInt(constants)
                    }));
                }
                return(child_elem);
            }
            else if (path is IndexComponent index_path)
            {
                limits.RemoveRange(limits.Count - index_path.Indices.Count(), index_path.Indices.Count());
                limits.AddRange(index_path.Indices.Select(x => new Limits()
                {
                    Low = x, High = x
                }));
                return(elem);
            }
            else
            {
                return(null);
            }
        }
Exemplo n.º 2
0
        public void TestDataType()
        {
            string left;

            Assert.AreEqual(DataTypeParser.Parse("Int", out left).GetType(), typeof(INT));
            Assert.AreEqual(left, "");

            Assert.AreEqual(DataTypeParser.Parse(" udint jsadl", out left).GetType(), typeof(UDINT));
            Assert.AreEqual(left, " jsadl");

            DataType type = DataTypeParser.Parse(" array [ #start.. \"end\" ] of ulint garbage", out left);

            Assert.AreEqual(type.GetType(), typeof(ARRAY));
            Assert.AreEqual(left, " garbage");
            Assert.AreEqual(((ARRAY)type).Limits[0].LowLimit, new LocalConstant("start"));
            Assert.AreEqual(((ARRAY)type).Limits[0].HighLimit, new GlobalConstant("end"));
            Assert.AreEqual(((ARRAY)type).MemberType.GetType(), typeof(ULINT));

            type = DataTypeParser.Parse(" array [ -3.. 72 ] of struct garbage", out left);
            Assert.AreEqual(type.GetType(), typeof(ARRAY));
            Assert.AreEqual(((ARRAY)type).Limits[0].LowLimit, new IntegerLiteral(-3));
            Assert.AreEqual(((ARRAY)type).Limits[0].HighLimit, new IntegerLiteral(72));
            Assert.AreEqual(((ARRAY)type).MemberType.GetType(), typeof(STRUCT));

            Assert.AreEqual(left, " garbage");


            type = DataTypeParser.Parse(" array [ #start.. \"end\",  -3.. 72 ] of ulint garbage", out left);
            Assert.AreEqual(type.GetType(), typeof(ARRAY));
            Assert.AreEqual(left, " garbage");
            Assert.AreEqual(((ARRAY)type).Limits[0].LowLimit, new LocalConstant("start"));
            Assert.AreEqual(((ARRAY)type).Limits[0].HighLimit, new GlobalConstant("end"));
            Assert.AreEqual(((ARRAY)type).MemberType.GetType(), typeof(ULINT));
            Assert.AreEqual(((ARRAY)type).Limits[1].LowLimit, new IntegerLiteral(-3));
            Assert.AreEqual(((ARRAY)type).Limits[1].HighLimit, new IntegerLiteral(72));

            type = DataTypeParser.Parse(" array [ #start.. \"end\",  -3.. 72 ] of string[10] garbage", out left);
            Assert.AreEqual(type.GetType(), typeof(ARRAY));
            Assert.AreEqual(left, " garbage");
            Assert.AreEqual("Array[#start..\"end\",-3..72] of String[10]", type.ToString());
        }
Exemplo n.º 3
0
        public static void SetPathValues(XmlElement tag_element, PathComponent path, ConstantLookup constants, object [] values)
        {
            List <Limits> limits = new List <Limits>();
            XmlElement    elem   = GetPathElement(tag_element, ref limits, path, constants);

            int[] lengths = limits.Select(x => (x.High - x.Low + 1)).ToArray();
            int[] lower   = limits.Select(x => x.Low).ToArray();
            if (limits.Count < values.Rank)
            {
                throw new Exception("Path " + path + " has " + limits.Count + " indices but the supplied values has " + values.Rank);
            }


            DataType value_type = DataTypeParser.Parse(elem.GetAttribute("Datatype"), out string _);

            if (value_type is ARRAY value_array)
            {
                value_type = value_array.MemberType;
            }
            int[] indices = new int[lengths.Count()];
            int   v_index = 0;

            SetStartValues(elem, values, value_type, indices, limits, 0, ref v_index);
        }
Exemplo n.º 4
0
 public virtual string GetTypeName(ColumnInfo ci)
 {
     return(GetTypeName(DataTypeParser.Parse(ci.ValueType), ci.IsUnicode, ci.Length, ci.DecimalPart));
 }
Exemplo n.º 5
0
        public static Array GetPathValues(XmlElement tag_element, PathComponent path, ConstantLookup constants)
        {
            List <Limits> limits       = new List <Limits>();
            XmlElement    elem         = GetPathElement(tag_element, ref limits, path, constants);
            XmlNodeList   start_values = elem.SelectNodes(".//if:Subelement/if:StartValue", XMLUtil.nameSpaces);

            int[] lengths = limits.Select(x => (x.High - x.Low + 1)).ToArray();
            int[] lower   = limits.Select(x => x.Low).ToArray();

            DataType value_type = DataTypeParser.Parse(elem.GetAttribute("Datatype"), out string _);

            if (value_type is ARRAY value_array)
            {
                value_type = value_array.MemberType;
            }

            Type array_type;

            if (value_type is BitString bits)
            {
                array_type = typeof(int);
            }
            else if (value_type is Integer)
            {
                array_type = typeof(int);
            }
            else if (value_type is Float)
            {
                array_type = typeof(double);
            }
            else if (value_type is BOOL)
            {
                array_type = typeof(bool);
            }
            else if (value_type is STRING)
            {
                array_type = typeof(string);
            }
            else
            {
                throw new NotImplementedException("Unhandled value type " + value_type.ToString());
            }
            Array array = Array.CreateInstance(array_type, lengths, lower);


            foreach (XmlElement start_value in start_values)
            {
                string subpath   = ((XmlElement)start_value.ParentNode).GetAttribute("Path");
                int[]  indices   = subpath.Split(new char[] { ',' }).Select(x => int.Parse(x)).ToArray();
                string value_str = start_value.InnerText.Trim(new char[1] {
                    '\''
                });
                Object value = ParseValue(value_str, value_type);

                if (indices.Length != limits.Count)
                {
                    throw new IndexOutOfRangeException("Wrong number of dimensions for " + path
                                                       + " (expected " + limits.Count + " got " + indices.Length);
                }
                int i;
                // Check if indices are within limits
                for (i = 0; i < indices.Length; i++)
                {
                    if (indices[i] < limits[i].Low || indices[i] > limits[i].High)
                    {
                        break;
                    }
                }

                if (i == indices.Length)
                {
                    array.SetValue(value, indices);
                }
            }
            return(array);
        }
Exemplo n.º 6
0
            protected MemberComponent readMember(XmlElement member_elem, PathComponent parent)
            {
                string name = member_elem.GetAttribute("Name");



                string          type_str = member_elem.GetAttribute("Datatype");
                string          left;
                DataType        type       = DataTypeParser.Parse(type_str, out left);
                MemberComponent member     = new MemberComponent(name, type, parent);
                PathComponent   child_path = member;

                if (type is ARRAY)
                {
                    ARRAY array = (ARRAY)type;
                    child_path = new IndexComponent(new int[array.Limits.Count], array.MemberType, member);

                    if ((options & Options.NoSubelement) != 0)
                    {
                        if (member != child_path)
                        {
                            handle_tag(new HandleTagEventArgs()
                            {
                                Path    = SubstituteIndicesLow(child_path),
                                Comment = null
                            });
                        }
                    }
                }


                XmlElement       comment_elem = member_elem.SelectSingleNode("if:Comment", XMLUtil.nameSpaces) as XmlElement;
                MultilingualText comment      = null;

                if (comment_elem != null)
                {
                    comment = readComment(comment_elem);
                }
                if (((options & Options.AllowNoComment) != 0) || comment != null)
                {
                    handle_tag(new HandleTagEventArgs()
                    {
                        Path    = SubstituteIndicesLow(member),
                        Comment = comment
                    });
                }

                XmlNodeList member_elems = member_elem.SelectNodes("if:Member", XMLUtil.nameSpaces);

                foreach (XmlNode m in member_elems)
                {
                    MemberComponent submember = readMember((XmlElement)m, child_path);
                    if (child_path.Type is STRUCT)
                    {
                        STRUCT struct_type = (STRUCT)child_path.Type;
                        struct_type.Members.Add(new StructMember()
                        {
                            Name = submember.Name, MemberType = submember.Type
                        });
                    }
                }

                if ((options & Options.NoSubelement) == 0)
                {
                    XmlNodeList sub_elems = member_elem.SelectNodes("if:Subelement", XMLUtil.nameSpaces);
                    foreach (XmlNode s in sub_elems)
                    {
                        readSubelement(s as XmlElement, child_path);
                    }
                }

                return(member);
            }
Exemplo n.º 7
0
 public void DataTypeParser_Validate_Parse_Integer_Correct_Entry()
 {
     Assert.AreEqual(1000000, DataTypeParser.Parse("1000000", DataType.Int));
 }
Exemplo n.º 8
0
 public void DataTypeParser_Validate_Parse_Integer_Incorrect_Entry()
 {
     Assert.AreEqual(0, DataTypeParser.Parse("erfeerg", DataType.Int));
 }