From bf20ea8c8ff1e272436839f74ae50ad6913e6a95 Mon Sep 17 00:00:00 2001 From: Gard Spreemann Date: Fri, 19 Oct 2018 14:02:22 +0200 Subject: Initial commit of cleaned-up version. --- include/misc.hpp | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 include/misc.hpp (limited to 'include/misc.hpp') diff --git a/include/misc.hpp b/include/misc.hpp new file mode 100644 index 0000000..614f30f --- /dev/null +++ b/include/misc.hpp @@ -0,0 +1,68 @@ +#pragma once + +#include +#include +#include +#include + +#include + +namespace Gudhi_laplacian +{ + // FIXME: Read from PETSc headers. + static const int32_t PETSC_MAT_FILE_CLASSID = 1211216; + static const int32_t PETSC_VEC_FILE_CLASSID = 1211214; + + bool preexisting_files(const std::string & prefix, const std::string & postfix) + { + glob_t globbuf; + std::string pattern = prefix + std::string(".*.") + postfix; + bool ret = (glob(pattern.c_str(), GLOB_ERR | GLOB_NOSORT, NULL, &globbuf) != GLOB_NOMATCH); + globfree(&globbuf); + return ret; + } + + + template inline void reverse_endianness(T & x) + { + uint8_t * p = reinterpret_cast(&x); + std::reverse(p, p + sizeof(T)); + } + + template inline T read_le(std::istream & s) + { + T result; + s.read(reinterpret_cast(&result), sizeof(T)); + #ifdef BIGENDIAN + reverse_endianness(result); + #endif + return result; + } + + template inline void write_le(std::ostream & s, T value) + { + #ifdef BIGENDIAN + reverse_endianness(value); + #endif + s.write(reinterpret_cast(&value), sizeof(T)); + } + + template inline T read_be(std::istream & s) + { + T result; + s.read(reinterpret_cast(&result), sizeof(T)); + #ifndef BIGENDIAN + reverse_endianness(result); + #endif + return result; + } + + template inline void write_be(std::ostream & s, T value) + { + #ifndef BIGENDIAN + reverse_endianness(value); + #endif + s.write(reinterpret_cast(&value), sizeof(T)); + } +} + -- cgit v1.2.3