public static void Save( string filename, Project project ) { using( StreamWriter stream = File.CreateText( filename ) ) { HtmlExport.Save( stream, project ); } }
public static Project Load( TextReader stream ) { Stopwatch timer = new Stopwatch(); timer.Start(); Project project = new Project(); using( XmlTextReader reader = new XmlTextReader( stream ) ) { while( reader.Read() ) { if( reader.NodeType == XmlNodeType.Element && (reader.Name == "folder" || reader.Name == "children") ) { XmlStorage.LoadEntries( reader, project, project.Children ); } else if( reader.NodeType == XmlNodeType.Element && reader.Name == "statistics" ) { XmlStorage.LoadStatistics( reader, project.DailyRecords ); } } } timer.Stop(); Console.WriteLine( "Project loaded in {0}", timer.Elapsed ); return project; }
public static void Save( TextWriter stream, Project project ) { Stopwatch timer = new Stopwatch(); timer.Start(); TextExport.WriteEntries( stream, project.Children ); timer.Stop(); Console.WriteLine( "Project saved in {0}", timer.Elapsed ); }
public static void Save( TextWriter stream, Project project ) { Stopwatch timer = new Stopwatch(); timer.Start(); // Compute daily statistics DateTime lastSaveTime = DateTime.Now; TimeSpan elapsedThisSession = lastSaveTime - project.SessionStartTime; project.DailyRecord.CurrentWords = project.NumWords; project.DailyRecord.Elapsed += elapsedThisSession; project.SessionStartTime = lastSaveTime; using( XmlTextWriter writer = new XmlTextWriter( stream ) ) { writer.Formatting = Formatting.Indented; writer.Indentation = 0; writer.IndentChar = ' '; writer.WriteStartDocument(); writer.WriteStartElement( "project" ); XmlStorage.WriteEntries( writer, project.Children ); XmlStorage.WriteStatistics( writer, project.DailyRecords ); writer.WriteEndElement(); // project writer.WriteEndDocument(); } timer.Stop(); Console.WriteLine( "Project saved in {0}", timer.Elapsed ); }
public static void Save( string filename, Project project ) { using( StreamWriter stream = File.CreateText( filename ) ) { XmlStorage.Save( stream, project ); } }
/// <summary> /// Legacy code to read from a plain text file. /// </summary> /// <param name="filename">Filename to load</param> /// <param name="encoding">Character encoding; usually UTF-8 but 1252 /// also useful.</param> /// <returns></returns> public static Project LoadFromText( string filename, Encoding encoding ) { Stopwatch timer = new Stopwatch(); timer.Start(); Project project = new Project(); using( FileStream stream = File.Open( filename, FileMode.Open ) ) { using( StreamReader reader = new StreamReader( stream, encoding ) ) { int index = 1; StringBuilder sb = new StringBuilder(); string line = reader.ReadLine(); do { if( line.StartsWith( "#" ) ) { Content content = new Content(); content.Parent = project; content.Title = string.Empty; content.Text = sb.ToString(); project.Children.Add( content ); index++; sb = new StringBuilder(); } else { sb.Append( line ); sb.AppendLine(); } line = reader.ReadLine(); } while( line != null ); if( sb.Length > 0 ) { Content content = new Content(); content.Parent = project; content.Title = string.Empty; content.Text = sb.ToString(); project.Children.Add( content ); index++; } } } timer.Stop(); Console.WriteLine( "Text loaded in {0}", timer.Elapsed ); return project; }
private static Project SaveAndLoadProject( Project project ) { MemoryStream stream = new MemoryStream(); using( StreamWriter writer = new StreamWriter( stream ) ) { XmlStorage.Save( writer, project ); } byte[] buffer = stream.GetBuffer(); //Console.WriteLine( System.Text.Encoding.UTF8.GetString( buffer ) ); MemoryStream stream2 = new MemoryStream( buffer ); using( StreamReader reader = new StreamReader( stream2 ) ) { Project testProject = XmlStorage.Load( reader ); return testProject; } }
private static void SaveAndLoadContent( Content content ) { Project project = new Project(); project.Children.Add( content ); Project testProject = SaveAndLoadProject( project ); Assert.AreEqual( project.Children.Count, testProject.Children.Count ); Content testContent = (Content)testProject.Children[0]; CompareContents( content, testContent ); }