File: books.cpp

package info (click to toggle)
bibledit-cloud 5.1.036-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 250,636 kB
  • sloc: xml: 915,934; ansic: 261,349; cpp: 92,628; javascript: 32,542; sh: 4,915; makefile: 586; php: 69
file content (223 lines) | stat: -rw-r--r-- 5,714 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
Copyright (©) 2003-2025 Teus Benschop.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/


#include <database/books.h>
#include <config/globals.h>
#include <database/sqlite.h>
#include <filter/string.h>
#include <filter/diff.h>
#include <locale/translate.h>
#include <database/booksdata.h>


namespace database::books {


// Internal function for the number of data elements.
constexpr size_t data_count = sizeof (books_table) / sizeof (*books_table);


std::vector <book_id> get_ids ()
{
  std::vector <book_id> ids;
  for (unsigned int i = 0; i < data_count; i++) {
    book_id id = books_table[i].id;
    ids.push_back (id);
  }
  return ids;
}


book_id get_id_from_english (const std::string& english)
{
  for (unsigned int i = 0; i < data_count; i++) {
    if (english == books_table[i].english) {
      return books_table[i].id;
    }
  }
  return book_id::_unknown;
}


std::string get_english_from_id (book_id id)
{
  for (unsigned int i = 0; i < data_count; i++) {
    if (id == books_table[i].id) {
      return books_table[i].english;
    }
  }
  return translate ("Unknown");
}


std::string get_usfm_from_id (book_id id)
{
  for (unsigned int i = 0; i < data_count; i++) {
    if (id == books_table[i].id) {
      return books_table[i].usfm;
    }
  }
  return "XXX";
}


std::string get_bibleworks_from_id (book_id id)
{
  for (unsigned int i = 0; i < data_count; i++) {
    if (id == books_table[i].id) {
      return books_table[i].bibleworks;
    }
  }
  return "Xxx";
}


std::string get_osis_from_id (book_id id)
{
  for (unsigned int i = 0; i < data_count; i++) {
    if (id == books_table[i].id) {
      return books_table[i].osis;
    }
  }
  return translate ("Unknown");
}


book_id get_id_from_usfm (const std::string& usfm)
{
  for (unsigned int i = 0; i < data_count; i++) {
    if (usfm == books_table[i].usfm) {
      return books_table[i].id;
    }
  }
  return book_id::_unknown;
}


book_id get_id_from_osis (const std::string& osis)
{
  for (unsigned int i = 0; i < data_count; i++) {
    if (osis == books_table[i].osis) {
      return books_table[i].id;
    }
  }
  return book_id::_unknown;
}


book_id get_id_from_bibleworks (const std::string& bibleworks)
{
  for (unsigned int i = 0; i < data_count; i++) {
    if (bibleworks == books_table[i].bibleworks) {
      return books_table[i].id;
    }
  }
  return book_id::_unknown;
}


// Tries to interprete $text as the name of a Bible book.
// Returns the book's identifier if it succeeds.
// If it fails, it returns 0.
book_id get_id_like_text (const std::string& text)
{
  // Go through all known book names and abbreviations.
  // Note how much the $text differs from the known names.
  // Then return the best match.
  std::vector <int> ids {};
  std::vector <int> similarities {};
  for (unsigned int i = 0; i < data_count; i++) {
    int id {static_cast<int>(books_table[i].id)};
    ids.push_back (id);
    similarities.push_back (filter_diff_character_similarity (text, filter::strings::unicode_string_casefold(books_table[i].english)));
    ids.push_back (id);
    similarities.push_back (filter_diff_character_similarity (text, filter::strings::unicode_string_casefold(books_table[i].osis)));
    ids.push_back (id);
    similarities.push_back (filter_diff_character_similarity (text, books_table[i].usfm));
    ids.push_back (id);
    similarities.push_back (filter_diff_character_similarity (text, filter::strings::unicode_string_casefold(books_table[i].bibleworks)));
    ids.push_back (id);
    similarities.push_back (filter_diff_character_similarity (text, filter::strings::unicode_string_casefold(books_table[i].onlinebible)));
  }
  filter::strings::quick_sort (similarities, ids, 0, static_cast<unsigned>(ids.size()));
  int id = ids.back ();
  return static_cast<book_id>(id);
}


book_id get_id_from_onlinebible (const std::string& onlinebible)
{
  for (unsigned int i = 0; i < data_count; i++) {
    if (onlinebible == books_table[i].onlinebible) {
      return books_table[i].id;
    }
  }
  return book_id::_unknown;
}


std::string get_onlinebible_from_id (book_id id)
{
  for (unsigned int i = 0; i < data_count; i++) {
    if (id == books_table[i].id) {
      return books_table[i].onlinebible;
    }
  }
  return std::string();
}


short get_order_from_id (book_id id)
{
  for (unsigned int i = 0; i < data_count; i++) {
    if (id == books_table[i].id) {
      return books_table[i].order;
    }
  }
  return 0;
}


book_type get_type (book_id id)
{
  for (unsigned int i = 0; i < data_count; i++) {
    if (id == books_table[i].id) {
      return books_table[i].type;
    }
  }
  return book_type::unknown;
}


std::string book_type_to_string (book_type type)
{
  switch (type) {
    case book_type::unknown: return std::string();
    case book_type::old_testament: return "ot";
    case book_type::new_testament: return "nt";
    case book_type::front_back: return "frontback";
    case book_type::other: return "other";
    case book_type::apocryphal: return "ap";
    default: return std::string();
  }
  return std::string();
}


} // End of namespace.