Advertisement
codemonkey

Journal.cs

Jan 20th, 2013
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.99 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.IO;
  7.  
  8. namespace Journal
  9. {
  10.     class Journal
  11.     {
  12.         public List<JournalEntry> Entries;
  13.  
  14.         private static readonly string EntryLineRegex =
  15.             @"-- Entry: (?<title>.*) \((?<year>\d{4})-(?<month>\d{2})" +
  16.             @"-(?<day>\d{2})\)";
  17.  
  18.         public static Journal FromFile(string filePath)
  19.         {
  20.             Journal returnValue = new Journal();
  21.  
  22.             StreamReader fileReader = new StreamReader(filePath);
  23.  
  24.             // Prepare variables for parsing the journal file.
  25.             bool hitFirstEntry = false;
  26.             DateTime currentEntryDate;
  27.             string currentEntryTitle;
  28.             StringBuilder currentEntryText = new StringBuilder();
  29.  
  30.             // Prepare a regular expression for the entry lines.
  31.             Regex entryLineRegex = new Regex(EntryLineRegex);
  32.  
  33.             while (!fileReader.EndOfStream)
  34.             {
  35.                 string line = fileReader.ReadLine();
  36.  
  37.                 if (line.StartsWith("--"))
  38.                 {
  39.                     // Is this the first entry encountered? If so, don't try to
  40.                     // process the previous entry.
  41.                     if (!hitFirstEntry)
  42.                     {
  43.                         hitFirstEntry = true;
  44.                     }
  45.                     else
  46.                     {
  47.                         // Create a JournalEntry with the current entry, then
  48.                         // reset for the next entry.
  49.                         returnValue.Entries.Add(
  50.                             new JournalEntry(
  51.                                 currentEntryText.ToString(), currentEntryDate
  52.                             )
  53.                         );
  54.  
  55.                         currentEntryDate = new DateTime();
  56.                         currentEntryText.Clear();
  57.                     }
  58.  
  59.                     // Extract the new entry title and date from this line and
  60.                     // save them.
  61.                     Match entryMatch = entryLineRegex.Match(line);
  62.                     GroupCollection matches = entryMatch.Groups;
  63.  
  64.                     currentEntryDate = new DateTime(
  65.                         Convert.ToInt16(matches["year"].Value),
  66.                         Convert.ToInt16(matches["month"].Value),
  67.                         Convert.ToInt16(matches["day"].Value)
  68.                     );
  69.  
  70.                     currentEntryTitle = matches["title"].Value;
  71.                 }
  72.                 else
  73.                 {
  74.                     currentEntryText.Append(line);
  75.                 }
  76.             }
  77.  
  78.             return returnValue;
  79.         }
  80.     }
  81.  
  82.     class JournalEntry
  83.     {
  84.         public string Text;
  85.         public DateTime EntryDate;
  86.  
  87.         public JournalEntry(string text, DateTime entryDate)
  88.         {
  89.             this.Text = text;
  90.             this.EntryDate = entryDate;
  91.         }
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement