/// <summary> /// Parses a spec (clientspec, branchspec, changespec) from an array of lines /// </summary> /// <param name="Lines">Text split into separate lines</param> /// <returns>Array of section names and values</returns> public static P4Spec FromString(string Text) { P4Spec Spec = new P4Spec(); string[] Lines = Text.Split('\n'); for(int LineIdx = 0; LineIdx < Lines.Length; LineIdx++) { if(Lines[LineIdx].EndsWith("\r")) { Lines[LineIdx] = Lines[LineIdx].Substring(0, Lines[LineIdx].Length - 1); } if(!String.IsNullOrWhiteSpace(Lines[LineIdx]) && !Lines[LineIdx].StartsWith("#")) { // Read the section name int SeparatorIdx = Lines[LineIdx].IndexOf(':'); if(SeparatorIdx == -1 || !Char.IsLetter(Lines[LineIdx][0])) { throw new P4Exception("Invalid spec format at line {0}: \"{1}\"", LineIdx, Lines[LineIdx]); } // Get the section name string SectionName = Lines[LineIdx].Substring(0, SeparatorIdx); // Parse the section value StringBuilder Value = new StringBuilder(Lines[LineIdx].Substring(SeparatorIdx + 1)); for(; LineIdx + 1 < Lines.Length; LineIdx++) { if(Lines[LineIdx + 1].Length == 0) { Value.AppendLine(); } else if(Lines[LineIdx + 1][0] == '\t') { Value.AppendLine(Lines[LineIdx + 1].Substring(1)); } else { break; } } Spec.Sections.Add(new KeyValuePair<string,string>(SectionName, Value.ToString().TrimEnd())); } } return Spec; }
/// <summary> /// Updates a label with a new spec /// </summary> /// <param name="Spec">Label specification</param> /// <param name="AllowSpew">Whether to allow log spew</param> public void UpdateLabelSpec(P4Spec Spec, bool AllowSpew = true) { LogP4("label -i", Input: Spec.ToString(), AllowSpew: AllowSpew); }