"Fossies" - the Fresh Open Source Software Archive 
Member "cb2bib-2.0.1/src/c2b/journalDB.cpp" (12 Feb 2021, 9405 Bytes) of package /linux/privat/cb2bib-2.0.1.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "journalDB.cpp" see the
Fossies "Dox" file reference documentation and the latest
Fossies "Diffs" side-by-side code changes report:
2.0.0_vs_2.0.1.
1 /***************************************************************************
2 * Copyright (C) 2004-2021 by Pere Constans
3 * constans@molspaces.com
4 * cb2Bib version 2.0.1. Licensed under the GNU GPL version 3.
5 * See the LICENSE file that comes with this distribution.
6 ***************************************************************************/
7 #include "journalDB.h"
8
9 #include "cb2bib_utilities.h"
10
11 #include <QFile>
12 #include <QObject>
13 #include <QTextStream>
14
15
16 journalDB::journalDB(const QString& dbfile)
17 {
18 if (dbfile.isEmpty())
19 {
20 c2bUtils::warn(QObject::tr("No journal file especified"));
21 return;
22 }
23 _nitems = 0;
24 QFile file(dbfile);
25 if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
26 {
27 c2bUtils::warn(QObject::tr("Could not open journal file %1 for reading").arg(dbfile));
28 return;
29 }
30 QTextStream stream(&file);
31 stream.setCodec("UTF-8");
32 stream.setAutoDetectUnicode(true);
33 QString line;
34 int line_number(0);
35 while (!stream.atEnd())
36 {
37 line = stream.readLine();
38 ++line_number;
39 if (line.isEmpty() || line.startsWith('#'))
40 continue;
41 const QStringList spLine(line.split('|', QString::SkipEmptyParts));
42 if (spLine.count() != 3)
43 {
44 c2bUtils::warn(QObject::tr("Syntax error in journal file at line %1").arg(line_number));
45 continue;
46 }
47 const QStringList spAbbreviated(spLine.at(1).split('=', QString::SkipEmptyParts));
48 const int na(spAbbreviated.count());
49 if (na < 1 || na > 2)
50 {
51 c2bUtils::warn(QObject::tr("Syntax error in journal file at line %1").arg(line_number));
52 continue;
53 }
54 const QStringList spExtended(spLine.at(2).split('=', QString::SkipEmptyParts));
55 const int ne(spExtended.count());
56 if (ne < 1 || ne > 2)
57 {
58 c2bUtils::warn(QObject::tr("Syntax error in journal file at line %1").arg(line_number));
59 continue;
60 }
61 for (int e = 0, c = 0; e < ne; ++e)
62 for (int a = 0; a < na; ++a)
63 {
64 _nitems++;
65 JCode += (c++ == 0) ? spLine.at(0).toLower() : QString();
66 JAbbrev += spAbbreviated.at(a);
67 JExtended += spExtended.at(e);
68 }
69 }
70 file.close();
71 JAbbrev_simp.resize(_nitems);
72 JExtended_simp.resize(_nitems);
73 for (int i = 0; i < _nitems; ++i)
74 {
75 JAbbrev_simp[i] = JAbbrev.at(i).toLower();
76 JAbbrev_simp[i].remove(c2bUtils::nonLetter);
77 JExtended_simp[i] = JExtended.at(i).toLower();
78 JExtended_simp[i].remove(c2bUtils::nonLetter);
79 }
80 }
81
82
83 /** \page journalproc Processing of Journal Names
84
85 cb2Bib processes journal names according to its editable database, stored at
86 <tt>abbreviations.txt</tt>. This file contains a list of journal names
87 equivalences: a capital-letter acronym, the abbreviated form, and the title
88 of the journal, all three on one single line.
89
90 The <tt>abbreviations.txt</tt> file has the following structure:
91 \verbatim
92 JA|J. Abbrev.|Journal of Abbreviations
93 AN|Am. Nat.=Amer. Naturalist|American Naturalist=The American Naturalist
94 \endverbatim
95
96 The first field, the capital-letter acronym, is a user-defined shorthand to
97 access a journal title by typing it at the extraction panel.
98
99 The second field is the abbreviated form of the journal. To adapt to multiple
100 abbreviations in use, cb2Bib allows one alternate version of the
101 abbreviation, indicated with an equal sign <tt>=</tt>. In the above example,
102 the ISO 4 abbreviation 'Am. Nat.' is the primary one and 'Amer. Naturalist'
103 is the alternate one.
104
105 Finally, the third field is the full title of the journal. As for the
106 abbreviations, the full title also admits one alternate form.
107
108 Abbreviated and full title alternates serve two purposes: journal recognition
109 and citation styling. The former is performed internally by cb2Bib as part of
110 a bibliographic reference extraction, and the latter is accomplished in the
111 embedded BibTeX editor by replacing back and forth abbreviated-full forms, in
112 order to set journals in accordance to the guidelines of a particular
113 publication.
114
115 <p> </p>
116
117 Journal names processing is performed whenever a string is recognized as
118 'journal', and, additionally, when pressing <tt>Intro Key</tt> at the journal
119 edit line.
120
121 <p> </p>
122
123 - Retrieves Journal name in <b>abbreviated form</b> if found.
124
125 - If Journal name is not found in the database, returns input Journal name.
126
127 - Search is case insensitive.
128
129 - **Warning:** Journal codes can be duplicated. If duplicated, returns
130 input Journal name.
131
132 <p> </p>
133
134 - Retrieves Journal name in <b>full form</b> if found.
135
136 - If Journal name is not found in the database, returns input Journal name.
137
138 - Search is case insensitive.
139
140 - **Warning:** Journal codes can be duplicated. If duplicated, returns
141 input Journal name.
142
143 <p> </p>
144
145 See \ref c2bconf_files, \ref c2bconf_bibtex, and \ref c2beditor_menu.
146
147 */
148 QString journalDB::retrieve(const QString& JQuery) const
149 {
150 const QString query(JQuery.toLower().remove(c2bUtils::nonLetter));
151 if (query.isEmpty())
152 return QString();
153 for (int i = 0; i < _nitems; ++i)
154 {
155 if (JAbbrev_simp.at(i) == query)
156 {
157 if (i > 0 && JExtended_simp.at(i) == JExtended_simp.at(i - 1))
158 return JAbbrev.at(i - 1);
159 else
160 return JAbbrev.at(i);
161 }
162 if (JExtended_simp.at(i) == query)
163 return JAbbrev.at(i);
164 }
165 int journal_found(0);
166 int journal_found_at(-1);
167 for (int i = 0; i < _nitems; ++i)
168 if (JCode.at(i) == query)
169 {
170 journal_found++;
171 journal_found_at = i;
172 }
173 if (journal_found == 1)
174 return JAbbrev.at(journal_found_at);
175 else
176 return JQuery;
177 }
178
179 QString journalDB::retrieveFull(const QString& JQuery) const
180 {
181 const QString query(JQuery.toLower().remove(c2bUtils::nonLetter));
182 if (query.isEmpty())
183 return QString();
184 for (int i = 0; i < _nitems; ++i)
185 {
186 if (JAbbrev_simp.at(i) == query)
187 return JExtended.at(i);
188 if (JExtended_simp.at(i) == query)
189 {
190 if (i > 0 && JAbbrev_simp.at(i) == JAbbrev_simp.at(i - 1))
191 return JExtended.at(i - 1);
192 else if (i > 1 && JAbbrev_simp.at(i) == JAbbrev_simp.at(i - 2))
193 return JExtended.at(i - 2);
194 else
195 return JExtended.at(i);
196 }
197 }
198 int journal_found(0);
199 int journal_found_at(-1);
200 for (int i = 0; i < _nitems; ++i)
201 if (JCode.at(i) == query)
202 {
203 journal_found++;
204 journal_found_at = i;
205 }
206 if (journal_found == 1)
207 return JExtended.at(journal_found_at);
208 else
209 return JQuery;
210 }
211
212 QString journalDB::retrieveAlternate(const QString& JQuery) const
213 {
214 const QString query(JQuery.toLower().remove(c2bUtils::nonLetter));
215 if (query.isEmpty())
216 return QString();
217 for (int i = _nitems - 1; i >= 0; --i)
218 {
219 if (JAbbrev_simp.at(i) == query)
220 {
221 if (i + 1 < _nitems && JExtended_simp.at(i) == JExtended_simp.at(i + 1))
222 return JAbbrev.at(i + 1);
223 else
224 return JAbbrev.at(i);
225 }
226 if (JExtended_simp.at(i) == query)
227 return JAbbrev.at(i);
228 }
229 int journal_found(0);
230 int journal_found_at(-1);
231 for (int i = 0; i < _nitems; i++)
232 if (JCode.at(i) == query)
233 {
234 journal_found++;
235 journal_found_at = i;
236 }
237 if (journal_found == 1)
238 {
239 if (journal_found_at + 1 < _nitems && JCode.at(journal_found_at + 1).isEmpty())
240 return JAbbrev.at(journal_found_at + 1);
241 else
242 return JAbbrev.at(journal_found_at);
243 }
244 else
245 return JQuery;
246 }
247
248 QString journalDB::retrieveAlternateFull(const QString& JQuery) const
249 {
250 const QString query(JQuery.toLower().remove(c2bUtils::nonLetter));
251 if (query.isEmpty())
252 return QString();
253 for (int i = _nitems - 1; i >= 0; --i)
254 {
255 if (JAbbrev_simp.at(i) == query)
256 return JExtended.at(i);
257 if (JExtended_simp.at(i) == query)
258 {
259 if (i + 2 < _nitems && JAbbrev_simp.at(i) == JAbbrev_simp.at(i + 2))
260 return JExtended.at(i + 2);
261 else if (i + 1 < _nitems && JAbbrev_simp.at(i) == JAbbrev_simp.at(i + 1))
262 return JExtended.at(i + 1);
263 else
264 return JExtended.at(i);
265 }
266 }
267 int journal_found(0);
268 int journal_found_at(-1);
269 for (int i = 0; i < _nitems; ++i)
270 if (JCode.at(i) == query)
271 {
272 journal_found++;
273 journal_found_at = i;
274 }
275 if (journal_found == 1)
276 {
277 if (journal_found_at + 2 < _nitems && JCode.at(journal_found_at + 1).isEmpty() &&
278 JCode.at(journal_found_at + 2).isEmpty())
279 return JExtended.at(journal_found_at + 2);
280 else if (journal_found_at + 1 < _nitems && JCode.at(journal_found_at + 1).isEmpty())
281 return JExtended.at(journal_found_at + 1);
282 else
283 return JExtended.at(journal_found_at);
284 }
285 else
286 return JQuery;
287 }