| 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 | 215 | file_name_(filename), | |
| 48 | 215 | line_num_(0) | |
| 49 | { | ||
| 50 |
1/2✓ Branch 2 taken 215 times.
✗ Branch 3 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 | 214 | 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 |
2/2✓ Branch 1 taken 198 times.
✓ Branch 2 taken 935600 times.
|
935798 | if(fgets(line_, MAX_LINE_LEN, F_) == nullptr) { |
| 71 | 198 | return false; | |
| 72 | } | ||
| 73 | } | ||
| 74 | // If the line ends with a backslash, append | ||
| 75 | // the next line to the current line. | ||
| 76 | 935588 | bool check_multiline = true; | |
| 77 | 935588 | Numeric::int64 total_length = MAX_LINE_LEN; | |
| 78 | 935588 | char* ptr = line_; | |
| 79 |
2/2✓ Branch 0 taken 935588 times.
✓ Branch 1 taken 935588 times.
|
1871176 | 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 taken 935588 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
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 | 935588 | 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 | 935588 | 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 |
1/2✓ Branch 1 taken 935588 times.
✗ Branch 2 not taken.
|
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 |
1/2✓ Branch 1 taken 3526729 times.
✗ Branch 2 not taken.
|
3526729 | 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 |
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 |