/// <summary> /// Defaults data to the section (IE, sets it if not present!) /// </summary> /// <param name="key">The key to set data from.</param> /// <param name="data">The key to set data to.</param> public void DefaultData(string key, FDSData data) { int lind = key.LastIndexOf(SectionPathSplit); if (lind < 0) { if (GetRootData(key) == null) { SetRootData(key, data); } return; } if (lind == key.Length - 1) { throw new FDSInputException("Invalid SetData key: Ends in a path splitter!"); } FDSSection sec = GetSectionInternal(key.Substring(0, lind), false, false); string k = key.Substring(lind + 1); if (sec.GetRootData(k) == null) { sec.SetRootData(k, data); } }
/// <summary> /// Gets data from the section. /// Returns null if not found. /// </summary> /// <param name="key">The key to get data from.</param> /// <returns>The data found, or null.</returns> public FDSData GetData(string key) { int lind = key.LastIndexOf(SectionPathSplit); if (lind < 0) { return(GetRootData(key)); } if (lind == key.Length - 1) { return(null); } FDSSection sec = GetSection(key.Substring(0, lind)); if (sec == null) { return(null); } return(sec.GetRootData(key.Substring(lind + 1))); }
/// <summary> /// Gets a sub-section of this FDS section. /// </summary> /// <param name="key">The key of the section.</param> /// <param name="allowNull">Whether to allow null returns, otherwise enforce the section's existence. If true, can throw an FDSInputException!</param> /// <param name="lowered">Whether to read lowercase section names. If set, expects lowercased input key!</param> /// <returns>The subsection.</returns> private FDSSection GetSectionInternal(string key, bool allowNull, bool lowered) { if (key == null || key.Length == 0) { return(this); } string[] dat = key.SplitFast(SectionPathSplit); FDSSection current = this; for (int i = 0; i < dat.Length; i++) { FDSData fdat = lowered ? current.GetRootDataLowered(dat[i]) : current.GetRootData(dat[i]); if (fdat != null && fdat.Internal is FDSSection) { current = (FDSSection)fdat.Internal; } else { if (allowNull) { return(null); } if (fdat != null) { throw new FDSInputException("Key contains non-section contents!"); } FDSSection temp = new FDSSection(); current.SetRootData(dat[i], new FDSData() { Internal = temp, PrecedingComments = new List <string>() }); current = temp; } } return(current); }