summaryrefslogtreecommitdiffstats
path: root/fitsy/smmap.C
blob: 5b25149f2a96007a0baf703266975c4b59303e2c (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright (C) 1999-2018
// Smithsonian Astrophysical Observatory, Cambridge, MA, USA
// For conditions of distribution and use, see copyright notice in "copyright"

#include "smmap.h"

#ifndef __WIN32

#include <unistd.h>

#include <sys/types.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/mman.h>

FitsSMMap::FitsSMMap(const char* hdr, const char* fn)
{
  // reset
  valid_ =0;

  // header
  {
    // Map the header.
    int file = open(hdr, O_RDONLY);
    if (file == -1)
      return;

    struct stat info;
    if (fstat(file, &info) < 0)
      return;

    // check for empty file
    if (info.st_size == 0)
      return;

    // map it
    hmapsize_ = info.st_size;
    hmapdata_ = (char*)mmap(NULL, hmapsize_, PROT_READ, MAP_SHARED, file, 0);

    // close the file
    close(file);

    // are we valid?
    if ((long)hmapdata_ == -1)
      return;
  }

  // data
  {
    // parse the fn and options
    parse(fn);
    if (!valid_)
      return;

    // reset
    valid_ =0;

    if (!pName_)
      return;

    // Map the file.
    int file = open(pName_, O_RDONLY);
    if (file == -1)
      return;

    struct stat info;
    if (fstat(file, &info) < 0)
      return;

    // check for empty file
    if (info.st_size == 0)
      return;

    // map it
    mapsize_ = info.st_size;
    mapdata_ = (char*)mmap(NULL, mapsize_, PROT_READ, MAP_SHARED, file, 0);

    // close the file
    close(file);

    // are we valid?
    if ((long)mapdata_ == -1)
      return;
  }

  // so far, so good
  valid_ = 1;
}

FitsSMMap::~FitsSMMap()
{
  if (mapdata_)
    munmap((caddr_t)mapdata_, mapsize_);
}

#else

FitsSMMap::FitsSMMap(const char* hdr, const char* fn)
{
  valid_ =0;
}

FitsSMMap::~FitsSMMap() {}

#endif