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

#include <tcl.h>

#include "var.h"

FitsVar::FitsVar(Tcl_Interp* interp, const char* var, const char* fn)
{
  parse(fn);
  if (!valid_)
    return;

  // reset
  valid_ =0;

  obj = Tcl_GetVar2Ex(interp, (char*)var, NULL, 
		      TCL_GLOBAL_ONLY | TCL_LEAVE_ERR_MSG);
  if (!obj)
    return;

  // just in case
  Tcl_ConvertToType(interp, obj, Tcl_GetObjType("bytearray"));

  typedef struct ByteArray {
    int used;			/* The number of bytes used in the byte
				 * array. */
    int allocated;		/* The amount of space actually allocated
				 * minus 1 byte. */
    unsigned char bytes[4];	/* The array of bytes.  The actual size of
				 * this field depends on the 'allocated' field
				 * above. */
  } ByteArray;

  ByteArray* ba = (ByteArray*)(obj->internalRep.otherValuePtr);
  mapsize_ = ba->used;
  mapdata_ = (char*)ba->bytes;

  Tcl_IncrRefCount(obj);

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

FitsVar::~FitsVar()
{
  if (obj)
    Tcl_DecrRefCount(obj);
}