PageRenderTime 51ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/Source Code/Build/CreateBibleArchive.vb

#
Visual Basic | 100 lines | 76 code | 19 blank | 5 comment | 0 complexity | f1fa5c31e69d8c2cba4ca8733462d3c8 MD5 | raw file
  1. Imports PowerSong.FileArchive
  2. Imports System.IO
  3. Imports PowerSong.FastSearching
  4. Imports PowerSong.SongDatabase
  5. Imports PowerSong.SongDatabase.Bibles
  6. Public NotInheritable Class CreateBibleArchive
  7. Public Shared Sub Create(ByVal sourceFileName As String, ByVal destinationFileName As String)
  8. Console.WriteLine(" *** CREATING BIBLE ARCHIVE FILE ***")
  9. Console.WriteLine(" Source: " + sourceFileName)
  10. Console.WriteLine(" Destination: " + destinationFileName)
  11. Console.WriteLine()
  12. Dim FileIn As New StreamReader(New FileStream(sourceFileName, FileMode.Open))
  13. Dim Chapters As New Dictionary(Of String, List(Of String))
  14. While Not FileIn.EndOfStream
  15. ' Get data from the line
  16. Dim NextLine As String = FileIn.ReadLine()
  17. If NextLine.Trim <> "" Then
  18. Dim ReferenceText As String = NextLine.Substring(0, NextLine.IndexOf(" "))
  19. Dim VerseText As String = NextLine.Substring(ReferenceText.Length + 1)
  20. Dim Reference As BibleReference = BibleSupport.DetermineSingleReference(ReferenceText)
  21. ' Determine if we need to add a chapter
  22. Dim ChapterKey As String = BibleSupport.BookNames(Reference.Book) + " " + Reference.Chapter.ToString
  23. If Not Chapters.ContainsKey(ChapterKey) Then
  24. Chapters.Add(ChapterKey, New List(Of String))
  25. Console.WriteLine("Adding chapter: " + ChapterKey)
  26. End If
  27. Chapters(ChapterKey).Add(VerseText)
  28. End If
  29. End While
  30. FileIn.Close()
  31. Console.WriteLine()
  32. ' Create temporary chapter files
  33. Dim FileTable As New FileTable
  34. For Each BookChapter As KeyValuePair(Of String, List(Of String)) In Chapters
  35. ' Determine the file name
  36. Dim ChapterFilename As String = Path.GetDirectoryName(destinationFileName) + "\" + BookChapter.Key + ".txt"
  37. If File.Exists(ChapterFilename) Then Throw New Exception("File already exists: '" + ChapterFilename + "'.")
  38. Console.WriteLine("Creating file: " + ChapterFilename)
  39. ' Create the file
  40. Dim FileOut As New StreamWriter(New FileStream(ChapterFilename, FileMode.Create))
  41. For LineIndex As Integer = 0 To BookChapter.Value.Count - 1
  42. FileOut.Write(BookChapter.Value(LineIndex))
  43. If LineIndex <> BookChapter.Value.Count - 1 Then FileOut.WriteLine()
  44. Next
  45. FileOut.Close()
  46. FileTable.AddFile(BookChapter.Key + ".txt", ChapterFilename)
  47. Next
  48. ' Create the archive file
  49. Console.Write("Creating archive file... ")
  50. ArchiveFile.Create(destinationFileName, FileTable)
  51. Console.WriteLine("DONE.")
  52. ' Create the index
  53. Console.Write("Creating index... ")
  54. Dim Indexer As New Indexer
  55. For Each BookChapter As KeyValuePair(Of String, List(Of String)) In Chapters
  56. Dim ChapterFilename As String = Path.GetDirectoryName(destinationFileName) + "\" + BookChapter.Key + ".txt"
  57. Indexer.AddFileToIndex(BookChapter.Key + ".txt", ChapterFilename)
  58. Next
  59. Console.WriteLine("DONE.")
  60. ' Save the index
  61. Console.Write("Saving index file... ")
  62. Dim IndexFilename As String = Path.GetDirectoryName(destinationFileName) + "\" + Path.GetFileNameWithoutExtension(destinationFileName) + ".Index"
  63. Indexer.Save(IndexFilename)
  64. Console.WriteLine("DONE.")
  65. ' Delete the temporary files
  66. Console.Write("Deleting temporary files... ")
  67. For Each BookChapter As KeyValuePair(Of String, List(Of String)) In Chapters
  68. Dim ChapterFilename As String = Path.GetDirectoryName(destinationFileName) + "\" + BookChapter.Key + ".txt"
  69. File.Delete(ChapterFilename)
  70. Next
  71. Console.WriteLine("DONE.")
  72. Console.WriteLine()
  73. End Sub
  74. Private Sub New()
  75. End Sub
  76. End Class