//-------------------------------- #endregion //========== MANAGEMENT ========== #region Mangement //-------------------------------- #region Setters /** <summary> Adds the specified section to the property list. </summary> */ public PropertySection Add(PropertySection section) { if (!Contains(section)) { sections.Add(section); return(section); } return(null); }
/** <summary> Adds a new section with the specified key to the property list. </summary> */ public PropertySection Add(string key, string comments = "") { if (allowDuplicateSections || !Contains(key)) { PropertySection section = new PropertySection(key, comments); sections.Add(section); return(section); } return(null); }
/** <summary> Returns true if the specified section exists in the property list. </summary> */ public bool Contains(PropertySection section) { for (int i = 0; i < sections.Count; i++) { if (section == sections[i]) { return(true); } } return(false); }
//-------------------------------- #endregion //========= CONSTRUCTORS ========= #region Constructors /** <summary> Constructs the default property list. </summary> */ public PropertyList() { // Format this.assignmentType = "="; this.commentType = "; "; this.maxLineLength = -1; this.strictFormatting = false; this.keepComments = false; this.allowDuplicateSections = false; // Properties this.global = new PropertySection(); this.sections = new List <PropertySection>(); }
/** <summary> Writes the section head. </summary> */ private static string WriteSection(PropertySection section, bool strictFormatting, string assignmentType, string commentType, int maxLineLength) { string text = ""; if (section.Comments.Length > 0) { text += WriteComments(section.Comments, commentType, maxLineLength); } if (section.Key.Length > 0) { string sectionText = ""; sectionText = Escape(section.Key, true, strictFormatting, strictFormatting, strictFormatting);; sectionText = "[" + sectionText + "]"; text += ContinueLine(sectionText, maxLineLength) + "\n"; } for (int i = 0; i < section.Count; i++) { text += WriteProperty(section.Properties[i], strictFormatting, assignmentType, commentType, maxLineLength); } return(text); }
//-------------------------------- #endregion //=========== READING ============ #region Reading /** <summary> Parses the property list from the specified text. </summary> */ public void ParseProperties(string text) { // The lines of the text List <string> lines = new List <string>(); // The current section being edited. (Set to global section by default) PropertySection section = null; // The current description for the next section or property string comment = ""; sections.Clear(); global = new PropertySection(); section = global; // Formating Order: // Replace '\r' with '\n' // Remove all empty lines // Remove '\n' if line ends with '\' // Split up lines // Remove lines beginning with ';', '#', or '!' // Split lines at "=", " = ", ":", or " : " and not "\\=" or "\\:" // Remove quotes around values // Implement all escape characters: // '\\', '\0', '\a', '\b', '\t', '\r', '\n', '\;', '\#', '\!', '\=', '\:', '\"', '\ ', '\x####' // Replace all carriage returns with new lines if (!keepComments) { text = text.Replace("\r", "\n"); } else { text = text.Replace("\r\n", "\n"); } // Remove all empty lines if (!keepComments) { text = Regex.Replace(text, @"\n+", "\n"); } // Remove all commented lines if (!keepComments) { text = Regex.Replace(Regex.Replace(text, @"\n[\;|\#|\!].*", ""), @"^[\;|\#|\!].*\n", ""); } // Remove new line if line ends with '\' if (!keepComments) { text = text.Replace("\\\n", ""); } else { lines.AddRange(text.Split('\n')); for (int i = 0; i < lines.Count; i++) { if (lines[i].Length > 0) { // Check if line isn't a comment and ends with '\' if (RegexMatchIndex(lines[i], @"^[^\;\#\!].*\\", 0)) { lines[i] = lines[i].Substring(0, lines[i].Length - 1); // Combine the two lines into one if (i + 1 < lines.Count) { lines[i] += lines[i + 1]; lines.RemoveAt(i + 1); } } } } text = String.Join("\n", lines); lines.Clear(); } // Split up the lines lines.AddRange(text.Split(new string[] { "\n" }, StringSplitOptions.None)); // Iterate through all the lines for (int i = 0; i < lines.Count; i++) { string line = lines[i]; if (line.Length == 0) // Empty line comment // If the comment is a blank line { comment += "\n"; } else if (RegexMatchIndex(line, @"^[\;|\#|\!].*", 0)) // Comment // If the comment is spaced or not spaced { if (RegexMatchIndex(line, @"^[\;|\#|\!] .*", 0)) { comment += line.Substring(2, line.Length - 2) + "\n"; } else { comment += line.Substring(1, line.Length - 1) + "\n"; } } else if (RegexMatchIndex(line, @"^\[.*\]$", 0)) // Section { string sectionKey = ParseSection(line); PropertySection newSection = Add(sectionKey, comment); if (newSection != null) { section = newSection; } comment = ""; } else // Propery // Split the line into [key, value] { string[] parts = ParseProperty(line).Split('\n'); if (parts.Length == 2) { string key = ParseKey(parts[0]); string value = ParseValue(parts[1]); /*if (allowDuplicateProperties) { * section.AddNewProperty(key, value, comment); * // Set property of correct index * } * else {*/ section.Add(key, value, comment); comment = ""; //} } else { // Not a valid line } } } }
/** <summary> Removes the specified section from the property list. </summary> */ public void Remove(PropertySection section) { sections.Remove(section); }