Geogram  Version 1.9.0
A programming library of geometric algorithms
line_stream.h
Go to the documentation of this file.
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 #ifndef GEOGRAM_BASIC_LINE_STREAM
41 #define GEOGRAM_BASIC_LINE_STREAM
42 
43 #include <geogram/basic/common.h>
44 #include <geogram/basic/assert.h>
45 #include <geogram/basic/numeric.h>
46 #include <geogram/basic/string.h>
47 #include <cstring>
48 #include <stdio.h>
49 
56 namespace GEO {
57 
82  class GEOGRAM_API LineInput {
83  public:
91  LineInput(const std::string& filename);
92 
98 
102  bool OK() const {
103  return ok_;
104  }
105 
111  bool eof() const {
112  return feof(F_) ? true : false;
113  }
114 
123  bool get_line();
124 
131  index_t nb_fields() const {
132  return index_t(field_.size());
133  }
134 
139  size_t line_number() const {
140  return line_num_;
141  }
142 
151  char* field(index_t i) {
152  geo_assert(i < nb_fields());
153  return field_[i];
154  }
155 
164  const char* field(index_t i) const {
165  geo_assert(i < nb_fields());
166  return field_[i];
167  }
168 
181  signed_index_t result = 0;
182  if(!String::from_string(field(i), result)) {
183  conversion_error(i, "integer");
184  }
185  return result;
186  }
187 
200  index_t result = 0;
201  if(!String::from_string(field(i), result)) {
202  conversion_error(i, "unsigned integer");
203  }
204  return result;
205  }
206 
218  double field_as_double(index_t i) const {
219  double result = 0;
220  if(!String::from_string(field(i), result)) {
221  conversion_error(i, "floating point");
222  }
223  return result;
224  }
225 
237  bool field_matches(index_t i, const char* s) const {
238  return strcmp(field(i), s) == 0;
239  }
240 
250  void get_fields(const char* separators = " \t\r\n");
251 
259  const char* current_line() const {
260  return line_;
261  }
262 
263  private:
272  GEO_NORETURN_DECL void conversion_error(
273  index_t index, const char* type
274  ) const GEO_NORETURN ;
275 
279  static const index_t MAX_LINE_LEN = 65535;
280 
281  FILE* F_;
282  std::string file_name_;
283  size_t line_num_;
284  char line_[MAX_LINE_LEN];
285  std::vector<char*> field_;
286  bool ok_;
287  };
288 }
289 
290 #endif
291 
Assertion checking mechanism.
#define geo_assert(x)
Verifies that a condition is met.
Definition: assert.h:149
Reads an ASCII file line per line.
Definition: line_stream.h:82
double field_as_double(index_t i) const
Gets a line field as a double.
Definition: line_stream.h:218
const char * field(index_t i) const
Gets a line field as a non-modifiable string.
Definition: line_stream.h:164
size_t line_number() const
Returns the current line number.
Definition: line_stream.h:139
~LineInput()
Destroys the line reader.
signed_index_t field_as_int(index_t i) const
Gets a line field as an integer.
Definition: line_stream.h:180
bool field_matches(index_t i, const char *s) const
Compares a field with a string.
Definition: line_stream.h:237
bool eof() const
Checks if line reader has reached the end of the input stream.
Definition: line_stream.h:111
index_t nb_fields() const
Gets the number of fields in the current line.
Definition: line_stream.h:131
index_t field_as_uint(index_t i) const
Gets a line field as an unsigned integer.
Definition: line_stream.h:199
bool get_line()
Reads a new line.
bool OK() const
Checks if the line reader is ready to read.
Definition: line_stream.h:102
void get_fields(const char *separators=" \t\r\n")
Splits the current line into fields.
const char * current_line() const
Gets the current line.
Definition: line_stream.h:259
LineInput(const std::string &filename)
Creates a new line reader from a file.
char * field(index_t i)
Gets a line field as a modifiable string.
Definition: line_stream.h:151
Common include file, providing basic definitions. Should be included before anything else by all head...
Global Vorpaline namespace.
Definition: basic.h:55
geo_signed_index_t signed_index_t
The type for storing and manipulating indices differences.
Definition: numeric.h:343
geo_index_t index_t
The type for storing and manipulating indices.
Definition: numeric.h:329
Types and functions for numbers manipulation.
Functions for string manipulation.