diff options
author | William Joye <wjoye@cfa.harvard.edu> | 2020-03-19 14:52:02 (GMT) |
---|---|---|
committer | William Joye <wjoye@cfa.harvard.edu> | 2020-03-19 14:52:02 (GMT) |
commit | d464af4522b4ecc906eb726a55fb01dc2db44930 (patch) | |
tree | fe8613cbd93424e933229e7ad7cc7d5ceae11fd8 /fitsy/smmap.C | |
parent | 36b673794f503c76e7dca7360bea4cd1ebf1e660 (diff) | |
download | blt-d464af4522b4ecc906eb726a55fb01dc2db44930.zip blt-d464af4522b4ecc906eb726a55fb01dc2db44930.tar.gz blt-d464af4522b4ecc906eb726a55fb01dc2db44930.tar.bz2 |
libfitsy
Diffstat (limited to 'fitsy/smmap.C')
-rw-r--r-- | fitsy/smmap.C | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/fitsy/smmap.C b/fitsy/smmap.C new file mode 100644 index 0000000..5b25149 --- /dev/null +++ b/fitsy/smmap.C @@ -0,0 +1,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 |