40#ifndef GEOGRAM_BASIC_GEOFILE
41#define GEOGRAM_BASIC_GEOFILE
47#ifdef GEOGRAM_USE_BUILTIN_DEPS
48#include <geogram/third_party/zlib/zlib.h>
106 template <class T> inline
bool read_ascii_attribute(
107 FILE* file, Memory::pointer base_addr,
index_t nb_elements
109 T* attrib =
reinterpret_cast<T*
>(base_addr);
110 for(
index_t i=0; i<nb_elements; ++i) {
113 while(
char(res = fgetc(file)) !=
'\n') {
117 buff.push_back(
char(res));
119 if(!String::from_string(buff.c_str(),attrib[i])) {
138 T* attrib =
reinterpret_cast<T*
>(base_addr);
139 for(
index_t i=0; i<nb_elements; ++i) {
142 file,
"%s\n", String::to_string(attrib[i]).c_str()
162 template <>
inline bool read_ascii_attribute<char>(
165 char* attrib =
reinterpret_cast<char*
>(base_addr);
166 for(
index_t i=0; i<nb_elements; ++i) {
168 if(fscanf(file,
"%d", &val) == 0) {
171 attrib[i] = char(val);
186 template <>
inline bool write_ascii_attribute<char>(
189 char* attrib =
reinterpret_cast<char*
>(base_addr);
190 for(
index_t i=0; i<nb_elements; ++i) {
191 if(fprintf(file,
"%d\n",
int(attrib[i])) == 0) {
208 template <>
inline bool read_ascii_attribute<bool>(
211 char* attrib =
reinterpret_cast<char*
>(base_addr);
212 for(
index_t i=0; i<nb_elements; ++i) {
214 if(fscanf(file,
"%d", &val) == 0) {
217 attrib[i] = char(val);
232 template <>
inline bool write_ascii_attribute<bool>(
235 char* attrib =
reinterpret_cast<char*
>(base_addr);
236 for(
index_t i=0; i<nb_elements; ++i) {
237 if(fprintf(file,
"%d\n",
int(attrib[i])) == 0) {
280 typedef bool (*AsciiAttributeSerializer)(
292 const std::string& type_name,
293 AsciiAttributeSerializer read,
294 AsciiAttributeSerializer write
330 return gargantua_mode_;
338 return current_chunk_class_;
346 return current_chunk_size_;
368 const std::string& name_in,
369 const std::string& element_type_in,
370 size_t element_size_in,
374 element_type(element_type_in),
375 element_size(element_size_in),
376 dimension(dimension_in) {
420 const std::string& name_in,
424 nb_items(nb_items_in),
436 const std::string& name_in
438 for(
index_t i=0; i<attributes.size(); ++i) {
439 if(attributes[i].name == name_in) {
440 return &(attributes[i]);
454 for(
index_t i=0; i<attributes.size(); ++i) {
455 if(attributes[i].name == name_in) {
456 return &(attributes[i]);
491 auto it = attribute_sets_.find(name);
492 if(it == attribute_sets_.end()) {
495 return &(it->second);
505 const std::string& name
507 auto it = attribute_sets_.find(name);
508 if(it == attribute_sets_.end()) {
511 return &(it->second);
573 void write_string(
const std::string& s,
const char* comment =
nullptr);
644 const std::vector<std::string>& strings
661 const std::string& chunk_class,
size_t size
685 std::string filename_;
689 std::string current_chunk_class_;
690 size_t current_chunk_size_;
691 size_t current_chunk_file_pos_;
692 std::map<std::string, AttributeSetInfo> attribute_sets_;
694 static std::map<std::string, AsciiAttributeSerializer>
695 ascii_attribute_read_;
697 static std::map<std::string, AsciiAttributeSerializer>
698 ascii_attribute_write_;
757 geo_assert(current_attribute_set_ !=
nullptr);
758 return *current_attribute_set_;
769 return *current_attribute_;
777 const std::string& current_comment()
const {
779 return current_comment_;
809 index_t* addr,
size_t nb_elements,
size_t element_size
814 std::string current_comment_;
850 const std::string& name,
index_t nb_items
867 const std::string& attribute_set_name,
868 const std::string& attribute_name,
869 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.