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 const T* attrib =
reinterpret_cast<const 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 const char* attrib =
reinterpret_cast<const 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 const char* attrib =
reinterpret_cast<const 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 (*AsciiAttributeReadSerializer)(
284 typedef bool (*AsciiAttributeWriteSerializer)(
296 const std::string& type_name,
297 AsciiAttributeReadSerializer read,
298 AsciiAttributeWriteSerializer write
334 return gargantua_mode_;
342 return current_chunk_class_;
350 return current_chunk_size_;
372 const std::string& name_in,
373 const std::string& element_type_in,
374 size_t element_size_in,
378 element_type(element_type_in),
379 element_size(element_size_in),
380 dimension(dimension_in) {
424 const std::string& name_in,
428 nb_items(nb_items_in),
440 const std::string& name_in
442 for(
index_t i=0; i<attributes.size(); ++i) {
443 if(attributes[i].name == name_in) {
444 return &(attributes[i]);
458 for(
index_t i=0; i<attributes.size(); ++i) {
459 if(attributes[i].name == name_in) {
460 return &(attributes[i]);
495 auto it = attribute_sets_.find(name);
496 if(it == attribute_sets_.end()) {
499 return &(it->second);
509 const std::string& name
511 auto it = attribute_sets_.find(name);
512 if(it == attribute_sets_.end()) {
515 return &(it->second);
577 void write_string(
const std::string& s,
const char* comment =
nullptr);
648 const std::vector<std::string>& strings
665 const std::string& chunk_class,
size_t size
689 std::string filename_;
693 std::string current_chunk_class_;
694 size_t current_chunk_size_;
695 size_t current_chunk_file_pos_;
696 std::map<std::string, AttributeSetInfo> attribute_sets_;
698 static std::map<std::string, AsciiAttributeReadSerializer>
699 ascii_attribute_read_;
701 static std::map<std::string, AsciiAttributeWriteSerializer>
702 ascii_attribute_write_;
761 geo_assert(current_attribute_set_ !=
nullptr);
762 return *current_attribute_set_;
773 return *current_attribute_;
781 const std::string& current_comment()
const {
783 return current_comment_;
813 index_t* addr,
size_t nb_elements,
size_t element_size
818 std::string current_comment_;
854 const std::string& name,
index_t nb_items
871 const std::string& attribute_set_name,
872 const std::string& attribute_name,
873 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.
static void register_ascii_attribute_serializer(const std::string &type_name, AsciiAttributeReadSerializer read, AsciiAttributeWriteSerializer write)
Declares a new attribute type that can be read from and written to ascii files.
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.
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)
const byte * const_pointer
Const 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::const_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.