Debian Codegraph @

w4/tabledefsubs.c

raw
1 /* File: tabledefsubs.c (formerly known as sprdop.c)
2 * Author: Jean Thierry-Mieg ([email protected])
3 * Copyright (C) J Thierry-Mieg and R Durbin, 1992
4 * -------------------------------------------------------------------
5 * Acedb is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 * or see the on-line version at http://www.gnu.org/copyleft/gpl.txt
19 * -------------------------------------------------------------------
20 * This file is part of the ACEDB genome database package, written by
21 * Richard Durbin (MRC LMB, UK) [email protected], and
22 * Jean Thierry-Mieg (CRBM du CNRS, France) [email protected]
23 *
24 * Description:
25 * Exported functions:
26 * HISTORY:
27 * Last edited: Nov 25 17:58 1999 (fw)
28 * * May 27 16:49 1999 (fw): calculation system rewritten, so
29 * results are written directly to TABLE type
30 * * Mar 18 10:17 1999 (edgrif): Add proper non-valid int/float constants.
31 * Created: Fri Dec 11 13:53:37 1992 (mieg)
32 *-------------------------------------------------------------------
33 */
34
35 /* $Id: tabledefsubs.c,v 1.17 2001/02/01 17:41:43 srk Exp $ */
36
37
38 #include "acedb.h"
39 #include "aceio.h"
40 #include "lex.h"
41 #include "session.h"
42 #include "pick.h"
43 #include "whooks/systags.h"
44 #include "whooks/tags.h"
45 #include "whooks/sysclass.h"
46 #include "query.h"
47 #include "bindex.h"
48 #include "java.h"
49 #include "client.h"
50 #include "spread_.h"
51
52 BITSET_MAKE_BITFIELD /* define bitField for bitset.h ops */
53
54 /************************************************************/
55
56 TABLE* (*externalTableMaker) (char *quer) = 0 ; /* allocation */
57 magic_t SPREAD_MAGIC = "SPREAD_MAGIC";
58
59 /************************************************************/
60
61 typedef struct SpreadCellStruct *SPREADCELL ;
62 struct SpreadCellStruct
63 {
64 BOOL empty ;
65 KEY key, parent, grandParent ;
66 int iCol ;
67 COL *col ;
68 SPREADCELL up; /* previous column */
69 SPREADCELL scFrom; /* from/rightof: has the correct mark */
70 SPREADCELL scGrandParent; /* has the correct obj */
71 BSunit u ;
72 OBJ obj ;
73 BSMARK mark ;
74 } ;
75
76 /*****************************************************/
77
78 static SPREAD spreadCreate (void);
79 static void sortSpreadTable (TABLE *table, int sortCol);
80 static void spreadDestroyCol (COL* c);
81 static void spreadDestroyConditions(SPREAD spread);
82 static void storePrecomputedTable(KEY defKey,
83 char *internalParams,
84 char *commandLineParams,
85 TABLE *table) ;
86 static int scLoopRight (SPREAD spread, SPREADCELL sc) ;
87 static void initSpreadTable (SPREAD spread);
88 static SPREADCELL scAlloc (void);
89 static void scFree (SPREADCELL sc);
90 static BOOL isParamInString (char *cp);
91 static char* getSpreadTableTypes (Array colonnes, STORE_HANDLE handle);
92 static char* getPrecomputationName (KEY tableKey,
93 char *internalParams,
94 char *commandLineParams,
95 STORE_HANDLE handle);
96
97 static Stack scStack = 0 ;
98
99 /******************************************************************
100 ************************* public functions ***********************
101 ******************************************************************/
102
103 SPREAD spreadCreateFromFile (char *filename, const char *spec, char *params)
104 {
105 SPREAD spread = NULL;
106 ACEIN spread_in;
107
108 if (!filename)
109 messcrash ("spreadCreateFromFile() - received NULL filename");
110
111 /* first pass we read without parameter substitution */
112 spread_in = aceInCreateFromFile (filename, spec, NULL, 0);
113 if (spread_in)
114 {
115 spread = spreadCreate();
116
117 spreadReadDef (spread, spread_in, params, FALSE);
118 if (params)
119 spread->extraParameters = strnew(params, 0);
120 spread->filename = strnew(aceInGetURL(spread_in), 0);
121
122 if (arrayMax (spread->colonnes) == 0)
123 /* no column definitions - invalid table-def */
124 spreadDestroy (spread);
125
126 aceInDestroy (spread_in);
127 }
128
129 return spread;
130 } /* spreadCreateFromFile */
131
132 /*****************************************/
133
134 SPREAD spreadCreateFromAceIn (ACEIN spread_in, char *params)
135 {
136 SPREAD spread;
137 char *filename;
138
139 spread = spreadCreate();
140 filename = aceInGetURL(spread_in);
141 if (filename && filename[0] == '/') /* real file-name */
142 spread->filename = strnew(filename, 0);
143
144 spreadReadDef (spread, spread_in, params, FALSE);
145 if (params)
146 spread->extraParameters = strnew(params, 0);
147
148 if (arrayMax (spread->colonnes) == 0)
149 /* no column definitions - invalid table-def */
150 spreadDestroy (spread);
151
152 return spread;
153 } /* spreadCreateFromAceIn */
154
155 /*****************************************/
156
157 SPREAD spreadCreateFromStack (Stack defStack, char *params)
158 {
159 SPREAD spread;
160 ACEIN spread_in;
161
162 spread = spreadCreate();
163
164 spread_in = aceInCreateFromText (stackText(defStack,0), NULL, 0);
165
166 spreadReadDef (spread, spread_in, params, FALSE);
167 if (params)
168 spread->extraParameters = strnew(params, 0);
169
170 if (arrayMax (spread->colonnes) == 0)
171 /* no column definitions - invalid table-def */
172 spreadDestroy (spread);
173
174 aceInDestroy (spread_in);
175
176 return spread;
177 } /* spreadCreateFromStack */
178
179 /*****************************************/
180
181 SPREAD spreadCreateFromObj (KEY tableDefKey, char *params)
182 {
183 SPREAD spread = NULL;
184 ACEIN def_io;
185 Stack objDefStack;
186
187 objDefStack = getDefinitionFromObj (tableDefKey);
188 if (objDefStack)
189 {
190 spread = spreadCreate();
191
192 /* don't substitute parameters */
193 def_io = aceInCreateFromText (stackText(objDefStack,0), NULL, 0);
194
195 spreadReadDef (spread, def_io, params, TRUE);
196 if (params)
197 spread->extraParameters = strnew(params, 0);
198
199 spread->tableDefKey = tableDefKey;
200
201 if (arrayMax (spread->colonnes) == 0)
202 /* no column definitions - invalid table-def */
203 spreadDestroy (spread);
204
205 aceInDestroy (def_io);
206 stackDestroy (objDefStack);
207 }
208
209 return spread;
210 } /* spreadCreateFromObj */
211
212 /*****************************************/
213
214 void uSpreadDestroy (SPREAD spread)
215 /* public function */
216 /* DON'T call directly, use the spreadDestroy() macro */
217
218 {
219 if (!spread)
220 return;
221
222 if (spread->magic != &SPREAD_MAGIC)
223 messcrash ("uSpreadDestroy received corrupt spread->magic");
224
225 if (spread->tableDefKey /* the defs came from an object */
226 && spread->precompute /* where precomute was switched on */
227 && spread->valuesModified /* which has changed ... */
228 && sessionGainWriteAccess())
229 /* ... so we store the new precomputation */
230 {
231 storePrecomputedTable (spread->tableDefKey,
232 spread->paramBuffer,
233 spread->extraParameters,
234 spread->values);
235 }
236
237 tableDestroy (spread->values) ;
238 spreadDestroyConditions(spread) ;
239 spreadDestroyColonnes (spread->colonnes);
240 stackDestroy (spread->comments) ;
241 messfree (spread->filename);
242 messfree (spread->extraParameters);
243
244 spread->magic = 0 ;
245 messfree (spread) ;
246
247 return;
248 } /* uSpreadDestroy */
249
250 /*****************************************************/
251
252 char *spreadGetTitle (SPREAD spread)
253 {
254 return spread->titleBuffer;
255 } /* spreadGetTitle */
256
257
258 Array spreadGetFlipInfo (SPREAD spread)
259 {
260 Array isFlipped;
261 int i;
262
263 isFlipped = arrayCreate(arrayMax(spread->colonnes), BOOL);
264 for (i = 0; i < arrayMax(spread->colonnes); i++)
265 {
266 if (arrp(spread->colonnes, i, COL)->flipped)
267 array(isFlipped, i, BOOL) = TRUE;
268 }
269 return isFlipped;
270 } /* spreadGetFlipInfo */
271
272 Array spreadGetWidths (SPREAD spread)
273 {
274 Array widthsArray;
275 int i;
276
277 widthsArray = arrayCreate(arrayMax(spread->colonnes), int);
278 for (i = 0; i < arrayMax(spread->colonnes); i++)
279 array(widthsArray, i, int) = arrp(spread->colonnes, i, COL)->width;
280
281 return widthsArray;
282 } /* spreadGetWidths */
283
284 /*****************************************************/
285
286 KEYSET spreadGetMasterKeyset (SPREAD spread)
287 /* public function */
288 /* return the alphabetically ordered 1st column of keys */
289 {
290 KEYSET ksFiltered ;
291 KEYSET ksTmp ;
292 COL *masterCol ;
293
294 if (arrayMax(spread->colonnes) == 0)
295 {
296 messout("First define at least one column");
297 return 0;
298 }
299
300 masterCol = arrp(spread->colonnes, 0, COL) ;
301 if (freelower(masterCol->type) != 'k')
302 {
303 messout ("Column 1 should be type Object") ;
304 return 0 ;
305 }
306 if (!masterCol->classe)
307 {
308 messout ("First choose the class of column 1") ;
309 return 0 ;
310 }
311
312 if (*masterCol->conditionBuffer &&
313 ! condCheckSyntax(messprintf(" %s", masterCol->conditionBuffer)))
314 {
315 messout ("First fix the syntax error in column 1's condition") ;
316 return 0 ;
317 }
318
319
320 ksTmp = query (0, messprintf ("FIND %s %s", name(masterCol->classe), masterCol->conditionBuffer));
321
322 ksFiltered = keySetAlphaHeap(ksTmp, keySetMax(ksTmp)) ;
323
324 keySetDestroy (ksTmp) ;
325
326 return ksFiltered ;
327 } /* spreadGetMasterKeyset */
328
329 TABLE* spreadCalculateOverKeySet (SPREAD spread, KEYSET ks, int *lastp)
330 /* public function */
331 /* compute the results table, based with the given
332 * keyset for the first column
333 * this function can be called multiple times on the same keyset,
334 * every time a new table is calculated with new results
335 * for another part of the 1st column :
336 * last - where the calculation got to last time round
337 * RETURNS :
338 * the position in the master column where we have to continue
339 * from next time, or 0 if the calculation finished */
340 {
341 int i, new = 0, mx = keySetMax (ks) ;
342 KEY key ;
343 BOOL stop ; /* fw - ?what?does?this?do? */
344 int nn = 200 ;
345 SPREADCELL sc ;
346 COL *col;
347 int colNum;
348 int last;
349
350 if (lastp)
351 last = *lastp;
352 else
353 last = 0;
354
355 if (last > 0 && spread->sortColonne == 1)
356 stop = TRUE;
357 else
358 stop = FALSE;
359
360 if (externalServer && spread->isActiveKeySet)
361 { /* export active keyset and compute on server side */
362 Stack ss = 0 ;
363 int n;
364 char *cp ;
365 ACEOUT dump_out;
366
367 if (!spreadCheckConditions(spread))
368 return 0 ;
369
370 /* export active keyset was done by the filter call,
371 * compute on server side */
372 ss = stackCreate (100) ;
373 dump_out = aceOutCreateToStack (ss, 0);
374 spreadDumpColonneDefinitions (dump_out, spread->colonnes) ;
375 aceOutDestroy (dump_out);
376 n = stackMark(ss) ;
377 if (!n)
378 { stackDestroy (ss) ;
379 return 0 ;
380 }
381 pushText (ss, "Table -active -a = ") ;
382 cp =stackText (ss, 0) ;
383 catText (ss, cp) ;
384 cp = stackText(ss, n) ;
385 spread->values = externalTableMaker (cp) ;
386 stackDestroy (ss) ;
387 spreadDestroyConditions(spread) ;
388 return 0 ;
389 }
390
391 if (last == 1) last = 0 ;
392 scStack = stackReCreate (scStack, 16) ;
393
394 spread->interrupt = FALSE ;
395
396 /* clean up the text-stacks carried by the columns */
397 for (colNum = 0; colNum < arrayMax(spread->colonnes); colNum++)
398 {
399 col = arrp (spread->colonnes, colNum, COL) ;
400 if (col->text)
401 {
402 col->text = stackReCreate (col->text, 100) ;
403 pushText (col->text, "toto") ; /* avoid zero */
404 }
405 }
406
407 /* kill the old value-table */
408 tableDestroy (spread->values);
409
410 initSpreadTable (spread);
411
412 messStatus ("TableCalculation, F4 to interrupt...");
413
414 if (last > 0 ||
415 spreadCheckConditions(spread)) /* no need to re-check
416 * if last had been set
417 * by a previous run */
418 {
419 for (i = last ; i < mx ; i++)
420 {
421 new = i + 1 ; /* for encore */
422 key = keySet(ks,i) ;
423
424 sc = scAlloc () ;
425 sc->key = key ; sc->u.k = key ;
426 sc->parent = key ;
427
428 scLoopRight (spread, sc) ;
429 scFree(sc) ;
430 if (spread->interrupt)
431 break ;
432 if (stop && --nn <= 0) /* will not stop if starts at 0 */
433 break ;
434 new = 0 ; /* remains 0 at end of loop */
435 }
436 }
437
438 sortSpreadTable (spread->values, spread->sortColonne) ;
439
440 spread->valuesModified = TRUE;
441
442 if (last >= mx)
443 new = 0 ; /* destroy */
444
445 if (!stop || !new)
446 spreadDestroyConditions(spread) ;
447
448 if (lastp)
449 *lastp = new;
450
451 return spread->values ;
452 } /* spreadCalculateOverKeySet */
453
454
455
456 KEYSET spreadFilterKeySet (SPREAD spread, KEYSET ks)
457 /* public function */
458 /* use the KEYSET ks and apply the condition of the master colonne
459 * RETURNS:
460 * a new keyset containing the filtered version of ks */
461 {
462 KEYSET ks1 = 0, ks2 = keySetCreate () ;
463 COL *masterCol ;
464
465 if (arrayMax(spread->colonnes) == 0)
466 messcrash("spreadFilterKeySet() called with empty spread->colonnes");
467
468 masterCol = arrp(spread->colonnes, 0, COL) ;
469 if (!masterCol->classe) /* class of mastercolumn undefined */
470 return ks2 ;
471
472 if (strlen(masterCol->conditionBuffer) > 0 &&
473 ! condCheckSyntax(messprintf(" %s", masterCol->conditionBuffer)))
474 return ks2 ;
475
476 ks1 = query (ks, messprintf("CLASS %s", name(masterCol->classe))) ;
477
478 if (strlen(masterCol->conditionBuffer) > 0)
479 ks2 = query (ks1, masterCol->conditionBuffer) ;
480 else
481 { ks2 = ks1 ; ks1 = 0 ; }
482 keySetDestroy (ks1) ;
483
484 return ks2 ;
485 } /* spreadFilterKeySet */
486
487 /*****************************************************/
488
489 void uSpreadDestroyColonnes (Array colonnes)
490 /* private within SpreadPackage */
491 /* DON'T call directly, use the spreadDestroyColonnes() macro */
492 /* clear all spread->colonnes */
493 {
494 int i;
495
496 if (arrayExists(colonnes))
497 {
498 i = arrayMax(colonnes);
499 while (i--)
500 spreadDestroyCol(arrp(colonnes,i,COL)) ;
501
502 arrayDestroy(colonnes);
503 }
504
505 return;
506 } /* uSpreadDestroyColonnes */
507
508
509 /*****************************************************/
510
511 COL *spreadColInit (SPREAD spread, int colNum)
512 /* private within SpreadPackage */
513 /* returns number of added colonne */
514 {
515 COL *c ;
516
517 c = arrayp(spread->colonnes, colNum, COL) ;
518 c->tagStack = 0 ;
519 c->type = 0 ;
520 c->realType = 0 ;
521 c->colonne = colNum ;
522 c->width = 12 ;
523 c->flipped = FALSE ;
524 c->showType = SHOW_ALL ;
525 c->nonLocal = FALSE ;
526 c->from = 1;
527
528 c->presence = -1; /* init with -1 ... */
529 c->extend = -1; /* ... to force change ... */
530 c->hidden = -1; /* ... in the functions below */
531
532 spreadColSetPresence(spread, colNum, VAL_IS_OPTIONAL);
533 spreadColSetExtend(spread, colNum, COLEXTEND_FROM);
534 spreadColSetHidden(spread, colNum, FALSE);
535
536 memset(c->headerBuffer, 0, 60);
537 memset(c->conditionBuffer, 0, 360);
538 memset(c->tagTextBuffer, 0, 360);
539
540
541 spread->isModified = TRUE ;
542
543 return c;
544 } /* spreadColInit */
545
546
547 /********************************/
548
549 int spreadRemoveColonne (SPREAD spread, int colNum)
550 {
551 COL *c, *c1 ;
552 int i ;
553
554 if (colNum <= 0)
555 return 0; /* cannot remove master-column */
556
557 c = arrayp(spread->colonnes, colNum, COL) ;
558
559 spreadDestroyCol(c) ;
560
561 for (i = colNum, c = arrayp(spread->colonnes, i, COL);
562 i < arrayMax(spread->colonnes) - 1 ; i++)
563 {
564 c1 = c++ ;
565 memcpy(c1, c, sizeof(COL)) ;
566 }
567
568 arrayMax(spread->colonnes) -- ;
569
570
571 for (i = colNum ; i < arrayMax(spread->colonnes) ; i++)
572 {
573 c = arrayp(spread->colonnes, i, COL) ;
574
575 if (c->from > colNum + 1)
576 c->from -- ;
577 else if (c->from == colNum + 1)
578 messout ("You should modify colonne %d, which is derived from the colonne you just destroyed", i + 2) ;
579 }
580
581 spread->isModified = TRUE ;
582
583 return colNum - 1;
584 } /* spreadRemoveColonne */
585
586 /*****************************************************/
587
588 TABLE *spreadCalculateOverWholeClass (SPREAD spread)
589 {
590 KEYSET ks;
591 int n ;
592 char *cp ;
593
594 if (!spread->isModified
595 && spread->precompute
596 && spread->precomputeAvailable
597 && spread->values)
598 /* return the precomputed table */
599 {
600 printf ("returning existing results\n");
601 return spread->values;
602 }
603
604
605 if (!spreadCheckConditions(spread))
606 goto abort;
607
608 if (externalServer) /* compute on server side */
609 {
610 Stack ss = stackCreate (1000);
611 ACEOUT dump_out = aceOutCreateToStack (ss, 0);
612 spreadDumpColonneDefinitions (dump_out, spread->colonnes) ;
613 aceOutDestroy (dump_out);
614 n = stackMark(ss) ;
615 if (!n)
616 { stackDestroy (ss) ;
617 return (TABLE*)NULL ;
618 }
619 pushText (ss, "Table -a = ") ;
620 cp =stackText (ss, 0) ;
621 catText (ss, cp) ;
622 cp = stackText(ss, n) ;
623 spread->values = externalTableMaker (cp) ;
624 stackDestroy (ss) ;
625 return spread->values ;
626 }
627
628 ks = spreadGetMasterKeyset (spread) ;
629 if (!ks)
630 goto abort ;
631
632 /* if we get here we have at least one correct colonne def */
633 spreadCalculateOverKeySet (spread, ks, NULL);
634 keySetDestroy(ks) ;
635
636 return spread->values;
637
638 abort:
639 return (TABLE*)NULL;
640 } /* spreadCalculateOverWholeClass */
641
642 /********************************/
643
644 BOOL spreadCheckConditions(SPREAD spread)
645 /* private within SpreadPackage */
646 {
647 COL *colonne = arrp (spread->colonnes, 0, COL) - 1 ;
648 COND cond = 0 ;
649 int from, icol = 0 , max = arrayMax (spread->colonnes) ;
650 int ctype ;
651
652 while (icol++, colonne++, max--)
653 {
654 colonne->tagCondition = colonne->conditionCondition = 0 ;
655
656 from = colonne->from - 1 ;
657 if (icol > 1)
658 {
659 if (from + 1 >= icol || from < 0)
660 {
661 messout ("From field %d of column %d is wrong",
662 from + 1, icol) ;
663 return FALSE ;
664 }
665 ctype = arrp (spread->colonnes, from, COL)->type ;
666 if (colonne->extend == COLEXTEND_FROM &&
667 ctype != 'k' && ctype != 'K' && ctype != 'n')
668 {
669 messout ("Column %d is computed from column %d which "
670 "is not a list of objects",
671 icol, from + 1) ;
672 return FALSE ;
673 }
674 }
675
676 if (colonne->type == 't' && !colonne->text)
677 { colonne->text = stackReCreate(colonne->text, 100) ;
678 pushText(colonne->text, "\177NULL") ; /* so 0 means undefined */
679 }
680
681 if (colonne->tagStack && stackText(colonne->tagStack, 0))
682 { if (!condConstruct (stackText(colonne->tagStack,0), &cond))
683 { messout ("syntax error in Tag field :%s",
684 stackText(colonne->tagStack, 0)) ;
685 return FALSE ;
686 }
687 colonne->tagCondition = cond ;
688 }
689 else
690 {
691 if (!colonne->tag)
692 cond = 0 ;
693 else if (!condConstruct (name(colonne->tag), &cond))
694 { messout ("syntax error in Tag field :%s",
695 name(colonne->tag)) ;
696 return FALSE ;
697 }
698 colonne->tagCondition = cond ;
699 }
700
701 colonne->condHasParam = isParamInString (colonne->conditionBuffer);
702 colonne->conditionCondition = 0 ;
703 if (!colonne->condHasParam)
704 {
705 /****** no parameters *******/
706 if (colonne->conditionBuffer &&
707 *colonne->conditionBuffer)
708 {
709 if (!condConstruct (colonne->conditionBuffer, &cond))
710 { messout ("syntax error in Tag field :%s",
711 colonne->conditionBuffer) ;
712 return FALSE ;
713 }
714 else
715 colonne->conditionCondition = cond ;
716 }
717 }
718 else
719 {
720 /********** do parameter substitution **********/
721 Stack s ;
722 int i, i1, mine, n1, n2 ;
723 char cc, *cp = colonne->conditionBuffer, *cq ;
724
725 /* must check here that substitution is compatible with syntax */
726 cp-- ;
727 while (*++cp)
728 if (*cp == '%')
729 {
730 cc = *(cp + 1) ;
731 if (cc == '%')
732 { messout("Give a value to %% before running in column %d at:\n%s",
733 icol , cp) ;
734 return FALSE ;
735 }
736 cq = cp + 1; i = 0 ; i1 = 1 ;
737 while (*cq >= '0' && *cq <= '9')
738 { i *= 10 ; i += (*cq - '0') ; cq++ ; }
739 if (i <=0 || i > icol)
740 { messout("Cannot substitute parameter in column %d at:\n%s",
741 icol, cp) ;
742 return FALSE ;
743 }
744 }
745
746 s = stackCreate(500) ;
747 pushText (s, colonne->conditionBuffer) ;
748 n1 = stackMark(s) ; pushText (s, " ") ;
749 for (i = 0 ; i < icol ; i++)
750 { /* use same value as in scMatch */
751 switch (arrp (spread->colonnes, i, COL)->type)
752 {
753 case 'b': case 'k': case'n': case 'K': case't':
754 catText (s," __toto__ ") ;
755 break ;
756 case 'i': case 'f': case 'c':
757 catText (s," 1 ") ;
758 break ;
759 case 'd':
760 catText (s," now ") ;
761 break ;
762 }
763 }
764 mine = freesettext (stackText(s, 0), stackText(s,n1)) ;
765 freespecial (";\n\t/%\\") ; /* No subshells ($ removed) */
766 n2 = stackMark(s) ;
767 while (freecard(mine))
768 pushText (s, freepos()) ;
769 if (!condCheckSyntax(stackText(s,n2)))
770 {
771 messout(
772 "After test substitution, I cannot parse condition of col %d :\n%s",
773 icol, stackText(s,n2)) ;
774 stackDestroy (s) ;
775 return FALSE ;
776 }
777 stackDestroy (s) ;
778 colonne->conditionCondition = 0 ; /* do not register in this case */
779 }
780 }
781
782 return TRUE ;
783 } /* spreadCheckConditions */
784
785
786
787 BOOL spreadGetPrecomputation (SPREAD spread)
788 /* find the stored table results for the tableDefinition object
789 * and return the precomuted table.
790 * no definition have to be loaded into 'spread' at this point.
791 *
792 * PARAMETERS:
793 * spread - the table-maker definitions -
794 * IN: ->tableDefKey needs to be set
795 * OUT: ->values
796 * ->tableResultKey
797 * ->precomputeAvailable
798 * may be initialised
799 *
800 * RETURNS :
801 * TRUE - if spread->tableDefKey is a valid object
802 * which has a 'params'-based precomputation stored
803 * FALSE - no precomputed table could be retrieved
804 */
805 {
806 KEY resultKey = spread->tableResultKey;
807 TABLE *table = 0;
808 char *resultName = 0;
809
810 resultName = getPrecomputationName(spread->tableDefKey,
811 spread->paramBuffer,
812 spread->extraParameters,
813 0);
814 if (!resultName)
815 goto unavailable; /* tableDefKey may be invalid */
816
817 lexword2key(resultName, &resultKey, _VTableResult) ;
818 messfree(resultName);
819
820 if (!resultKey)
821 goto unavailable;
822
823 table = tableGet(resultKey) ;
824
825 if (!table)
826 goto unavailable;
827
828 messout ("Load precompute from TableResult : \"%s\"\n",
829 name(resultKey));
830
831 spread->precomputeAvailable = TRUE;
832 spread->tableResultKey = resultKey;
833
834 if (spread->values)
835 tableDestroy(spread->values);
836 spread->values = table;
837
838 return TRUE;
839
840 unavailable:
841 spread->precomputeAvailable = FALSE;
842 return FALSE;
843 } /* spreadGetPrecomputation */
844
845
846
847
848 /******************************************************************
849 ************************ private functions ***********************
850 ******************************************************************/
851
852 static SPREAD spreadCreate (void)
853 {
854 SPREAD spread ;
855
856 spread = (SPREAD) messalloc(sizeof(struct SpreadStruct));
857
858 spread->magic = &SPREAD_MAGIC;
859 spread->colonnes = arrayCreate(6, COL) ;
860
861 spread->isModified = FALSE; /* definitions changed yet */
862 spread->valuesModified = FALSE; /* results haven't changed yet */
863
864 /* we don't know yet were the definitions will be loaded from */
865 spread->tableDefKey = 0;
866 spread->filename = NULL;
867
868 spread->extraParameters = NULL;
869
870 return spread ;
871 } /* spreadCreate */
872
873 /*****************************************/
874
875 static void spreadDestroyCol (COL* c)
876 /* clear just the specified colonne */
877 {
878 stackDestroy(c->text) ;
879 stackDestroy(c->tagStack) ;
880
881 return;
882 } /* spreadDestroyCol */
883
884 /********************************/
885
886 static void spreadDestroyConditions(SPREAD spread)
887 {
888 COL *colonne = arrp (spread->colonnes, 0, COL) - 1 ;
889 int max = arrayMax (spread->colonnes) ;
890 while (colonne++, max--)
891 { condDestroy (colonne->tagCondition) ;
892 condDestroy (colonne->conditionCondition) ;
893 colonne->tagCondition = colonne->conditionCondition = 0 ;
894 }
895
896 return;
897 } /* spreadDestroyConditions */
898
899 /********************************/
900
901 static void sortSpreadTable (TABLE *table, int sortCol)
902 /* sort the results-table according to the sortCol */
903 {
904 char *sortSpec = (char*)messalloc((table->ncol*2)+1);
905 int i, n;
906
907 if (sortCol == 0)
908 {
909 /* sort left to right */
910 for (i = 0; i < table->ncol; i++)
911 {
912 sortSpec[i*2] = '+';
913 sortSpec[i*2+1] = '1'+i;
914 }
915 }
916 else
917 {
918 /* sortColonne has first sort priority, rest of columns in
919 * left-right order - all ascending */
920 sortSpec[0] = '+';
921 sortSpec[1] = '1'+(sortCol-1);
922 n = 1;
923 for (i = 0; i < table->ncol; i++)
924 {
925 if (i == (sortCol-1))
926 continue;
927 sortSpec[n*2] = '+';
928 sortSpec[n*2+1] = '1'+i;
929 n++;
930 }
931 }
932
933 tableSort(table, sortSpec);
934
935 tableMakeUnique(table);
936
937 messfree(sortSpec);
938
939 return;
940 } /* sortSpreadTable */
941
942
943 /**************************************************************/
944
945 /* self managed calloc */
946
947 static Stack freeScStack = 0 ;
948 static int nScUsed = 0 , nScAlloc = 0 ; /* useful for debug */
949
950 static SPREADCELL scAlloc (void) /* self managed calloc */
951 {
952 static int blocSize = 256 ; /* i need no more SPREADCELL than columns */
953 SPREADCELL p = 0 ;
954 int i ;
955
956 if (!freeScStack)
957 { nScUsed = nScAlloc = 0 ;
958 blocSize = 256 ;
959 freeScStack = stackCreate (sizeof(SPREADCELL)*blocSize) ;
960 }
961 if (stackEmpty (freeScStack))
962 { p = (SPREADCELL) messalloc (blocSize * sizeof (struct SpreadCellStruct)) ;
963 for (i = blocSize ; i-- ; ++p)
964 push (freeScStack,p,SPREADCELL) ;
965 nScAlloc += blocSize ;
966 blocSize *= 2 ;
967 }
968 p = pop (freeScStack, SPREADCELL) ;
969 memset (p, 0, sizeof (struct SpreadCellStruct)) ;
970 nScUsed ++ ;
971
972 return p ;
973 } /* scAlloc */
974
975 /********************************/
976
977 static void scFree (SPREADCELL sc)
978 {
979 if (sc && freeScStack)
980 {
981 if (sc->obj)
982 bsDestroy (sc->obj) ;
983 if (sc->mark) messfree (sc->mark) ;
984 push (freeScStack, sc, SPREADCELL) ;
985 nScUsed-- ;
986 }
987 else
988 invokeDebugger() ;
989
990 return;
991 } /* scFree */
992
993 /*****************************************************/
994
995 static BOOL isParamInString (char *cp)
996 {
997 if (cp) /* find '%' char in the condition-string */
998 while (*cp)
999 {
1000 if (*cp++ == '%')
1001 return TRUE ;
1002 }
1003 return FALSE ;
1004 } /* isParamInString */
1005
1006 /**********************************************************************/
1007 /************************ Actual calculation **************************/
1008 /**********************************************************************/
1009
1010 static BOOL scMatch (SPREADCELL sc, OBJ *objp, KEY key)
1011 {
1012 static Stack s = 0 ;
1013 SPREADCELL cc ;
1014 int mine, n0, n1, n2 ;
1015 BOOL found = FALSE ;
1016 char *cp = 0 ;
1017
1018 switch (sc->col->type)
1019 {
1020 case 'i': case 'c':
1021 cp = strnew(messprintf("%d", sc->u.i), 0) ; key = _Text ;
1022 break ;
1023 case 'f':
1024 cp = strnew(messprintf("%g", sc->u.f), 0) ; key = _Text ;
1025 break ;
1026 case 'd':
1027 cp = strnew(messprintf("\"%s\"", timeShow(sc->u.time)), 0) ; key = _Text ;
1028 break ;
1029 case 't':
1030 cp = strnew(stackText(sc->col->text, sc->u.i), 0) ; key = _Text ;
1031 break ;
1032 default:
1033 break ;
1034 }
1035
1036 s = stackReCreate(s, 256) ;
1037 /* prepare the text of the query with param substituted */
1038 n0 = stackMark(s) ;
1039 pushText(s, sc->col->conditionBuffer) ;
1040 n1 = stackMark(s) ; pushText (s, " ") ; /* push now, so I can then cat */
1041
1042 stackClear (scStack) ;
1043 cc = sc ;
1044 do
1045 { push (scStack, cc, SPREADCELL) ;
1046 } while ((cc = cc->up)) ;
1047
1048 while (!stackEmpty(scStack))
1049 {
1050 catText(s, " ") ;
1051 cc = pop(scStack, SPREADCELL) ;
1052 if (!cc->iCol)
1053 { catText(s, freeprotect (name(cc->u.k))) ;
1054 continue ;
1055 }
1056 if (cc->empty) /* use same value as in checkSyntax */
1057 switch (cc->col->type)
1058 {
1059 case 'i': case 'f': case 'c':
1060 catText (s," 1 ") ;
1061 break ;
1062 case 'd':
1063 catText (s," now ") ;
1064 break ;
1065 case 'b': case 'k': case'n': case 'K': case't':
1066 default:
1067 catText (s," __toto__ ") ;
1068 break ;
1069 }
1070 else
1071 switch (cc->col->type)
1072 {
1073 case 'i': case 'c':
1074 catText (s,messprintf("%d", cc->u.i) ) ;
1075 break ;
1076 case 'f':
1077 catText (s,messprintf("%g", cc->u.f) ) ;
1078 break ;
1079 case 'd':
1080 catText (s,messprintf("\"%s\"", timeShow(cc->u.time))) ;
1081 break ;
1082 case't':
1083 catText(s, freeprotect (stackText(cc->col->text, cc->u.i))) ;
1084 break ;
1085 case 'b': case 'k': case'n': case 'K':
1086 default:
1087 catText(s, freeprotect (name(cc->u.k))) ;
1088 break ;
1089 }
1090
1091 }
1092
1093 /* rely on freesubs to actually substitute the param in the query */
1094 mine = freesettext (stackText(s, n0), stackText(s,n1)) ;
1095 freespecial (";\n\t/%\\") ; /* No subshells ($ removed) */
1096 n2 = stackMark(s) ;
1097 while (freecard(mine))
1098 pushText (s, freepos()) ;
1099
1100 /* evaluate the query */
1101 if (condCheckSyntax(stackText(s,n2)) &&
1102 queryFind21(objp,key,stackText(s,n2), cp))
1103 found = TRUE ;
1104 messfree (cp) ;
1105
1106 return found ;
1107 } /* scMatch */
1108
1109 /*****************************************************/
1110
1111 static BOOL scQuery (SPREAD spread, SPREADCELL s2, SPREADCELL sc)
1112 {
1113 KEY k = 0, direction = _bsRight ;
1114 OBJ obj = s2->obj ;
1115 int nn = 0 , xi, xxi = 0, xxi2 = 0 ;
1116 float xf = 0, xxf = 0, xxf2 = 0 ;
1117 mytime_t xtm , xxtm = 0 ;
1118 char *xt ;
1119
1120 if (!sc->iCol) /* accept column one */
1121 return TRUE ;
1122 if (sc->mark)
1123 {
1124 if (!obj) messcrash("Error 1 in scQuery, sorry") ;
1125 bsGoto (obj, sc->mark) ;
1126 direction = _bsDown ;
1127 }
1128 else
1129 {
1130 if (sc->col->extend == COLEXTEND_RIGHT_OF) /* start inside obj */
1131 {
1132 if (!obj && sc->scFrom->mark) messcrash("Error 1 in scQuery, sorry") ;
1133 if (!obj && !(obj = s2->obj = bsCreate (sc->scGrandParent->key)))
1134 return FALSE ;
1135 bsGoto (obj, sc->scFrom->mark) ;
1136 }
1137 else /* start at root */
1138 {
1139 if (obj)
1140 bsGoto (obj, 0) ;
1141 }
1142 if (!obj && sc->col->tag && !bIndexFind(s2->key, sc->col->tag))
1143 return FALSE ;
1144 if (!queryFind3(sc->col->tagCondition, &(s2->obj), sc->scGrandParent->key))
1145 return FALSE ;
1146 obj = s2->obj ; /* in case we just created obj */
1147 if (!obj && !(obj = s2->obj = bsCreate (sc->scGrandParent->key)))
1148 return FALSE ; /* if queryFind3 did not create the object */
1149 direction = _bsRight ;
1150 }
1151
1152 /* do not iterate inside a column on tags or on arithmetic evaluators */
1153 if (sc->mark &&
1154 (sc->col->type == 'b' || sc->col->showType != SHOW_ALL))
1155 return FALSE ;
1156
1157 while (TRUE)
1158 {
1159 switch (sc->col->type)
1160 {
1161 case 'b':
1162 if (sc->mark ||
1163 !bsGetKeyTags (obj, _bsHere, &k))
1164 return nn ? TRUE : FALSE ;
1165 direction = _bsDown ;
1166 while (k &&
1167 ((class(k) == _VComment) || (class(k) == _VUserSession))
1168 )
1169 { k = 0 ; bsGetKeyTags (obj, _bsHere, &k) ; }
1170 if (!k)
1171 return nn ? TRUE : FALSE ;
1172 sc->u.k = sc->key = k ;
1173 break ;
1174 case 'c': /* count */
1175 if (sc->mark ||
1176 !bsGetKeyTags (obj, direction, &k))
1177 return FALSE ;
1178 direction = _bsDown ; nn = 0 ;
1179 do
1180 {
1181 if (class(k) != _VComment &&
1182 class(k) != _VUserSession) nn++ ;
1183 } while (bsGetKeyTags (obj, direction, &k)) ;
1184 if (!nn) return FALSE ;
1185 sc->u.i = nn ;
1186 goto done ;
1187 break ;
1188 case 'k': case'n': case 'K':
1189 if (!bsGetKeyTags (obj, direction, &k))
1190 return nn ? TRUE : FALSE ;
1191 direction = _bsDown ;
1192 while (k &&
1193 ((class(k) == _VComment) || (class(k) == _VUserSession))
1194 )
1195 { k = 0 ; bsGetKeyTags (obj, _bsHere, &k) ; }
1196 if (!k)
1197 return nn ? TRUE : FALSE ;
1198 sc->u.k = sc->key = k ;
1199 break ;
1200 case 'i':
1201 if (!bsGetData (obj, direction, _Int, &xi))
1202 return nn ? TRUE : FALSE ;
1203 sc->u.i = xi ;
1204 break ;
1205 case 'f':
1206 if (!bsGetData (obj, direction, _Float, &xf))
1207 return nn ? TRUE : FALSE ;
1208 sc->u.f = xf ;
1209 break ;
1210 case 'd':
1211 if (!bsGetData (obj, direction, _DateType, &xtm))
1212 return nn ? TRUE : FALSE ;
1213 sc->u.time = xtm ;
1214 break ;
1215 case 't':
1216 {
1217 /* this is a hack, but it seems that a Text is preceded by a cell
1218 of type Text but with no text inside it, at least sometimes */
1219 encore:
1220 if (!bsGetData (obj, direction, _Text, &xt))
1221 return nn ? TRUE : FALSE ;
1222 if (!xt || !*xt)
1223 { direction = _bsDown ; goto encore ; }
1224 }
1225 sc->u.i = stackMark(sc->col->text) ;
1226 pushText (sc->col->text, xt) ;
1227 break ;
1228 }
1229 /* SHOW_ALL=0, SHOW_MIN, SHOW_MAX, SHOW_AVG */
1230 switch (sc->col->showType)
1231 {
1232 case SHOW_ALL:
1233 goto done ;
1234 case SHOW_MIN:
1235 switch (sc->col->type)
1236 {
1237 case 'i':
1238 if (!nn || xxi > xi) xxi = xi ;
1239 sc->u.i = xxi ; /* reset */
1240 break ;
1241 case 'f':
1242 if (!nn || xxf > xf) xxf = xf ;
1243 sc->u.f = xxf ;
1244 break ;
1245 case 'd':
1246 if (!nn || xxtm > xtm) xxtm = xtm ;
1247 sc->u.time = xxtm ;
1248 break ;
1249 default: /* undefined on other types */
1250 return FALSE ;
1251 }
1252 break ;
1253 case SHOW_MAX:
1254 switch (sc->col->type)
1255 {
1256 case 'i':
1257 if (!nn || xxi < xi) xxi = xi ;
1258 sc->u.i = xxi ; /* reset */
1259 break ;
1260 case 'f':
1261 if (!nn || xxf < xf) xxf = xf ;
1262 sc->u.f = xxf ;
1263 break ;
1264 case 'd':
1265 if (!nn || xxtm < xtm) xxtm = xtm ;
1266 sc->u.time = xxtm ;
1267 break ;
1268 default: /* undefined on other types */
1269 return FALSE ;
1270 }
1271 break ;
1272 case SHOW_AVG:
1273 switch (sc->col->type)
1274 {
1275 case 'i':
1276 if (!nn) xxi = 0 ;
1277 xxi += xi ;
1278 sc->u.i = xxi/(nn + 1) ; /* reset */
1279 break ;
1280 case 'f':
1281 if (!nn) xxf = 0 ;
1282 xxf += xf ;
1283 sc->u.f = xxf/(nn + 1) ;
1284 break ;
1285 default: /* undefined on other types */
1286 return FALSE ;
1287 }
1288 break ;
1289 case SHOW_VAR:
1290 switch (sc->col->type)
1291 {
1292 case 'i':
1293 if (!nn) { xxi = 0 ; xxi2 = 0 ; sc->u.i = 0 ; }
1294 xxi += xi ; xxi2 += xi * xi ;
1295 if (nn > 1)
1296 sc->u.i = (xxi2 -(xxi*xxi/((float)nn)))/(nn - 1.0); /* reset */
1297 break ;
1298 case 'f':
1299 if (!nn) { xxf = 0 ; xxf2 = 0 ; sc->u.f = 0 ; }
1300 xxf += xf ; xxf2 += xf * xf ;
1301 if (nn > 1)
1302 sc->u.f = (xxf2 - xxf*xxf/nn)/(nn - 1.0) ;
1303 break ;
1304 default: /* undefined on other types */
1305 return FALSE ;
1306 }
1307 break ;
1308 case SHOW_SUM:
1309 switch (sc->col->type)
1310 {
1311 case 'i':
1312 if (!nn) xxi = 0 ;
1313 xxi += xi ;
1314 sc->u.i = xxi ; /* reset */
1315 break ;
1316 case 'f':
1317 if (!nn) xxf = 0 ;
1318 xxf += xf ;
1319 sc->u.f = xxf ;
1320 break ;
1321 default: /* undefined on other types */
1322 return FALSE ;
1323 }
1324 break ;
1325 default:
1326 break ;
1327 }
1328 nn++ ;
1329 direction = _bsDown ;
1330 }
1331 done:
1332 sc->mark = bsMark (obj, sc->mark) ;
1333 sc->parent = bsParentKey(obj) ;
1334
1335 return TRUE ;
1336 } /* scQuery */
1337
1338 /*****************************************************/
1339
1340 static BOOL scCondition (SPREAD spread, SPREADCELL sc)
1341 {
1342 BOOL found = FALSE;
1343 BOOL isCond = *sc->col->conditionBuffer ? TRUE : FALSE;
1344 BOOL hasParam = sc->col->condHasParam;
1345 OBJ obj = 0 ;
1346 KEY key ;
1347 char *cp = 0 ;
1348
1349 if (!isCond) return TRUE ;
1350 switch (sc->col->type)
1351 {
1352 case 'b':
1353 return TRUE ;
1354 break ;
1355 case 'n': case 'k':
1356 if (!lexIsInClass(sc->key, sc->col->classe))
1357 return FALSE ;
1358 key = sc->key ;
1359 break ;
1360 case 'K': /* no class check in case K */
1361 key = sc->key ;
1362 break ;
1363 case 'i': case 'c':
1364 cp = strnew(messprintf("%d", sc->u.i),0 ) ; key = _Text ;
1365 break ;
1366 case 'f':
1367 cp = strnew(messprintf("%g", sc->u.f),0 ) ; key = _Text ;
1368 break ;
1369 case 'd':
1370 cp = strnew(messprintf("\"%s\"", timeShow(sc->u.time)),0) ; key = _Text ;
1371 break ;
1372 case 't':
1373 cp = strnew(stackText(sc->col->text, sc->u.i),0) ; key = _Text ;
1374 break ;
1375 default:
1376 key = _Text ;
1377 break ;
1378 }
1379
1380 if (hasParam)
1381 found = scMatch (sc, &obj, key) ;
1382 else
1383 found = sc->col->conditionCondition &&
1384 queryFind31(sc->col->conditionCondition, &obj, key, cp) ;
1385 messfree (cp) ;
1386 bsDestroy (obj) ;
1387
1388 return found ;
1389 } /* scCondition */
1390
1391 /*****************************************************/
1392
1393 static int scLoopDown (SPREAD spread, SPREADCELL s2, SPREADCELL sc)
1394 {
1395 int n = 0;
1396 SpPresenceType mand = sc->col->presence ;
1397
1398 if (!(sc->scFrom && sc->scFrom->empty) &&
1399 !(sc->scFrom && sc->scFrom->col && sc->scFrom->col->nonLocal))
1400 while (TRUE) /* loop on the whole column */
1401 {
1402 if (messIsInterruptCalled())
1403 {
1404 spread->interrupt = TRUE ;
1405 return n ;
1406 }
1407
1408 if (!scQuery (spread, s2, sc))
1409 break ; /* no more target */
1410 if (mand == VAL_MUST_BE_NULL)
1411 return 1 ; /* do not export */
1412 if (scCondition(spread, sc)) /* target acceptable */
1413 n += scLoopRight (spread, sc) ; /* expand and export */
1414 if (sc->col->showType != SHOW_ALL)
1415 break ;
1416 }
1417
1418 if (n == 0 && mand != VAL_IS_REQUIRED) /* null or optional */
1419 {
1420 sc->empty = TRUE ;
1421 n = scLoopRight (spread, sc) ; /* explore once */
1422 }
1423 return n ;
1424 } /* scLoopDown */
1425
1426 /*****************************************************/
1427
1428 static void scExport (SPREAD spread, SPREADCELL sc)
1429 /* finished to calclulate the row for one value in the
1430 * first column - now add the resulting row of values
1431 * to the results table */
1432 {
1433 int i = 0;
1434 COL *colonne ;
1435
1436 stackClear (scStack) ;
1437
1438 for (i = sc->iCol + 1, colonne = arrp (spread->colonnes, sc->iCol, COL) + 1 ;
1439 i < arrayMax(spread->colonnes) ; i++, colonne++)
1440 if (colonne->presence == VAL_IS_REQUIRED)
1441 return ; /* missing non optional column, do not export */
1442
1443
1444 {
1445 COL *colonne;
1446 SPREADCELL cc ;
1447 int rowNum, colNum;
1448
1449 rowNum = tableMax(spread->values);
1450 colNum = spread->values->ncol - 1;
1451 cc = sc;
1452 do {
1453 colonne = cc->col;
1454 if (!colonne)
1455 /* master-column has cc->col == NULL */
1456 colonne = arrayp (spread->colonnes, 0, COL);
1457
1458 if (colonne && colonne->type && !colonne->hidden)
1459 {
1460 if (cc->empty)
1461 tableSetEmpty(spread->values, rowNum, colNum);
1462 else
1463 switch(spread->values->type[colNum])
1464 {
1465 case '0':
1466 tableSetEmpty(spread->values, rowNum, colNum); break;
1467 case 'i':
1468 tableInt (spread->values, rowNum, colNum) = cc->u.i; break;
1469 case 'f':
1470 tableFloat (spread->values, rowNum, colNum) = cc->u.f; break;
1471 case 'k':
1472 tableKey (spread->values, rowNum, colNum) = cc->u.k; break;
1473 case 't':
1474 tableDate (spread->values, rowNum, colNum) = cc->u.time; break;
1475 case 's':
1476 if (stackExists(cc->col->text))
1477 tableSetString (spread->values, rowNum, colNum, stackText(cc->col->text, cc->u.i));
1478 else
1479 tableSetEmpty (spread->values, rowNum, colNum);
1480 break;
1481 default:
1482 messcrash("scExport() - unkown table->type");
1483 }
1484
1485
1486 tableParent(spread->values, rowNum, colNum) = cc->parent;
1487 tableGrandParent(spread->values, rowNum, colNum) = cc->scFrom ? cc->scFrom->key : cc->key ;
1488
1489 colNum--;
1490 }
1491 } while ((cc = cc->up));
1492 }
1493
1494 return;
1495 } /* scExport */
1496
1497 /*****************************************************/
1498
1499 static int scLoopRight (SPREAD spread, SPREADCELL sc)
1500 {
1501 int n = 0, from ;
1502 SPREADCELL sr = 0, s2, srFrom = 0 ;
1503
1504 if(!spread->interrupt &&
1505 (sc->iCol == 0 || (sc->col && sc->col->type)) &&
1506 sc->iCol + 1 < arrayMax(spread->colonnes))
1507 {
1508 /* go to the right */
1509 sr = scAlloc () ;
1510 sr->up = sc ;
1511 sr->iCol = sc->iCol + 1 ;
1512 sr->col = arrp (spread->colonnes, sr->iCol, COL) ;
1513 srFrom = sr ;
1514 from = sr->col->from - 1 ;
1515 while (srFrom->iCol != from && srFrom->up)
1516 srFrom = srFrom->up ;
1517
1518 if (srFrom == sr || srFrom->iCol != from)
1519 { messerror ("Confusion 1 in scLoopRight") ; return 1 ; }
1520 sr->scFrom = srFrom ;
1521 s2 = sr ;
1522 while (s2 && s2->col && s2->col->extend == COLEXTEND_RIGHT_OF)
1523 s2 = s2->scFrom ;
1524 if (s2 && s2->col)
1525 s2 = s2->scFrom ;
1526 sr->scGrandParent = s2 ;
1527
1528 if (sr->col && sr->col->type)
1529 n = scLoopDown (spread, s2, sr) ;
1530
1531 scFree (sr) ;
1532 }
1533 if (n == 0) /* then export self */
1534 {
1535 n = 1 ;
1536 scExport (spread, sc) ;
1537 }
1538
1539 bsDestroy (sc->obj) ; /* release memory NOW */
1540
1541 return n ;
1542 } /* scLoopRight */
1543
1544 /********************************/
1545
1546 static void initSpreadTable (SPREAD spread)
1547 {
1548 COL *col;
1549 int colNum, i;
1550 char *typeString;
1551
1552 typeString = getSpreadTableTypes(spread->colonnes, 0);
1553
1554 spread->values = tableCreate(50, typeString);
1555
1556 messfree (typeString);
1557
1558 i = 0;
1559 for (colNum = 0; colNum < arrayMax(spread->colonnes); colNum++)
1560 {
1561 col = arrp(spread->colonnes, colNum, COL);
1562
1563 if (!col->type)
1564 continue;
1565
1566 if (col->hidden)
1567 continue;
1568
1569 if (col->headerBuffer == NULL)
1570 messcrash("initSpreadTable() - col->headerBuffer == NULL");
1571 tableSetColumnName(spread->values, i, col->headerBuffer);
1572
1573 i++;
1574 }
1575
1576 return;
1577 } /* initSpreadTable */
1578
1579 /******************************************************/
1580
1581 static char* getSpreadTableTypes (Array colonnes, STORE_HANDLE handle)
1582 {
1583 int i, colNum, colMax = arrayMax(colonnes) ;
1584 COL *col ;
1585 char *types = (char*)halloc (colMax + 1, handle) ;
1586
1587 i = 0;
1588 for(colNum = 0 ; colNum < colMax; colNum++)
1589 {
1590 col = arrp(colonnes, colNum, COL) ;
1591 if (!col->hidden)
1592 switch (col->type)
1593 {
1594 case 'c':
1595 case 'i':
1596 types[i++] = 'i' ; break ;
1597
1598 case 'f':
1599 types[i++] = 'f' ; break ;
1600
1601 case 'd':
1602 types[i++] = 't' ; break ; /* dates */
1603
1604 case 'k':
1605 case 'K':
1606 case 'n':
1607 case 'b':
1608 types[i++] = 'k' ; break ;
1609
1610 case 't':
1611 types[i++] = 's' ; break ; /* text */
1612
1613 case 0:
1614 /* type undefined (colonne definition incomplete)
1615 * -> no column in result-table here */
1616 break;
1617
1618 default:
1619 messcrash("getSpreadTableTypes() - invalid col->type");
1620 break;
1621 }
1622 }
1623 types[i] = 0 ;
1624
1625 if (i == 0)
1626 /* this means that there is no colonne with a valid type
1627 * this can happen is all column are hidden.
1628 * Since the table package barfs on zero-column tables
1629 * we fake something up. */
1630 {
1631 types[0] = 't';
1632 types[1] = 0;
1633 }
1634
1635
1636 return types ;
1637 } /* getSpreadTableTypes */
1638
1639
1640 /*****************/
1641
1642 static char* getPrecomputationName (KEY tableKey,
1643 char *internalParams,
1644 char *commandLineParams,
1645 STORE_HANDLE handle)
1646 {
1647 char *cp;
1648 char *fullname = 0;
1649
1650 if ((class(tableKey) == _VTable))
1651 {
1652 Stack s = stackCreate(100);
1653 ACEIN param_in;
1654 int mark;
1655
1656 pushText(s, name(tableKey));
1657 catText (s, "#");
1658
1659 /* construct the full name of the TableResult object where the
1660 * result-table is stored. This name incorporates the parameters */
1661 param_in = aceInCreateFromText
1662 (messprintf("%s %s",
1663 internalParams ? internalParams : "",
1664 commandLineParams ? commandLineParams : ""),
1665 NULL, 0);
1666 aceInSpecial (param_in, "");
1667 aceInCard (param_in);
1668
1669 mark = stackMark(s);
1670 while ((cp = aceInWord(param_in)))
1671 {
1672 if (strncmp(stackText(s, mark), "#", 1) != 0)
1673 catText(s, "#");
1674 catText(s, cp);
1675 mark = stackMark(s);
1676 }
1677 aceInDestroy (param_in);
1678
1679 fullname = strnew(stackText(s, 0), handle);
1680
1681 stackDestroy (s);
1682 /*
1683 fullname = halloc ((strlen(name(tableKey)) +
1684 (params ? strlen(params) : 0) + 8), handle);
1685
1686 cp = fullname ; cq = name(tableKey) ;
1687 while ((*cp++ = *cq++)) ;
1688 if (params) { cp-- ; *cp++ = '#' ;}
1689 cq = params ? params : "" ;
1690 while (*cq == ' ' && *cq) cq++ ;
1691 while(*cq)
1692 {
1693 if (*cq == ' ') { *cp++ = '#' ; while (*cq == ' ') cq++ ; }
1694 *cp++ = *cq++ ;
1695 }
1696 */
1697 }
1698
1699 return fullname;
1700 } /* getPrecomputationName */
1701
1702 /******************************************************/
1703
1704 static void storePrecomputedTable(KEY defKey,
1705 char *internalParams,
1706 char *commandLineParams,
1707 TABLE *table)
1708 {
1709 OBJ obj = 0 ;
1710 KEY _Precompute ;
1711 KEY resultKey;
1712 char *resultName;
1713
1714
1715 if (!defKey ||
1716 (class(defKey) != _VTable))
1717 /* can only store TableResult, if the definitions are
1718 * stored in a Table-object */
1719 return;
1720
1721 if (!table) /* has to have a results table */
1722 return;
1723
1724 if (tableMax(table) == 0)
1725 return; /* don't store empty results-tables */
1726
1727 /* init the TableResult object which will store the precomputed table */
1728 resultName = getPrecomputationName (defKey,
1729 internalParams,
1730 commandLineParams, 0);
1731 lexaddkey(resultName, &resultKey, _VTableResult) ;
1732 messfree(resultName);
1733
1734 /* add a tag to the definition object to remember
1735 * where the precomputed table was stored */
1736 if (!(obj = bsUpdate(defKey)))
1737 return ;
1738
1739 messout ("Store precompute in TableResult : \"%s\"\n",
1740 name(resultKey));
1741
1742 lexaddkey("Precompute", &_Precompute, 0) ;
1743 bsAddKey (obj, _Precompute, resultKey) ;
1744 bsSave (obj) ;
1745
1746 /* write the table-data to the TableResult-object */
1747 tableStore (resultKey, table) ;
1748
1749 return;
1750 } /* storePrecomputedTable */
1751
1752
1753
1754 /******************** eof **********************************/
1755