File: channel_maint.c

package info (click to toggle)
clif 0.93-9.1
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 4,228 kB
  • sloc: ansic: 31,464; yacc: 5,067; lex: 819; makefile: 383; sh: 48
file content (437 lines) | stat: -rw-r--r-- 9,896 bytes parent folder | download | duplicates (7)
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/*
    Clif - A C-like Interpreter Framework
    Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997 T. Hruz, L. Koren

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

/*
 * channel_maint.c
 */

#include "channel_maint.h"
#include "mystdio.h"
#include "allocx.h"
#include <math.h>
#include <stdlib.h>
#include <stdio.h>

#ifndef PROTO
#if defined (USE_PROTOTYPES) ? USE_PROTOTYPES : defined(__STDC__)
#define PROTO(ARGS) ARGS
#else
#define PROTO(ARGS) ()
#endif 
#endif

extern int yy_channelparse PROTO((void));
extern void init_ch PROTO((char *));
extern char *allocate PROTO((unsigned, unsigned));

int channel_handle = 0;
char window_name_init[30] = "clif_channel_handle_";
char window_name_cur[30] = "clif_channel_handle_";
char window_name_wr_init[30] = "clif_alpha_channel_handle_";
char window_name_wr_cur[30] = "clif_alpha_channel_handle_";
char window_name_tmp[5];
struct CHANNEL channel[NUMBER_OF_CHANNELS];

#ifdef XWIN
XEvent event;
GC default_gc, set_gc_write;
Display *disp;
#include "xwin.c"
#include "wind.c"
#include "wind_w.c"
#endif

#ifdef MSWIN
#include "mswin.c"
#include "mswind.c"
#include "mswind_w.c"
#endif

void chwrite PROTO((char **));
void chclose PROTO((char **));
void chflush PROTO((void));
static void append PROTO((struct LIST **, struct LIST **));
static int append_record PROTO((struct LIST **, double));
static void redraw_dur_aut PROTO((int));
static void reset_start_aut PROTO((int, double));
static void free_list PROTO((struct RECORD **));
static int fill_write PROTO((int));
int chopen PROTO((char **));


/*
 * Writing to a channel
 */
void 
chwrite (a)
  char **a;
{
  int *handle;
  double *x;
  int n;
  
  handle = (int *)a[0];
  x = (double *)a[1];

  append_record(&channel[*handle].list_tmp,*x);
/*
 * Creating a list from data written into the channel
 * Sampling of data to fill a record
 */
  channel[*handle].cnt++;
  if(channel[*handle].fields == channel[*handle].cnt) 
	/* 
	 * checking if record is full
	 */
    {
      channel[*handle].global_cnt++;
      if ((channel[*handle].global_cnt
	   > (LINEAR_WINDOW_CONSTANT
	      * (channel[*handle].start_time 
		 + pow (channel[*handle].duration_time 
			+ channel[*handle].d_time, 2.))))
	  && (channel[*handle].d_time > 0))
	
	/* 
	 * Redrawing for DURATION_TIME = automatic
	 */
	{
	  d_flush ();
	  if (!strcmp (channel[*handle].on_leave_w, "suspend"))
	    d_pause (*handle);
	  d_clear (*handle);
	  redraw_dur_aut (*handle);
	}
      if ((channel[*handle].global_cnt
	   > (channel[*handle].start_time 
	      + channel[*handle].duration_time))
	  && (channel[*handle].s_time > 0))
	
	/* 
	 * Redrawing for START_TIME = automatic
	 */
	{
	  reset_start_aut (*handle, *x);
	  d_flush ();
	  if (!strcmp (channel[*handle].on_leave_w, "suspend"))
	    d_pause (*handle);
	  d_clear (*handle);
	}
      if (!strcmp (channel[*handle].type, "alpha")) 
	/*
	 * Checking if alphanumerical window were specified
	 * if yes it is filled with appropriate data
	 */
	fill_write (*handle);
      for (n = 0; n < channel[*handle].fields; n++)
	{
	  if(!strcmp (channel[*handle].print_format, "line"))
	    /* 
	     * Drawing the line
	     */
	    {
	      if (channel[*handle].global_cnt == 1)
		{
		  channel[*handle].member[n].x_cur
		    = channel[*handle].global_cnt;
		  channel[*handle].member[n].y_cur = *x;
		}
	      else
		{
		  move (*handle, n, channel[*handle].member[n].x_cur,
			channel[*handle].member[n].y_cur);
		  draw (*handle, n, channel[*handle].global_cnt, *x);
		  channel[*handle].member[n].x_cur
		    = channel[*handle].global_cnt;
		  channel[*handle].member[n].y_cur = *x;
		}
	    }
	  else	/* 
		 * Drawing the points
		 */
	    draw_point (*handle, n, channel[*handle].global_cnt, *x);
	}
      append (&channel[*handle].list, &channel[*handle].list_tmp);
      /*
       * Creating a global list of records written to the channel
       * The data are writen into the list because we support
       * redrawing of the window by DURATION_TIME or START_TIME 
       * specified as automatic
       */
      
      channel[*handle].cnt = 0;
      
    }
}

/*
 * Closing the window with handle a
 */
void
chclose (a)
  char **a;
{
  int *handle;

  handle = (int *)a[0];

  d_destroy_window (*handle);
  if (!strcmp (channel[*handle].type, "alpha"))
/* If it exists alphanumerical window it is destroyed as well */
    d_destroy_window_write (*handle);
}

/*
 * Flush of buffers
 */
void
chflush ()
{
  d_flush ();
}

/*
 * Appending to the global list
 */
