//public static bool HasParam(this ParsedData data, string param) => data.Values.Any(a => a.Key == param); // public static bool IsValuePossible(this ParsedData data, string param) => data.Values[param] != null; public static string GetValue(this ParsedData data, int index) { if (data.IsEmpty) { throw new NullReferenceException("Data empty. Use Read() before."); } if (index - 1 < 0 || data.Values.Count < index - 1) { throw new IndexOutOfRangeException(); } return(data.Values.ElementAt(index - 1).Value); }
public static T GetValue <T>(this ParsedData data, string name) where T : IConvertible { string result = GetValue(data, name); return((T)Convert.ChangeType(result, typeof(T))); }
public static ParsedData Parse(string raw) { ParsedData result = new ParsedData { IsEmpty = true }; //, Raw = raw}; if (String.IsNullOrWhiteSpace(raw)) { return(result); } if (raw.StartsWith("//")) { return(result); } Regex regex = new Regex("\\s*=\\s*"); string refinedRaw = regex.Replace(raw, "="); var splited = refinedRaw.Split(SplitChars, StringSplitOptions.RemoveEmptyEntries); if (splited.Length == 0) { return(result); } List <KeyValuePair <string, string> > list = new List <KeyValuePair <string, string> >(); for (var index = 0; index < splited.Length; index++) { string current = splited[index]; KeyValuePair <string, string> keyvalue; if (index > 0 && current.EndsWith("_begin")) { string collectSubData = current; while (!current.EndsWith("_end")) { index++; current = splited[index]; if (!String.IsNullOrWhiteSpace(collectSubData)) { collectSubData += '\t'; } collectSubData += current; } keyvalue = new KeyValuePair <string, string>(null, collectSubData); } else { keyvalue = GetPair(current); } list.Add(keyvalue); } result.Values = new ReadOnlyCollection <KeyValuePair <string, string> >(list); result.IsEmpty = false; return(result); }