GCC Code Coverage Report


Directory: ./
File: lib/geogram/basic/line_stream.cpp
Date: 2026-09-07 02:25:23
Exec Total Coverage
Lines: 37 42 88.1%
Functions: 5 5 100.0%
Branches: 21 42 50.0%

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/basic/line_stream.h>
41 #include <geogram/basic/logger.h>
42 #include <ctype.h>
43
44 namespace GEO {
45
46 215 LineInput::LineInput(const std::string& filename) :
47
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 215 times.
215 file_name_(filename),
48
1/2
✓ Branch 1 taken 215 times.
✗ Branch 2 not taken.
215 line_num_(0)
49 {
50
1/2
✓ Branch 1 taken 215 times.
✗ Branch 2 not taken.
215 F_ = fopen(filename.c_str(), "r");
51 215 ok_ = (F_ != nullptr);
52 215 line_[0] = '\0';
53 215 }
54
55 215 LineInput::~LineInput() {
56
2/2
✓ Branch 0 taken 214 times.
✓ Branch 1 taken 1 times.
215 if(F_ != nullptr) {
57 214 fclose(F_);
58 F_ = nullptr;
59 }
60 215 }
61
62 935786 bool LineInput::get_line() {
63
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 935786 times.
935786 if(F_ == nullptr) {
64 return false;
65 }
66 935786 line_[0] = '\0';
67 // Skip the empty lines
68
3/4
✓ Branch 0 taken 935798 times.
✓ Branch 1 taken 935588 times.
✓ Branch 2 taken 935798 times.
✗ Branch 3 not taken.
1871386 while(!isprint(line_[0]) && line_[0] != '\t') {
69 935798 ++line_num_;
70
3/4
✓ Branch 0 taken 935798 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 198 times.
✓ Branch 3 taken 935600 times.
1871596 if(fgets(line_, MAX_LINE_LEN, F_) == nullptr) {
71 return false;
72 }
73 }
74 // If the line ends with a backslash, append
75 // the next line to the current line.
76 bool check_multiline = true;
77 Numeric::int64 total_length = MAX_LINE_LEN;
78 935588 char* ptr = line_;
79 935588 while(check_multiline) {
80 935588 size_t L = strlen(ptr);
81 935588 total_length -= Numeric::int64(L);
82 935588 ptr = ptr + L - 2;
83
1/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 935588 times.
935588 if(*ptr == '\\' && total_length > 0) {
84 *ptr = ' ';
85 ptr++;
86 if(fgets(ptr, int(total_length), F_) == nullptr) {
87 return false;
88 }
89 ++line_num_;
90 } else {
91 check_multiline = false;
92 }
93 }
94
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 935588 times.
935588 if(total_length < 0) {
95 Logger::err("LineInput")
96 << "MultiLine longer than "
97 << MAX_LINE_LEN << " bytes" << std::endl;
98 }
99 return true;
100 }
101
102 #if 1
103 #ifdef GEO_OS_WINDOWS
104 #define safe_strtok strtok_s
105 #else
106 #define safe_strtok strtok_r
107 #endif
108
109 935588 void LineInput::get_fields(const char* separators) {
110 935588 field_.resize(0);
111 935588 char* context = nullptr;
112 935588 char* tok = safe_strtok(line_, separators, &context);
113
2/2
✓ Branch 0 taken 3526729 times.
✓ Branch 1 taken 935588 times.
4462317 while(tok != nullptr) {
114 field_.push_back(tok);
115 3526729 tok = safe_strtok(nullptr, separators, &context);
116 }
117 935588 }
118
119 #else
120 void LineInput::get_fields(const char* separators) {
121 field_.resize(0);
122 char* tok = strtok(line_, separators);
123 while(tok != nullptr) {
124 field_.push_back(tok);
125 tok = strtok(nullptr, separators);
126 }
127 }
128
129 #endif
130
131 5 void LineInput::conversion_error(index_t index, const char* type) const {
132 5 std::ostringstream out;
133
1/2
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
5 out << "Line " << line_num_
134 << ": field #" << index
135
3/6
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 5 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 5 times.
✗ Branch 8 not taken.
10 << " is not a valid " << type << " value: " << field(index);
136
1/2
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
15 throw std::logic_error(out.str());
137 5 }
138 }
139