Exemplo n.º 1
0
        /// <summary>
        /// Attempts to set the address line type based
        /// on the address contained within.
        /// </summary>
        /// <returns></returns>
        public HostFileLineEntryType SetTypeFromAddress()
        {
            IPAddress ipAddr;

            if (IPAddress.TryParse(Address, out ipAddr))
            {
                Type = (ipAddr.AddressFamily == AddrFamily.InterNetwork)
                                        ? HostFileLineEntryType.HostForIPv4
                                        : (ipAddr.AddressFamily == AddrFamily.InterNetworkV6)
                                        ? HostFileLineEntryType.HostForIPv6
                                        : HostFileLineEntryType.Unknown;
            }

            return(Type);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Takes a single line from the host file and parses
        /// it. A host entry is returned for valid lines.
        /// </summary>
        /// <param name="line">line to parse</param>
        /// <returns>A host entry</returns>
        private static HostFileEntry ParseLine(string line, out HostFileLineEntryType type)
        {
            type = HostFileLineEntryType.Blank;
            if (String.IsNullOrWhiteSpace(line))
            {
                return(null);
            }

            var empty = new Regex(EmptyCommentLineRegex);

            if (empty.IsMatch(line) || line.Trim().StartsWith("#"))
            {
                type = HostFileLineEntryType.Comment;
                return(null);
            }

            var items = line.Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);

            if (items.Length < 2)
            {
                type = HostFileLineEntryType.Unknown;
                return(null);
            }

            var entry = new HostFileEntry()
            {
                Address  = items[0].Trim(),
                Hostname = items[1].Trim()
            };

            type = entry.SetTypeFromAddress();

            return(entry);

            // END FUNCTION
        }