summaryrefslogtreecommitdiff
path: root/include/gudhi_laplacian/misc.hpp
blob: 614f30fea02f1243b87f2147776a5e91008c2e00 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#pragma once

#include <cstdint>
#include <istream>
#include <ostream>
#include <string>

#include <glob.h>

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 <typename T> inline void reverse_endianness(T & x)
  {
    uint8_t * p = reinterpret_cast<uint8_t *>(&x);
    std::reverse(p, p + sizeof(T));
  }
  
  template <typename T> inline T read_le(std::istream & s)
  {
    T result;
    s.read(reinterpret_cast<char *>(&result), sizeof(T));
    #ifdef BIGENDIAN
    reverse_endianness(result);
    #endif
    return result;
  }

  template <typename T> inline void write_le(std::ostream & s, T value)
  {
    #ifdef BIGENDIAN
    reverse_endianness(value);
    #endif
    s.write(reinterpret_cast<char *>(&value), sizeof(T));
  }

  template <typename T> inline T read_be(std::istream & s)
  {
    T result;
    s.read(reinterpret_cast<char *>(&result), sizeof(T));
    #ifndef BIGENDIAN
    reverse_endianness(result);
    #endif
    return result;
  }

  template <typename T> inline void write_be(std::ostream & s, T value)
  {
    #ifndef BIGENDIAN
    reverse_endianness(value);
    #endif
    s.write(reinterpret_cast<char *>(&value), sizeof(T));
  }   
}