/Source Code/Build/CreateBibleArchive.vb
Visual Basic | 100 lines | 76 code | 19 blank | 5 comment | 0 complexity | f1fa5c31e69d8c2cba4ca8733462d3c8 MD5 | raw file
- Imports PowerSong.FileArchive
- Imports System.IO
- Imports PowerSong.FastSearching
- Imports PowerSong.SongDatabase
- Imports PowerSong.SongDatabase.Bibles
-
- Public NotInheritable Class CreateBibleArchive
-
- Public Shared Sub Create(ByVal sourceFileName As String, ByVal destinationFileName As String)
-
- Console.WriteLine(" *** CREATING BIBLE ARCHIVE FILE ***")
- Console.WriteLine(" Source: " + sourceFileName)
- Console.WriteLine(" Destination: " + destinationFileName)
- Console.WriteLine()
-
- Dim FileIn As New StreamReader(New FileStream(sourceFileName, FileMode.Open))
- Dim Chapters As New Dictionary(Of String, List(Of String))
-
- While Not FileIn.EndOfStream
-
- ' Get data from the line
- Dim NextLine As String = FileIn.ReadLine()
- If NextLine.Trim <> "" Then
-
- Dim ReferenceText As String = NextLine.Substring(0, NextLine.IndexOf(" "))
- Dim VerseText As String = NextLine.Substring(ReferenceText.Length + 1)
- Dim Reference As BibleReference = BibleSupport.DetermineSingleReference(ReferenceText)
-
- ' Determine if we need to add a chapter
- Dim ChapterKey As String = BibleSupport.BookNames(Reference.Book) + " " + Reference.Chapter.ToString
- If Not Chapters.ContainsKey(ChapterKey) Then
- Chapters.Add(ChapterKey, New List(Of String))
- Console.WriteLine("Adding chapter: " + ChapterKey)
- End If
-
- Chapters(ChapterKey).Add(VerseText)
-
- End If
-
- End While
-
- FileIn.Close()
- Console.WriteLine()
-
- ' Create temporary chapter files
- Dim FileTable As New FileTable
- For Each BookChapter As KeyValuePair(Of String, List(Of String)) In Chapters
-
- ' Determine the file name
- Dim ChapterFilename As String = Path.GetDirectoryName(destinationFileName) + "\" + BookChapter.Key + ".txt"
- If File.Exists(ChapterFilename) Then Throw New Exception("File already exists: '" + ChapterFilename + "'.")
- Console.WriteLine("Creating file: " + ChapterFilename)
-
- ' Create the file
- Dim FileOut As New StreamWriter(New FileStream(ChapterFilename, FileMode.Create))
- For LineIndex As Integer = 0 To BookChapter.Value.Count - 1
- FileOut.Write(BookChapter.Value(LineIndex))
- If LineIndex <> BookChapter.Value.Count - 1 Then FileOut.WriteLine()
- Next
- FileOut.Close()
- FileTable.AddFile(BookChapter.Key + ".txt", ChapterFilename)
-
- Next
-
- ' Create the archive file
- Console.Write("Creating archive file... ")
- ArchiveFile.Create(destinationFileName, FileTable)
- Console.WriteLine("DONE.")
-
- ' Create the index
- Console.Write("Creating index... ")
- Dim Indexer As New Indexer
- For Each BookChapter As KeyValuePair(Of String, List(Of String)) In Chapters
- Dim ChapterFilename As String = Path.GetDirectoryName(destinationFileName) + "\" + BookChapter.Key + ".txt"
- Indexer.AddFileToIndex(BookChapter.Key + ".txt", ChapterFilename)
- Next
- Console.WriteLine("DONE.")
-
- ' Save the index
- Console.Write("Saving index file... ")
- Dim IndexFilename As String = Path.GetDirectoryName(destinationFileName) + "\" + Path.GetFileNameWithoutExtension(destinationFileName) + ".Index"
- Indexer.Save(IndexFilename)
- Console.WriteLine("DONE.")
-
- ' Delete the temporary files
- Console.Write("Deleting temporary files... ")
- For Each BookChapter As KeyValuePair(Of String, List(Of String)) In Chapters
- Dim ChapterFilename As String = Path.GetDirectoryName(destinationFileName) + "\" + BookChapter.Key + ".txt"
- File.Delete(ChapterFilename)
- Next
- Console.WriteLine("DONE.")
- Console.WriteLine()
-
- End Sub
-
- Private Sub New()
-
- End Sub
-
- End Class