summaryrefslogtreecommitdiffstats
path: root/fitsy++/outfile.C
blob: dd53f249c1dcf383c8bae97b1f29229b9439395d (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
// Copyright (C) 1999-2018
// Smithsonian Astrophysical Observatory, Cambridge, MA, USA
// For conditions of distribution and use, see copyright notice in "copyright"

#include <stdio.h>
#include "outfile.h"

OutFitsFile::OutFitsFile(const char* fn)
{
  if ((fd_ = fopen(fn, "wb")))
    valid_ = 1;
}

OutFitsFile::~OutFitsFile()
{
  if (fd_)
    fclose(fd_);
}

int OutFitsFile::write(char* where, size_t size)
{
  // size_t size is unsigned
  long long ss =size;
  size_t rr = 0;
  int r = 0;

  do {
    r = fwrite(where+rr, 1, (ss>B1MB) ? B1MB : ss, fd_);
    ss -= r;
    rr += r;
  } while (r>0 && rr<size);

  return rr;
}

OutFitsFileGZ::OutFitsFileGZ(const char* fn)
{
  if ((fd_ = gzopen(fn, "wb")))
    valid_ = 1;
}

OutFitsFileGZ::~OutFitsFileGZ()
{
  if (fd_)
    gzclose(fd_);
}

int OutFitsFileGZ::write(char* where, size_t size)
{
  // size_t size is unsigned
  long long ss =size;
  size_t rr = 0;
  int r = 0;

  do {
    r = gzwrite(fd_, where+rr, (ss>B1MB) ? B1MB : ss);
    ss -= r;
    rr += r;
  } while (r>0 && rr<size);

  return rr;
}