~ubuntu-branches/debian/jessie/eso-midas/jessie

« back to all changes in this revision

Viewing changes to prim/table/libsrc/tbadec.c

  • Committer: Package Import Robot
  • Author(s): Ole Streicher
  • Date: 2014-04-22 14:44:58 UTC
  • Revision ID: package-import@ubuntu.com-20140422144458-okiwi1assxkkiz39
Tags: upstream-13.09pl1.2+dfsg
ImportĀ upstreamĀ versionĀ 13.09pl1.2+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*===========================================================================
 
2
  Copyright (C) 1989-2009 European Southern Observatory (ESO)
 
3
 
 
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.
 
8
 
 
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.
 
13
 
 
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, 
 
17
  MA 02139, USA.
 
18
 
 
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 
 
25
                        GERMANY
 
26
===========================================================================*/
 
27
 
 
28
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
29
.TYPE        Module
 
30
.NAME        tbadec.c
 
31
.LANGUAGE    C
 
32
.AUTHOR      IPG-ESO Garching
 
33
.CATEGORY    table utilities
 
34
 
 
35
.COMMENTS    Interpretation of datatypes and formats.
 
36
\begin{TeX}
 
37
The datatype can be one of the following:
 
38
 
 
39
\begin{itemize}
 
40
\item   R[eal], R[eal]*4   - floating point single precision.
 
41
\item   D[ouble], R[eal]*8 - floating point double precision.
 
42
\item   I[nteger], I[nteger]*4 - long integer (4 bytes).
 
43
\item   S[hort],   I[nteger]*2 - short integer (2 bytes).
 
44
\item   B[yte],    I[nteger]*1 - byte integer (1 byte).
 
45
\item   U[nsigned]*$n$ - Unisgned integer with $n = 1, 2, 4$
 
46
\item   C[haracter]*n          - character string.
 
47
\end{itemize}
 
48
\end{TeX}
 
49
 
 
50
 
 
51
.VERSION 1.0    25-Mar-1989   Definition     J.D. Ponz
 
52
.VERSION 3.0    05-Jul-1990   New version with column arrays   F.O.
 
53
.VERSION 3.1    27-Nov-1990   Merge of tbdecfmt and tbdectyp
 
54
.VERSION 3.15   04-Feb-1991   length of string = length of field  M.P
 
55
 
 
56
 090707         last modif
 
57
------------------------------------------------------------*/
 
58
 
 
59
#include <tbldef.h>
 
60
#include <midas_def.h>
 
61
#include <str.h>                /* String Utilities     */
 
62
#include <proto_tbl.h>
 
63
 
 
64
#include <stdio.h>
 
65
#include <stdlib.h>
 
66
 
 
67
 
 
68
 
 
69
int tbl_dectyp(etype,type,noelem,form)
 
70
/*++++++++++++++++++
 
71
.PURPOSE Interpret the Type (e.g. R*4) declaration
 
72
.RETURNS Status
 
73
------------------*/
 
74
 char *etype;           /* IN: Text to scan     */
 
75
 int  *type;            /* OUT: Midas data type */
 
76
 int  *noelem;          /* OUT: Array size      */
 
77
 char *form;            /* OUT: Default Format  */
 
