public static Dictionary <string, double> readDictionaryD(string fileName, Encoding encoding, int keyIndex, int valueIndex, char separator, bool toLower, keyDelegate keyDelegate, valDelegateDouble valDelegate) { Dictionary <string, double> ret = new Dictionary <string, double>(); if (File.Exists(fileName)) { StreamReader rdr = new StreamReader(fileName, encoding); string line = ""; while ((line = rdr.ReadLine()) != null) { if (toLower) { line = line.ToLower(); } string[] parts = line.Trim().Split(separator); if (keyIndex < parts.Length && valueIndex < parts.Length) { string key = parts[keyIndex]; string value = parts[valueIndex]; double dValue = double.Parse(value, CultureInfo.InvariantCulture); if (keyDelegate != null) { key = keyDelegate(key); } if (valDelegate != null) { dValue = valDelegate(value); } if (!ret.ContainsKey(key)) { ret.Add(key, dValue); } } } rdr.Close(); } return(ret); }
public static Dictionary <string, string> readDictionary(string fileName, Encoding encoding, int keyIndex, int valueIndex, char separator, bool toLower, keyDelegate keyDelegate, valDelegate valDelegate) { Dictionary <string, string> ret = new Dictionary <string, string>(); StreamReader rdr = new StreamReader(fileName, encoding); string line = ""; while ((line = rdr.ReadLine()) != null) { if (toLower) { line = line.ToLower(); } string[] parts = line.Trim().Split(separator); if (keyIndex < parts.Length && valueIndex < parts.Length) { string key = parts[keyIndex]; string value = parts[valueIndex]; if (keyDelegate != null) { key = keyDelegate(key); } if (valDelegate != null) { value = valDelegate(value); } if (!ret.ContainsKey(key)) { ret.Add(key, value); } } } rdr.Close(); return(ret); }
public static HashSet <string> readHashSet(string fileName, Encoding encoding, int keyIndex, char separator, bool toLower, keyDelegate keyDelegate) { HashSet <string> ret = new HashSet <string>(); StreamReader rdr = new StreamReader(fileName, encoding); string line = ""; while ((line = rdr.ReadLine()) != null) { if (toLower) { line = line.ToLower(); } string[] parts = line.Trim().Split(separator); if (keyIndex < parts.Length) { string key = parts[keyIndex]; if (keyDelegate != null) { key = keyDelegate(key); } ret.Add(key); } } rdr.Close(); return(ret); }
public static Dictionary <string, List <string> > readDictionary(string fileName, Encoding encoding, char separator_1, char separator_2, bool toLower, bool uniqueValues, keyDelegate keyDelegate, valDelegate valDelegate) { Dictionary <string, List <string> > ret = new Dictionary <string, List <string> >(); StreamReader rdr = new StreamReader(fileName, encoding); string line = ""; while ((line = rdr.ReadLine()) != null) { line = line.Trim(); if (toLower) { line = line.ToLower(); } string[] parts = line.Split(new char[] { separator_1 }, 2); if (parts.Length == 2) { string key = parts[0]; if (keyDelegate != null) { key = keyDelegate(key); } string[] vParts = parts[1].Split(separator_2); List <string> values = new List <string>(); for (int i = 0; i < vParts.Length; i++) { string value = vParts[i]; if (valDelegate != null) { value = valDelegate(value); } if (!uniqueValues || !values.Contains(value)) { values.Add(value); } } ret.Add(key, values); } } return(ret); }
public static void saveDictionary(Dictionary <T1, List <T2> > dictionary, string fileName, bool append, Encoding encoding, char separator_1, char separator_2, keyDelegate keyDelegate, valDelegate valDelegate) { StreamWriter wrt = new StreamWriter(fileName, append, encoding); T1[] keys = dictionary.Keys.ToArray(); for (int i = 0; i < keys.Length; i++) { T1 key = keys[i]; if (keyDelegate != null) { key = keyDelegate(key); } wrt.Write("{0}{1}", key.ToString(), separator_1.ToString()); T2 value; if (dictionary[key].Count > 0) { if (dictionary[key].Count > 1) { for (int j = 0; j < dictionary[key].Count - 1; j++) { value = dictionary[key][j]; if (valDelegate != null) { value = valDelegate(value); } wrt.Write("{0}{1}", value.ToString(), separator_2.ToString()); } } value = dictionary[key][dictionary[key].Count - 1]; if (valDelegate != null) { value = valDelegate(value); } wrt.WriteLine(value.ToString()); } } wrt.AutoFlush = true; wrt.Close(); }
public static void saveDictionary(Dictionary <T1, T2> dictionary, string fileName, bool append, Encoding encoding, char separator, keyDelegate keyDelegate, valDelegate valDelegate) { StreamWriter wrt = new StreamWriter(fileName, append, encoding); T1[] keys = dictionary.Keys.ToArray(); for (int i = 0; i < keys.Length; i++) { T1 key = keys[i]; T2 value = dictionary[key]; if (keyDelegate != null) { key = keyDelegate(key); } if (valDelegate != null) { value = valDelegate(value); } wrt.WriteLine("{0}{1}{2}", key.ToString(), separator.ToString(), value.ToString()); } wrt.AutoFlush = true; wrt.Close(); }