PageRenderTime 13ms CodeModel.GetById 2ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/Source Code/Build/CreateBibleArchive.vb

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