Пример #1
0
 public bool Parse(string value)
 {
     ParsedString = value;
     try
     {
         var match = HostRowPattern.Match(value);
         if (!match.Success)
         {
             throw new FormatException();
         }
         Enabled = value[0] != '#';
         IP      = new NetAddress(match.Groups["ip"].Value);
         Aliases = new HostAliases(match.Groups["hosts"].Value);
         Comment = match.Groups["comment"].Value;
         Hidden  = false;
         if (!String.IsNullOrEmpty(Comment))
         {
             Hidden = Comment[0] == '!';
             if (Hidden)
             {
                 Comment = Comment.Substring(1).Trim();
             }
         }
         Valid = true;
     }
     catch
     {
         Enabled = false;
         Hidden  = false;
         IP      = null;
         Aliases = null;
         Comment = null;
         Valid   = false;
     }
     ParsedHash = HalfMD5.ComputeHash(ToString());
     return(Valid);
 }
Пример #2
0
        public bool Parse(string value)
        {
            SourceString = value;
            try
            {
                value = value.Trim();
                if (String.IsNullOrEmpty(value))
                {
                    throw new FormatException();
                }
                if (value.IndexOfAny(new char[] { '\r', '\n' }) != -1)
                {
                    throw new FormatException();
                }

                // Check if enabled.
                Enabled = value[0] != '#';
                if (!Enabled)
                {
                    value = value.Substring(1).TrimStart();
                }
                if (String.IsNullOrEmpty(value))
                {
                    throw new FormatException();
                }

                // Parse comment part.
                Hidden  = false;
                Comment = null;
                var comment_pos = value.IndexOf('#');
                if (comment_pos != -1)
                {
                    Comment = value.Substring(comment_pos + 1).Trim();
                    if (!String.IsNullOrEmpty(Comment))
                    {
                        Hidden = Comment[0] == '!';
                        if (Hidden)
                        {
                            Comment = Comment.Substring(1).Trim();
                        }
                    }
                    value = value.Substring(0, comment_pos).TrimEnd();
                    if (String.IsNullOrEmpty(value))
                    {
                        throw new FormatException();
                    }
                }

                // Parse IP and hosts.
                var parts = value.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
                if (parts.Length < 2)
                {
                    throw new FormatException();
                }

                IP      = new NetAddress(parts[0]);
                Aliases = new HostAliases(parts.Skip(1).ToArray());
                Valid   = true;
            }
            catch
            {
                Enabled = false;
                Hidden  = false;
                IP      = null;
                Aliases = null;
                Comment = null;
                Valid   = false;
            }
            SourceHash = HalfMD5.ComputeHash(ToString());
            return(Valid);
        }