GCC Code Coverage Report


Directory: ./
File: lib/geogram/bibliography/bibliography.cpp
Date: 2026-09-07 02:25:23
Exec Total Coverage
Lines: 15 71 21.1%
Functions: 4 6 66.7%
Branches: 7 166 4.2%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2000-2022 Inria
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * * Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * * Neither the name of the ALICE Project-Team nor the names of its
14 * contributors may be used to endorse or promote products derived from this
15 * software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 *
29 * Contact: Bruno Levy
30 *
31 * https://www.inria.fr/fr/bruno-levy
32 *
33 * Inria,
34 * Domaine de Voluceau,
35 * 78150 Le Chesnay - Rocquencourt
36 * FRANCE
37 *
38 */
39
40 #include <geogram/bibliography/bibliography.h>
41 #include <geogram/basic/memory.h>
42 #include <geogram/basic/command_line.h>
43 #include <geogram/basic/logger.h>
44 #include <geogram/basic/stopwatch.h>
45 #include <string>
46
47 namespace {
48 using namespace GEO;
49
50 double timeorigin;
51
52 vector<const char*> bib_refs_;
53
54 struct CitationRecord {
55 CitationRecord(
56 const std::string& k,
57 const std::string& f, int l,
58 const std::string& func,
59 const std::string& inf
60 ) : key(k), file(f), line(l), function(func), info(inf) {
61 timestamp = Stopwatch::now() - timeorigin;
62 }
63 std::string key;
64 std::string file;
65 int line;
66 std::string function;
67 std::string info;
68 double timestamp;
69 };
70
71 vector<CitationRecord> citations_;
72
73 bool cite_flag_initialized = false;
74 bool cite_flag = false;
75 bool use_biblio = false;
76 Process::spinlock lock = GEOGRAM_SPINLOCK_INIT;
77 }
78
79 void register_embedded_bib_file(void);
80
81 namespace GEO {
82
83 namespace Biblio {
84 249 void initialize() {
85 249 register_embedded_bib_file();
86 249 timeorigin = Stopwatch::now();
87 249 use_biblio =
88
3/8
✓ Branch 1 taken 249 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 249 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 249 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
249 CmdLine::arg_is_declared("biblio") &&
89
1/8
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 249 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
249 CmdLine::get_arg_bool("biblio");
90 249 }
91
92 249 void terminate() {
93 if(
94
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 249 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
249 use_biblio &&
95 citations_.size() != 0
96 ) {
97 Logger::div("Bibliography");
98 {
99 Logger::out("Bibliography")
100 << "Saving references to geogram.bib"
101 << std::endl;
102 std::ofstream out("geogram.bib");
103 FOR(i,bib_refs_.size()) {
104 out << bib_refs_[i];
105 }
106 }
107 {
108 Logger::out("Bibliography")
109 << "Saving citations to geogram.tex"
110 << std::endl;
111 std::ofstream out("geogram.tex");
112 out << "\\documentclass{article}" << std::endl;
113 out << "\\usepackage{url}" << std::endl;
114 out << "\\title{Geogram Bibliography Report}" << std::endl;
115 out << "\\date{\\today}" << std::endl;
116 out << "\\author{Geogram ver. "
117 << Environment::instance()->get_value("version")
118 << " citation subsystem"
119 << "}"
120 << std::endl;
121 out << "\\begin{document}" << std::endl;
122 out << "\\maketitle" << std::endl;
123
124 if(CmdLine::get_arg_bool("biblio:command_line")) {
125 out << "\\section*{Command Line}" << std::endl;
126 std::vector<std::string> args;
127 CmdLine::get_args(args);
128 out << "\\begin{small}" << std::endl;
129 out << "\\begin{enumerate}" << std::endl;
130 FOR(i,args.size()) {
131 out << "\\item \\verb|"
132 << args[i] << "| " << std::endl;
133 }
134 out << "\\end{enumerate}" << std::endl;
135 out << "\\end{small}" << std::endl;
136 }
137
138 out << "\\section*{Citation report}" << std::endl;
139
140 out << "\\begin{enumerate}" << std::endl;
141 FOR(i,citations_.size()) {
142 const CitationRecord& R = citations_[i];
143 std::string context =
144 R.file + ":" +
145 String::to_string(R.line);
146 out << "\\item "
147 << "\\cite{" << R.key << "}: cited from: "
148 << "\\verb|" << context << "| \\\\" << std::endl;
149 out << "\\begin{small}" << std::endl;
150 out << "\\verb|" << R.function << "|" << std::endl;
151 out << "\\end{small} \\\\" << std::endl;
152 if(R.info != "") {
153 out << "Info: " << R.info << "\\\\" << std::endl;
154 }
155 out << "Timestamp: " << R.timestamp << std::endl;
156 }
157 out << "\\end{enumerate}" << std::endl;
158 out << "\\bibliographystyle{alpha}" << std::endl;
159 out << "\\bibliography{geogram}" << std::endl;
160 out << "\\end{document}" << std::endl;
161 }
162 }
163 249 }
164
165
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 249 times.
249 void register_references(const char* bib_refs) {
166 bib_refs_.push_back(bib_refs);
167 249 }
168
169 666 void cite(
170 const char* ref, const char* file, int line, const char* function,
171 const char* info
172 ) {
173
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 666 times.
666 if(!use_biblio) {
174 666 return;
175 }
176
177 if (bib_refs_.empty()) {
178 return;
179 }
180
181 if(!cite_flag_initialized) {
182 cite_flag = (
183 CmdLine::arg_is_declared("biblio") &&
184 CmdLine::get_arg_bool("biblio")
185 );
186 cite_flag_initialized = true;
187 if(cite_flag) {
188 geo_cite("WEB:GEOGRAM");
189 }
190 }
191
192 if(!cite_flag) {
193 return;
194 }
195
196 std::string shortfile = file;
197 size_t pos = shortfile.find("src/lib/");
198 if(pos != std::string::npos) {
199 shortfile = shortfile.substr(pos+8, shortfile.length()-pos-8);
200 }
201 pos = shortfile.find("OGF/");
202 if(pos != std::string::npos) {
203 shortfile = shortfile.substr(pos, shortfile.length()-pos);
204 }
205
206 std::string shortfunction = function;
207 pos = shortfunction.find('(');
208 if(pos != std::string::npos) {
209 shortfunction = shortfunction.substr(0,pos);
210 shortfunction += "()";
211 }
212
213 pos = 0;
214 for(int i = int(shortfunction.length())-1; i>0; --i) {
215 if(shortfunction[size_t(i)] == ' ') {
216 pos = size_t(i);
217 break;
218 }
219 }
220 shortfunction =
221 shortfunction.substr(pos, shortfunction.length()-pos);
222
223 Process::acquire_spinlock(lock);
224 citations_.push_back(
225 CitationRecord(
226 ref, shortfile, line, shortfunction,
227 (info != nullptr) ? info : ""
228 )
229 );
230 Process::release_spinlock(lock);
231
232 std::string context = std::string(shortfunction) + " (" +
233 shortfile + ":" +
234 String::to_string(line) + ")" ;
235
236 Logger::out("Bibliography")
237 << "[" << ref << "] cited from: "
238 << context << std::endl;
239 }
240
241 void reset_citations() {
242 citations_.clear();
243 timeorigin = Stopwatch::now();
244 }
245 }
246
247
248 }
249