| 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 | 217 | LineInput::LineInput(const std::string& filename) : | |
| 47 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 217 times.
|
217 | file_name_(filename), |
| 48 |
1/2✓ Branch 1 taken 217 times.
✗ Branch 2 not taken.
|
217 | line_num_(0) |
| 49 | { | ||
| 50 |
1/2✓ Branch 1 taken 217 times.
✗ Branch 2 not taken.
|
217 | F_ = fopen(filename.c_str(), "r"); |
| 51 | 217 | ok_ = (F_ != nullptr); | |
| 52 | 217 | line_[0] = '\0'; | |
| 53 | 217 | } | |
| 54 | |||
| 55 | 217 | LineInput::~LineInput() { | |
| 56 |
2/2✓ Branch 0 taken 216 times.
✓ Branch 1 taken 1 times.
|
217 | if(F_ != nullptr) { |
| 57 | 216 | fclose(F_); | |
| 58 | F_ = nullptr; | ||
| 59 | } | ||
| 60 | 217 | } | |
| 61 | |||
| 62 | 971782 | bool LineInput::get_line() { | |
| 63 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 971782 times.
|
971782 | if(F_ == nullptr) { |
| 64 | return false; | ||
| 65 | } | ||
| 66 | 971782 | line_[0] = '\0'; | |
| 67 | // Skip the empty lines | ||
| 68 |
3/4✓ Branch 0 taken 971794 times.
✓ Branch 1 taken 971582 times.
✓ Branch 2 taken 971794 times.
✗ Branch 3 not taken.
|
1943376 | while(!isprint(line_[0]) && line_[0] != '\t') { |
| 69 | 971794 | ++line_num_; | |
| 70 |
3/4✓ Branch 0 taken 971794 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 200 times.
✓ Branch 3 taken 971594 times.
|
1943588 | 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 | 971582 | char* ptr = line_; | |
| 79 | 971582 | while(check_multiline) { | |
| 80 | 971582 | size_t L = strlen(ptr); | |
| 81 | 971582 | total_length -= Numeric::int64(L); | |
| 82 | 971582 | ptr = ptr + L - 2; | |
| 83 |
1/4✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 971582 times.
|
971582 | 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 971582 times.
|
971582 | 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 | 971582 | void LineInput::get_fields(const char* separators) { | |
| 110 | 971582 | field_.resize(0); | |
| 111 | 971582 | char* context = nullptr; | |
| 112 | 971582 | char* tok = safe_strtok(line_, separators, &context); | |
| 113 |
2/2✓ Branch 0 taken 3646703 times.
✓ Branch 1 taken 971582 times.
|
4618285 | while(tok != nullptr) { |
| 114 | field_.push_back(tok); | ||
| 115 | 3646703 | tok = safe_strtok(nullptr, separators, &context); | |
| 116 | } | ||
| 117 | 971582 | } | |
| 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 |