78
{
 
79
        int     i, l1, alen;
 
80
        char    msg[256];
 
81
 
 
82
  *noelem = 1;
 
83
  alen    = 0;          /* ASCII length */
 
84
  i = strloc (etype, '(');
 
85
  if (etype[i]) {
 
86
        l1 = atoi(etype+1+i);
 
87
        if ((l1 < 1) || (l1 > TBL_MAXSIZ)) {
 
88
                sprintf(msg, "****Bad array size in `%s'", etype);
 
89
                SCTPUT (msg);
 
90
                return (ERR_TBLCOL);
 
91
        }
 
92
        *noelem = l1;
 
93
  }
 
94
 
 
95
  l1 = 0;                               /* decode type */
 
96
  i = strloc (etype, '*');
 
97
  if (etype[i]) l1 = atoi(etype + i + 1);
 
98
  switch (etype[0]) {
 
99
    case 'd': case 'D':
 
100
    case_double:
 
101
        if (!alen)      alen = 24;
 
102
        sprintf (form, "D%d.%d", alen, alen-7);
 
103
        *type = D_R8_FORMAT;
 
104
        break;
 
105
    case 'R': case 'r': 
 
106
        if (l1 == 8)    goto case_double;
 
107
        if (!alen)      alen = 12;
 
108
        sprintf (form, "E%d.%d", alen, alen-6);
 
109
        *type = D_R4_FORMAT;
 
110
        break;
 
111
    case 'I': case 'i': 
 
112
    case_int:
 
113
        if (l1 == 1)    { *type = D_I1_FORMAT; if (!alen)  alen = 4; }
 
114
        else if (l1==2) { *type = D_I2_FORMAT; if (!alen)  alen = 6; }
 
115
        else            { *type = D_I4_FORMAT; if (!alen)  alen = 11;}
 
116
        sprintf (form, "I%d", alen);
 
117
        break;
 
118
    case 'l': case 'L': 
 
119
    case 'u': case 'U': 
 
120
        if (l1 == 1)    {
 
121
                l1 = *noelem;   *noelem = 1;
 
122
                goto case_ascii;
 
123
        }
 
124
        if (l1==2)      { *type = D_L1_FORMAT; if (!alen)  alen = 4; }
 
125
        else            { *type = D_L4_FORMAT; if (!alen)  alen = 8;}
 
126
        sprintf (form, "X%d", alen);
 
127
        break;
 
128
    case 'a': case 'A': 
 
129
    case 'c': case 'C': 
 
130
    case_ascii:
 
131
/*      if (*noelem > 1) {
 
132
                sprintf(msg, "**** C*n columns can't be arrays `%s'", etype);
 
133
                SCTPUT (msg);
 
134
                return (ERR_TBLCOL);
 
135
        } */
 
136
        if (l1 == 0)    l1 = alen;
 
137
        if ((l1 < 1) || (l1 > TBL_MAXSIZ)) {
 
138
                sprintf(msg, "**** Bad character size in `%s'", etype);
 
139
                SCTPUT (msg);
 
140
                return (ERR_TBLCOL);
 
141
        }
 
142
        sprintf(form,"A%d",l1);
 
143
/*      *noelem = 1;  */
 
144
        *type = D_C_FORMAT;
 
145
        break;  
 
146
    case 'B': case 'b': 
 
147
        l1 = 1;         goto case_int;
 
148
    case 'S': 
 
149
        l1 = 2;         goto case_int;
 
150
    default:
 
151
        sprintf (msg, "**** Bad datatype %s", etype);
 
152
        SCTPUT  (msg);
 
153
        return (ERR_TBLCOL);
 
154
  }
 
155
 
 
156
  return (ERR_NORMAL);
 
157
}
 
158
 
 
159
 
 
160
int tbl_decfmt(line,i1,i2,type,noelem,form,unit,label)
 
161
/*++++++++++++++++++
 
162
.PURPOSE Decode one line of the FMT file, which looks like
 
163
\begin{TeX}
 
164
        {\tt DEFINE/FIELD} [pos1 pos2] type [format] label [unit]
 
165
\end{TeX}
 
166
.RETURNS Status
 
167
.REMARKS If type==0 on return, we've an emtpy line (comment).
 
168
        Beware, the input line IS MODIFIED !!!
 
169
-i----------------*/
 
170
 char *line;    /* IN: Line to scan     */
 
171
 int  *i1;      /* OUT: position (1)    */
 
172
 int  *i2;      /* OUT: position (2)    */
 
173
 int  *type;    /* OUT: Element type in midas_def format        */
 
