/// <summary> /// Returns the verse text. /// </summary> /// <param name="version">Bible version - obtained from getBibles()</param> /// <param name="b">1-based book (1..66)</param> /// <param name="c">1-based chapter (1..150)</param> /// <param name="v">2-based verse (1..?)</param> /// <returns></returns> public string getVerseText(string version, int b, int c, int v) { SWModule module = getModule(version); VerseKey vk = new VerseKey(); vk.AutoNormalize((char)0); // Or else it will get improperly normalized before it's fully configured. if (b < 40) { vk.Testament((char)1); // OT = 1..39 -> Sword = 1..39 vk.Book((char)b); } else { vk.Testament((char)2); // NT = 40..66 -> Sword = 1..27 vk.Book((char)(b - 39)); } vk.Chapter(c); vk.Verse(v); /* vk.Error() would return an error (non-zero) if only vk.Testament is set (even though * it sets Book = Chapter = Verse = 1). * * If there's an error in vk, the increment below would be ignored, so we either have * to call vk.Error() to clear it, or set the Book, Chapter, and Verse. * vk.Error(); * vk.Book((char)1); * vk.Chapter(1); * vk.Verse(1); * * The following two don't work because incrementing the index takes us through * headings and other non-verse indices: * vk.increment(verseIdx); * vk.Index(vk.Index() + verseIdx); */ if (vk.Error() != '\0') { // report the error } return(DreamTools.Sword_ConvertEncoding(module.RenderText(vk)).Trim()); }
/// <summary> /// Searching with regular expressions through bible texts that contain /// punctuation and foreign characters is no fun. This function strips /// out or replaces all the undesirables to make searching easier. /// </summary> /// <param name="verses">The bible text</param> /// <returns>The converted text</returns> private string Replace(string text) { if (_replacements == null) { return(DreamTools.RemoveNonAlpha(DreamTools.RemoveDiacritics(text))); } foreach (System.Data.DataRow r in _replacements.Rows) { // If the user entered an invalid regex, we need to trap it try { text = Regex.Replace(text, r.ItemArray[0].ToString(), r.ItemArray[1].ToString()); } catch { } } return(text); }
/// <summary> /// Finds the first bible book that starts with the letters passed in. /// </summary> /// <param name="book">The letters the book starts with.</param> /// <returns>A number: 1-66, or -1 if no book starts with the letters passed in.</returns> public int BookNumber(string book) { int i = 0; string bLC; book = DreamTools.RemoveDiacritics(book.ToLower()); foreach (BibleBook b in BibleBooks) { bLC = DreamTools.RemoveDiacritics(b.Long.ToLower()); if (bLC.StartsWith(book)) { return(i); } i++; } return(-1); }
/// <summary> /// Returns a bible reference (ex. Genesis 4:7) after removing special /// characters from the book name. /// </summary> /// <param name="verseIdx"></param> /// <param name="Abbreviated">If true, returns the abbreviated name of the bible book</param> /// <returns></returns> public string GetSimpleRef(int verseIdx, bool Abbreviated) { if (verseIdx < 0 || verseIdx >= _VerseCount) { return(""); } BibleVerse v = verses[verseIdx]; if (Abbreviated) { return(string.Format("{0} {1}:{2}", DreamTools.RemoveDiacritics(BibleBooks[v.b].Short), v.c, v.v)); } else { return(string.Format("{0} {1}:{2}", DreamTools.RemoveDiacritics(BibleBooks[v.b].Long), v.c, v.v)); } }
/// <summary> /// Finds a verse matching the regex, and updates the RTF control to make the verse visible. /// </summary> /// <param name="bible"></param> /// <param name="dirFwd"></param> /// <param name="regex"></param> /// <returns>The result of calling BibleVersion.Find (negative number to indicate failure, or a 0-based verse index)</returns> public int Find(BibleVersion bible, bool dirFwd, string regex) { DreamTools.ElapsedTime("Start regex search"); if (regex.Length == 0) { return(-1); } // We need to handle "FindNext" and "FindPrevious" type searches if (regex == lastRegex) { if (currentVerse == matchFound && dirFwd) { currentVerse++; } else if (currentVerse == matchFound && !dirFwd) { currentVerse--; } } int target = bible.Find(currentVerse, dirFwd, regex); DreamTools.ElapsedTime("End regex search"); lastRegex = regex; if (target >= 0) { currentVerse = target; matchFound = target; Populate(bible, target); this.HighlightVerseRegex(target, regex); } else if (matchFound >= 0) { // Re-populate to remove highlight Populate(bible, currentVerse); matchFound = -1; } return(target); }
public BibleVersion(BackgroundWorker worker, DoWorkEventArgs evArg, string version, System.Data.DataTable replacements) { _version = version; _replacements = replacements; int i = 0, b = -1, book; System.EventArgs e = new System.EventArgs(); SWModule module = SwordW.Instance().getModule(version); VerseKey vk = new VerseKey("Gen 1:1"); while (vk.Error() == '\0') { if ((i % 300) == 0) { int progress = (int)(((float)i / 31102F) * 100); worker.ReportProgress(progress); } string t = DreamTools.Sword_ConvertEncoding(module.RenderText(vk)).Trim(); book = vk.Book(); // Book numbering is 1-based and starts back up from 1 in the New Testament // OT = 1, NT = 2 if (vk.Testament() == (char)2) { book += 39; } BibleVerse v = new BibleVerse(i, book - 1, vk.Chapter(), vk.Verse(), Replace(t)); verses[i] = v; // book is 1-based, b is 0-based if (b < book - 1) { b = book - 1; BibleBooks[b].Long = vk.getBookName(); Console.WriteLine("Processing " + BibleBooks[b].Long); if (worker.CancellationPending) { evArg.Cancel = true; } } i++; vk.increment(); } _VerseCount = i; for (i = 0; i < this.BibleBooks.Length; i++) { if (BibleBooks[i].Long == null) { BibleBooks[i].Long = ""; } string bk = BibleBooks[i].Long; bk = Regex.Replace(bk.Trim(), @"^III ", "3 "); bk = Regex.Replace(bk, @"^II ", "2 "); bk = Regex.Replace(bk, @"^I ", "1 "); BibleBooks[i].Long = bk; BibleBooks[i].Short = bk; } worker.ReportProgress(100); }