forked from vermaseren/form
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspectator.c
706 lines (670 loc) · 19.8 KB
/
spectator.c
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
/** @file spectator.c
*
* File contains the code for the spectator files and their control.
*
*/
/* #[ License : */
/*
* Copyright (C) 1984-2022 J.A.M. Vermaseren
* When using this file you are requested to refer to the publication
* J.A.M.Vermaseren "New features of FORM" math-ph/0010025
* This is considered a matter of courtesy as the development was paid
* for by FOM the Dutch physics granting agency and we would like to
* be able to track its scientific use to convince FOM of its value
* for the community.
*
* This file is part of FORM.
*
* FORM 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 3 of the License, or (at your option) any later
* version.
*
* FORM 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 FORM. If not, see <http://www.gnu.org/licenses/>.
*/
/* #] License : */
/*
#[ includes :
*/
#include "form3.h"
/*
#] includes :
#[ Commentary :
We use an array of SPECTATOR structs in AM.SpectatorFiles.
When a spectator is removed this leaves a hole. This means that
we cannot use AM.NumSpectatorFiles but always have to scan up to
AM.SizeForSpectatorFiles which is the size of the array.
An element is in use when it has a name. This is the name of the
expression that is associated with it. There is also the number of
the expression, but because the expressions are frequently renumbered
at the end of a module, we always search for the spectators by name.
The expression number is only valid in the current module.
During execution we use the number of the spectator.
The FILEHANDLE struct is borrowed from the structs for the scratch
files, but we do not keep copies for all workers as with the scratch
files. This brings some limitations (but saves much space). Basically
the reading can only be done by one master or worker. And because
we use the buffer both for writing and for reading we cannot read and
write in the same module.
Processor can see that an expression is to be filled with a spectator
because we replace the compiler buffer number in the prototype by
-specnum-1. Of course, after this filling has taken place we should
make sure that in the next module there is a nonnegative number there.
The input is then obtained from GetFromSpectator instead from GetTerm.
This needed an extra argument in ThreadsProcessor. InParallelProcessor
can figure it out by itself. ParFORM still needs to be treated for this.
The writing is to a single buffer. Hence it needs a lock. It is possible
to give all workers their own buffers (at great memory cost) and merge
the results when needed. That would be friendlier on ParFORM. We ALWAYS
assume that the order of the terms in the spectator file is random.
In the first version there is no compression in the file. This could
change in later versions because both the writing and the reading are
purely sequential. Brackets are not possible.
Currently, ParFORM allows use of spectators only in the sequential
mode. The parallelization is switched off in modules containing
ToSpectator or CopySpectator. Workers never create or access to
spectator files. Their file handles are always -1. We leave the
parallelization of modules with spectators for future work.
#] Commentary :
#[ CoCreateSpectator :
Syntax: CreateSpectator name_of_expr "filename";
*/
int CoCreateSpectator(UBYTE *inp)
{
UBYTE *p, *q, *filename, c, cc;
WORD c1, c2, numexpr = 0, specnum, HadOne = 0;
FILEHANDLE *fh;
while ( *inp == ',' ) inp++;
if ( ( q = SkipAName(inp) ) == 0 || q[-1] == '_' ) {
MesPrint("&Illegal name for expression");
return(1);
}
c = *q; *q = 0;
if ( GetVar(inp,&c1,&c2,ALLVARIABLES,NOAUTO) != NAMENOTFOUND ) {
if ( c2 == CEXPRESSION &&
Expressions[c1].status == DROPSPECTATOREXPRESSION ) {
numexpr = c1;
Expressions[numexpr].status = SPECTATOREXPRESSION;
HadOne = 1;
}
else {
MesPrint("&The name %s has been used already.",inp);
*q = c;
return(1);
}
}
p = q+1;
while ( *p == ',' ) p++;
if ( *p != '"' ) goto Syntax;
p++; filename = p;
while ( *p && *p != '"' ) {
if ( *p == '\\' ) p++;
p++;
}
if ( *p != '"' ) goto Syntax;
q = p+1;
while ( *q && ( *q == ',' || *q == ' ' || *q == '\t' ) ) q++;
if ( *q ) goto Syntax;
cc = *p; *p = 0;
/*
Now we need to: create a struct for the spectator file.
*/
if ( HadOne == 0 )
numexpr = EntVar(CEXPRESSION,inp,SPECTATOREXPRESSION,0,0,0);
fh = AllocFileHandle(1,(char *)filename);
/*
Make sure there is space in the AM.spectatorfiles array
*/
if ( AM.NumSpectatorFiles >= AM.SizeForSpectatorFiles || AM.SpectatorFiles == 0 ) {
int newsize, i;
SPECTATOR *newspectators;
if ( AM.SizeForSpectatorFiles == 0 ) {
newsize = 10;
AM.NumSpectatorFiles = AM.SizeForSpectatorFiles = 0;
}
else newsize = AM.SizeForSpectatorFiles*2;
newspectators = (SPECTATOR *)Malloc1(newsize*sizeof(SPECTATOR),"Spectators");
for ( i = 0; i < AM.NumSpectatorFiles; i++ )
newspectators[i] = AM.SpectatorFiles[i];
for ( ; i < newsize; i++ ) {
newspectators[i].fh = 0;
newspectators[i].name = 0;
newspectators[i].exprnumber = -1;
newspectators[i].flags = 0;
PUTZERO(newspectators[i].position);
PUTZERO(newspectators[i].readpos);
}
AM.SizeForSpectatorFiles = newsize;
if ( AM.SpectatorFiles != 0 ) M_free(AM.SpectatorFiles,"Spectators");
AM.SpectatorFiles = newspectators;
specnum = AM.NumSpectatorFiles++;
}
else {
for ( specnum = 0; specnum < AM.SizeForSpectatorFiles; specnum++ ) {
if ( AM.SpectatorFiles[specnum].name == 0 ) break;
}
AM.NumSpectatorFiles++;
}
PUTZERO(AM.SpectatorFiles[specnum].position);
AM.SpectatorFiles[specnum].name = (char *)(strDup1(inp,"Spectator expression name"));
AM.SpectatorFiles[specnum].fh = fh;
AM.SpectatorFiles[specnum].exprnumber = numexpr;
*p = cc;
return(0);
Syntax:
MesPrint("&Proper syntax is: CreateSpectator,exprname,\"filename\";");
return(-1);
}
/*
#] CoCreateSpectator :
#[ CoToSpectator :
*/
int CoToSpectator(UBYTE *inp)
{
UBYTE *q;
WORD c1, numexpr;
int i;
while ( *inp == ',' ) inp++;
if ( ( q = SkipAName(inp) ) == 0 || q[-1] == '_' ) {
MesPrint("&Illegal name for expression");
return(1);
}
if ( *q != 0 ) goto Syntax;
if ( GetVar(inp,&c1,&numexpr,ALLVARIABLES,NOAUTO) == NAMENOTFOUND ||
c1 != CEXPRESSION ) {
MesPrint("&%s is not a valid expression.",inp);
return(1);
}
if ( Expressions[numexpr].status != SPECTATOREXPRESSION ) {
MesPrint("&%s is not an active spectator.",inp);
return(1);
}
for ( i = 0; i < AM.SizeForSpectatorFiles; i++ ) {
if ( AM.SpectatorFiles[i].name != 0 ) {
if ( StrCmp((UBYTE *)(AM.SpectatorFiles[i].name),(UBYTE *)(inp)) == 0 ) break;
}
}
if ( i >= AM.SizeForSpectatorFiles ) {
MesPrint("&Spectator %s not found.",inp);
return(1);
}
if ( ( AM.SpectatorFiles[i].flags & READSPECTATORFLAG ) != 0 ) {
MesPrint("&Spectator %s: It is not permitted to read from and write to the same spectator in one module.",inp);
return(1);
}
AM.SpectatorFiles[i].exprnumber = numexpr;
Add3Com(TYPETOSPECTATOR,i);
#ifdef WITHMPI
/*
* In ParFORM, ToSpectator has to be executed on the master.
*/
AC.mparallelflag |= NOPARALLEL_SPECTATOR;
#endif
return(0);
Syntax:
MesPrint("&Proper syntax is: ToSpectator,exprname;");
return(-1);
}
/*
#] CoToSpectator :
#[ CoRemoveSpectator :
*/
int CoRemoveSpectator(UBYTE *inp)
{
UBYTE *q;
WORD c1, numexpr;
int i;
SPECTATOR *sp;
while ( *inp == ',' ) inp++;
if ( ( q = SkipAName(inp) ) == 0 || q[-1] == '_' ) {
MesPrint("&Illegal name for expression");
return(1);
}
if ( *q != 0 ) goto Syntax;
if ( GetVar(inp,&c1,&numexpr,ALLVARIABLES,NOAUTO) == NAMENOTFOUND ||
c1 != CEXPRESSION ) {
MesPrint("&%s is not a valid expression.",inp);
return(1);
}
if ( Expressions[numexpr].status != SPECTATOREXPRESSION ) {
MesPrint("&%s is not a spectator.",inp);
return(1);
}
for ( i = 0; i < AM.SizeForSpectatorFiles; i++ ) {
if ( StrCmp((UBYTE *)(AM.SpectatorFiles[i].name),(UBYTE *)(inp)) == 0 ) break;
}
if ( i >= AM.SizeForSpectatorFiles ) {
MesPrint("&Spectator %s not found.",inp);
return(1);
}
sp = AM.SpectatorFiles+i;
Expressions[numexpr].status = DROPSPECTATOREXPRESSION;
if ( sp->fh->handle != -1 ) {
CloseFile(sp->fh->handle);
sp->fh->handle = -1;
remove(sp->fh->name);
}
M_free(sp->fh,"Temporary FileHandle");
M_free(sp->name,"Spectator expression name");
PUTZERO(sp->position);
PUTZERO(sp->readpos);
sp->fh = 0;
sp->name = 0;
sp->exprnumber = -1;
sp->flags = 0;
AM.NumSpectatorFiles--;
return(0);
Syntax:
MesPrint("&Proper syntax is: RemoveSpectator,exprname;");
return(-1);
}
/*
#] CoRemoveSpectator :
#[ CoEmptySpectator :
*/
int CoEmptySpectator(UBYTE *inp)
{
UBYTE *q;
WORD c1, numexpr;
int i;
SPECTATOR *sp;
while ( *inp == ',' ) inp++;
if ( ( q = SkipAName(inp) ) == 0 || q[-1] == '_' ) {
MesPrint("&Illegal name for expression");
return(1);
}
if ( *q != 0 ) goto Syntax;
if ( GetVar(inp,&c1,&numexpr,ALLVARIABLES,NOAUTO) == NAMENOTFOUND ||
c1 != CEXPRESSION ) {
MesPrint("&%s is not a valid expression.",inp);
return(1);
}
if ( Expressions[numexpr].status != SPECTATOREXPRESSION ) {
MesPrint("&%s is not a spectator.",inp);
return(1);
}
for ( i = 0; i < AM.SizeForSpectatorFiles; i++ ) {
if ( StrCmp((UBYTE *)(AM.SpectatorFiles[i].name),(UBYTE *)(inp)) == 0 ) break;
}
if ( i >= AM.SizeForSpectatorFiles ) {
MesPrint("&Spectator %s not found.",inp);
return(1);
}
sp = AM.SpectatorFiles+i;
if ( sp->fh->handle != -1 ) {
CloseFile(sp->fh->handle);
sp->fh->handle = -1;
remove(sp->fh->name);
}
sp->fh->POfill = sp->fh->POfull = sp->fh->PObuffer;
PUTZERO(sp->position);
PUTZERO(sp->readpos);
return(0);
Syntax:
MesPrint("&Proper syntax is: EmptySpectator,exprname;");
return(-1);
}
/*
#] CoEmptySpectator :
#[ PutInSpectator :
We need to use locks! There is only one file.
The code was copied (and modified) from PutOut.
Here we use no compression.
*/
int PutInSpectator(WORD *term,WORD specnum)
{
GETBIDENTITY
WORD i, *p, ret;
LONG RetCode;
SPECTATOR *sp = &(AM.SpectatorFiles[specnum]);
FILEHANDLE *fi = sp->fh;
if ( ( i = *term ) <= 0 ) return(0);
LOCK(fi->pthreadslock);
ret = i;
p = fi->POfill;
do {
if ( p >= fi->POstop ) {
if ( fi->handle < 0 ) {
if ( ( RetCode = CreateFile(fi->name) ) >= 0 ) {
fi->handle = (WORD)RetCode;
PUTZERO(fi->filesize);
PUTZERO(fi->POposition);
}
else {
MLOCK(ErrorMessageLock);
MesPrint("Cannot create spectator file %s",fi->name);
MUNLOCK(ErrorMessageLock);
UNLOCK(fi->pthreadslock);
return(-1);
}
}
SeekFile(fi->handle,&(sp->position),SEEK_SET);
if ( ( RetCode = WriteFile(fi->handle,(UBYTE *)(fi->PObuffer),fi->POsize) ) != fi->POsize ) {
MLOCK(ErrorMessageLock);
MesPrint("Error during spectator write. Disk full?");
MesPrint("Attempt to write %l bytes on file %d at position %15p",
fi->POsize,fi->handle,&(fi->POposition));
MesPrint("RetCode = %l, Buffer address = %l",RetCode,(LONG)(fi->PObuffer));
MUNLOCK(ErrorMessageLock);
UNLOCK(fi->pthreadslock);
return(-1);
}
ADDPOS(fi->filesize,fi->POsize);
p = fi->PObuffer;
ADDPOS(sp->position,fi->POsize);
fi->POposition = sp->position;
}
*p++ = *term++;
} while ( --i > 0 );
fi->POfull = fi->POfill = p;
Expressions[AM.SpectatorFiles[specnum].exprnumber].counter++;
UNLOCK(fi->pthreadslock);
return(ret);
}
/*
#] PutInSpectator :
#[ FlushSpectators :
*/
void FlushSpectators(VOID)
{
SPECTATOR *sp = AM.SpectatorFiles;
FILEHANDLE *fh;
LONG RetCode;
int i;
LONG size;
if ( AM.NumSpectatorFiles <= 0 ) return;
for ( i = 0; i < AM.SizeForSpectatorFiles; i++, sp++ ) {
if ( sp->name == 0 ) continue;
fh = sp->fh;
if ( ( sp->flags & READSPECTATORFLAG ) != 0 ) { /* reset for writing */
sp->flags &= ~READSPECTATORFLAG;
fh->POfill = fh->PObuffer;
if ( fh->handle >= 0 ) {
SeekFile(fh->handle,&(sp->position),SEEK_SET);
fh->POposition = sp->position;
}
continue;
}
if ( fh->POfill <= fh->PObuffer ) continue; /* is clean */
if ( fh->handle < 0 ) { /* File needs to be created */
if ( ( RetCode = CreateFile(fh->name) ) >= 0 ) {
PUTZERO(fh->filesize);
PUTZERO(fh->POposition);
fh->handle = (WORD)RetCode;
}
else {
MLOCK(ErrorMessageLock);
MesPrint("Cannot create spectator file %s",fh->name);
MUNLOCK(ErrorMessageLock);
Terminate(-1);
}
PUTZERO(sp->position);
}
SeekFile(fh->handle,&(sp->position),SEEK_SET);
size = (fh->POfill - fh->PObuffer)*sizeof(WORD);
if ( ( RetCode = WriteFile(fh->handle,(UBYTE *)(fh->PObuffer),size) ) != size ) {
MLOCK(ErrorMessageLock);
MesPrint("Write error synching spectator file. Disk full?");
MesPrint("Attempt to write %l bytes on file %s at position %15p",
size,fh->name,&(sp->position));
MUNLOCK(ErrorMessageLock);
Terminate(-1);
}
fh->POfill = fh->PObuffer;
SeekFile(fh->handle,&(sp->position),SEEK_END);
fh->POposition = sp->position;
}
return;
}
/*
#] FlushSpectators :
#[ CoCopySpectator :
*/
int CoCopySpectator(UBYTE *inp)
{
GETIDENTITY
UBYTE *q, c, *exprname, *p;
WORD c1, c2, numexpr;
int specnum, error = 0;
SPECTATOR *sp;
while ( *inp == ',' ) inp++;
if ( ( q = SkipAName(inp) ) == 0 || q[-1] == '_' ) {
MesPrint("&Illegal name for expression");
return(1);
}
if ( *q == 0 ) goto Syntax;
c = *q; *q = 0;
if ( GetVar(inp,&c1,&c2,ALLVARIABLES,NOAUTO) != NAMENOTFOUND ) {
MesPrint("&%s is the name of an existing variable.",inp);
return(1);
}
numexpr = EntVar(CEXPRESSION,inp,LOCALEXPRESSION,0,0,0);
p = q;
exprname = inp;
*q = c;
while ( *q == ' ' || *q == ',' || *q == '\t' ) q++;
if ( *q != '=' ) goto Syntax;
q++;
while ( *q == ' ' || *q == ',' || *q == '\t' ) q++;
inp = q;
if ( ( q = SkipAName(inp) ) == 0 || q[-1] == '_' ) {
MesPrint("&Illegal name for spectator expression");
return(1);
}
if ( *q != 0 ) goto Syntax;
if ( AM.NumSpectatorFiles <= 0 ) {
MesPrint("&CopySpectator: There are no spectator expressions!");
return(1);
}
sp = AM.SpectatorFiles;
for ( specnum = 0; specnum < AM.SizeForSpectatorFiles; specnum++, sp++ ) {
if ( sp->name != 0 ) {
if ( StrCmp((UBYTE *)(sp->name),(UBYTE *)(inp)) == 0 ) break;
}
}
if ( specnum >= AM.SizeForSpectatorFiles ) {
MesPrint("&Spectator %s not found.",inp);
return(1);
}
sp->flags |= READSPECTATORFLAG;
PUTZERO(sp->fh->POposition);
PUTZERO(sp->readpos);
sp->fh->POfill = sp->fh->PObuffer;
if ( sp->fh->handle >= 0 ) {
SeekFile(sp->fh->handle,&(sp->fh->POposition),SEEK_SET);
}
/*
Now we have:
1: The name of the target expression: numexpr
2: The spectator: sp (or specnum).
Time for some action. We need:
a: Write a prototype to create the expression
b: Signal to Processor that this is a spectator.
We do this by giving a negative compiler buffer number.
*/
{
WORD *OldWork, *w;
POSITION pos;
OldWork = w = AT.WorkPointer;
*w++ = TYPEEXPRESSION;
*w++ = 3+SUBEXPSIZE;
*w++ = numexpr;
AC.ProtoType = w;
AR.CurExpr = numexpr; /* Block expression numexpr */
*w++ = SUBEXPRESSION;
*w++ = SUBEXPSIZE;
*w++ = numexpr;
*w++ = 1;
*w++ = -specnum-1; /* Indicates "spectator" to Processor */
FILLSUB(w)
*w++ = 1;
*w++ = 1;
*w++ = 3;
*w++ = 0;
SeekScratch(AR.outfile,&pos);
Expressions[numexpr].counter = 1;
Expressions[numexpr].onfile = pos;
Expressions[numexpr].whichbuffer = 0;
#ifdef PARALLELCODE
Expressions[numexpr].partodo = AC.inparallelflag;
#endif
OldWork[2] = w - OldWork - 3;
AT.WorkPointer = w;
if ( PutOut(BHEAD OldWork+2,&pos,AR.outfile,0) < 0 ) {
c = *p; *p = 0;
MesPrint("&Cannot create expression %s",exprname);
*p = c;
error = -1;
}
else {
OldWork[2] = 4+SUBEXPSIZE;
OldWork[4] = SUBEXPSIZE;
OldWork[5] = numexpr;
OldWork[SUBEXPSIZE+3] = 1;
OldWork[SUBEXPSIZE+4] = 1;
OldWork[SUBEXPSIZE+5] = 3;
OldWork[SUBEXPSIZE+6] = 0;
if ( PutOut(BHEAD OldWork+2,&pos,AR.outfile,0) < 0
|| FlushOut(&pos,AR.outfile,0) ) {
c = *p; *p = 0;
MesPrint("&Cannot create expression %s",exprname);
*p = c;
error = -1;
}
AR.outfile->POfull = AR.outfile->POfill;
}
OldWork[2] = numexpr;
/*
Seems unnecessary (13-feb-2018)
AddNtoL(OldWork[1],OldWork);
*/
AT.WorkPointer = OldWork;
if ( AC.dumnumflag ) Add2Com(TYPEDETCURDUM)
}
#ifdef WITHMPI
/*
* In ParFORM, substitutions of spectators has to be done on the master.
*/
AC.mparallelflag |= NOPARALLEL_SPECTATOR;
#endif
return(error);
Syntax:
MesPrint("&Proper syntax is: CopySpectator,exprname=spectatorname;");
return(-1);
}
/*
#] CoCopySpectator :
#[ GetFromSpectator :
Note that if we did things right, we do not need a lock for the reading.
*/
WORD GetFromSpectator(WORD *term,WORD specnum)
{
SPECTATOR *sp = &(AM.SpectatorFiles[specnum]);
FILEHANDLE *fh = sp->fh;
WORD i, size, *t = term;
LONG InIn;
if ( fh-> handle < 0 ) {
*term = 0;
return(0);
}
/*
sp->position marks the 'end' of the file: the point where writing should
take place. sp->readpos marks from where to read.
fh->POposition marks where the file is currently positioned.
Note that when we read, we need to
*/
if ( ISZEROPOS(sp->readpos) ) { /* we start reading. Fill buffer. */
FillBuffer:
SeekFile(fh->handle,&(sp->readpos),SEEK_SET);
InIn = ReadFile(fh->handle,(UBYTE *)(fh->PObuffer),fh->POsize);
if ( InIn < 0 || ( InIn & 1 ) ) {
MLOCK(ErrorMessageLock);
MesPrint("Error reading information for %s spectator",sp->name);
MUNLOCK(ErrorMessageLock);
Terminate(-1);
}
InIn /= sizeof(WORD);
if ( InIn == 0 ) { *term = 0; return(0); }
SeekFile(fh->handle,&(sp->readpos),SEEK_CUR);
fh->POposition = sp->readpos;
fh->POfull = fh->PObuffer+InIn;
fh->POfill = fh->PObuffer;
}
if ( fh->POfill == fh->POfull ) { /* not even the size of the term! */
if ( ISLESSPOS(sp->readpos,sp->position) ) goto FillBuffer;
*term = 0;
return(0);
}
size = *fh->POfill++; *t++ = size;
for ( i = 1; i < size; i++ ) {
if ( fh->POfill >= fh->POfull ) {
SeekFile(fh->handle,&(sp->readpos),SEEK_SET);
InIn = ReadFile(fh->handle,(UBYTE *)(fh->PObuffer),fh->POsize);
if ( InIn < 0 || ( InIn & 1 ) ) {
MLOCK(ErrorMessageLock);
MesPrint("Error reading information for %s spectator",sp->name);
MUNLOCK(ErrorMessageLock);
Terminate(-1);
}
InIn /= sizeof(WORD);
if ( InIn == 0 ) {
MLOCK(ErrorMessageLock);
MesPrint("Reading incomplete information for %s spectator",sp->name);
MUNLOCK(ErrorMessageLock);
Terminate(-1);
}
SeekFile(fh->handle,&(sp->readpos),SEEK_CUR);
fh->POposition = sp->readpos;
fh->POfull = fh->PObuffer+InIn;
fh->POfill = fh->PObuffer;
}
*t++ = *fh->POfill++;
}
return(size);
}
/*
#] GetFromSpectator :
#[ ClearSpectators :
Removes all spectators.
In case of .store, the ones that are protected by .global stay.
*/
void ClearSpectators(WORD par)
{
SPECTATOR *sp = AM.SpectatorFiles;
WORD numexpr, c1;
int i;
if ( AM.NumSpectatorFiles > 0 ) {
for ( i = 0; i < AM.SizeForSpectatorFiles; i++, sp++ ) {
if ( sp->name == 0 ) continue;
if ( ( sp->flags & GLOBALSPECTATORFLAG ) == 1 && par == STOREMODULE ) continue;
if ( GetVar((UBYTE *)(sp->name),&c1,&numexpr,ALLVARIABLES,NOAUTO) == NAMENOTFOUND ||
c1 != CEXPRESSION ) {
MesPrint("&%s is not a valid expression.",sp->name);
continue;
}
Expressions[numexpr].status = DROPPEDEXPRESSION;
if ( sp->fh->handle != -1 ) {
CloseFile(sp->fh->handle);
sp->fh->handle = -1;
remove(sp->fh->name);
}
M_free(sp->fh,"Temporary FileHandle");
M_free(sp->name,"Spectator expression name");
PUTZERO(sp->position);
sp->fh = 0;
sp->name = 0;
sp->exprnumber = -1;
sp->flags = 0;
AM.NumSpectatorFiles--;
}
}
}
/*
#] ClearSpectators :
*/