//This function is the main method to get enum value by passing fidId and enumValue and return true/false when it found or not found the value in enumtype.def.
        //This function will return RDMEnumDisplayValue object which contains all information about the enum value including the description.
        public bool GetEnumDisplayValue(int fidId, int enumValue, out RdmEnumDisplayValue displayValue)
        {
            displayValue = null;
            var groupId    = from x in _enumEntries where x.Fid == fidId select x.EnumGroupId;
            var enumerable = groupId as int[] ?? groupId.ToArray();

            if (!enumerable.Any())
            {
                return(false);
            }
            var enumDisplay = from y in _enumDisplayValues
                              where y.EnumGroupId == enumerable.First() && y.EnumValue == enumValue
                              select y;

            var enumDisplayValues = enumDisplay as RdmEnumDisplayValue[] ?? enumDisplay.ToArray();

            if (!enumDisplayValues.Any())
            {
                return(false);
            }
            displayValue = enumDisplayValues.First();
            return(true);
        }
Ejemplo n.º 2
0
        public static async Task <Tuple <bool, RdmEnumTypeDef, string> > LoadEnumTypeDefAsync(string filename)
        {
            var errorMsg             = string.Empty;
            var enumFidList          = new List <RdmEnumEntry>();
            var enumDisplayValueList = new List <RdmEnumDisplayValue>();
            var rdmEnumTypeDef       = new RdmEnumTypeDef(enumFidList, enumDisplayValueList);

            if (!File.Exists(filename))
            {
                errorMsg += $"\nCannot find file {filename}";
                return(new Tuple <bool, RdmEnumTypeDef, string>(false, rdmEnumTypeDef, errorMsg));
            }

            var enumIndex = 0;
            var isFidList = false;

            try
            {
                using (var fs = File.OpenRead(filename))
                    using (var bs = new BufferedStream(fs))
                        using (var sr = new StreamReader(bs))
                        {
                            string readLine;
                            while ((readLine = await sr.ReadLineAsync()) != null)
                            {
                                try
                                {
                                    if (readLine[0] == '!')
                                    {
                                        continue;
                                    }
                                    var splitStr = Regex.Matches(readLine, @"[\""].+?[\""]|[^ ]+")
                                                   .Cast <Match>()
                                                   .Select(column => column.Value)
                                                   .ToList();

                                    if (!int.TryParse(splitStr[0], out var enumValue))
                                    {
                                        if (!isFidList)
                                        {
                                            enumIndex++;
                                            isFidList = true;
                                        }

                                        var enumEntry = new RdmEnumEntry
                                        {
                                            Acronym     = splitStr[0],
                                            Fid         = int.Parse(splitStr[1]),
                                            EnumGroupId = enumIndex
                                        };
                                        enumFidList.Add(enumEntry);
                                    }
                                    else
                                    {
                                        if (isFidList)
                                        {
                                            isFidList = false;
                                        }

                                        var enumDisplayValue = new RdmEnumDisplayValue
                                        {
                                            EnumGroupId = enumIndex,
                                            EnumValue   = enumValue,
                                            EnumDisplay = splitStr[1]
                                        };


                                        if (splitStr.Count > 2)
                                        {
                                            var tempArray = new string[splitStr.Count - 2];
                                            Array.Copy(splitStr.ToArray(), 2, tempArray, 0, splitStr.Count - 2);
                                            var meaningStr = string.Join(" ", tempArray);
                                            enumDisplayValue.EnumMeaning = meaningStr;
                                        }
                                        else
                                        {
                                            enumDisplayValue.EnumMeaning = string.Empty;
                                        }

                                        enumDisplayValueList.Add(enumDisplayValue);
                                    }
                                }
                                catch (Exception exception)
                                {
                                    errorMsg = $"{exception.Message}\n{exception.StackTrace}";
                                    return(new Tuple <bool, RdmEnumTypeDef, string>(false, rdmEnumTypeDef, errorMsg));
                                }
                            }
                        }

                return(new Tuple <bool, RdmEnumTypeDef, string>(true, rdmEnumTypeDef, errorMsg));
            }
            catch (Exception exception)
            {
                errorMsg += $"\n{exception.Message}\n{exception.StackTrace}";
                return(new Tuple <bool, RdmEnumTypeDef, string>(false, rdmEnumTypeDef, errorMsg));
            }
        }