File: kjv.cpp

package info (click to toggle)
bibledit-cloud 5.1.036-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, 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 (181 lines) | stat: -rw-r--r-- 6,563 bytes parent folder | download | duplicates (4)
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
/*
 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 <sources/kjv.h>
#include <database/logs.h>
#include <database/kjv.h>
#include <database/sqlite.h>
#include <filter/string.h>
#include <filter/url.h>
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wsuggest-override"
#pragma GCC diagnostic ignored "-Wzero-as-null-pointer-constant"
#ifndef HAVE_PUGIXML
#include <pugixml/pugixml.hpp>
#endif
#ifdef HAVE_PUGIXML
#include <pugixml.hpp>
#endif
#pragma GCC diagnostic pop


// Declarations local functions.
void sources_kjv_store (int book, int chapter, int verse, std::string lemma, std::string english);
void sources_kjv_parse_loop (pugi::xml_node element, int & book, int & chapter, int & verse, bool & within_verse, std::string & lemma);


void sources_kjv_store (int book, int chapter, int verse, std::string lemma, std::string english)
{
  Database_Kjv database_kjv;
  std::vector <std::string> lemmas = filter::strings::explode (lemma, ' ');
  bool output_done = false;
  for (auto & strong : lemmas) {
    if (strong.find ("strong") == std::string::npos) continue;
    if (output_done) {
      // Two separate two identical words.
      database_kjv.store (book, chapter, verse, "", " / ");
    }
    strong = filter::strings::replace ("strong:", "", strong);
    database_kjv.store (book, chapter, verse, strong, english);
    output_done = true;
  }
  if (lemmas.empty ()) {
    database_kjv.store (book, chapter, verse, "", english);
  }
}


void sources_kjv_parse_loop (pugi::xml_node element,
                             int & book, int & chapter, int & verse,
                             bool & within_verse, std::string & lemma)
{
  std::string element_name = element.name ();
  if (element_name == "verse") {
    std::string sID = element.attribute ("sID").value ();
    if (!sID.empty ()) {
      verse++;
      within_verse = true;
    }
    std::string eID = element.attribute ("eID").value ();
    if (!eID.empty ()) {
      within_verse = false;
    }
  } else if (element_name == "w") {
    if (within_verse) {
      lemma = element.attribute ("lemma").value ();
      lemma = filter::strings::trim (lemma);
      for (pugi::xml_node child : element.children ()) {
        sources_kjv_parse_loop (child, book, chapter, verse, within_verse, lemma);
      }
    }
  } else if (element_name.empty ()) {
    if (within_verse) {
      std::string english = element.value ();
      sources_kjv_store (book, chapter, verse, lemma, english);
    }
  } else if (element_name == "note") {
    pugi::xml_node textnode = element.first_child ();
    std::string english = textnode.text ().get ();
    english.insert (0, " [");
    english.append ("]");
    sources_kjv_store (book, chapter, verse, "", english);
  } else if (element_name == "milestone") {
  } else if (element_name == "transChange") {
    pugi::xml_node textnode = element.first_child ();
    std::string english = textnode.text ().get ();
    english.insert (0, "<span style=\"font-style:italic;\">");
    english.append ("</span>");
    sources_kjv_store (book, chapter, verse, "", english);
  } else if (element_name == "inscription") {
    for (pugi::xml_node child : element.children ()) {
      std::string tmp_lemma;
      sources_kjv_parse_loop (child, book, chapter, verse, within_verse, tmp_lemma);
    }
  } else if (element_name == "q") {
    for (pugi::xml_node child : element.children ()) {
      std::string tmp_lemma;
      sources_kjv_parse_loop (child, book, chapter, verse, within_verse, tmp_lemma);
    }
  } else if (element_name == "divineName") {
    for (pugi::xml_node child : element.children ()) {
      std::string tmp_lemma;
      sources_kjv_parse_loop (child, book, chapter, verse, within_verse, tmp_lemma);
    }
  } else if (element_name == "title") {
    for (pugi::xml_node child : element.children ()) {
      std::string tmp_lemma;
      sources_kjv_parse_loop (child, book, chapter, verse, within_verse, tmp_lemma);
    }
  } else if (element_name == "foreign") {
    for (pugi::xml_node child : element.children ()) {
      std::string tmp_lemma;
      sources_kjv_parse_loop (child, book, chapter, verse, within_verse, tmp_lemma);
    }
  } else {
    if (within_verse) {
      pugi::xml_node textnode = element.first_child ();
      std::string english = textnode.text ().get ();
      std::string location = filter_passage_display (book, chapter, std::to_string (verse));
      Database_Logs::log (location + ": Failed to parse element " + element_name + " with value " + english);
    }
  }
}


// Parses the XML data from kjv.xml.
void sources_kjv_parse ()
{
  Database_Logs::log ("Parsing data from Crosswire's KJV XML file");
  Database_Kjv database_kjv;
  database_kjv.create ();

  int book = 0;
  int chapter = 0;
  int verse = 0;
  bool within_verse = false;

  pugi::xml_document document;
  document.load_file ("sources/kjv.xml", pugi::parse_ws_pcdata);
  for (pugi::xml_node osis : document.children ()) {
    for (pugi::xml_node osisText : osis.children ()) {
      for (pugi::xml_node divbook : osisText.children ()) {
        if (strcmp (divbook.name (), "div") == 0) {
          book++;
          Database_Logs::log ("Book " + std::to_string (book));
          chapter = 0;
          for (pugi::xml_node chapter_element : divbook.children ()) {
            if (strcmp (chapter_element.name (), "chapter") == 0) {
              chapter++;
              verse = 0;
              within_verse = false;
              for (pugi::xml_node element : chapter_element.children ()) {
                std::string lemma;
                sources_kjv_parse_loop (element, book, chapter, verse, within_verse, lemma);
              }
            }
          }
        }
      }
    }
  }

  database_kjv.optimize ();
  Database_Logs::log ("Finished parsing data from the KJV XML file");
}