40#ifndef GEOGRAM_BASIC_GEOFILE
41#define GEOGRAM_BASIC_GEOFILE
47#include <geogram/third_party/zlib/zlib.h>
102 template <class T> inline
bool read_ascii_attribute(
103 FILE* file, Memory::pointer base_addr,
index_t nb_elements
105 T* attrib =
reinterpret_cast<T*
>(base_addr);
106 for(
index_t i=0; i<nb_elements; ++i) {
109 while(
char(res = fgetc(file)) !=
'\n') {
113 buff.push_back(
char(res));
115 if(!String::from_string(buff.c_str(),attrib[i])) {
134 T* attrib =
reinterpret_cast<T*
>(base_addr);
135 for(
index_t i=0; i<nb_elements; ++i) {
138 file,
"%s\n", String::to_string(attrib[i]).c_str()
158 template <>
inline bool read_ascii_attribute<char>(
161 char* attrib =
reinterpret_cast<char*
>(base_addr);
162 for(
index_t i=0; i<nb_elements; ++i) {
164 if(fscanf(file,
"%d", &val) == 0) {
167 attrib[i] = char(val);
182 template <>
inline bool write_ascii_attribute<char>(
185 char* attrib =
reinterpret_cast<char*
>(base_addr);
186 for(
index_t i=0; i<nb_elements; ++i) {
187 if(fprintf(file,
"%d\n",
int(attrib[i])) == 0) {
204 template <>
inline bool read_ascii_attribute<bool>(
207 char* attrib =
reinterpret_cast<char*
>(base_addr);
208 for(
index_t i=0; i<nb_elements; ++i) {
210 if(fscanf(file,
"%d", &val) == 0) {
213 attrib[i] = char(val);
228 template <>
inline bool write_ascii_attribute<bool>(
231 char* attrib =
reinterpret_cast<char*
>(base_addr);
232 for(
index_t i=0; i<nb_elements; ++i) {
233 if(fprintf(file,
"%d\n",
int(attrib[i])) == 0) {
276 typedef bool (*AsciiAttributeSerializer)(
288 const std::string& type_name,
289 AsciiAttributeSerializer read,
290 AsciiAttributeSerializer write
326 return gargantua_mode_;
334 return current_chunk_class_;
342 return current_chunk_size_;
364 const std::string& name_in,
365 const std::string& element_type_in,
366 size_t element_size_in,
370 element_type(element_type_in),
371 element_size(element_size_in),
372 dimension(dimension_in) {
416 const std::string& name_in,
420 nb_items(nb_items_in),
432 const std::string& name_in
434 for(
index_t i=0; i<attributes.size(); ++i) {
435 if(attributes[i].name == name_in) {
436 return &(attributes[i]);
450 for(
index_t i=0; i<attributes.size(); ++i) {
451 if(attributes[i].name == name_in) {
452 return &(attributes[i]);
487 auto it = attribute_sets_.find(name);
488 if(it == attribute_sets_.end()) {
491 return &(it->second);
501 const std::string& name
503 auto it = attribute_sets_.find(name);
504 if(it == attribute_sets_.end()) {
507 return &(it->second);
569 void write_string(
const std::string& s,
const char* comment =
nullptr);
640 const std::vector<std::string>& strings
657 const std::string& chunk_class,
size_t size
681 std::string filename_;
685 std::string current_chunk_class_;
686 size_t current_chunk_size_;
687 size_t current_chunk_file_pos_;
688 std::map<std::string, AttributeSetInfo> attribute_sets_;
690 static std::map<std::string, AsciiAttributeSerializer>
691 ascii_attribute_read_;
693 static std::map<std::string, AsciiAttributeSerializer>
694 ascii_attribute_write_;
753 geo_assert(current_attribute_set_ !=
nullptr);
754 return *current_attribute_set_;
765 return *current_attribute_;
773 const std::string& current_comment()
const {
775 return current_comment_;
805 index_t* addr,
size_t nb_elements,
size_t element_size
810 std::string current_comment_;
846 const std::string& name,
index_t nb_items
863 const std::string& attribute_set_name,
864 const std::string& attribute_name,
865 const std::string& element_type,
#define geo_assert(x)
Verifies that a condition is met.
GeoFileException(const std::string &s)
GeoFileException constructor.
GeoFileException(const GeoFileException &rhs)
GeoFileException copy constructor.
~GeoFileException() GEO_NOEXCEPT override
GeoFileException destructor.
Base class for reading or writing Geogram structured binary files.
std::string read_chunk_class()
Reads a chunk class from the file.
void check_chunk_size()
Checks that the actual chunk size corresponds to the specified chunk size.
void write_chunk_class(const std::string &chunk_class)
Writes a chunk class into the file.
bool is_ascii() const
Tests whether this GeoFile is ascii.
const std::string & current_chunk_class() const
Gets the current chunk class.
size_t string_array_size(const std::vector< std::string > &strings) const
Gets the size in bytes used by a given string array in the file.
size_t read_size_t()
Reads an unsigned 64 bits integer from the file.
bool gargantua_mode() const
Tests whether this GeoFile is in GARGANTUA mode.
AttributeSetInfo * find_attribute_set(const std::string &name)
Finds an attribute set by name.
const AttributeSetInfo * find_attribute_set(const std::string &name) const
Finds an attribute set by name.
bool gargantua_mode_
True if current file is in GARGANTUA mode.
GeoFile(const std::string &filename)
GeoFile constructor.
void clear_attribute_maps()
Clears all memorized information about attributes and attribute sets.
bool convert_64_to_32_
True if reading a GARGANTUA file in standard mode.
std::string read_string()
Reads a string from the file.
size_t string_size(const std::string &s) const
Gets the size in bytes used by a given string in the file.
void write_size_t(size_t x)
Writes an unsigned 64 bits integer into the file.
void read_string_array(std::vector< std::string > &strings)
Reads a string array from the file.
size_t current_chunk_size() const
Gets the size of the current chunk.
void write_string(const std::string &s, const char *comment=nullptr)
Writes a string into the file.
void check_zlib_version()
Compares the zlib version declared in the header file with the zlib version obtained from the runtime...
bool convert_32_to_64_
True if reading a standard file in GARGANTUA mode.
index_t read_index_t()
Reads an integer from the file.
void write_index_t_32(index_t x, const char *comment=nullptr)
Writes a 32-bit integer into the file.
index_t read_index_t_32()
Reads a 32-bit integer from the file.
void write_string_array(const std::vector< std::string > &strings)
Writes a string array into the file.
~GeoFile()
GeoFile destructor.
void write_index_t(index_t x, const char *comment=nullptr)
Writes an integer into the file.
void write_chunk_header(const std::string &chunk_class, size_t size)
Writes a chunk header into the file.
void read_chunk_header()
Reads a chunk header from the file.
static void register_ascii_attribute_serializer(const std::string &type_name, AsciiAttributeSerializer read, AsciiAttributeSerializer write)
Declares a new attribute type that can be read from and written to ascii files.
Used to write a structured binary file.
void write_attribute_set(const std::string &name, index_t nb_items)
Writes a new attribute set to the file.
void write_separator()
Writes a separator into the file.
OutputGeoFile(const std::string &filename, index_t compression_level=3)
OutputGeoFile constructor.
void write_comment(const std::string &comment)
Writes a new comment to the file.
void write_attribute(const std::string &attribute_set_name, const std::string &attribute_name, const std::string &element_type, size_t element_size, index_t dimension, const void *data)
Writes a new attribute to the file.
void write_command_line(const std::vector< std::string > &args)
Writes the command line to the file.
Vector with aligned memory allocation.
Common include file, providing basic definitions. Should be included before anything else by all head...
Types and functions for memory manipulation.
byte * pointer
Pointer to unsigned byte(s)
Global Vorpaline namespace.
geo_index_t index_t
The type for storing and manipulating indices.
bool write_ascii_attribute(FILE *file, Memory::pointer base_addr, index_t nb_elements)
Writes an ASCII attribute to a file.
Types and functions for numbers manipulation.
Functions for string manipulation.
Internal representation of attributes.
std::string name
Name of the attribute.
AttributeInfo()
AttributeInfo constructor.
index_t dimension
The number of elements per item.
size_t element_size
The size in bytes of each element.
std::string element_type
A string with the name fo the C++ type of the elements.
AttributeInfo(const std::string &name_in, const std::string &element_type_in, size_t element_size_in, index_t dimension_in)
AttributeInfo constructor.
Internal representation of an attribute set.
AttributeSetInfo(const std::string &name_in, index_t nb_items_in)
AttributeSetInfo constructor.
bool skip
if set, all attributes in the set are skipped when reading the file.
AttributeInfo * find_attribute(const std::string &name_in)
Finds an AttributeInfo by name.
index_t nb_items
number of items in each attribute of the set.
vector< AttributeInfo > attributes
the attributes of the set.
AttributeSetInfo()
AttributeSetInfo constructor.
std::string name
name of the attribute set.
const AttributeInfo * find_attribute(const std::string &name_in) const
Finds an AttributeInfo by name.