static void 
append (z1, z2)
  struct LIST **z1, **z2;
{
  if((*z1)->tail == NULL)
    {
      (*z1)->head = (*z2)->head;
      (*z2)->head = NULL;
      (*z1)->tail = (*z2)->tail;
      (*z2)->tail = NULL;
    }
  else
    {
      (*z1)->tail->next = (*z2)->head;
      (*z2)->head = NULL;
      (*z1)->tail = (*z2)->tail;
      (*z2)->tail = NULL;
    }
}

/*
 * Appending to the record's list
 */
static int
append_record (z2, x)	
  struct LIST **z2;
  double x;
{
  if((*z2)->tail == NULL)
    {
      (*z2)->head =
	(struct RECORD *) allocate (sizeof(struct RECORD), PERM);
      (*z2)->head->x = x;
      (*z2)->head->next = NULL;
      (*z2)->tail = (*z2)->head;
    }
  else
    {
      (*z2)->tail->next = 
	(struct RECORD *) allocate (sizeof(struct RECORD), PERM);
      (*z2)->tail->next->x = x;
      (*z2)->tail->next->next = NULL;
      (*z2)->tail = (*z2)->tail->next;
    }
  return (0);
}

/*
 * Redrawing by DURATION_TIME = automatic
 */
static void
redraw_dur_aut (handle)
  int handle;
{
  struct RECORD *scan;
  double global_cnt;
  int n;

  channel[handle].d_time++;
  for (n = 0; n < channel[handle].fields; n++)
    window (handle, n, channel[handle].start_time,
	    channel[handle].member[n].lower,
	    LINEAR_WINDOW_CONSTANT
	    * (channel[handle].start_time
	       + pow(channel[handle].duration_time
		     + channel[handle].d_time,2.)),
	    channel[handle].member[n].upper);
	/*
 	 * Changes world coordinates of the window
	 */

  scan = channel[handle].list->head;
  global_cnt = 0;
  while (scan != NULL)
    {
      global_cnt++;
      for (n = 0; n < channel[handle].fields; n++)
	{
	  if (!strcmp (channel[handle].print_format, "line"))
	    {			
	      if (scan == channel[handle].list->head)
		{
		  channel[handle].member[n].x_cur = global_cnt;
		  channel[handle].member[n].y_cur = scan->x;
		}
	      else
		{
		  move (handle, n, channel[handle].member[n].x_cur,
			channel[handle].member[n].y_cur);
		  draw (handle, n, global_cnt, scan->x);
		  channel[handle].member[n].x_cur = global_cnt;
		  channel[handle].member[n].y_cur = scan->x;
		}
	    }
	  else
	    draw_point (handle, n, global_cnt, scan->x);

	  scan = scan->next;
	}
    }
}

/*
 * Resetting of new begin by START_TIME = automatic
 */
static void
reset_start_aut (handle,x) 
  int handle;
  double x;
{
  struct RECORD *scan;
  int n;

  scan = channel[handle].list->head;
  while (NULL != scan->next)
    {
      channel[handle].list->head = scan->next;
      free_list (&scan);
      scan = channel[handle].list->head;
    }

  channel[handle].start_time += channel[handle].duration_time;

  for (n = 0; n < channel[handle].fields; n++)
    window (handle, n, channel[handle].start_time,
	    channel[handle].member[n].lower,
	    channel[handle].start_time
	    + channel[handle].duration_time,
	    channel[handle].member[n].upper);
  for (n = 0; n < channel[handle].fields; n++)
    {
      channel[handle].member[n].x_cur = channel[handle].global_cnt;
      channel[handle].member[n].y_cur = scan->x;
    }
}

static void
free_list (z3)
  struct RECORD **z3;
{
#if 0  
  free (*z3);
#endif
}

/*
 * Writing to the text (alphanumerical) window
 */
static int
fill_write (handle)
  int handle;
{
  int cnt;
  char str[RECORD_WIDTH];
  char str_tmp[RECORD_WIDTH];
  struct RECORD *z3;
  int y = 9;

  z3 = channel[handle].list_tmp->head;
  d_clear_text( handle);
  while(NULL != z3)
    {
      str[0] = '\0';
      for (cnt = 0; cnt < 5; cnt++)
	{
	  sprintf (str_tmp, "%g", z3->x);
	  strcat (str, str_tmp);
	  strcat (str, " ");
	  z3 = z3->next;
	  if (z3 == NULL)
	    break;
	}
#ifdef XWIN
      channel[handle].item.chars = str;
      channel[handle].item.nchars = strlen
	(channel[handle].item.chars);
/* nemusi byt strlen +1 lebo chcem pocet znakov iba a nie alloc pamate */
      d_draw_text (handle, 0, y);
      y += 15; 
      channel[handle].item.chars = NULL;
#endif
#ifdef MSWIN
      channel[handle].item = allocate (strlen (str) + 1, PERM);
      strcpy (channel[handle].item,str);
      d_draw_text (handle,0,y);
      y += 15;
#if 0
      free (channel[handle].item);
#endif
      channel[handle].item = NULL;
#endif
    }
  return (0);
}

/*
 * Opening of the channel
 * It starts the compiler for parsing of the input string
 */
int
chopen (input_buffer)
  char **input_buffer;
{
  int handle = 0;
  init_ch (*input_buffer);
/*
 * Initialization of the input
 */
  if (yy_channelparse () == -2)
    return(-2); /* Syntax error */

  handle = wind ();
  if ((!strcmp (channel[channel_handle].type, "alpha")) 
/*
 * Checking if alphanumerical window was specified as well
 */
      && (wind_w () == -1))
    return (-1); /* Return if the window was not opened */

  channel_handle++;
  return (handle);
}