private Catagory newCatagory(string catagory) { Catagory newCatagory; if (catagories.TryGetValue(catagory, out newCatagory)) { return(newCatagory); } else { newCatagory = new Catagory(catagory); catagories.Add(catagory, newCatagory); return(newCatagory); } }
/// <summary> /// 从指定文件中加载属性分类 /// </summary> /// <param name="filePath">指定文件路径</param> /// <returns>若成功加载,返回true,否则返回false</returns> public bool readConfigFromFile(string filePath) { this.clearConfig(); FileStream fs; try { fs = new FileStream(filePath, FileMode.Open, FileAccess.Read); } catch (Exception) { return(false); } StreamReader reader = new StreamReader(fs); try { Catagory catagory = newCatagory(""); while (!reader.EndOfStream) { string line = reader.ReadLine(); switch (parseLine(line)) { case LineType.CATAGORY: catagory = newCatagory(parseCatagoryName(line)); break; case LineType.ATTRIBUTE: parseAttribute(line, catagory); break; default: break; } } } catch (Exception) { catagories.Clear(); return(false); } finally { reader.Close(); } configFileName = filePath; return(true); }
/// <summary> /// 将属性分类写入指定的文件中 /// </summary> /// <param name="filename">指定的文件路径</param> /// <returns>若成功写入,返回true,否则返回false</returns> public bool writeConfigToFile(string filename) { FileStream fs; try { fs = new FileStream(filename, FileMode.Create, FileAccess.Write); } catch (Exception) { return(false); } StreamWriter writer = new StreamWriter(fs); try { Catagory defaultCatagory; if (catagories.TryGetValue("", out defaultCatagory)) { writeCatagory(writer, defaultCatagory); writer.WriteLine(); } Dictionary <string, Catagory> .Enumerator enumerator = catagories.GetEnumerator(); while (enumerator.MoveNext()) { Catagory current = enumerator.Current.Value; if (current.Name != "") { writer.WriteLine("[{0}]", current.Name); writeCatagory(writer, current); writer.WriteLine(); } } } catch (Exception) { return(false); } finally { writer.Close(); } return(true); }
private void parseAttribute(string line, Catagory catagory) { int eqIdx = line.IndexOf('='); string key = line.Substring(0, eqIdx).Trim('\t', ' '); string value = line.Substring(eqIdx + 1).Trim('\t', ' '); if (value[0] == '\"' && value[value.Length - 1] == '\"') value = value.Substring(1, value.Length - 2); catagory.addAttribute(key, value); }
private Catagory newCatagory(string catagory) { Catagory newCatagory; if (catagories.TryGetValue(catagory, out newCatagory)) return newCatagory; else { newCatagory = new Catagory(catagory); catagories.Add(catagory, newCatagory); return newCatagory; } }
private static void writeCatagory(StreamWriter writer, Catagory current) { Dictionary<string, string>.Enumerator enumAttr = current.getEnumerator(); while (enumAttr.MoveNext()) { writer.WriteLine("{0} = \"{1}\"", enumAttr.Current.Key, enumAttr.Current.Value); } }
/// <summary> /// 加入一个键(分类名)/分类对,若键已存在,则用新分类覆盖旧分类 /// </summary> /// <param name="key">要添加的键(分类名)</param> /// <param name="value">要添加的分类</param> public void addCatagory(string key, Catagory value) { catagories[key] = value; }
private void saveTask(Task task) { ConfigAccessor accessor = new ConfigAccessor(); Catagory cat = new Catagory("Task"); cat.addAttribute("File", task.FileName); cat.addAttribute("Process", task.Process.ToString()); accessor.addCatagory(cat.Name, cat); accessor.writeConfigToFile(TASK_FILE); }