source: trunk/gsdl/lib/display.cpp@ 9593

Last change on this file since 9593 was 9593, checked in by kjdon, 19 years ago

added some x++ -> ++x changes submitted by Emanuel Dejanu

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 51.8 KB
Line 
1/**********************************************************************
2 *
3 * display.cpp -- Context sensitive macro language
4 * Copyright (C) 1999 The New Zealand Digital Library Project
5 *
6 * A component of the Greenstone digital library software
7 * from the New Zealand Digital Library Project at the
8 * University of Waikato, New Zealand.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 *
24 *********************************************************************/
25
26#include "display.h"
27#include "gsdlunicode.h"
28#include "unitool.h"
29#include <assert.h>
30
31// include to get NULL
32#include <stdlib.h>
33
34text_t displayclass::defaultpackage = "Global";
35
36/////////////////////////////////////
37// misc classes
38/////////////////////////////////////
39
40
41// precedencetype defines a 'precedence value' for each parameter
42typedef map<text_t, double, lttext_t> precedencetype;
43
44
45
46inline bool operator==(const mvalue &x, const mvalue &y) {
47 return (x.filename==y.filename && x.value==y.value);
48}
49
50inline bool operator<(const mvalue &x, const mvalue &y) {
51 return (x.filename<y.filename ||
52 (x.filename==y.filename && x.filename<y.filename));
53}
54
55
56
57/////////////////////////////////////
58// stuff for parammacros_t
59/////////////////////////////////////
60
61typedef map<text_t, mvalue, lttext_t> defparammap;
62typedef map<text_t, defparammap, lttext_t> defmacromap;
63typedef map<text_t, defmacromap, lttext_t> defpackagemap;
64
65// all methods in parammacros_t assume the parameters
66// and packages in a standard form and not empty
67class parammacros_t
68{
69protected:
70 defpackagemap macros;
71
72public:
73 typedef defpackagemap::const_iterator const_package_iterator;
74 typedef defpackagemap::size_type package_size_type;
75
76 // setmacro returns 0 if there was no error,
77 // -1 if it redefined a macro
78 // -2 if it hid a Global macro
79 // -3 if it redefined a macro and hid a Global macro
80 // -4 if either a package, macroname, or params were not supplied
81 int setmacro(const text_t &package,
82 const text_t &macroname,
83 const text_t &params,
84 const text_t &filename,
85 const text_t &macrovalue);
86
87 void delmacro(const text_t &package,
88 const text_t &macroname,
89 const text_t &params);
90
91 void clear () {macros.erase(macros.begin(), macros.end());}
92
93 // top level stuff
94 const_package_iterator package_begin () const {return macros.begin();}
95 const_package_iterator package_end () const {return macros.end();}
96 bool package_empty () const {return macros.empty();}
97 package_size_type package_size() const {return macros.size();}
98
99 // methods to find things
100 defmacromap *package_find (const text_t &packagename);
101 defparammap *macro_find (const text_t &packagename,
102 const text_t &macroname);
103 mvalue *parameter_find (const text_t &packagename,
104 const text_t &macroname,
105 const text_t &parametername);
106};
107
108
109// setmacro returns 0 if there was no error,
110// -1 if it redefined a macro
111// -2 if it hid a Global macro
112// -3 if it redefined a macro and hid a Global macro
113// -4 if either a package, macroname, or params were not supplied
114int parammacros_t::setmacro(const text_t &package,
115 const text_t &macroname,
116 const text_t &params,
117 const text_t &filename,
118 const text_t &macrovalue)
119{
120 paramhashtype paramhash;
121
122 defmacromap *macromapptr;
123 defparammap *parammapptr;
124 mvalue *mvalueptr;
125
126 int warning = 0;
127
128 // check to see if we would be hiding a Global macro with the
129 // same name. Note: it doesn't matter what parameters are used
130 // a Global macro will still be hid by a non-Global one with
131 // the same name.
132 if ((package != displayclass::defaultpackage) &&
133 (macro_find (displayclass::defaultpackage, macroname) != NULL))
134 {
135 warning -= 2; // found the macroname
136 }
137
138 // define this macro
139 macromapptr = &(macros[package]); // -- package
140 parammapptr = &((*macromapptr)[macroname]); // -- macro name
141 // -- parameters
142 if ((*parammapptr).find(params) != (*parammapptr).end()) {
143 warning -= 1; // found the parameters
144 }
145 mvalueptr = &((*parammapptr)[params]);
146
147 // -- value
148 (*mvalueptr).filename = filename;
149 (*mvalueptr).value = macrovalue;
150
151 return warning;
152}
153
154void parammacros_t::delmacro(const text_t &package,
155 const text_t &macroname,
156 const text_t &params)
157{
158 // make sure everything was supplied
159 if (package.empty() || macroname.empty() || params.empty()) return;
160
161 // find the package and macroname
162 defparammap *parammapptr = macro_find(package, macroname);
163 if (parammapptr == NULL) return;
164
165 // find the parameters
166 defparammap::iterator parammapit = parammapptr->find(params);
167 if (parammapit == parammapptr->end()) return;
168
169 // finally delete this element
170 parammapptr->erase(parammapit);
171}
172
173defmacromap *parammacros_t::package_find (const text_t &packagename)
174{
175 if (packagename.empty()) return NULL;
176
177 defpackagemap::iterator it = macros.find(packagename);
178 if (it == macros.end()) return NULL;
179
180 return &((*it).second);
181}
182
183defparammap *parammacros_t::macro_find (const text_t &packagename,
184 const text_t &macroname)
185{
186 defmacromap *macromapptr = package_find(packagename);
187 if (macromapptr == NULL || macroname.empty()) return NULL;
188
189 defmacromap::iterator it = (*macromapptr).find(macroname);
190 if (it == (*macromapptr).end()) return NULL;
191
192 return &((*it).second);
193}
194
195mvalue *parammacros_t::parameter_find (const text_t &packagename,
196 const text_t &macroname,
197 const text_t &parametername)
198{
199 defparammap *parammapptr = macro_find(packagename, macroname);
200 if (parammapptr == NULL || parametername.empty()) return NULL;
201
202 defparammap::iterator it = (*parammapptr).find(parametername);
203 if (it == (*parammapptr).end()) return NULL;
204
205 return &((*it).second);
206}
207
208
209
210/////////////////////////////////////
211// stuff for currentmacros_t
212/////////////////////////////////////
213
214typedef map<text_t, mvalue, lttext_t> curmacromap;
215typedef map<text_t, curmacromap, lttext_t> curpackagemap;
216
217// all methods in currentmacros_t assume the parameters
218// and packages in a standard form and not empty
219class currentmacros_t
220{
221protected:
222 curpackagemap macros;
223
224public:
225 typedef curpackagemap::const_iterator const_package_iterator;
226 typedef curpackagemap::size_type package_size_type;
227
228 // setmacro returns 0 if there was no error,
229 // -1 if it redefined a macro
230 // -4 if either a package, or macroname were not supplied
231 int setmacro(const text_t &package,
232 const text_t &macroname,
233 const text_t &filename,
234 const text_t &macrovalue);
235
236 void delmacro(const text_t &package,
237 const text_t &macroname);
238
239 void clear () {macros.erase(macros.begin(), macros.end());}
240
241 // top level stuff
242 const_package_iterator package_begin () const {return macros.begin();}
243 const_package_iterator package_end () const {return macros.end();}
244 bool package_empty () const {return macros.empty();}
245 package_size_type package_size() const {return macros.size();}
246
247 // methods to find things
248 curmacromap *package_find (const text_t &packagename);
249 mvalue *macro_find (const text_t &packagename,
250 const text_t &macroname);
251};
252
253
254// setmacro returns 0 if there was no error,
255// -1 if it redefined a macro
256// -4 if either a package, or macroname were not supplied
257int currentmacros_t::setmacro(const text_t &package,
258 const text_t &macroname,
259 const text_t &filename,
260 const text_t &macrovalue)
261{
262 int warning = 0;
263
264 // make sure everything was supplied
265 if (package.empty() || macroname.empty()) return -4;
266
267 // define this macro
268 curmacromap *macromapptr = &(macros[package]);
269 if ((*macromapptr).find(macroname) != (*macromapptr).end())
270 {
271 warning -= 1; // found the macroname
272 }
273 mvalue *mvalueptr = &((*macromapptr)[macroname]);
274
275 // -- value
276 (*mvalueptr).filename = filename;
277 (*mvalueptr).value = macrovalue;
278
279 return warning;
280}
281
282void currentmacros_t::delmacro(const text_t &package,
283 const text_t &macroname)
284{
285 // make sure everything was supplied
286 if (package.empty() || macroname.empty()) return;
287
288 // find the package
289 curmacromap *macromapptr = package_find(package);
290 if (macromapptr == NULL) return;
291
292 // find the macroname
293 curmacromap::iterator macromapit = macromapptr->find(macroname);
294 if (macromapit == macromapptr->end()) return;
295
296 // finally delete this element
297 macromapptr->erase(macromapit);
298}
299
300curmacromap *currentmacros_t::package_find (const text_t &packagename)
301{
302 if (packagename.empty()) return NULL;
303
304 curpackagemap::iterator it = macros.find(packagename);
305 if (it == macros.end()) return NULL;
306
307 return &((*it).second);
308}
309
310mvalue *currentmacros_t::macro_find (const text_t &packagename,
311 const text_t &macroname)
312{
313 curmacromap *macromapptr = package_find(packagename);
314 if (macromapptr == NULL || macroname.empty()) return NULL;
315
316 curmacromap::iterator it = (*macromapptr).find(macroname);
317 if (it == (*macromapptr).end()) return NULL;
318
319 return &((*it).second);
320}
321
322
323
324
325/////////////////////////////////////
326// support functions
327/////////////////////////////////////
328
329// this calculation of precedence will make one occurance of a high
330// precedence item override many occurances of lower precedence items
331void calcprecedence (const text_t &precedstr, precedencetype &precedhash)
332{
333 text_t param;
334 double multiple = 5.0;
335 double nextprec = multiple;
336
337 text_t::const_iterator here, end;
338
339 // set precedhash to an empty hash
340 precedhash.erase(precedhash.begin(), precedhash.end());
341
342 // iterate through paramstring ignoring space and extracting
343 // out key value pairs.
344 here = precedstr.begin();
345 end = precedstr.end();
346 while (here != end)
347 {
348 // get the next parameter
349 param.clear(); // set param to an empty string
350
351 while (here != end)
352 {
353 if (*here == ',')
354 {
355 // found the end of the parameter
356 ++here;
357 break;
358 }
359 else if (*here == ' ')
360 {
361 // do nothing (ignore spaces)
362 }
363 else
364 {
365 // character in parameter
366 param.push_back (*here); // copy this character
367 }
368
369 ++here;
370 }
371
372 // if a parameter was found insert its value
373 if (!param.empty())
374 {
375 precedhash[param] = nextprec;
376 nextprec *= multiple;
377 }
378 }
379}
380
381
382// decides how specific any given parameter is to the page parameters. Returns
383// negative if any options are different or else the sum of the precedence for
384// the options which match.
385double getspecificness(const paramhashtype &pageparams,
386 const paramhashtype &theseparams,
387 const precedencetype &precedhash)
388{
389 double specificness = 0.0;
390 paramhashtype::const_iterator here, pagevalueit;
391 precedencetype::const_iterator precedit;
392 text_t thesekey, thesevalue, constignore("ignore");
393
394 here = theseparams.begin();
395 while (here != theseparams.end())
396 {
397 thesekey = (*here).first;
398 thesevalue = (*here).second;
399
400 if (thesekey == constignore)
401 {
402 // do nothing, this is a placeholder
403 }
404 else
405 {
406 pagevalueit = pageparams.find(thesekey);
407 if ((pagevalueit != pageparams.end()) &&
408 ((*pagevalueit).second == thesevalue))
409 {
410 // this parameter fits, add its precedence value
411 precedit = precedhash.find(thesekey);
412 if (precedit == precedhash.end())
413 specificness += 1.0; // no precedence defined
414 else
415 specificness += (*precedit).second;
416 }
417 else
418 {
419 return -1.0; // did not match
420 }
421 }
422
423 ++here;
424 }
425
426 return specificness;
427}
428
429
430void splitparams (const text_t &paramstring, paramhashtype &paramhash)
431{
432 text_t key;
433 text_t value;
434
435 text_t::const_iterator here, end;
436
437 // set paramhash to an empty hash
438 paramhash.erase(paramhash.begin(), paramhash.end());
439
440 // iterate through paramstring ignoring space and extracting
441 // out key value pairs.
442 here = paramstring.begin();
443 end = paramstring.end();
444 while (here != end)
445 {
446 // reset key and value
447 key.clear();
448 value.clear();
449
450 // get key
451 while (here != end)
452 {
453 if (*here == ',')
454 {
455 // have a key with no value
456 break;
457 }
458 else if (*here == '=')
459 {
460 // found the end of the key
461 ++here;
462 break;
463 }
464 else if (*here == ' ')
465 {
466 // do nothing (ignore spaces)
467 }
468 else
469 {
470 // character in key
471 key.push_back (*here); // copy this character
472 }
473 ++here;
474 }
475
476 // get value
477 while (here != end)
478 {
479 if (*here == '=')
480 {
481 // multiple values for one key, ignore previous
482 // values
483 value.clear();
484 }
485 else if (*here == ',')
486 {
487 // found the end of the value
488 ++here;
489 break;
490 }
491 else if (*here == ' ')
492 {
493 // do nothing (ignore spaces)
494 }
495 else
496 {
497 // character in value
498 value.push_back (*here); // copy this character
499 }
500 ++here;
501 }
502
503 // if a key was found insert the key/value pair in
504 // the map
505 if (!key.empty())
506 {
507 paramhash[key] = value;
508 }
509 }
510}
511
512
513void joinparams (const paramhashtype &paramhash, text_t &paramstring)
514{
515 //set paramstring to an empty string
516 paramstring.clear();
517
518 //iterate through the paramhash
519 paramhashtype::const_iterator thepair;
520 int firstparam = 1;
521
522 for (thepair=paramhash.begin();thepair!=paramhash.end(); ++thepair)
523 {
524 if (!firstparam) paramstring.push_back(',');
525 firstparam = 0;
526
527 // insert pairs of "key=value"
528 text_t thevalue;
529 paramstring.append((*thepair).first);
530 paramstring.push_back('=');
531 paramstring.append((*thepair).second);
532 }
533}
534
535
536
537/////////////////////////////////////
538// parsing support functions
539/////////////////////////////////////
540
541inline int my_isalpha (unsigned short c)
542{
543 return ((c >= 'A' && c <= 'Z') ||
544 (c >= 'a' && c <= 'z'));
545}
546
547inline int my_isalphanum (unsigned short c)
548{
549 return ((c >= 'A' && c <= 'Z') ||
550 (c >= 'a' && c <= 'z') || (c >='0' && c <= '9'));
551}
552
553
554
555inline unsigned short my_ttnextchar (text_t::const_iterator &here,
556 text_t::const_iterator &end)
557{
558 if (here != end)
559 {
560 ++here;
561 if (here != end) return (*here);
562 }
563 return '\0';
564}
565
566
567
568/////////////////////////////////////
569// methods for display
570/////////////////////////////////////
571
572// public methods for display
573
574displayclass::displayclass ()
575{
576 defaultmacros = new parammacros_t;
577 collectionmacros = new parammacros_t;
578 defaultfiles = new fileinfomap;
579
580 orderparamlist = new paramspeclist;
581 currentmacros = new currentmacros_t;
582
583 outc = NULL;
584
585 logout = NULL;
586
587 /* this is a bit of a hack, but if the displayclass has never seen this
588 parameter before when it does openpage() then macros with this set will
589 always be ignored. And we set collection-specific macros after doing
590 openpage() - jrm */
591 allparams.insert("l=en");
592}
593
594
595displayclass::~displayclass ()
596{
597 delete defaultmacros;
598 delete collectionmacros;
599 delete defaultfiles;
600
601 delete orderparamlist;
602 delete currentmacros;
603}
604
605// setdefaultmacro adds an entry to the list of default macros
606// returns 0 if there was no error,
607// -1 if it redefined a macro
608// -2 if it hid a Global macro
609// -3 if it redefined a macro and hid a Global macro
610// -4 if no macroname was supplied
611int displayclass::setdefaultmacro (const text_t &package, const text_t &macroname,
612 const text_t &mparams, const text_t &macrovalue)
613{
614 return setdefaultmacro (package, macroname, mparams, "memory", macrovalue);
615}
616
617
618
619// as we are using one character lookahead the
620// value of line might be off by one.
621// the input file must be in the utf-8 or unicode format
622// initially for each file isunicode should be set to 0 and
623// bigendian should be set to 1
624// 0 will be returned when the end of the file has been found
625unsigned short my_uni_get (/*unistream*/ifstream &fin, int &line,
626 int &isunicode, int &bigendian) {
627 unsigned short c = 0;
628
629 if (isunicode) {
630 // unicode text
631 // get the next two characters
632 unsigned char c1 = 0, c2 = 0;
633
634 if (!fin.eof()) c1=fin.get();
635 if (!fin.eof()) c2=fin.get();
636 else c1 = 0;
637
638 // if they indicate the order get the next character
639 // otherwise just get these characters
640 if (c1 == 0xff && c2 == 0xfe) {
641 bigendian = 0;
642 c = my_uni_get (fin, line, isunicode, bigendian);
643 } else if (c1 == 0xfe && c2 == 0xff) {
644 bigendian = 1;
645 c = my_uni_get (fin, line, isunicode, bigendian);
646 } else c = (bigendian) ? (c1*256+c2) : (c2*256+c1);
647
648 } else {
649 // utf-8 text
650 // how many characters we get depends on what we find
651 unsigned char c1 = 0, c2 = 0, c3 = 0;
652 while (!fin.eof()) {
653 c1=fin.get();
654 if (c1 == 0xfe || c1 == 0xff) {
655 // switch to unicode
656 isunicode = 1;
657 if (!fin.eof()) c2=fin.get();
658
659 if (c1 == 0xff && c2 == 0xfe) bigendian = 0;
660 else bigendian = 1;
661
662 c = my_uni_get (fin, line, isunicode, bigendian);
663 break;
664
665 } else if (c1 <= 0x7f) {
666 // one byte character
667 c = c1;
668 break;
669
670 } else if (c1 >= 0xc0 && c1 <= 0xdf) {
671 // two byte character
672 if (!fin.eof()) c2=fin.get();
673 c = ((c1 & 0x1f) << 6) + (c2 & 0x3f);
674 break;
675
676 } else if (c1 >= 0xe0 && c1 <= 0xef) {
677 // three byte character
678 if (!fin.eof()) c2=fin.get();
679 if (!fin.eof()) c3=fin.get();
680 c = ((c1 & 0xf) << 12) + ((c2 & 0x3f) << 6) + (c3 & 0x3f);
681 break;
682 }
683
684 // if we get here there was an error in the file, we should
685 // be able to recover from it however, maybe the file is in
686 // another encoding
687 }
688 }
689
690 if (c == '\n') ++line;
691 return c;
692}
693
694
695
696// loads a macro file into the specified macrotable datastructure
697// returns 0 if didn't need to load the file (it was already loaded)
698// 1 if was (re)loaded
699// -1 an error occurred while trying to load the file
700int displayclass::loadparammacros (parammacros_t* macrotable, const text_t &thisfilename) {
701 // convert the filename to a C string
702 char *filenamestr = thisfilename.getcstr();
703 outconvertclass text_t2ascii;
704
705 // see if we need to open this file -- this isn't implemented yet
706
707 // open the file
708 // unistream fin (filenamestr);
709 ifstream fin (filenamestr);
710
711 if (fin.fail()) return -1; // read failed
712
713 text_t package = defaultpackage;
714 int line = 1;
715 int isunicode = 0, bigendian = 1;
716
717 // pre-fetch the next character
718 unsigned short c = my_uni_get(fin, line, isunicode, bigendian);
719
720 text_t macropackage, macroname, macroparameters, macrovalue;
721 int err; // for keeping track of whether an error occurred somewhere
722
723 while (!fin.eof()) {
724 // expect: white space, comment, "package", or macroname
725 if (is_unicode_space(c)) {
726 // found some white-space
727 c = my_uni_get(fin, line, isunicode, bigendian);
728
729 } else if (c == '#') {
730 // found the start of a comment
731 // skip all characters up to the end of the line
732 c = my_uni_get(fin, line, isunicode, bigendian); // skip the '#'
733 while (!fin.eof ()) {
734 if (c == '\n') break;
735 c = my_uni_get(fin, line, isunicode, bigendian);
736 }
737
738 } else if (c == 'p') {
739 // found the start of 'package' (hopefully)
740 // get everything up to the next space
741 text_t tmp;
742 while (!fin.eof() && my_isalpha(c)) {
743 tmp.push_back(c);
744 c = my_uni_get(fin, line, isunicode, bigendian);
745 }
746 // see if we have a package name
747 if (tmp == "package") {
748 // skip all white space
749 while (!fin.eof() && is_unicode_space(c))
750 c = my_uni_get(fin, line, isunicode, bigendian);
751
752 // get the package name
753 tmp.clear(); // init tmp
754 while (!fin.eof() && my_isalphanum(c)) {
755 tmp.push_back(c);
756 c = my_uni_get(fin, line, isunicode, bigendian);
757 }
758 package = tmp;
759 if (package.empty()) package = defaultpackage;
760
761 } else {
762 // error
763 if (logout != NULL) {
764 (*logout) << text_t2ascii << text_t("Expected 'package' on line ") + line + text_t(" of ") + thisfilename + "\n";
765 }
766 }
767
768 } else if (c == '_') {
769 // found the start of a macro (hopefully)
770 c = my_uni_get(fin, line, isunicode, bigendian); // skip the _
771
772 // init variables
773 err = 0;
774 macropackage = package;
775 macroname.clear(); // init macroname
776 macroparameters.clear(); // init macroname
777 macrovalue.clear(); // init macroname
778
779 // get the macro name
780 while ((!fin.eof()) && (!is_unicode_space(c)) &&
781 (c != '\\') && (c != '_') &&(c != ':') &&
782 (macroname.size() < 80)) {
783 macroname.push_back(c);
784 c = my_uni_get(fin, line, isunicode, bigendian);
785 }
786
787 if (c == ':') {
788 // we actually had the macro package
789 c = my_uni_get(fin, line, isunicode, bigendian); // skip :
790 macropackage = macroname;
791 macroname.clear ();
792
793 // get the macro name (honest!)
794 while ((!fin.eof()) && (!is_unicode_space(c)) &&
795 (c != '\\') && (c != '_') &&(c != ':') &&
796 (macroname.size() < 80)) {
797 macroname.push_back(c);
798 c = my_uni_get(fin, line, isunicode, bigendian);
799 }
800 }
801
802 if (!err && c == '_') {
803 c = my_uni_get(fin, line, isunicode, bigendian); // skip the _
804
805 // skip all white space
806 while (!fin.eof() && is_unicode_space(c)) c = my_uni_get(fin, line, isunicode, bigendian);
807 } else if (!err) err = 1;
808
809 // get the macro parameters (optional)
810 if (!err && c == '[') {
811 c = my_uni_get(fin, line, isunicode, bigendian); // skip the [
812 while ((!fin.eof()) && (c != '\n') && (c != '\\') && (c != ']')) {
813 macroparameters.push_back(c);
814 c = my_uni_get(fin, line, isunicode, bigendian);
815 }
816
817 if (c == ']') {
818 c = my_uni_get(fin, line, isunicode, bigendian); // skip the ]
819
820 // skip all white space
821 while (!fin.eof() && is_unicode_space(c)) c = my_uni_get(fin, line, isunicode, bigendian);
822 }
823 else if (!err) err = 2;
824 }
825
826 // get the macro value
827 if (!err && c == '{') {
828 c = my_uni_get(fin, line, isunicode, bigendian); // skip the {
829 while ((!fin.eof()) && (c != '}')) {
830 if (c == '\\') {
831 macrovalue.push_back(c); // keep the '\'
832 c = my_uni_get(fin, line, isunicode, bigendian); // store the *next* value regardless
833 if (!fin.eof()) macrovalue.push_back(c);
834 c = my_uni_get(fin, line, isunicode, bigendian);
835 }
836 macrovalue.push_back(c);
837 c = my_uni_get(fin, line, isunicode, bigendian);
838 }
839
840 if (c == '}') {
841 c = my_uni_get(fin, line, isunicode, bigendian); // skip the }
842
843 // define the macro
844 err = setparammacro (macrotable, macropackage, macroname, macroparameters,
845 thisfilename, macrovalue);
846 if ((err == -1 || err == -3) && logout != NULL) {
847 (*logout) << text_t2ascii << ("Warning: redefinition of _" + package + ":" + macroname + "_[" + macroparameters + "] on line " + line + " of " + thisfilename + "\n");
848
849 } else if (err == -2 && logout != NULL) {
850 // I don't think we really want this warning since it's a very
851 // common occurance - Stefan.
852
853 // (*logout) << text_t2ascii << "Warning: _" <<
854 // package << ":" << macroname << "_[" << macroparameters <<
855 // "] on line ";
856 // (*logout) << line;
857 // (*logout) << text_t2ascii << " of " <<
858 // thisfilename << " hides a Global macro with the same name\n";
859
860 } else if (err == -4 && logout != NULL) {
861 (*logout) << text_t2ascii << text_t("Error: macro name expected on line ") + line + text_t(" of ") + thisfilename + "\n";
862 }
863
864 err = 0; // for the test below
865 }
866 else if (!err) err = 3;
867 }
868 else if (!err) err = 4;
869
870 if (err) {
871 // found an error, skip to the end of the line
872 if (logout != NULL) {
873 text_t message = "Error: ";
874 if (err == 1) message += "'_'";
875 else if (err == 2) message += "']'";
876 else if (err == 3) message += "'}'";
877 else if (err == 4) message += "'{'";
878 message += " expected on line ";
879 message += line;
880 message += " of " + thisfilename + "\n";
881 (*logout) << text_t2ascii << message;
882
883// (*logout) << text_t2ascii << "Error: ";
884// if (err == 1) (*logout) << text_t2ascii << "'_'";
885// else if (err == 2) (*logout) << text_t2ascii << "']'";
886// else if (err == 3) (*logout) << text_t2ascii << "'}'";
887// else if (err == 4) (*logout) << text_t2ascii << "'{'";
888// (*logout) << text_t2ascii << " expected on line ";
889// (*logout) << line ;
890// (*logout) << text_t2ascii << " of " << thisfilename << "\n";
891 }
892 while (!fin.eof ()) {
893 if (c == '\n') break;
894 c = my_uni_get(fin, line, isunicode, bigendian);
895 }
896 }
897
898 } else {
899 // found an error, skip to the end of the line
900 if (logout != NULL) {
901 (*logout) << text_t2ascii << text_t("Error: Unexpected input on line ") + line + text_t(" of ") + thisfilename + "\n";
902 }
903 while (!fin.eof ()) {
904 if (c == '\n') break;
905 c = my_uni_get(fin, line, isunicode, bigendian);
906 }
907
908 }
909 }
910
911 fin.close ();
912
913 // free up memory
914 delete []filenamestr;
915 return 0;
916}
917
918
919// loads a default macro file (if it isn't already loaded)
920// returns 0 if didn't need to load the file (it was already loaded)
921// 1 if was (re)loaded
922// -1 an error occurred while trying to load the file
923int displayclass::loaddefaultmacros (const text_t &thisfilename)
924{
925 return loadparammacros(defaultmacros,thisfilename);
926}
927
928// loads a collection specific macro file
929// returns 0 if didn't need to load the file (it was already loaded)
930// 1 if was (re)loaded
931// -1 an error occurred while trying to load the file
932int displayclass::loadcollectionmacros (const text_t &thisfilename)
933{
934 return loadparammacros(collectionmacros,thisfilename);
935}
936
937
938void displayclass::unloaddefaultmacros () {
939 defaultmacros->clear();
940}
941
942void displayclass::unloadcollectionmacros () {
943 collectionmacros->clear();
944}
945
946// prepares to create a page.
947void displayclass::openpage (const text_t &thispageparams,
948 const text_t &thisprecedence)
949{
950 // init variables
951 currentmacros->clear();
952
953 // reload any default macro files which have changed.
954 if (checkdefaultmacrofiles() < 0)
955 {
956 // print error message
957 }
958
959 setpageparams (thispageparams, thisprecedence);
960}
961
962
963// changes the parameters for the current page.
964void displayclass::setpageparams (text_t thispageparams,
965 text_t thisprecedence)
966{
967 paramhashtype thissplitparams, splittheseparams;
968 precedencetype precedhash;
969 text_tset::iterator text_tsetit;
970 paramspec tmpps;
971
972 precedence = thisprecedence;
973 calcprecedence(thisprecedence, precedhash);
974
975 splitparams(thispageparams, thissplitparams);
976 joinparams(thissplitparams, thispageparams);
977 params = thispageparams;
978
979 // get a list of parameters with their specificness
980 orderparamlist->erase(orderparamlist->begin(), orderparamlist->end());
981 for (text_tsetit=allparams.begin();
982 text_tsetit!=allparams.end();
983 ++text_tsetit)
984 {
985 tmpps.param = (*text_tsetit);
986 splitparams(tmpps.param, splittheseparams);
987 tmpps.spec = getspecificness(thissplitparams, splittheseparams, precedhash);
988 if (tmpps.spec >= 0)
989 {
990 orderparamlist->push_back (tmpps);
991 }
992 }
993
994 // sort the list
995 sort(orderparamlist->begin(), orderparamlist->end());
996
997#if 0 // debugging
998 outconvertclass text_t2ascii;
999
1000 paramspeclist::iterator pshere = orderparamlist->begin();
1001 paramspeclist::iterator psend = orderparamlist->end();
1002 while (pshere != psend)
1003 {
1004 cerr << text_t2ascii << "param=" << (*pshere).param;
1005 cerr << " spec=" << (int)((*pshere).spec) << "\n";
1006 ++pshere;
1007 }
1008#endif
1009}
1010
1011
1012// overrides (or sets) a macro for the current page.
1013// returns 0 if there was no error,
1014// -1 if it redefined a macro
1015// -4 if no macroname was supplied
1016int displayclass::setmacro (const text_t &macroname,
1017 const text_t &package,
1018 const text_t &macrovalue)
1019{
1020 // make sure a macroname was supplied
1021 if (macroname.empty()) return -4;
1022
1023 // make package "Global" if it doesn't point to anything yet
1024 if (package.empty())
1025 return currentmacros->setmacro(defaultpackage, macroname, "memory", macrovalue);
1026
1027 // set the macro
1028 return currentmacros->setmacro(package, macroname, "memory", macrovalue);
1029}
1030
1031int displayclass::setmacroif (const text_t &macro_to_set_and_test,
1032 const text_t &macro_to_set_if_macro_not_set,
1033 const text_t &macroname,
1034 const text_t &package) {
1035
1036 if (macroname.empty()) return -4;
1037 if (macro_to_set_and_test.empty()) return -40;
1038
1039 text_t temp;
1040 expandstring(defaultpackage, macro_to_set_and_test, temp);
1041
1042 if (package.empty()) {
1043 if (temp == macro_to_set_and_test) return currentmacros->setmacro(defaultpackage, macroname, "memory", macro_to_set_if_macro_not_set);
1044 return currentmacros->setmacro(defaultpackage, macroname, "memory", macro_to_set_and_test);
1045 } else {
1046 if (temp == macro_to_set_and_test) return currentmacros->setmacro(package, macroname, "memory", macro_to_set_if_macro_not_set);
1047 return currentmacros->setmacro(package, macroname, "memory", macro_to_set_and_test);
1048
1049 }
1050}
1051
1052void displayclass::expandstring (const text_t &package, const text_t &inputtext,
1053 text_t &outputtext, int recursiondepth)
1054{
1055 text_t macroname, macropackage, macroargs;
1056 text_t::const_iterator tthere = inputtext.begin();
1057 text_t::const_iterator ttend = inputtext.end();
1058 unsigned short c = '\0';
1059 int openbrackets=0;
1060
1061 if (package.empty()) expandstring(defaultpackage, inputtext, outputtext, recursiondepth);
1062
1063 outputtext.clear();
1064 // use one-character lookahead
1065 if (tthere != ttend) c = (*tthere);
1066 while (tthere != ttend)
1067 {
1068 if (c == '\\')
1069 {
1070 // found an escape character
1071 c = my_ttnextchar (tthere, ttend); // skip the escape character
1072 if (tthere != ttend)
1073 {
1074 if (c == 'n') outputtext.push_back('\n');
1075 else if (c == 't') outputtext.push_back('\t');
1076 else outputtext.push_back(c);
1077 }
1078 c = my_ttnextchar (tthere, ttend);
1079
1080 }
1081 else if (c == '_')
1082 {
1083 // might have found the start of a macro
1084
1085 // embedded macros (macros within the names
1086 // of other macros) are no longer supported -- Rodger.
1087
1088 // save the current state (in case we need to back out)
1089 text_t::const_iterator savedtthere = tthere;
1090 unsigned short savedc = c;
1091
1092 macroname.clear();
1093 macropackage = package;
1094 macroargs.clear();
1095
1096 c = my_ttnextchar (tthere, ttend); // skip the '_'
1097
1098 // get the macroname
1099 while (tthere != ttend && (!is_unicode_space(c)) &&
1100 (c != '\\') && (c != '_') &&(c != ':') &&
1101 (macroname.size() < 80))
1102 {
1103 macroname.push_back((unsigned char)c);
1104 c = my_ttnextchar (tthere, ttend);
1105 }
1106
1107 if (c == ':')
1108 {
1109 // we actually had the macro package
1110 c = my_ttnextchar (tthere, ttend);
1111 macropackage = macroname;
1112 macroname.clear ();
1113
1114 // get the macro name (honest!)
1115 while ((tthere != ttend) && (!is_unicode_space(c)) &&
1116 (c != '\\') && (c != '_') &&(c != ':') &&
1117 (macroname.size() < 80))
1118 {
1119 macroname.push_back((unsigned char)c);
1120 c = my_ttnextchar (tthere, ttend);
1121 }
1122 }
1123
1124 if ((tthere != ttend) && (c == '_') &&
1125 (macroname.size() > 0))
1126 {
1127 // found a macro!!!! (maybe)
1128 c = my_ttnextchar (tthere, ttend); // skip the '_'
1129
1130 // get the parameters (if there are any)
1131 if ((tthere != ttend) && (c == '('))
1132 {
1133 c = my_ttnextchar (tthere, ttend); // skip '('
1134 ++openbrackets;
1135 // have to be careful of quotes
1136 unsigned short quote = '\0';
1137 while (tthere != ttend)
1138 {
1139 if (c == '\\')
1140 {
1141 // have an escape character, save escape
1142 // character and next character as well
1143 macroargs.push_back (c);
1144 c = my_ttnextchar (tthere, ttend);
1145
1146 }
1147 else if (quote == '\0')
1148 {
1149 // not in a quote at the moment
1150 if (c == '(')
1151 {
1152 ++openbrackets;
1153//// macroargs.push_back (c);
1154//// c = my_ttnextchar (tthere, ttend);
1155 }
1156 else if (c == ')')
1157 {
1158 --openbrackets;
1159 if (openbrackets==0) {
1160 // found end of the arguments
1161 c = my_ttnextchar (tthere, ttend); // skip ')'
1162 break;
1163 } else {
1164 // nested brackets
1165 macroargs.push_back (c);
1166 c = my_ttnextchar (tthere, ttend);
1167 }
1168 }
1169 else if (c == '\'' || c == '\"')
1170 {
1171 // found a quote
1172 quote = c;
1173 }
1174
1175 }
1176 else
1177 {
1178 // we are in a quote
1179 if (c == quote) quote = '\0';
1180 }
1181
1182 // save this character
1183 if (tthere != ttend) macroargs.push_back (c);
1184 c = my_ttnextchar (tthere, ttend);
1185 } // while (tthere != ttend)
1186 }
1187
1188 //
1189 // we now have the macropackage, macroname, and macroargs
1190 //
1191
1192 // try to expand out this macro
1193 text_t expandedmacro;
1194 if (macro (macroname, macropackage.empty()?defaultpackage:macropackage, macroargs, expandedmacro, recursiondepth))
1195 {
1196 // append the expanded macro
1197 outputtext += expandedmacro;
1198 }
1199 else
1200 {
1201 // wasn't a macro
1202 tthere = savedtthere;
1203 c = savedc;
1204 outputtext.push_back(c); // the '_'
1205 c = my_ttnextchar (tthere, ttend);
1206 }
1207
1208
1209 }
1210 else
1211 {
1212 // wasn't a macro
1213 tthere = savedtthere;
1214 c = savedc;
1215 outputtext.push_back(c); // the '_'
1216 c = my_ttnextchar (tthere, ttend);
1217 }
1218
1219 }
1220 else
1221 {
1222 // nothing of interest
1223 outputtext.push_back(c); // the '_'
1224 c = my_ttnextchar (tthere, ttend);
1225 }
1226 } // end of while (tthere != ttend)
1227}
1228
1229ostream *displayclass::setlogout (ostream *thelogout) {
1230
1231 ostream *oldlogout = logout;
1232 logout = thelogout;
1233 return oldlogout;
1234}
1235
1236// protected methods for display
1237
1238// reloads any default macro files which have changed
1239// returns 0 no errors occurred
1240// -1 an error occurred while trying to load one of the files
1241int displayclass::checkdefaultmacrofiles ()
1242{
1243 // this isn't implemented yet
1244 return 0;
1245}
1246
1247// isdefaultmacro sees if there is an entry in the list of
1248// default macros with the given package and macro name
1249// returns 0 if no macros in the package or in the global package
1250// were found
1251// 1 if no macros in the package were found but a macro
1252// in the global package was found
1253// 2 if a macro in the given package was found
1254int displayclass::isdefaultmacro (const text_t &package, const text_t &macroname) {
1255 // make sure a macroname was supplied
1256 if (macroname.empty()) return 0;
1257
1258 if (!package.empty()) {
1259 // check the given package
1260 if (defaultmacros->macro_find(package, macroname) != NULL) return 2;
1261 }
1262
1263 // check the Global package
1264 if (defaultmacros->macro_find(defaultpackage, macroname) != NULL) return 1;
1265 // not found
1266 return 0;
1267}
1268
1269// havemacro sees if there is an entry in the list of macros or
1270// default macros with the given package and macro name
1271// returns 0 if no macros in the package or in the global package
1272// were found
1273// 1 if no macros in the package were found but a macro
1274// in the global package was found
1275// 2 if a macro in the given package was found
1276// 4 if no macros in the package were found but a macro
1277// in the global package was found in default macros
1278// 8 if a macro in the given package was found in default
1279int displayclass::havemacro(const text_t &package, const text_t &macroname)
1280{
1281 // make sure a macroname was supplied
1282 if (macroname.empty()) return 0;
1283
1284 // make package "Global" if it doesn't point to anything yet
1285 if (package.empty()) {
1286 // check the Global package in current macros list
1287 if (currentmacros->macro_find(defaultpackage, macroname) != NULL) return 1;
1288
1289 // check the Global package in default macros list
1290 if (defaultmacros->macro_find(defaultpackage, macroname) != NULL) return 4;
1291 } else {
1292 // check the given package in current macros list
1293 if (currentmacros->macro_find(package, macroname) != NULL) return 2;
1294
1295 // check the Global package in current macros list
1296 if (currentmacros->macro_find(defaultpackage, macroname) != NULL) return 1;
1297
1298 // check the given package in default macros list
1299 if (defaultmacros->macro_find(package, macroname) != NULL) return 8;
1300
1301 // check the Global package in default macros list
1302 if (defaultmacros->macro_find(defaultpackage, macroname) != NULL) return 4;
1303 }
1304
1305 // nothing was found
1306 return 0;
1307}
1308
1309
1310// setparammacro adds an entry to the list of default macros
1311// returns 0 if there was no error,
1312// -1 if it redefined a macro
1313// -2 if it hid a Global macro
1314// -3 if it redefined a macro and hid a Global macro
1315// -4 if no macroname was supplied
1316int displayclass::setparammacro (parammacros_t* macrotable,
1317 const text_t &package,
1318 const text_t &macroname,
1319 text_t macroparams,
1320 const text_t &filename,
1321 const text_t &macrovalue)
1322{
1323 // make sure a macroname was supplied
1324 if (macroname.empty()) return -4;
1325
1326 // put the parameters in a standard form
1327 paramhashtype paramhash;
1328 if (macroparams.empty()) macroparams = "ignore=yes";
1329 splitparams (macroparams, paramhash);
1330 joinparams (paramhash, macroparams);
1331
1332 // remember these parameters
1333 allparams.insert (macroparams);
1334
1335 // remember this filename (this part isn't finished yet -- Rodger).
1336
1337 // set the macro
1338 if (package.empty()) {
1339 return macrotable->setmacro(defaultpackage, macroname, macroparams, filename, macrovalue);
1340 } else {
1341 return macrotable->setmacro(package, macroname, macroparams, filename, macrovalue);
1342 }
1343}
1344
1345// setdefaultmacro adds an entry to the list of default macros
1346// returns 0 if there was no error,
1347// -1 if it redefined a macro
1348// -2 if it hid a Global macro
1349// -3 if it redefined a macro and hid a Global macro
1350// -4 if no macroname was supplied
1351int displayclass::setdefaultmacro (const text_t &package,
1352 const text_t &macroname,
1353 text_t macroparams,
1354 const text_t &filename,
1355 const text_t &macrovalue)
1356{
1357 return setparammacro(defaultmacros,package,macroname,
1358 macroparams,filename,macrovalue);
1359}
1360
1361// public function
1362int displayclass::setcollectionmacro(const text_t &package,
1363 const text_t &macroname,
1364 text_t mparams,
1365 const text_t &macrovalue)
1366{
1367 return setcollectionmacro(package, macroname, mparams, "memory", macrovalue);
1368}
1369
1370// setcollectionmacro adds an entry to the list of collection specific macros
1371// returns 0 if there was no error,
1372// -1 if it redefined a macro
1373// -2 if it hid a Global macro
1374// -3 if it redefined a macro and hid a Global macro
1375// -4 if no macroname was supplied
1376int displayclass::setcollectionmacro (const text_t &package,
1377 const text_t &macroname,
1378 text_t mparams,
1379 const text_t &filename,
1380 const text_t &macrovalue)
1381{
1382 return setparammacro(collectionmacros,package,macroname,mparams,
1383 filename,macrovalue);
1384}
1385
1386
1387// evaluates a boolean expression
1388// returns false if expr equals "" or "0".
1389// otherwise returns true *unless* expr is
1390// format "XXXX" eq/ne "dddd" in which case
1391// the two quoted strings are compared
1392bool displayclass::boolexpr (const text_t &package, const text_t &expr, int recursiondepth) {
1393
1394 if (expr.empty()) return false;
1395
1396 text_t expexpr;
1397 if (package.empty()) {
1398 expandstring (defaultpackage, expr, expexpr, recursiondepth);
1399 } else {
1400 expandstring (package, expr, expexpr, recursiondepth);
1401 }
1402 if (expexpr.empty() || expexpr == "0") return false;
1403 if (expr[0] != '\"') return true;
1404
1405 // don't use expexpr while separating quoted parts of
1406 // expression just in case expanded out macros contain
1407 // quotes
1408 text_t::const_iterator here = expr.begin();
1409 text_t::const_iterator end = expr.end();
1410
1411 int quotecount = 0;
1412 text_t string1, expstring1;
1413 text_t string2, expstring2;
1414 text_t op;
1415 text_t combineop;
1416 bool result = false; // an empty string is false
1417 bool result2 = false;
1418
1419 while (here != end) {
1420 // get a comparison
1421 quotecount = 0;
1422 string1.clear();
1423 string2.clear();
1424 op.clear();
1425 while (here != end) {
1426 if (*here == '"') ++quotecount;
1427 else if (quotecount == 1) string1.push_back(*here);
1428 else if ((quotecount == 2) && !is_unicode_space (*here))
1429 op.push_back(*here);
1430 else if (quotecount == 3) string2.push_back(*here);
1431 else if (quotecount >= 4) break;
1432 ++here;
1433 }
1434
1435 expandstring (package, string1, expstring1, recursiondepth);
1436 expandstring (package, string2, expstring2, recursiondepth);
1437
1438 // get next result
1439 result2 = true; // any badly formatted string will return true
1440 if (op == "eq") result2 = (expstring1 == expstring2);
1441 else if (op == "ne") result2 = (expstring1 != expstring2);
1442 else if (op == "gt") result2 = (expstring1 > expstring2);
1443 else if (op == "ge") result2 = (expstring1 >= expstring2);
1444 else if (op == "lt") result2 = (expstring1 < expstring2);
1445 else if (op == "le") result2 = (expstring1 <= expstring2);
1446 else if (op == "==") result2 = (expstring1.getint() == expstring2.getint());
1447 else if (op == "!=") result2 = (expstring1.getint() != expstring2.getint());
1448 else if (op == ">") result2 = (expstring1.getint() > expstring2.getint());
1449 else if (op == ">=") result2 = (expstring1.getint() >= expstring2.getint());
1450 else if (op == "<") result2 = (expstring1.getint() < expstring2.getint());
1451 else if (op == "<=") result2 = (expstring1.getint() <= expstring2.getint());
1452
1453 // combine the results
1454 if (combineop == "&&") result = (result && result2);
1455 else if (combineop == "||") result = (result || result2);
1456 else result = result2;
1457
1458 // get next combination operator
1459 combineop.clear();
1460 while (here != end && *here != '"') {
1461 if (!is_unicode_space(*here)) combineop.push_back(*here);
1462 ++here;
1463 }
1464 }
1465
1466 return result;
1467}
1468
1469
1470mvalue* displayclass::macro_find (parammacros_t* macrotable,
1471 const text_t &packagename,
1472 const text_t &macroname)
1473{
1474 mvalue *macrovalue = NULL;
1475
1476 // next look in the named parammacros for the named package
1477 if (macrotable->macro_find (packagename, macroname) != NULL)
1478 {
1479 paramspeclist::iterator paramhere, paramend;
1480
1481 paramhere = orderparamlist->begin(); paramend = orderparamlist->end();
1482 while ((macrovalue == NULL) && (paramhere != paramend))
1483 {
1484 macrovalue = macrotable->parameter_find(packagename, macroname,
1485 (*paramhere).param);
1486 ++paramhere;
1487 }
1488 }
1489
1490 return macrovalue;
1491}
1492
1493
1494// (recursively) expand out a macro
1495// returns true if the macro was found
1496// false if the macro was not found
1497// macropackage should never be empty
1498bool displayclass::macro (const text_t &macroname,
1499 const text_t &macropackage,
1500 const text_t &macroparam,
1501 text_t &outputtext,
1502 int recursiondepth)
1503{
1504 assert(!macropackage.empty()); //???
1505 //outconvertclass text_t2ascii;
1506 text_tlist splitmacroparam;
1507 text_t::const_iterator hereit, endit;
1508 text_t aparam;
1509 unsigned short c = '\0', quote = '\0';
1510
1511 // cerr << "r: " << recursiondepth << "\n";
1512 // check for deep recursion
1513 if (recursiondepth >= MAXRECURSIONDEPTH)
1514 {
1515 if (logout != NULL)
1516 (*logout) << "Warning: deep recursion, limiting macro expansion\n";
1517 return false;
1518 }
1519
1520 // check the macropackage
1521 //if (macropackage.empty()) macropackage = "Global";
1522
1523 // get the parameters (but don't expand them)
1524 if (macroparam.size() > 0) {
1525 // set up for loop
1526 hereit = macroparam.begin();
1527 endit = macroparam.end();
1528 if (hereit != endit) c = (*hereit);
1529 int openbrackets=0; // keep track of bracket level eg _If_(1, _macro_(arg))
1530 // this allows commas inside brackets (as arguments)
1531 while (hereit != endit) {
1532 // get the next parameter
1533 aparam.clear();
1534 quote = '\0'; // not-quoted unless proven quoted
1535
1536 // ignore initial whitespace
1537 while ((hereit!=endit)&&is_unicode_space(c)) c=my_ttnextchar(hereit,endit);
1538
1539 // look for the end of the parameter
1540 while (hereit != endit) {
1541 if (c == '\\') {
1542 // found escape character, also get next character
1543 aparam.push_back(c);
1544 c = my_ttnextchar (hereit, endit);
1545 if (hereit != endit) aparam.push_back(c);
1546
1547 } else if (quote=='\0' && (c=='\'' /*|| c=='"'*/)) {
1548 // found a quoted section
1549 quote = c;
1550 if (openbrackets>0) aparam.push_back(c);
1551
1552 } else if (quote!='\0' && c==quote) {
1553 // found the end of a quote
1554 quote = '\0';
1555 if (openbrackets<0) aparam.push_back(c);
1556
1557 } else if (quote=='\0' && (c==',' || c==')') && openbrackets==0) {
1558 // found the end of a parameter
1559 c = my_ttnextchar (hereit, endit);
1560 break;
1561
1562 } else if (c=='(') {
1563 aparam.push_back(c);
1564 ++openbrackets;
1565 } else if (c==')') {
1566 --openbrackets;
1567 aparam.push_back(c);
1568 } else {
1569 // ordinary character
1570 aparam.push_back(c);
1571 }
1572
1573 c = my_ttnextchar (hereit, endit);
1574 }
1575
1576
1577 // add this parameter to the list
1578 splitmacroparam.push_back(aparam);
1579 }
1580 }
1581
1582 if (macroname == "If") {
1583 // get the condition, then clause and else clause
1584 text_tlist::iterator paramcond = splitmacroparam.begin();
1585 text_tlist::iterator paramend = splitmacroparam.end();
1586 text_tlist::iterator paramthen = paramend;
1587 text_tlist::iterator paramelse = paramend;
1588
1589 if (paramcond != paramend) {
1590 paramthen = paramcond;
1591 ++paramthen;
1592 }
1593 if (paramthen != paramend) {
1594 paramelse = paramthen;
1595 ++paramelse;
1596 }
1597
1598 // will always output something
1599 outputtext.clear();
1600
1601
1602 // expand out the first parameter
1603 if (paramcond != paramend) {
1604 /// text_t tmpoutput;
1605 /// expandstring (macropackage, *paramcond, tmpoutput, recursiondepth+1);
1606 /// lc (tmpoutput);
1607 // test the expanded string
1608 if (boolexpr (macropackage, *paramcond, recursiondepth+1)) {
1609 //(tmpoutput.size()) && (tmpoutput != "false") && (tmpoutput != "0")) {
1610 // true
1611 if (paramthen != paramend)
1612 expandstring (macropackage, *paramthen, outputtext, recursiondepth+1);
1613 } else {
1614 // false
1615 if (paramelse != paramend)
1616 expandstring (macropackage, *paramelse, outputtext, recursiondepth+1);
1617 }
1618 }
1619
1620 return true;
1621 }
1622
1623 // try and find this macro
1624
1625 // this list might be replaced by something a little more
1626 // sophisticated in the future.
1627 text_tlist packagelist;
1628 packagelist.push_back (macropackage);
1629 packagelist.push_back ("Global");
1630
1631 text_tlist::iterator packagehere = packagelist.begin();
1632 text_tlist::iterator packageend = packagelist.end();
1633 mvalue *macrovalue = NULL;
1634 while ((macrovalue == NULL) && (packagehere != packageend))
1635 {
1636 // first look in the currentmacros
1637 macrovalue = currentmacros->macro_find(*packagehere, macroname);
1638 if (macrovalue == NULL)
1639 macrovalue = currentmacros->macro_find("Style", macroname);
1640
1641 // look in the collection specific macros
1642 if (macrovalue == NULL) {
1643 macrovalue = macro_find(collectionmacros,*packagehere,macroname);
1644 }
1645
1646 // and again in the collection specific package "Style"
1647 if (macrovalue == NULL) {
1648 macrovalue = macro_find(collectionmacros,(char*)"Style",macroname);
1649 }
1650
1651 // look in the default macros
1652 if (macrovalue == NULL) {
1653 macrovalue = macro_find(defaultmacros,*packagehere,macroname);
1654 }
1655
1656 // and again in the package "Style"
1657 if (macrovalue == NULL) {
1658 macrovalue = macro_find(defaultmacros,(char*)"Style",macroname);
1659 }
1660
1661 if (macrovalue == NULL) ++packagehere;
1662 }
1663
1664 if (macrovalue != NULL)
1665 {
1666 // we found a macro
1667
1668 // replace all the option macros (_1_, _2_, ...) in the value of this macro
1669 // and decide if we need to expand the value of this macro
1670 bool needsexpansion = false;
1671 text_t tempmacrovalue;
1672 tempmacrovalue.clear();
1673 hereit = macrovalue->value.begin();
1674 endit = macrovalue->value.end();
1675 if (hereit != endit) c = (*hereit);
1676 while (hereit != endit)
1677 {
1678 if (c == '\\')
1679 {
1680 // found an escape character
1681 needsexpansion = true;
1682 tempmacrovalue.push_back(c);
1683 c = my_ttnextchar (hereit, endit);
1684 if (hereit != endit) tempmacrovalue.push_back(c);
1685 c = my_ttnextchar (hereit, endit);
1686 }
1687 else if (c == '_')
1688 {
1689 text_t::const_iterator savedhereit = hereit;
1690
1691 // attempt to get a number
1692 int argnum = 0;
1693 unsigned short digc = my_ttnextchar (hereit, endit);
1694 while (digc >= '0' && digc <= '9') {
1695 argnum = argnum*10 + digc - '0';
1696 digc = my_ttnextchar (hereit, endit);
1697 }
1698 if (digc == '_' && argnum > 0) {
1699 // found an option macro, append the appropriate text
1700 text_tlist::iterator ttlisthere = splitmacroparam.begin();
1701 text_tlist::iterator ttlistend = splitmacroparam.end();
1702 while ((argnum > 1) && (ttlisthere != ttlistend)) {
1703 --argnum;
1704 ++ttlisthere;
1705 }
1706 if (ttlisthere != ttlistend) {
1707 tempmacrovalue.append(*ttlisthere);
1708 if (findchar((*ttlisthere).begin(), (*ttlisthere).end(), '_') != (*ttlisthere).end())
1709 needsexpansion = true;
1710 }
1711
1712 c = my_ttnextchar (hereit, endit);
1713
1714 } else {
1715 // wasn't a option macro
1716 needsexpansion = true;
1717 tempmacrovalue.push_back(c);
1718 hereit = savedhereit;
1719 c = my_ttnextchar (hereit, endit);
1720 }
1721 }
1722 else
1723 {
1724 tempmacrovalue.push_back(c);
1725 c = my_ttnextchar (hereit, endit);
1726 }
1727 }
1728
1729 // recursively replace this macro (if we need to)
1730 if (needsexpansion) {
1731 expandstring (*packagehere, tempmacrovalue, outputtext, recursiondepth+1);
1732 } else {
1733 outputtext += tempmacrovalue;
1734 }
1735
1736 return true;
1737 }
1738
1739 // GRB: deal with remaining code properly as it faults
1740 return false;
1741 outconvertclass text_t2ascii;
1742 if (logout != NULL) {
1743 (*logout) << text_t2ascii << "Warning: _" <<
1744 macropackage << ":" << macroname << "_ not found\n";
1745 }
1746
1747 return false; // didn't find the macro
1748}
1749
1750
1751void displayclass::printdefaultmacros ()
1752{
1753 defpackagemap::const_iterator packagemapit;
1754 defmacromap::const_iterator macromapit;
1755 defparammap::const_iterator parammapit;
1756 const mvalue *mvalueptr;
1757 outconvertclass text_t2ascii;
1758
1759 text_t packagename, macroname, macroparam;
1760
1761 for (packagemapit = defaultmacros->package_begin();
1762 packagemapit != defaultmacros->package_end();
1763 ++packagemapit)
1764 {
1765 packagename = (*packagemapit).first;
1766 // cerr << "***package : \"" << packagename << "\"\n";
1767
1768 for (macromapit = (*packagemapit).second.begin();
1769 macromapit != (*packagemapit).second.end();
1770 ++macromapit)
1771 {
1772 macroname = (*macromapit).first;
1773 // cerr << "***macroname : \"" << macroname << "\"\n";
1774
1775 for (parammapit = (*macromapit).second.begin();
1776 parammapit != (*macromapit).second.end();
1777 ++parammapit)
1778 {
1779 macroparam = (*parammapit).first;
1780 mvalueptr = &((*parammapit).second);
1781 cerr << text_t2ascii << "package : \"" << packagename << "\"\n";
1782 cerr << text_t2ascii << "macroname : \"" << macroname << "\"\n";
1783 cerr << text_t2ascii << "parameters: [" << macroparam << "]\n";
1784 cerr << text_t2ascii << "filename : \"" << mvalueptr->filename << "\"\n";
1785 cerr << text_t2ascii << "value : {" << mvalueptr->value << "}\n\n";
1786 }
1787 }
1788 }
1789}
1790
1791void displayclass::printallparams ()
1792{
1793 text_tset::iterator text_tsetit;
1794 outconvertclass text_t2ascii;
1795
1796 cerr << text_t2ascii << "** allparams\n";
1797 for (text_tsetit=allparams.begin();
1798 text_tsetit!=allparams.end();
1799 ++text_tsetit) {
1800 cerr << text_t2ascii << (*text_tsetit) << "\n";
1801 }
1802 cerr << text_t2ascii << "\n";
1803}
1804
1805
1806
1807/////////////////////////////////////
1808// stuff to do tricky output
1809/////////////////////////////////////
1810
1811displayclass &operator<< (outconvertclass &theoutc, displayclass &display)
1812{
1813 display.setconvertclass (&theoutc);
1814 return display;
1815}
1816
1817displayclass &operator<< (displayclass &display, const text_t &t)
1818{
1819 outconvertclass *theoutc = display.getconvertclass();
1820
1821 if (theoutc == NULL) return display;
1822
1823 text_t output;
1824 display.expandstring (t, output);
1825 (*theoutc) << output;
1826
1827 return display;
1828}
Note: See TracBrowser for help on using the repository browser.