public string ExtractFormat(string content) { feState feState = feState.BeforeEvery; string str = ""; StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < content.Length; i++) { char c = content[i]; if (char.IsLetterOrDigit(c)) { switch (feState) { case feState.BeforeEvery: stringBuilder.Append('?'); feState = feState.AfterKey; break; case feState.BeforeVal: stringBuilder.Append('$'); feState = feState.AfterVal; break; } } else if (feState == feState.AfterKey && content.Length - i >= IniFileSettings.EqualsString.Length && content.Substring(i, IniFileSettings.EqualsString.Length) == IniFileSettings.EqualsString) { stringBuilder.Append(str); feState = feState.BeforeVal; stringBuilder.Append('='); } else if (IniFileSettings.ofAny(i, content, IniFileSettings.CommentChars) != null) { stringBuilder.Append(str); stringBuilder.Append(';'); } else if (char.IsWhiteSpace(c)) { string str2 = (c != '\t' || IniFileSettings.TabReplacement == null) ? c.ToString() : IniFileSettings.TabReplacement; if (feState == feState.AfterKey || feState == feState.AfterVal) { str += str2; continue; } stringBuilder.Append(str2); } str = ""; } if (feState == feState.BeforeVal) { stringBuilder.Append('$'); feState = feState.AfterVal; } string text = stringBuilder.ToString(); if (text.IndexOf(';') == -1) { text += " ;"; } return(text); }
/// <summary>Creates a formatting string basing on an actual content of a line.</summary> public string ExtractFormat(string content) { //bool afterKey = false; bool beforeVal = false; bool beforeEvery = true; bool afterVal = false; //return IniFileSettings.DefaultValueFormatting; feState pos = feState.BeforeEvery; char currC; string comChar; string insideWhiteChars = ""; string theWhiteChar;; StringBuilder form = new StringBuilder(); for (int i = 0; i < content.Length; i++) { currC = content[i]; if (char.IsLetterOrDigit(currC)) { if (pos == feState.BeforeEvery) { form.Append('?'); pos = feState.AfterKey; //afterKey = true; beforeEvery = false; ; } else if (pos == feState.BeforeVal) { form.Append('$'); pos = feState.AfterVal; } } else if (pos == feState.AfterKey && content.Length - i >= IniFileSettings.EqualsString.Length && content.Substring(i, IniFileSettings.EqualsString.Length) == IniFileSettings.EqualsString) { form.Append(insideWhiteChars); pos = feState.BeforeVal; //afterKey = false; beforeVal = true; form.Append('='); } else if ((comChar = IniFileSettings.ofAny(i, content, IniFileSettings.CommentChars)) != null) { form.Append(insideWhiteChars); form.Append(';'); } else if (char.IsWhiteSpace(currC)) { if (currC == '\t' && IniFileSettings.TabReplacement != null) { theWhiteChar = IniFileSettings.TabReplacement; } else { theWhiteChar = currC.ToString(); } if (pos == feState.AfterKey || pos == feState.AfterVal) { insideWhiteChars += theWhiteChar; continue; } else { form.Append(theWhiteChar); } } insideWhiteChars = ""; } if (pos == feState.BeforeVal) { form.Append('$'); pos = feState.AfterVal; } string ret = form.ToString(); if (ret.IndexOf(';') == -1) { ret += " ;"; } return(ret); }