GCC Code Coverage Report


Directory: ./
File: basic/line_stream.cpp
Date: 2026-09-27 03:22:43
Exec Total Coverage
Lines: 45 54 83.3%
Functions: 5 5 100.0%
Branches: 29 64 45.3%

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 219 LineInput::LineInput(const std::string& filename) :
47 219 file_name_(filename),
48 219 line_num_(0)
49 {
50
1/2
✓ Branch 2 taken 219 times.
✗ Branch 3 not taken.
219 F_ = fopen(filename.c_str(), "r");
51 219 ok_ = (F_ != nullptr);
52 219 line_[0] = '\0';
53 219 }
54
55 219 LineInput::~LineInput() {
56
2/2
✓ Branch 0 taken 218 times.
✓ Branch 1 taken 1 times.
219 if(F_ != nullptr) {
57 218 fclose(F_);
58 218 F_ = nullptr;
59 }
60 219 }
61
62 1125590 bool LineInput::get_line() {
63
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1125590 times.
1125590 if(F_ == nullptr) {
64 ✗ return false;
65 }
66 1125590 line_[0] = '\0';
67 // Skip the empty lines
68
3/4
✓ Branch 0 taken 1125602 times.
✓ Branch 1 taken 1125388 times.
✓ Branch 2 taken 1125602 times.
✗ Branch 3 not taken.
2250990 while(!isprint(line_[0]) && line_[0] != '\t') {
69 1125602 ++line_num_;
70
2/2
✓ Branch 1 taken 202 times.
✓ Branch 2 taken 1125400 times.
1125602 if(fgets(line_, MAX_LINE_LEN, F_) == nullptr) {
71 202 return false;
72 }
73 }
74 // If the line ends with a backslash, append
75 // the next line to the current line.
76 1125388 bool check_multiline = true;
77 1125388 Numeric::int64 total_length = MAX_LINE_LEN;
78 1125388 char* ptr = line_;
79
2/2
✓ Branch 0 taken 1125388 times.
✓ Branch 1 taken 1125388 times.
2250776 while(check_multiline) {
80 1125388 size_t L = strlen(ptr);
81 1125388 total_length -= Numeric::int64(L);
82 1125388 ptr = ptr + L - 2;
83
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1125388 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1125388 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 1125388 check_multiline = false;
92 }
93 }
94
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1125388 times.
1125388 if(total_length < 0) {
95 ✗ Logger::err("LineInput")
96 ✗ << "MultiLine longer than "
97 ✗ << MAX_LINE_LEN << " bytes" << std::endl;
98 }
99 1125388 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 1125388 void LineInput::get_fields(const char* separators) {
110
1/2
✓ Branch 1 taken 1125388 times.
✗ Branch 2 not taken.
1125388 field_.resize(0);
111 1125388 char* context = nullptr;
112 1125388 char* tok = safe_strtok(line_, separators, &context);
113
2/2
✓ Branch 0 taken 4221947 times.
✓ Branch 1 taken 1125388 times.
5347335 while(tok != nullptr) {
114
1/2
✓ Branch 1 taken 4221947 times.
✗ Branch 2 not taken.
4221947 field_.push_back(tok);
115 4221947 tok = safe_strtok(nullptr, separators, &context);
116 }
117 1125388 }
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
1/2
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
5 std::ostringstream out;
133
2/4
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 5 times.
✗ Branch 5 not taken.
5 out << "Line " << line_num_
134
2/4
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 5 times.
✗ Branch 5 not taken.
5 << ": field #" << index
135
5/10
✓ 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.
✓ Branch 10 taken 5 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 5 times.
✗ Branch 14 not taken.
5 << " is not a valid " << type << " value: " << field(index);
136
2/4
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 5 times.
✗ Branch 6 not taken.
5 throw std::logic_error(out.str());
137 5 }
138 }
139