Geogram  Version 1.9.0
A programming library of geometric algorithms
b_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_B_STREAM
41 #define GEOGRAM_BASIC_B_STREAM
42 
43 #include <geogram/basic/common.h>
44 #include <geogram/basic/numeric.h>
45 
46 #include <iostream>
47 #include <fstream>
48 #include <string>
49 
55 namespace GEO {
56 
76  class GEOGRAM_API BinaryStream {
77  public:
79  static const int GEO_LITTLE_ENDIAN = 0;
80 
82  static const int GEO_BIG_ENDIAN = 1;
83 
92  void set_stream_endian(int stream_endian);
93 
99  inline int stream_endian() const {
100  return stream_endian_;
101  }
102 
108  inline int machine_endian() const {
109  return machine_endian_;
110  }
111 
116  inline bool has_record_markers() const {
117  return has_record_markers_;
118  }
119 
127  inline void set_has_record_markers(bool b) {
128  has_record_markers_ = b;
129  }
130 
131  protected:
141  BinaryStream(int stream_endian = GEO_BIG_ENDIAN);
142 
147 
153  template <size_t N>
154  struct ItemSize {
155  };
156 
157  protected:
159  bool swapped_;
160 
161  private:
162  int machine_endian_;
163  int stream_endian_;
164  bool has_record_markers_;
165  };
166 
167  /************************************************************************/
168 
175  class GEOGRAM_API BinaryInputStream : public BinaryStream {
176  public:
188  const std::string& file_name, int stream_endian = GEO_BIG_ENDIAN
189  );
190 
202  std::istream& input, int stream_endian = GEO_BIG_ENDIAN
203  );
204 
211 
217  bool OK() const;
218 
224  bool more() const;
225 
236  template <class T>
237  inline BinaryInputStream& operator>> (T& x) {
238  return read(
239  (char*) &x, 1,
241  );
242  }
243 
253  inline BinaryInputStream& read_opaque_data(void* ptr, size_t n) {
254  input_->read((char*) ptr, (std::streamsize) n);
255  return *this;
256  }
257 
269  void* ptr, size_t size, size_t n
270  ) {
271  return read_opaque_data(ptr, size * n);
272  }
273 
285  template <class T>
286  inline BinaryInputStream& read_array(T* data, size_t n) {
287  return read(
288  (char*) data, n,
290  );
291  }
292 
305  void begin_record();
306 
314  void end_record();
315 
326  template <class T>
327  inline BinaryInputStream& read_record(T* data, size_t n) {
328  begin_record();
329  read(
330  (char*) data, n,
332  );
333  end_record();
334  return *this;
335  }
336 
341  std::streamoff tell() const {
342  return input_->tellg();
343  }
344 
349  void seek(std::streamoff pos) {
350  input_->seekg(pos);
351  }
352 
361  void seek(std::streamoff off, std::ios_base::seekdir dir) {
362  input_->seekg(off, dir);
363  }
364 
365  protected:
372  inline BinaryInputStream& read(char* data, size_t n, ItemSize<1>) {
373  return read_opaque_data(data, n);
374  }
375 
382  BinaryInputStream& read(char* data, size_t n, ItemSize<2>);
383 
390  BinaryInputStream& read(char* data, size_t n, ItemSize<4>);
391 
398  BinaryInputStream& read(char* data, size_t n, ItemSize<8>);
399 
400  private:
401  std::istream* input_;
402  bool owns_input_;
404  bool record_OK_;
406  Numeric::uint32 count1_;
408  Numeric::uint32 count2_;
410  Numeric::int32 record_count_;
411  };
412 
413  /************************************************************************/
414 
421  class GEOGRAM_API BinaryOutputStream : public BinaryStream {
422  public:
434  const std::string& file_name, int stream_endian = GEO_BIG_ENDIAN
435  );
436 
448  std::ostream& output, int stream_endian = GEO_BIG_ENDIAN
449  );
450 
457 
463  bool OK() const;
464 
472  template <class T>
473  inline BinaryOutputStream& operator<< (T x) {
474  return write(
475  (const char*) &x, 1,
477  );
478  }
479 
488  const void* ptr, size_t size
489  ) {
490  output_->write((const char*) ptr, (std::streamsize) size);
491  count_ += size;
492  return *this;
493  }
494 
505  const void* ptr, size_t size, size_t n
506  ) {
507  return write_opaque_data(ptr, size * n);
508  }
509 
520  template <class T>
521  inline BinaryOutputStream& write_array(const T* data, size_t n) {
522  return write(
523  (const char*) data, n,
525  );
526  }
527 
540  void begin_record();
541 
548  void end_record();
549 
561  template <class T>
562  BinaryOutputStream& write_record(const T* data, size_t n) {
563  begin_record();
564  write(
565  (const char*) data, n,
567  );
568  end_record();
569  return *this;
570  }
571 
572  protected:
579  BinaryOutputStream& write(const char* data, size_t n, ItemSize<1>) {
580  return write_opaque_data(data, n);
581  }
582 
589  BinaryOutputStream& write(const char* data, size_t n, ItemSize<2>);
590 
597  BinaryOutputStream& write(const char* data, size_t n, ItemSize<4>);
598 
605  BinaryOutputStream& write(const char* data, size_t n, ItemSize<8>);
606 
607  private:
612  void write_marker(Numeric::uint32 value);
613 
614  std::ostream* output_;
615  bool owns_output_;
617  size_t count_;
623  std::streamoff pos_;
624  };
625 
626  /************************************************************************/
627 }
628 
629 #endif
630 
Binary input file.
Definition: b_stream.h:175
BinaryInputStream & read_array(T *data, size_t n)
Reads an array of elements.
Definition: b_stream.h:286
BinaryInputStream(std::istream &input, int stream_endian=GEO_BIG_ENDIAN)
Creates a new binary input stream.
BinaryInputStream & read(char *data, size_t n, ItemSize< 1 >)
Reads an array of elements of size 1.
Definition: b_stream.h:372
std::streamoff tell() const
Gets the position in the input sequence.
Definition: b_stream.h:341
bool OK() const
Gets the status of the stream.
bool more() const
Checks if there are more bytes to read.
void begin_record()
Starts reading a data record.
void seek(std::streamoff pos)
Sets the position in input sequence.
Definition: b_stream.h:349
BinaryInputStream & read(char *data, size_t n, ItemSize< 2 >)
Reads an array of elements of size 2.
BinaryInputStream(const std::string &file_name, int stream_endian=GEO_BIG_ENDIAN)
Creates a new binary input stream.
BinaryInputStream & read(char *data, size_t n, ItemSize< 8 >)
Reads an array of elements of size 8.
~BinaryInputStream()
Deletes the input stream.
void end_record()
Stops reading a data record.
BinaryInputStream & read(char *data, size_t n, ItemSize< 4 >)
Reads an array of elements of size 4.
BinaryInputStream & read_opaque_data(void *ptr, size_t size, size_t n)
Reads opaque data.
Definition: b_stream.h:268
BinaryInputStream & read_opaque_data(void *ptr, size_t n)
Reads opaque data.
Definition: b_stream.h:253
void seek(std::streamoff off, std::ios_base::seekdir dir)
Sets the position in input sequence.
Definition: b_stream.h:361
BinaryInputStream & read_record(T *data, size_t n)
Reads an array of elements in a record.
Definition: b_stream.h:327
Binary output file.
Definition: b_stream.h:421
BinaryOutputStream & write_opaque_data(const void *ptr, size_t size, size_t n)
Writes opaque data.
Definition: b_stream.h:504
BinaryOutputStream & write_opaque_data(const void *ptr, size_t size)
Writes opaque data.
Definition: b_stream.h:487
BinaryOutputStream(const std::string &file_name, int stream_endian=GEO_BIG_ENDIAN)
Creates a new binary output stream.
bool OK() const
Gets the status of the stream.
BinaryOutputStream & write(const char *data, size_t n, ItemSize< 2 >)
Writes an array of elements of size 2.
BinaryOutputStream & write(const char *data, size_t n, ItemSize< 1 >)
Writes an array of elements of size 1.
Definition: b_stream.h:579
BinaryOutputStream(std::ostream &output, int stream_endian=GEO_BIG_ENDIAN)
Creates a new binary output stream.
BinaryOutputStream & write(const char *data, size_t n, ItemSize< 4 >)
Writes an array of elements of size 4.
~BinaryOutputStream()
Deletes the output stream.
BinaryOutputStream & write_array(const T *data, size_t n)
Writes an array of elements.
Definition: b_stream.h:521
BinaryOutputStream & write_record(const T *data, size_t n)
Writes an array of elements in a record.
Definition: b_stream.h:562
void begin_record()
Starts writing a data record.
BinaryOutputStream & write(const char *data, size_t n, ItemSize< 8 >)
Writes an array of elements of size 8.
void end_record()
Stops writing a data record.
Binary stream base class.
Definition: b_stream.h:76
void set_has_record_markers(bool b)
Enables/disables support for record markers.
Definition: b_stream.h:127
bool has_record_markers() const
Checks support for record markers.
Definition: b_stream.h:116
BinaryStream(int stream_endian=GEO_BIG_ENDIAN)
Base constructor.
void detect_machine_endian()
Detects the current architecture's endianness.
int machine_endian() const
Gets the current architecture's endianness.
Definition: b_stream.h:108
void set_stream_endian(int stream_endian)
Sets the stream endianness.
int stream_endian() const
Gets the stream endianness.
Definition: b_stream.h:99
Common include file, providing basic definitions. Should be included before anything else by all head...
int32_t int32
Definition: numeric.h:129
uint32_t uint32
Definition: numeric.h:141
Global Vorpaline namespace.
Definition: basic.h:55
Types and functions for numbers manipulation.
Extends std::numeric_limits with additional information.
Definition: numeric.h:260