174
 int  *noelem;  /* OUT: Size of array   */
 
175
 char *form;    /* OUT: Format          */
 
176
 char *unit;    /* OUT: Unit            */
 
177
 char *label;   /* OUT: Label (column name)     */
 
178
{
 
179
 int status, l1, l2, i,j;
 
180
 char *pf, *pl, *pu, *dtype;
 
181
 char *p,*pt,mpt[6];
 
182
 
 
183
  *i1   = 0;
 
184
  *i2   = 0;
 
185
  *type = 0;
 
186
  *noelem = 1;
 
187
  *form = *unit = '\0';
 
188
  l1 = l2 = 0;
 
189
  pt = mpt;
 
190
 
 
191
  line[strloc(line, '!')] = '\0';               /* Strip comments       */
 
192
 
 
193
                                                /* DEFINE/KEY           */
 
194
  p = line + strskip (line, ' ');
 
195
  if (*p != 'd' && *p != 'D') return(ERR_NORMAL);
 
196
  p += strloc (p, ' ');
 
197
 
 
198
                                                /* Get pos2  pos2       */
 
199
  p += strskip(p, ' '); 
 
200
  if (isdigit (*p)) {
 
201
        l1 = atoi(p);   p += strloc(p, ' ');    p += strskip(p, ' '); 
 
202
        l2 = atoi(p);   p += strloc(p, ' ');
 
203
        *i1 = l1; *i2 = l2; 
 
204
        l2 = l2 - l1 + 1;
 
205
  }
 
206
 
 
207
  p += strskip(p, ' ');
 
208
  dtype = p;
 
209
  p+=strloc(p,' ' ); 
 
210
  if (*p) *(p++) = '\0'; 
 
211
  p += strskip(p, ' '); pf = p;    p += strloc(p, ' '); if (*p) *(p++) = '\0';
 
212
  p += strskip(p, ' '); pl = p;    p += strloc(p, ' '); if (*p) *(p++) = '\0';
 
213
  p += strskip(p, ' '); pu = p;    p += strloc(p, ' '); if (*p) *(p++) = '\0';
 
214
  if (! *pl)            pl = pf, pf = pu;       /* Only 1 string */
 
215
  if (*pf == ':')       p = pl, pl = pf+1, pf = p;
 
216
  if (*pu == ':')       p = pl, pl = pu+1, pu = p;
 
217
  if (*pf == '"')       p = pu, pu = pf,   pf = p;
 
218
  i = strloc(dtype,'*');
 
219
  if ((*dtype =='C' || *dtype =='c') && (!dtype[i])) sprintf(pt,"C*%d",l2); 
 
220
      else
 
221
          {
 
222
       j=0;
 
223
       while (*dtype!='\0'){
 
224
                pt[j] = *dtype++;
 
225
                j++;}
 
226
          pt[j] = '\0';
 
227
          }
 
228
 status = tbl_dectyp(pt, type, noelem, form);
 
229
 if (status) return(status);
 
230
 if (!pt[i] && (*pt =='C' || *pt =='c')) *noelem = l2;
 
231
 else if (( *pt=='C' || *pt =='c')) {
 
232
          pt +=2;
 
233
          *noelem = atoi(pt);
 
234
          }
 
235
 
 
236
  if (*pf)      strcpy (form, pf);
 
237
  if (*pu)      strcpy (unit, pu);
 
238
 
 
239
 
 
240
  i = (int) strlen(pl);
 
241
  if (i > TBL_LABLEN)
 
242
     {                                 /* truncate if necessary */
 
243
     (void) strncpy(label,pl,TBL_LABLEN);
 
244
     label[TBL_LABLEN] = '\0';
 
245
     (void) SCTPUT("(I/W) - column label truncated..");
 
246
     }
 
247
  else
 
248
     strcpy (label, pl);
 
249
 
 
250
  return(status);
 
251
}