Пример #1
0
        private static List<PhoneInformation> ParseFromBytes(byte[] data)
        {
            var phones = new List<PhoneInformation>();
            var str =  Encoding.UTF8.GetString(data);

            string[] lines = str.Split('\n');
            string currentCountry = "";
            PhoneInformation currentPhone = new PhoneInformation();
            bool nextIsCountry = false;
            bool nextIsCode = false;
            foreach (string line in lines)
            {
                var trimmedLine = line.Trim();
                if (!trimmedLine.StartsWith("#"))
                {
                    phones.Add(currentPhone);
                    currentPhone = new PhoneInformation();
                    currentPhone.Country = currentCountry;

                    if (trimmedLine == "")
                    {
                        nextIsCode = false;
                        nextIsCountry = true;
                    }
                    else
                    {
                        if (nextIsCountry)
                        {
                            currentCountry = trimmedLine;
                            nextIsCountry = false;
                            nextIsCode = true;
                        }
                        else if (nextIsCode)
                        {
                            var commentedBits = trimmedLine.Split('#');
                            var parts = commentedBits[0].Split(',');
                            if (commentedBits.Length > 1) currentPhone.Comment = commentedBits[1];
                            int num = 0;
                            if (int.TryParse(parts[0], out num)) currentPhone.CountryCode = num;
                            if (int.TryParse(parts[1], out num)) currentPhone.MobilePrefix = num;
                            if (int.TryParse(parts[2], out num)) currentPhone.NumberOfDigitsAfterMobilePrevix = num;
                        }
                    }
                }
            }
            var good = new List<PhoneInformation>();
            foreach (var p in phones)
            {
                if ((!string.IsNullOrEmpty(p.Country)) && p.Reliable)
                    good.Add(p);
            }
            return good;
        }
Пример #2
0
        private static List <PhoneInformation> ParseFromBytes(byte[] data)
        {
            var phones = new List <PhoneInformation>();
            var str    = Encoding.UTF8.GetString(data);

            string[]         lines          = str.Split('\n');
            string           currentCountry = "";
            PhoneInformation currentPhone   = new PhoneInformation();
            bool             nextIsCountry  = false;
            bool             nextIsCode     = false;

            foreach (string line in lines)
            {
                var trimmedLine = line.Trim();
                if (!trimmedLine.StartsWith("#"))
                {
                    phones.Add(currentPhone);
                    currentPhone         = new PhoneInformation();
                    currentPhone.Country = currentCountry;

                    if (trimmedLine == "")
                    {
                        nextIsCode    = false;
                        nextIsCountry = true;
                    }
                    else
                    {
                        if (nextIsCountry)
                        {
                            currentCountry = trimmedLine;
                            nextIsCountry  = false;
                            nextIsCode     = true;
                        }
                        else if (nextIsCode)
                        {
                            var commentedBits = trimmedLine.Split('#');
                            var parts         = commentedBits[0].Split(',');
                            if (commentedBits.Length > 1)
                            {
                                currentPhone.Comment = commentedBits[1];
                            }
                            int num = 0;
                            if (int.TryParse(parts[0], out num))
                            {
                                currentPhone.CountryCode = num;
                            }
                            if (int.TryParse(parts[1], out num))
                            {
                                currentPhone.MobilePrefix = num;
                            }
                            if (int.TryParse(parts[2], out num))
                            {
                                currentPhone.NumberOfDigitsAfterMobilePrevix = num;
                            }
                        }
                    }
                }
            }
            var good = new List <PhoneInformation>();

            foreach (var p in phones)
            {
                if ((!string.IsNullOrEmpty(p.Country)) && p.Reliable)
                {
                    good.Add(p);
                }
            }
            return(good);
        }