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

« back to all changes in this revision

Viewing changes to prim/edit/libsrc/tbledit.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) 1995-2005 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
.LANGUAGE       C
 
30
.TYPE           Module
 
31
.NAME           tbledit.c
 
32
.VERSION 0.0    17-Mar-1989:    First design
 
33
.VERSION 1.1    24-Nov-1989:    Corrected (FO)
 
34
.VERSION 1.2    12-Feb-1990:    Row boundary checking. JDP.
 
35
                                Check if several commands are given, to speed up
 
36
.VERSION 1.3    28-Oct-1990: Just patch for new version.
 
37
.AUTHOR         Daniel Ponz, Francois Ochsenbein
 
38
.CATEGORY       Table Editor
 
39
.COMMENTS       Just here Field i/o
 
40
        *** The provisional implementation assumes the functions
 
41
                ClearError()            removes error text, if any
 
42
                ShowError(text)         shows error text
 
43
        *** Actions
 
44
                NEXTCOL  returned for `asking to move to next (right) col'
 
45
                PREVCOL  returned for `asking to move to previous (left) col'
 
46
                HOMECOL  returned for `asking to move to first (left) col'
 
47
                BAD_KEY  returned for unknown key
 
48
.ENVIRONMENT    MIDAS, TermWindows
 
49
 
 
50
.VERSION
 
51
051012          last modif
 
52
 
 
53
------------------------------------------------------------------------*/
 
54
 
 
55
#include <tw.h>                         /* Window system        */
 
56
#include <ok.h>                         /* Just OK / NOK definitions */
 
57
#include <macrogen.h>                   /* General purpose macros, e.g. MAX */
 
58
#include <str.h>                        /* General string handling  */
 
59
 
 
60
#include <tbldef.h>     
 
61
#include <tfmdef.h>     
 
62
#include <tbledt.h>     
 
63
#include <tblfmt.h>     
 
64
#include <proto_tbl.h>
 
65
 
 
66
#include <stdio.h>
 
67
 
 
68
 
 
69
#define MAXEDT_ELEMENT  255     /* Maximal size for getting a single
 
70
                                 * table element */
 
71
static char     buffer[MAXEDT_ELEMENT+1];
 
72
static char     buf_er[MAXEDT_ELEMENT+1];
 
73
 
 
74
static double variable[(MAXEDT_ELEMENT+7)/8];
 
75
 
 
76
 
 
77
/*========================================================================*/
 
78
int compute_col()
 
79
/*++++++++
 
80
.PURPOSE Compute the current column from cursor position
 
81
                in range [0, edt_nc]
 
82
.REMARKS Should be a general function ? should thecol be external
 
83
        (known to all edt functions) ?
 
84
.RETURNS The number
 
85
---------*/
 
86
{
 
87
        int     j, col1;
 
88
 
 
89
  GetCursor(data_subwindow, cursor_pos);
 
90
  col1 = cursor_pos[1];
 
91
 
 
92
  for (j=0; col1 > FMT[j].LAST; j++)    ;
 
93
 
 
94
  return(j);
 
95
}
 
96
 
 
97
static int Edit(str)
 
98
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
99
.PURPOSE Convert from Binary (in variable) to str
 
100
.REMARKS Used in cunjunction with ModsWithCheck
 
101
.RETURNS OK=1 (str is correct) / NOK=0 (Bad char)
 
102
------------------------------------------------------------------------*/
 
103
        char    *str;   /* OUT: Edited number   */
 
104
{
 
105
  return (TCEEDC(edt_tid, (char *)variable, FMT[thecol].colno, str));
 
106
}
 
107
 
 
108
static int Convert(str) 
 
109
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
110
.PURPOSE Convert from Char to Binary
 
111
.REMARKS Used in conjunction with ModsWithCheck
 
112
.RETURNS 0 (NULL) / -1 (error) / positive number if OK 
 
113
------------------------------------------------------------------------*/
 
114
        char    *str;   /* IN: String to Convert        */
 
115
{
 
116
        int     status, i;
 
117
        
 
118
  status = TCETRC(edt_tid, (char *)variable, FMT[thecol].colno, str);
 
119
  if (status)   status = -1;    /* Error */
 
120
  else          status = *str;
 
121
  return(status);
 
122
}
 
123
 
 
124
/*========================================================================*/
 
125
static int check(w, str, size) 
 
126
/*++++++++++++++
 
127
.PURPOSE Check Function for Input of an Element
 
128
.REMARKS Used in cunjunction with ModsWithCheck
 
129
.RETURNS OK=1 (input is correct) / NOK=0 (Bad input)
 
130
.METHOD  Convert str to Binary (if not a char string!), tne convert back.
 
131
--------------*/
 
132
        WINDOW  *w;     /* IN: The Window       */
 
133
        char    *str;   /* MOD: What the user typed     */
 
134
        int     size;   /* IN: Size of str buffer       */
 
135
{     
 
136
        char    temp[MAXEDT_ELEMENT+80];
 
137
        int     status;
 
138
        int    cur_col, cur_row;        
 
139
    
 
140
  status = -1;
 
141
  if (strcomp(buf_er,str) == 0) status = -2;
 
142
  if (status == -1)             status = Convert(str);
 
143
  if (status == -1) {
 
144
        status = NOK; 
 
145
        ShowError(">>> Format Error"), Bell();
 
146
  }
 
147
  else {                /* Correct Entry. Modify underlying table       */
 
148
        SetAttr(w, _NORMAL_);      /* Will be redisplayed correctly     */
 
149
        if (status >= 0) {      /* A modification of the value ... */
 
150
                GetCursor(data_subwindow, cursor_pos);
 
151
                cur_col = edt_column[compute_col()];
 
152
                cur_row = edt_row[cursor_pos[0]];
 
153
                if (cur_row <= 0 || cur_row > edt_narow) {
 
154
                        status = NOK;           /* check row boundaries */
 
155
                        ShowError(" Limit of the table ");
 
156
                        Bell();
 
157
                        return(status);
 
158
                }
 
159
                if (status == 0)  {     /* NULL value */
 
160
                        sprintf(temp,"Delete row %d, column %d", 
 
161
                                cur_row,cur_col);
 
162
                        TCEDEL(edt_tid, cur_row, cur_col);
 
163
                }
 
164
                else {
 
165
                        sprintf(temp,"Rewrite row %d, column %d, element %s ",
 
166
                        cur_row, cur_col, str);
 
167
                        TCEWRC(edt_tid, cur_row, cur_col, str);
 
168
                }
 
169
                ShowError(temp);
 
170
        }
 
171
        if (status > 0) Edit(str); /* Converts back using Format        */
 
172
        SetAttr(w, _NORMAL_);      /* Will be redisplayed correctly     */
 
173
        status = OK;
 
174
  }
 
175
 
 
176
  return(status);
 
177
}
 
178
 
 
179
/*========================================================================*/
 
180
int edt_edfield()
 
181
/*++++++++
 
182
.PURPOSE Edit any table element. What the user entered is checked.
 
183
        **** Note: EnableArrows(data_subwindow) and EnableKeypad(data_subwindow)
 
184
        is in effect (normally called after OpenWindow)
 
185
.RETURNS The action hit by the user. Can be just Return.
 
186
.REMARKS Process here only one element.
 
187
----------*/
 
188
{
 
189
 
 
190
        int     lencol;         /* Width of the current column  */
 
191
        int     key_group;      /* The group of keys (_KEYPAD_, etc) */
 
192
        char    key_hit;        /* Explanation within key_group */
 
193
        int     key1, key2;     /* input key(s)                 */
 
194
        int     i;
 
195
  
 
196
        
 
197
  thecol = compute_col();       /* Just know the index in FMT tables */
 
198
 
 
199
  lencol = FMT[thecol].LAST - FMT[thecol].FIRST; 
 
200
  lencol = MIN(lencol, sizeof(buffer)-1);
 
201
  
 
202
        /* The acquisistion of the contents of the current column
 
203
         * is made via TranslateWindow. Is there a better solution ? */
 
204
 
 
205
        /* Suppress cursor mouvements echo */
 
206
  DeactiveWindow(data_subwindow); 
 
207
 
 
208
  CursorTo(data_subwindow, cursor_pos[0], FMT[thecol].FIRST);
 
209
  TranslateWindow(data_subwindow, buffer, lencol);
 
210
  buffer[lencol] = '\0';
 
211
  for (i = lencol-1; i >= 0 && buffer[i] == ' '; buffer[i--] = '\0')   ;
 
212
  oscopy(buf_er,buffer,lencol+1);
 
213
 
 
214
  Input_Loop:
 
215
  SetAttr(data_subwindow, _REVERSE_);           
 
216
  
 
217
        /* Before asking for the input, check if some command
 
218
         * was already typed in. Useless to repaint the screen... */
 
219
 
 
220
  if (CheckInput()){
 
221
        key_group = GetChar(&key_hit);
 
222
        if ( (key_group == _STANDARD_) && isprint(key_hit)) {
 
223
                UngetChar(&key_hit);
 
224
                goto Get_User_Input;
 
225
        }
 
226
        if (key_hit == '\r')    key_hit = EOS;
 
227
        buffer[0] = key_hit;
 
228
        goto Execute_User_Input;
 
229
  }
 
230
 
 
231
        /* Ready now for Input. Buffer contains a copy of the
 
232
         * element (in Character). It will be redisplayed in Reverse */
 
233
 
 
234
  Get_User_Input:
 
235
  ActiveWindow(data_subwindow);  
 
236
 
 
237
  key_group = ModsWithCheck(data_subwindow, buffer, lencol+1, check);
 
238
 
 
239
  Execute_User_Input:
 
240
  SetAttr(data_subwindow, _NORMAL_);    
 
241
 
 
242
        /* The result of Mods may be:
 
243
         * _INTERRUPT_  : take the original (no modif) 
 
244
         * _STANDARD_   : terminated by the Carriage Return
 
245
         * 0            : We must look what was exactly the key via GetKey
 
246
         * Other        : A special key was hit as the FIRST input
 
247
         */
 
248
 
 
249
  switch(key_group)
 
250
  {
 
251
        case _INTERRUPT_:       
 
252
                        ShowError("***INTERRUPT*** Hit Return");
 
253
                        oscopy(buffer,buf_er,lencol+1);
 
254
                        goto Input_Loop;
 
255
        case _STANDARD_:        if (buffer[0]  && iscntrl(buffer[0])) 
 
256
                                 {key_hit = buffer[0], buffer[0] = EOS; break;}
 
257
                                return(NEXTFIELD);
 
258
        case 0:                 key_group = GetKey(data_subwindow, &key_hit);
 
259
                                break;
 
260
        case _EOF_:             break; 
 
261
        default:                key_hit = buffer[0], buffer[0] = EOS;
 
262
                                break;
 
263
  }
 
264
 
 
265
  if (key_group == _EOF_)       return(COMMAND);
 
266
  if (key_group == _STANDARD_)  key_group = 0;
 
267
  key1 = key_group<<8 | key_hit;
 
268
 
 
269
  key2 = 0;
 
270
        
 
271
        switch(tk_check(key1))
 
272
        {
 
273
          case 0: return (BAD_KEY);     /* Doesn't map to an action */
 
274
 
 
275
          case 2: key_group = GetKey(data_subwindow, &key_hit);
 
276
                  key2      = key_group<<8 | key_hit;
 
277
 
 
278
          case 1: return (tk_exec(key1, key2));
 
279
        }
 
280
  
 
281
 return(BAD_KEY);
 
282
}
 
283
 
 
284
/*========================================================================*/
 
285
 
 
286
int tbl_edit()
 
287
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
288
.PURPOSE
 
289
        Edit table      
 
290
.KEYWORDS
 
291
        Tables, Editor
 
292
.ALGORITHM
 
293
 
 
294
.RETURNS
 
295
        Status 0 OK - save table; -1 NOK - no save the table
 
296
------------------------------------------------------------------*/
 
297
 
 
298
{
 
299
        int     iend, old_direction, old_state;
 
300
        int     old_err;
 
301
        
 
302
        /* iteration on input */;
 
303
 
 
304
   TCOGET("display_errors", &old_err);  /* Don't Display */
 
305
   TCOSET("display_errors", 0); /* Don't Display */
 
306
   while ((edt_action = edt_edfield()) != TERMINATE) 
 
307
 
 
308
   {                                    
 
309
                                        /* decide upon main actions */
 
310
      switch (edt_action) {
 
311
 
 
312
      case NEXTFIELD: 
 
313
        GetCursor(data_subwindow, cursor_pos);
 
314
        thecol = compute_col();
 
315
        if ((thecol+1 >= edt_nc) && (edt_column[edt_nc-1] >= edt_ncol))
 
316
        {               /* Must go to First Column, next line */
 
317
                if (cursor_pos[0]+1 >= data_lines)      /* Must scroll */
 
318
                {       old_direction = edt_advance;
 
319
                        edt_advance = TRUE;
 
320
                        iend = edt_row[edt_nr-1];
 
321
                        if (++iend > edt_narow)         /* End of Table */
 
322
                                edt_nextcol();
 
323
                        else    
 
324
                        {       old_state = DeactiveWindow(editor_window);
 
325
                                CursorHome(data_subwindow),
 
326
                                cursor_pos[0] = cursor_pos[1] = 0;
 
327
                                if (edt_column[0] > 1)  edt_leftpage();
 
328
                                edt_newpage();
 
329
                                CursorDown(data_subwindow, iend - edt_row[0]);
 
330
                                if (old_state) ActiveWindow(editor_window);
 
331
                        }
 
332
                        edt_advance = old_direction;
 
333
                }
 
334
                else    /* I don't have to scroll. However, verify */
 
335
                {       if (edt_column[0] > 1)  edt_leftpage();
 
336
                        edt_nextline();
 
337
                }
 
338
        }
 
339
        else    edt_nextcol();
 
340
        break;
 
341
      case INTERRUPT :  return (-1);                  
 
342
      case BAD_KEY:     edt_badkey();                 break; 
 
343
      case NEXTCOL:     edt_nextcol();                break;
 
344
      case COMMAND:     edt_command();  
 
345
                        if (edt_action == TERMINATE)  return (0);
 
346
                        break;
 
347
     }
 
348
   }
 
349
   TCOSET("display_errors", old_err);   /* Reset at its previous value */
 
350
   return (0);
 
351
}