public static void Save() { // TODO: possibly make this safer (in case of crash during write?) StreamWriter stream; stream = null; try { stream = new StreamWriter(Filename); if (stream == null) { Debug.WriteLine(1, "Argh, couldn't open the file!"); } } catch (DirectoryNotFoundException e) { Debug.WriteLine(10, e.Message); Debug.WriteLine(1, "Directory {0} not found!", Path.GetDirectoryName(Filename)); Directory.CreateDirectory(Path.GetDirectoryName(Filename)); } if (stream != null) { // good to go for (int i = 0; i < fields.Count; i++) { BibtexRecordFieldType field = (BibtexRecordFieldType)fields[i]; stream.WriteLine(field.name); stream.WriteLine(field.description); stream.WriteLine(field.spec ? 1 : 0); stream.WriteLine(); } stream.Close(); } }
public static void Add(BibtexRecordFieldType field) { fields.Add(field); }
public static void Load() { fields = new ArrayList(); StreamReader stream = null; do { try { stream = new StreamReader(Filename); if (stream == null) { Debug.WriteLine(1, "Argh, couldn't open the file!"); } break; } catch (DirectoryNotFoundException e) { Debug.WriteLine(10, e.Message); Debug.WriteLine(1, "Directory {0} not found! Creating it...", Path.GetDirectoryName(Filename)); Directory.CreateDirectory(Path.GetDirectoryName(Filename)); } catch (FileNotFoundException e) { Debug.WriteLine(10, e.Message); Debug.WriteLine(1, "File {0} not found! Instantiating it...", Filename); Stream recStream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("bibtex_fields"); FileStream outRecStream = new FileStream(Filename, FileMode.CreateNew); byte[] data = new byte[recStream.Length]; recStream.Read(data, 0, (int)recStream.Length); recStream.Close(); outRecStream.Write(data, 0, data.Length); outRecStream.Close(); } } while (true); if (stream != null) { do { string fieldName = stream.ReadLine(); if (fieldName == null) { break; } string description = stream.ReadLine(); if (description == null) { break; } string spec = stream.ReadLine(); stream.ReadLine(); // blank line between records BibtexRecordFieldType field = new BibtexRecordFieldType(); field.name = fieldName; field.description = description; field.spec = (System.Convert.ToInt32(spec) == 1); fields.Add(field); Debug.WriteLine(5, "Read in info for field '" + fieldName + "'"); } while (true); stream.Close(); } }