/// <summary> /// Fills the first and last address on the view. /// </summary> private void ShowCidrAddresses() { Cidr cidr = null; var isEmpty = String.IsNullOrEmpty(_View.Cidr); var isValid = !isEmpty && Cidr.TryParse(_View.Cidr, out cidr); _View.FirstMatchingAddress = isEmpty ? "" : !isValid ? Strings.CidrInvalid : cidr.FirstMatchingAddress.ToString(); _View.LastMatchingAddress = isEmpty ? "" : !isValid ? Strings.CidrInvalid : cidr.LastMatchingAddress.ToString(); }
/// <summary> /// Called when the list view wants the columns for a CIDR address. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void listView_FetchRecordContent(object sender, ListContentEventArgs e) { Cidr cidr = null; var cidrText = e.Record as string; if (!String.IsNullOrEmpty(cidrText)) { Cidr.TryParse(cidrText, out cidr); } e.ColumnTexts.Add(cidrText); e.ColumnTexts.Add(cidr == null ? "" : cidr.FirstMatchingAddress.ToString()); e.ColumnTexts.Add(cidr == null ? "" : cidr.LastMatchingAddress.ToString()); }
public void Cidr_TryParse_Parses_Addresses_Correctly() { var worksheet = new ExcelWorksheetData(TestContext); Cidr cidr; var address = worksheet.EString("Parse"); var parsed = Cidr.TryParse(address, out cidr); Assert.AreEqual(!parsed, worksheet.NBool("Exception").GetValueOrDefault()); if (parsed) { Assert.AreEqual(GetExcelIPAddress(worksheet, "Address"), cidr.Address); Assert.AreEqual(GetExcelIPAddress(worksheet, "MaskedAddress"), cidr.MaskedAddress); Assert.AreEqual(worksheet.Int("BitmaskBits"), cidr.BitmaskBits); Assert.AreEqual(worksheet.UInt("IPv4Bitmask"), cidr.IPv4Bitmask); } }
/// <summary> /// Adds or inserts the CIDR address to the CIDR list. /// </summary> /// <param name="cidrText"></param> /// <param name="index"></param> private void AddOrInsertCidr(string cidrText, int index = -1) { Cidr cidr; if (Cidr.TryParse(cidrText, out cidr)) { var normalisedCidr = String.Format("{0}/{1}", cidr.MaskedAddress, cidr.BitmaskBits); if (!Addresses.Contains(normalisedCidr)) { if (index == -1 || index >= Addresses.Count) { Addresses.Add(normalisedCidr); } else { Addresses.Insert(index, normalisedCidr); } } listView.SelectedRecord = normalisedCidr; } }