1
/*===========================================================================
2
Copyright (C) 1995-2009 European Southern Observatory (ESO)
4
This program is free software; you can redistribute it and/or
5
modify it under the terms of the GNU General Public License as
6
published by the Free Software Foundation; either version 2 of
7
the License, or (at your option) any later version.
9
This program is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
GNU General Public License for more details.
14
You should have received a copy of the GNU General Public
15
License along with this program; if not, write to the Free
16
Software Foundation, Inc., 675 Massachusetts Ave, Cambridge,
19
Correspondence concerning ESO-MIDAS should be addressed as follows:
20
Internet e-mail: midas@eso.org
21
Postal address: European Southern Observatory
22
Data Management Division
23
Karl-Schwarzschild-Strasse 2
24
D 85748 Garching bei Muenchen
26
===========================================================================*/
28
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
32
.AUTHOR IPG-ESO Garching
33
.CATEGORY table interface
34
.VERSION 1.0 25-Mar-1987 Definition J.D. Ponz
35
.VERSION 3.0 01-Jul-1990 New Version with Arrays / Elementary IO
41
This module contains the routines to handle tables as a whole.
42
Table initialization and open table functions return the
43
table identification tid, used by other table routines.
45
Tables are physically stored on disk in two different formats:
46
by records, corresponding to the natural way of storing sequentially
47
the rows, and transposed, where all the values of a single variable
48
-- column -- are stored together.
49
It is the responsibility of the user to decide the physical format
50
when initializing the table file.
52
The functions provided by this module are:
54
Create or Initialize a table (TCTINI)
55
Open an existing table (TCTOPN)
56
Map an opened table to virtual memory (TCTMAP)
57
Unmap a mapped table (TCTUNM): write modified parts, unmap mapped pieces
58
Close a table (TCTCLO).
60
Main arguments used by the routines are:
61
allcol: number of words per record allocated physically in the
63
allrow: number of records (rows) allocated physically in the
65
mode: file opening mode. There are several modes to open a table:
66
F_I\_MODE input (TBTOPN),
67
F_O\_MODE output (TBTINI),
68
F_IO\_MODE input/output (TBTOPN),
69
F_X\_MODE scratch (TBTINI),
70
F_D\_MODE descriptors only (TBTOPN).
71
These symbols are provided in the file 'midas_def.h'
72
in the directory 'MID_INCLUDE'.
74
The access mode may be forced to mapping with the F_MAP_FORCE
75
option, or to elementary i/o mode with the F_EIO_FORCE
76
option. The default is EIO mode for tables larger than
77
TBL_EIO_LIMIT bytes, a configuration parameter
78
which may be changed with TCOSET
79
name: table file name. It is a character string defining the
80
file name of the table as follows: '[path_name]table_name[.ext]',
81
where 'path_name' is the directory path name, defaulted to
82
the working directory, 'table_name' is the name of the file
83
and 'ext' is the file extension, defaulted to 'tbl'.
84
storage: physical file format. Defined as F_RECORD if the table
85
is stored by records, or F_TRANS for transposed format.
86
These symbols are provided in the file 'midas_def.h'
87
in the directory 'MID_INCLUDE'.
88
tid: table identifier. It is an integer number provided by the
89
system when the table is created or opened.
90
------------------------------------------------------------*/
92
#include <fileexts.h> /* General MIDAS Symbols */
94
#include <tblsys.h> /* Table System parameters */
95
#include <tbldef.h> /* Symbols used for Tables */
96
#include <tblerr.h> /* List of Table Errors */
98
#include <macrogen.h> /* Classical macros */
100
#include <stdlib.h> /* for malloc, free */
104
static int dunit=-1; /* For future use */
106
static int eio_limit = TBL_EIO_LIMIT;
108
char *osmmget(), *osmmexp(); /* Memory Allocation Functions */
114
/*=======================================================================
115
* Conversion of NULL values from old to New
116
*=======================================================================*/
119
static int convert_nulls(TABLE *tp)
121
static int convert_nulls(tp)
122
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
123
.PURPOSE Change NULL values to -Infinity
124
.METHOD Assume that table is MAPPED.
125
.RETURNS Number of Modified Columns
126
------------------------------------------------------------------*/
127
TABLE *tp; /* MOD: Table concerned */
130
int i, j, di, o, cols;
135
TBL_toNULL((TBL_D_R4<<TBL_D_BITS)|1, (char *)&fnull);
136
TBL_toNULL((TBL_D_R8<<TBL_D_BITS)|1, (char *)&ddnull);
139
for (j = 0; j < tp->cols; j++) {
140
if (tp->swise == F_TRANS)
141
o = tp->offset[j] * tp->arows, di = tp->bytes[j];
142
else o = tp->offset[j], di = tp->reclen;
144
switch(tp->dtypes[j] >> TBL_D_BITS) {
148
for (pv = tp->addres + o, i = tp->arows; --i >= 0; pv += di) {
149
if (*((float *)pv) > 1.e38) *(float *)pv = fnull;
154
for (pv = tp->addres + o, i = tp->arows; --i >= 0; pv += di) {
155
if (*((double *)pv) > 1.e38) *(double *)pv = ddnull;
166
int dummyPUT(imno,size)
175
struct FCT_STRUCT *fctpntr;
182
fctpntr = FCT.ENTRIES + imno;
183
fctpntr->FILTYP = 22;
185
fpt = osmmget((unsigned int) (size * sizeof(float))); /* already set to 0 */
186
status = SCFPUT(imno,1,size,fpt);
197
/*=======================================================================
199
*=======================================================================*/
202
static int TBL_setcols(TABLE *tp)
204
static int TBL_setcols(tp)
205
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
206
.PURPOSE Set up configuration of the arrays with physical columns
207
.ALGORITHM Allocate memory, and compute offset + length, set formats
208
.RETURNS length of 1 record
209
------------------------------------------------------------------*/
210
TABLE *tp; /* MOD: Table concerned */
214
int n, len1, icode, ncur, i;
219
/* Compute derived quantities */
221
tp->reclen = (tp->acols + 1) * 4;
222
tp->wsize = (tp->acols + 1) * tp->arows;
224
/* Store correct locations of arrays */
227
tp->bytes = (int *)osmmget(n*2*sizeof(int));
228
tp->abytes = tp->bytes + n;
229
tp->label = osmmget(n*(2+TBL_FORLEN+TBL_LABLEN));
230
tp->format = tp->label + n*(1+TBL_LABLEN);
232
/* Compute length + offset of each column */
235
for (i=0; i<tp->cols; i++)
237
icode = tp->dtypes[i];
239
/* we don't check this anymore - KB 040121
242
{ /. Convert OLD definitions to new ones ./
246
case -4: icode = TBL_D_R4; break;
247
case -8: icode = TBL_D_R8; break;
248
case -11: icode = TBL_D_I1; break;
249
case -12: icode = TBL_D_I2; break;
250
case -14: icode = TBL_D_I4; break;
252
icode = (icode << TBL_D_BITS) | 1;
253
tp->dtypes[i] = icode;
257
len1 = TBL_ElementSize(icode);
258
n = TBL_Items(icode);
260
if (tp->version == 0)
261
{ /* BEWARE ! (MP) Old to New implies
262
true len of CHAR is multiple of 4 */
263
n = ((3+len1*n)>>2)<<2; /* n is now Multiple of 4 */
265
tp->offset[i] = ncur;
266
if (!(icode>>TBL_D_BITS)) tp->dtypes[i] = n; /* MP 070291 */
269
tp->bytes[i] = len1*n;
270
ncur += tp->bytes[i];
274
/* Zero unused values */
276
for (; i < tp->colitems; i++)
277
tp->dtypes[i] = tp->offset[i] = tp->bytes[i] = 0;
280
/* Formats not loaded */
284
for (i=0; i < tp->colitems; i++)
286
tp->format[n] = '\0';
291
/* Labels not loaded */
295
for (i=0; i < tp->colitems; i++)
308
static int do_map(TABLE *tp, int mode)
310
static int do_map(tp, mode)
311
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
312
.PURPOSE Choose the mode for file access (EIO / Mapping / Simulated Mapping)
314
------------------------------------------------------------------*/
315
TABLE *tp; /* IN: Table to map */
316
int mode; /* IN: Mode for Mapping */
327
tp->cbuf = tp->nbuf = 0;
328
tp->tbuf = (TABLE_BUF *)0;
330
tp->addres = (char *)0;
331
tp->loaded_map = (unsigned char *)0;
332
tp->vsel = (unsigned char *)0;
335
/* Check, for old versions, of possible bad NULL values.
336
Changing NULL values also implies loding the table,
337
and therefore the F_MAP_FORCE is set.
338
Note that this test is only temporarly required, until
339
no table with old format exists any more...
342
if (tp->version == 0) {
343
TBL_toNULL((TBL_D_R4<<TBL_D_BITS)|1, (char *)&null_float);
344
/* if (null_float < -1.e38) tp->tflags |= TBL__BADNULLS; */
345
tp->tflags |= TBL__BADNULLS;
346
mode = F_MAP_FORCE|F_IO_MODE;
350
/* Choose the mode: TBL__MAPPED, or TBL__EIO.
351
If no option, use TBL_EIO if size > TBL_EIO_LIMIT */
353
if (mode & F_MAP_FORCE) tp->tflags |= TBL__MAPPED;
354
else if (mode & F_EIO_FORCE) tp->tflags |= TBL__EIO;
355
if ((tp->tflags& (TBL__MAPPED|TBL__EIO)) == 0) {
356
if (sizeof(int)*tp->acols*tp->arows > eio_limit)
357
tp->tflags |= TBL__EIO;
359
if (tp->tflags & TBL__MAPPED) {
360
status = SCFMAP(tp->imno, mode&0xf, 1,0,&dummy,&tp->addres);
361
if_not(tp->tflags & TBL__READONLY) tp->tflags |= TBL__MODIFIED;
363
else if (tp->tflags & TBL__EIO) { /* Elementary Mode. Define Buffers */
364
/* Let's assume at least one buffer per column (in case the table
365
is stored columnwise and accessed recordwise) + Selection + another
367
tp->nbuf = tp->acols + 2;
368
if (tp->nbuf < TBL_TBUFS) tp->nbuf = TBL_TBUFS;
369
i = tp->nbuf * sizeof(TABLE_BUF);
370
tp->tbuf = (TABLE_BUF *)osmmget(i);
371
oscfill ((char *)tp->tbuf, i, 0);
373
else { /* BitMap contains size/64k bytes */
374
i = sizeof(float)*(tp->acols + 1) * tp->arows;
375
tp->addres = osmmget(i);
376
i = (1 + (i|0xffff)) >> 16;
377
tp->loaded_map = (unsigned char *)osmmget(2*i);
378
tp->modified_map = tp->loaded_map + i;
379
oscfill ((char *)tp->loaded_map, 2*i, 0);
381
if (status) TBL_errf(status, "can't map table: %s", tp->phname);
385
/*=======================================================================
387
*=======================================================================*/
390
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
391
.PURPOSE Closes table file .
392
.ALGORITHM First Unmap, then release used memory.
394
------------------------------------------------------------------*/
395
int tid; /* IN : table id */
408
if ((status = CheckTable(tp))) return(TBL_errs(tid, status,0));
410
if (tp->intlFITS == 'F')
411
{ /* SCFCLO may still need FITS data */
412
if ((status = TCTUNMF(tid,0))) return(status);
414
status = SCFCLO(tp->imno);
415
(void) TCTUNMF(tid,1); /* now, also free table data memory */
419
if ((status = TCTUNM(tid))) return(status);
421
status = SCFCLO(tp->imno);
422
if (status == -99) /* we have to convert to FITS */
423
(void) strcpy(save_name,tp->phname);
426
/* Free allocated memory of table */
429
if (tp->phname != (char *) 0) osmmfree(tp->phname);
430
if (tp->loaded_map != (unsigned char *) 0) osmmfree((char *)tp->loaded_map);
431
if (tp->usname != (char *) 0) osmmfree(tp->usname);
433
if (!(tp->tflags&TBL__MAPPED))
435
if (tp->addres != (char *) 0) osmmfree(tp->addres);
438
if (tp->label != (char *) 0) osmmfree(tp->label);
440
if (tp->dtypes != (int *) 0) osmmfree((char *)tp->dtypes);
441
if (tp->bytes != (int *) 0) osmmfree((char *)tp->bytes);
442
if (tp->tbuf != (TABLE_BUF *) 0) osmmfree((char *)tp->tbuf);
443
if (tp->vsel != (unsigned char *) 0) osmmfree((char *)tp->vsel);
447
if (status == -99) /* we have to convert to FITS */
449
char temp[12], delname[80];
452
status = TCTOPN(save_name,F_I_MODE,&i);
456
status = SCFSAV(tid,2); /* convert table to FITS */
457
if (status != ERR_NORMAL) return (status);
459
status = SCFCLO(tid);
460
if (status == ERR_NORMAL)
462
status = osfrename("midFITS.mt",save_name);
466
(void) sprintf(temp,"TCTCLO/osfrename: %s ",save_name);
467
MID_ERROR("MID",temp,status,0);
480
int TCTCRV(name, refer_name, mode)
481
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
483
Create a view (a table with only a Selection Column)
484
taking columns from another (existing!) table.
486
The view is ONLY CREATED. It must later be opened ...
489
------------------------------------------------------------------*/
490
char *name; /* IN : Name of view to create */
491
char *refer_name; /* IN : Name of related table */
492
int mode; /* IN : creation mode (future) */
500
1- TCTOPN refer_table in read-only mode; check.
501
2- Create file. Set all bits to 1.
502
3- Copy column definitions
509
if ((status = TCTOPN (refer_name, F_I_MODE | F_EIO_FORCE, &vid)))
513
if (vp->swise & F_B_VIEW) {
515
return (TBL_errf(ERR_TBLENT, "can't create view of the view: %s",
518
vp->swise |= F_B_VIEW;
519
vp->vsize = 4 + (vp->rows>>3) ;
520
vp->vsel = (unsigned char *)osmmget( vp->vsize );
521
oscfill ((char *)vp->vsel, vp->vsize, ~0);
523
if ((status = SCFCRE (name, D_R4_FORMAT, F_O_MODE, F_TBL_TYPE,
524
vp->vsize>>2, &vp->vno))) goto error;
526
status = SCFPUT(vp->vno,1,vp->vsize,(char *)vp->vsel);
527
/* Add refer_table as descriptor */
528
status = SCDWRC(vp->vno, TBL_Dview, 1, refer_name, 1, strlen(refer_name), &dunit);
529
if (status) goto error;
530
/* copy descriptors */
531
if ((status = SCDCOP(vp->imno, vp->vno, 1, " ")))
533
/* Rewrite Main Descriptor */
534
if ((status = SCDWRI(vp->vno, TBL_Dmain, &vp->acols,
535
1,TBL_Dmain_SIZE,&dunit))) goto error;
538
error: /* error in SC routines */
540
if (status) TBL_errs(vid, status, 0);
547
int TCTUNMF(tid,flag)
548
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
549
.PURPOSE Unmap (Flush) FITS table file in 2 calls
550
.ALGORITHM 1st call: write back modified parts
551
2nd call: release used memory
553
------------------------------------------------------------------*/
554
int tid; /* IN : table id */
555
int flag; /* IN : 0 = do 1st part of TCTUNM
556
1 = do 2nd part of TCTUNM */
562
int status, i, newtid;
572
if (tp->phname == (char *) 0) return ERR_NORMAL; /* nothing to do */
575
if (tp->vsize != 0 && tp->usname)
577
if (tp->tflags & TBL__VREADONLY)
581
status = SCFOPN(tp->usname,D_R4_FORMAT,F_O_MODE,F_TBL_TYPE, &newtid);
582
status = SCFPUT (newtid, 1, tp->vsize, (char *)tp->vsel);
583
status = SCFCLO(newtid);
588
if ((tp->tflags & (TBL__READONLY|TBL__MODIFIED)) == TBL__MODIFIED)
590
if_not (tp->tflags & TBL__KEEPVERS)
591
tp->version = TBL_VERSION;
593
/* If ALL is selected, correct number of selected rows */
595
if ((tp->selected < 0) && (tp->select == '\1'))
596
tp->selected = tp->rows;
599
if (tp->tflags != TBL__READONLY) /* if_not (tp->tflags & TBL__MAPPED) */
600
status = TBL_WR (tp);
605
if (tp->addres != (char *) 0)
607
osmmfree(tp->addres);
608
tp->addres = (char *) 0; /* MP 920909 */
611
for (i=0; i<tp->nbuf; i++)
614
if (bp && bp->buf) osmmfree(bp->buf);
625
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
626
.PURPOSE free allocated memory
631
------------------------------------------------------------------*/
632
int tid; /* IN : table id */
642
if ((status = TCTUNM(tid))) return(status);
644
/* Free allocated memory */
645
osmmfree(tp->phname);
646
tp->phname = (char *) 0;
647
/* tp->usname is NOT used currently */
648
osmmfree((char *)tp->dtypes);
649
tp->dtypes = (int *) 0;
651
osmmfree((char *)tp->bytes);
652
tp->bytes = (int *) 0;
655
tp->label = (char *) 0;
657
if (!(tp->tflags&TBL__MAPPED))
659
if (tp->addres != (char *) 0)
661
osmmfree(tp->addres);
662
tp->addres = (char *) 0;
665
osmmfree((char *)tp->tbuf);
666
tp->tbuf = (TABLE_BUF *) 0;
668
osmmfree((char *)tp->loaded_map);
669
tp->loaded_map = (unsigned char *) 0;
671
osmmfree((char *)tp->vsel);
672
tp->vsel = (unsigned char *) 0;
678
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
679
.PURPOSE Unmap (Flush) table file
680
.ALGORITHM Writes back modified parts and releases used memory.
682
------------------------------------------------------------------*/
683
int tid; /* IN : table id */
693
if ((status = CheckTable(tp))) return(TBL_errs(tid, status,0));
695
if (tp->vsize != 0 && tp->usname)
697
if (tp->tflags & TBL__VREADONLY)
701
status = SCFOPN(tp->usname,D_R4_FORMAT,F_O_MODE,F_TBL_TYPE, &newtid);
702
status = SCFPUT (newtid, 1, tp->vsize, (char *)tp->vsel);
703
status = SCFCLO(newtid);
708
if ((tp->tflags & (TBL__READONLY|TBL__MODIFIED)) == TBL__MODIFIED)
710
if_not (tp->tflags & TBL__KEEPVERS) tp->version = TBL_VERSION;
712
/* If ALL is selected, correct number of selected rows */
714
if ((tp->selected < 0) && (tp->select == '\1')) tp->selected = tp->rows;
716
/* write descriptors */
718
status = SCDWRI(tp->imno, TBL_Ddtypes, tp->dtypes, 1,tp->colitems, &dunit);
719
if (status == ERR_NORMAL)
720
status = SCDWRI(tp->imno, TBL_Doffset, tp->offset,1,tp->colitems, &dunit);
721
if (status == ERR_NORMAL)
722
status = SCDWRI(tp->imno, TBL_Dmain, &tp->acols,1,TBL_Dmain_SIZE,&dunit);
723
if (status != ERR_NORMAL) goto error;
726
if_not (tp->tflags & TBL__MAPPED)
727
{ /* Must write everything */
728
if ((status = TBL_WR (tp))) return(status);
730
osmmfree(tp->addres);
731
tp->addres = (char *) 0; /* MP 920909 */
734
for (i=0; i<tp->nbuf; i++)
737
if (bp && bp->buf) osmmfree(bp->buf);
740
error: /* error in SC routines */
743
TBL_enter("TCTUNM problems");
744
TBL_errs(tid, status, 0);
750
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
751
.PURPOSE Find number from Name
752
.RETURNS Corresponding tid / -1 if not found
753
--------------------------------------------------------------------*/
754
char *name; /* IN: Name to look for */
756
return (TBL_tid(name));
759
int TCTINI(name, storage, mode, allcol, allrow, tid)
760
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
762
Initializes table file on disk and in memory
764
Creates output file and opens it
765
according to the desired storage option and mode.
766
The storage option can be by records (F_RECORD) or
767
transposed (F_TRANS).
768
The openning mode can be
769
F_O_MODE for output or F_X_MODE for scratch.
771
The mode may include modifiers F_MAP_FORCE to force the
772
mapping, F_EIO_FORCE to force an elementary i/o mode,
773
F_ALL_FORCE to force exact numbers of lines / cols.
774
In this last case (F_ALL_FORCE), the number of physical
775
columns to allocate may be given in the 2 leftmost bytes of mode,
776
i.e. mode = (physical_columns*2**16 + F_ALL_FORCE + mode)
779
------------------------------------------------------------------*/
780
char *name; /* IN : table name */
781
int storage; /* IN : physical structure on disk */
782
int mode; /* IN : opening mode */
783
int allcol; /* IN : number of words per record alloc.*/
784
int allrow; /* IN : number of rows allocated */
785
int *tid; /* MOD: table identifier (try to keep it) */
791
int imno, len, acol, arow, colitems;
793
struct FCT_STRUCT *fctpntr;
800
acol = (allcol <= 0) ? TBL_DALCOL : allcol ;
801
arow = (allrow <= 0) ? TBL_DALROW : allrow ;
803
/* Restrictions: Rows multiple of 8, columns odd.
804
* This ensures alignments on 8-byte boundaries */
806
if (mode & F_ALL_FORCE)
808
colitems = mode >> 16;
812
arow = ((arow+7)>>3)<<3;
813
if (!(acol&1)) acol++;
815
if (colitems == 0) colitems = MIN (acol,COLS_MAX);
818
len = arow * (acol + 1);
821
if (mode == F_FO_MODE) /* set in fitsrhd() */
825
status = SCFCRE(name,D_R4_FORMAT,mode&0xf,F_FTBL_TYPE,len,&imno);
830
status = SCFCRE(name,D_R4_FORMAT,mode&0xf,F_TBL_TYPE,len,&imno);
832
if (status != ERR_NORMAL) goto error;
833
/* initialize control variables */
834
if (TBL_new(imno) < 0)
835
return(TBL_errf(ERR_TBLFUL, "too many tables, can't create %s", name));
840
fctpntr = FCT.ENTRIES + imno;
841
tp->phname = TBL_ssave(fctpntr->NAME); /* use cleaned name */
842
tp->usname = (char *)0;
851
tp->version= TBL_VERSION;
852
tp->colitems = colitems;
853
tp->selected = -1; /* unknown */
857
tp->intlFITS = 'F'; /* internal FITS table */
860
/* initialize columns for dtype */
861
tp->dtypes = (int *)osmmget(2*sizeof(int)*tp->colitems);
862
tp->offset = tp->dtypes + tp->colitems;
864
/* write basic descriptors */
865
status = SCDWRI(tp->imno, TBL_Ddtypes, tp->dtypes, 1,tp->colitems, &dunit);
866
if (status != ERR_NORMAL) goto error;
868
status = SCDWRI(tp->imno, TBL_Doffset, tp->offset, 1,tp->colitems, &dunit);
869
if (status != ERR_NORMAL) goto error;
871
status = SCDWRI(tp->imno, TBL_Dmain, &tp->acols,1,TBL_Dmain_SIZE,&dunit);
872
if (status != ERR_NORMAL) goto error;
874
status = do_map(tp, mode); /* map file */
875
if (status != ERR_NORMAL) goto error;
877
if (tp->intlFITS == 'F') /* we have to write some data */
878
{ /* because TCSINI uses SCFGET ... */
879
status = dummyPUT(imno,len);
880
if (status != ERR_NORMAL) goto error;
889
if (tp) osmmfree (tp->phname),TBL_kill(tp->imno);
890
if (status < ERR_TBLFUL) /* SC error */
891
MID_ERROR("MIDAS","TCTINI: ",status,1);
900
int TCTMAP(tid, addr)
901
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
902
.PURPOSE Maps complete Table
904
------------------------------------------------------------------*/
905
int tid; /* IN : table id */
906
char **addr; /* OUT: Where table Mapped */
908
TABLE_BUF *bp; /* MOD: The buffer concerned */
917
if ((status = CheckTable(tp))) return(TBL_errs(tid, status,0));
919
if (tp->tflags & TBL__MAPPED) { /* Already Done */
923
if (tp->tflags & TBL__EIO) { /* Verify all empty */
924
for (i = tp->nbuf, bp = tp->tbuf; (--i >= 0) && (bp->size == 0); bp++);
925
} else { /* Just check bitmap */
926
i = sizeof(float)*(tp->acols + 1) * tp->arows;
927
i = (1 + (i|0xffff)) >> 16;
928
for (pmap = tp->loaded_map; (--i >= 0) && (*pmap == 0); pmap++) ;
931
if (i >= 0) status = ERR_TBLMAP;
933
TBL_errf(status, "Table partly mapped. First Unmap table %s",
937
/* Free allocated memory for Buffers */
938
osmmfree((char *)tp->tbuf);
939
osmmfree((char *)tp->loaded_map);
940
osmmfree((char *)tp->vsel);
942
i = F_MAP_FORCE | (tp->tflags & TBL__READONLY ? F_I_MODE : F_IO_MODE);
943
status = do_map (tp, i);
948
int TCTOPN(name,mode,tid)
949
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
951
Reads table file from disk.
954
according to the desired mode. The openning mode can be
955
F_I_MODE for input, F_D_MODE for descriptors only,
956
F_IO_MODE for update.
958
The mode may include modifiers F_MAP_FORCE to force the
959
mapping, or F_EIO_FORCE to force an elementary i/o mode.
962
------------------------------------------------------------------*/
963
char *name; /* IN : table name */
964
int mode; /* IN : openning mode */
965
int *tid; /* MOD: table identifier (try to keep it) */
971
int status, flags[3];
977
flags[0] = ERRO_CONT; /* continue always, and */
978
flags[1] = ERRO_LOG; /* log nothing, and */
979
flags[2] = ERRO_DISP; /* display nothing */
984
imno = *tid; /* in case a specific `index' is wanted... */
985
if ((status = SCFOPN(name, D_R4_FORMAT, 0, F_TBL_TYPE, &imno)))
987
SCECNT("PUT", flags, flags+1, flags+2);
988
return(TBL_errf(status,"Error opening Table %s",name));
991
if (TBL_new(imno) < 0) /* Not too many opened tables? */
993
TBL_errf(ERR_TBLFUL, "too many tables, can't open %s", name);
999
tp->phname = TBL_ssave(name);
1000
tp->usname = (char *)0;
1001
tp->tflags = ((mode&0xf) == F_I_MODE ? TBL__READONLY : 0);
1003
tp->addres = (char *)0;
1005
/* get control variables */
1006
status = SCDRDI(tp->imno, TBL_Dmain,1,TBL_Dmain_SIZE,
1007
&maxval,&tp->acols,&dunit,&dnull);
1008
if (status != ERR_NORMAL) goto error;
1010
if (maxval < 8) tp->version = 0;
1011
if (maxval < 9) tp->colitems = MAX (tp->acols, tp->cols);
1012
if (maxval <10) tp->selected = -1; /* Unknown */
1013
if (tp->selected == -1) tp->selected = tp->rows;
1015
if (tp->swise & F_B_VIEW)
1017
int vid, oldtid, kk;
1019
char view[1+TBL_Dview_SIZE];
1021
status = SCDRDC(tp->imno, TBL_Dview, 1,1,TBL_Dview_SIZE, &maxval,
1022
view, &dunit, &dnull);
1023
if (status != ERR_NORMAL)
1025
TBL_errf (status, "Bad View: %s", name);
1029
view[maxval] = '\0';
1030
if (osfdate(view) > osfdate(name))
1031
TBL_errf (-1, "table '%s' refered by view '%s' was modified ?",
1034
oldtp = tp; /* save table pointer */
1037
ERRO_CONT = flags[0];
1038
ERRO_LOG = flags[1]; /* reset error flags */
1039
ERRO_DISP = flags[2]; /* reset error flags */
1041
status = TCTOPN (view, F_I_MODE, &vid);
1042
if (status != ERR_NORMAL) goto error;
1045
vp->vno = oldtp->imno;
1047
if (oldtp->tflags & TBL__READONLY) vp->tflags |= TBL__VREADONLY;
1049
/* Read Selection Table */
1051
vp->vsize = 1 + (vp->rows>>3);
1052
if (vp->vsize < kk) vp->vsize = kk; /* space for at least 1 value */
1053
vp->vsel = (unsigned char *)osmmget( vp->vsize );
1054
vp->usname = TBL_ssave(name);
1056
kk = tp->vsize>>2; /* really tp->vsize, not vp->vsize? KB 050603 */
1060
(void) sprintf(mesg,
1061
"tp->vsize>>2 = %d, vp->vsize = %d, so we truncate...",
1066
/* status = SCFGET (vp->vno,1,tp->vsize>>2,&maxval,(char *)vp->vsel); */
1067
status = SCFGET (vp->vno, 1, kk, &maxval, (char *)vp->vsel);
1069
oldtp->tflags |= TBL__MAPPED; /* to avoid going through TBL_WR() */
1072
*tid = vid; /* return the new table id */
1077
status = do_map(tp, mode);
1078
if (status != ERR_NORMAL) goto error;
1079
/* Get Column datatypes */
1080
tp->dtypes = (int *)osmmget(2*sizeof(int)*tp->colitems);
1081
tp->offset = tp->dtypes + tp->colitems;
1083
status = SCDRDI(tp->imno, TBL_Ddtypes,1,tp->colitems,&maxval,
1084
tp->dtypes,&dunit,&dnull);
1085
if (status != ERR_NORMAL) goto error;
1087
if (tp->version) status = SCDRDI(tp->imno, TBL_Doffset,1,tp->colitems,
1088
&maxval,tp->offset,&dunit,&dnull);
1089
/* layout of physical columns */
1092
/* Convert NULL values if required */
1093
if (tp->tflags & TBL__BADNULLS)
1096
tp->tflags &= ~TBL__BADNULLS;
1102
ERRO_CONT = flags[0]; /* reset error flags */
1103
ERRO_LOG = flags[1];
1104
ERRO_DISP = flags[2];
1105
if ((status) && (status < ERR_TBLFUL)) /* SC error */
1106
TBL_errs(imno, status, 0);
1113
int TCTVIS(tid,name)
1114
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1116
Reads table file from disk.
1119
according to the desired mode. The openning mode can be
1120
F_I_MODE for input, F_D_MODE for descriptors only,
1121
F_IO_MODE for update.
1123
The mode may include modifiers F_MAP_FORCE to force the
1124
mapping, or F_EIO_FORCE to force an elementary i/o mode.
1127
------------------------------------------------------------------*/
1128
int tid; /* MOD: table identifier (try to keep it) */
1129
char *name; /* IN : table name */
1136
strncpy(name,tp->usname,60);