source: main/trunk/greenstone2/common-src/src/lib/display.cpp@ 26652

Last change on this file since 26652 was 26652, checked in by davidb, 11 years ago

More careful typecase to (const char*) rather than (char*). Change motivated by mingw compiler

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 52.2 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 // DL CONSULTING FIX: Fix bug where the end of the macro definition would be missed if a backslash
837 // was the second-to-last character of the macro. [Michael Dewsnip, DL Consulting Ltd.]
838 else
839 {
840 macrovalue.push_back(c);
841 c = my_uni_get(fin, line, isunicode, bigendian);
842 }
843 }
844
845 if (c == '}') {
846 c = my_uni_get(fin, line, isunicode, bigendian); // skip the }
847
848 // define the macro
849 err = setparammacro (macrotable, macropackage, macroname, macroparameters,
850 thisfilename, macrovalue);
851 if ((err == -1 || err == -3) && logout != NULL) {
852 (*logout) << text_t2ascii << ("Warning: redefinition of _" + package + ":" + macroname + "_[" + macroparameters + "] on line " + line + " of " + thisfilename + "\n");
853
854 } else if (err == -2 && logout != NULL) {
855 // I don't think we really want this warning since it's a very
856 // common occurance - Stefan.
857
858 // (*logout) << text_t2ascii << "Warning: _" <<
859 // package << ":" << macroname << "_[" << macroparameters <<
860 // "] on line ";
861 // (*logout) << line;
862 // (*logout) << text_t2ascii << " of " <<
863 // thisfilename << " hides a Global macro with the same name\n";
864
865 } else if (err == -4 && logout != NULL) {
866 (*logout) << text_t2ascii << text_t("Error: macro name expected on line ") + line + text_t(" of ") + thisfilename + "\n";
867 }
868
869 err = 0; // for the test below
870 }
871 else if (!err) err = 3;
872 }
873 else if (!err) err = 4;
874
875 if (err) {
876 // found an error, skip to the end of the line
877 if (logout != NULL) {
878 text_t message = "Error: ";
879 if (err == 1) message += "'_'";
880 else if (err == 2) message += "']'";
881 else if (err == 3) message += "'}'";
882 else if (err == 4) message += "'{'";
883 message += " expected on line ";
884 message += line;
885 message += " of " + thisfilename + "\n";
886 (*logout) << text_t2ascii << message;
887
888// (*logout) << text_t2ascii << "Error: ";
889// if (err == 1) (*logout) << text_t2ascii << "'_'";
890// else if (err == 2) (*logout) << text_t2ascii << "']'";
891// else if (err == 3) (*logout) << text_t2ascii << "'}'";
892// else if (err == 4) (*logout) << text_t2ascii << "'{'";
893// (*logout) << text_t2ascii << " expected on line ";
894// (*logout) << line ;
895// (*logout) << text_t2ascii << " of " << thisfilename << "\n";
896 }
897 while (!fin.eof ()) {
898 if (c == '\n') break;
899 c = my_uni_get(fin, line, isunicode, bigendian);
900 }
901 }
902
903 } else {
904 // found an error, skip to the end of the line
905 if (logout != NULL) {
906 (*logout) << text_t2ascii << text_t("Error: Unexpected input on line ") + line + text_t(" of ") + thisfilename + "\n";
907 }
908 while (!fin.eof ()) {
909 if (c == '\n') break;
910 c = my_uni_get(fin, line, isunicode, bigendian);
911 }
912
913 }
914 }
915
916 fin.close ();
917
918 // free up memory
919 delete []filenamestr;
920 return 0;
921}
922
923
924// loads a default macro file (if it isn't already loaded)
925// returns 0 if didn't need to load the file (it was already loaded)
926// 1 if was (re)loaded
927// -1 an error occurred while trying to load the file
928int displayclass::loaddefaultmacros (const text_t &thisfilename)
929{
930 return loadparammacros(defaultmacros,thisfilename);
931}
932
933// loads a collection specific macro file
934// returns 0 if didn't need to load the file (it was already loaded)
935// 1 if was (re)loaded
936// -1 an error occurred while trying to load the file
937int displayclass::loadcollectionmacros (const text_t &thisfilename)
938{
939 return loadparammacros(collectionmacros,thisfilename);
940}
941
942
943void displayclass::unloaddefaultmacros () {
944 defaultmacros->clear();
945}
946
947void displayclass::unloadcollectionmacros () {
948 collectionmacros->clear();
949}
950
951// prepares to create a page.
952void displayclass::openpage (const text_t &thispageparams,
953 const text_t &thisprecedence)
954{
955 // init variables
956 currentmacros->clear();
957
958 // reload any default macro files which have changed.
959 if (checkdefaultmacrofiles() < 0)
960 {
961 // print error message
962 }
963
964 setpageparams (thispageparams, thisprecedence);
965}
966
967
968// changes the parameters for the current page.
969void displayclass::setpageparams (text_t thispageparams,
970 text_t thisprecedence)
971{
972 paramhashtype thissplitparams, splittheseparams;
973 precedencetype precedhash;
974 text_tset::iterator text_tsetit;
975 paramspec tmpps;
976
977 precedence = thisprecedence;
978 calcprecedence(thisprecedence, precedhash);
979
980 splitparams(thispageparams, thissplitparams);
981 joinparams(thissplitparams, thispageparams);
982 params = thispageparams;
983
984 // get a list of parameters with their specificness
985 orderparamlist->erase(orderparamlist->begin(), orderparamlist->end());
986 for (text_tsetit=allparams.begin();
987 text_tsetit!=allparams.end();
988 ++text_tsetit)
989 {
990 tmpps.param = (*text_tsetit);
991 splitparams(tmpps.param, splittheseparams);
992 tmpps.spec = getspecificness(thissplitparams, splittheseparams, precedhash);
993 if (tmpps.spec >= 0)
994 {
995 orderparamlist->push_back (tmpps);
996 }
997 }
998
999 // sort the list
1000 sort(orderparamlist->begin(), orderparamlist->end());
1001
1002#if 0 // debugging
1003 outconvertclass text_t2ascii;
1004
1005 paramspeclist::iterator pshere = orderparamlist->begin();
1006 paramspeclist::iterator psend = orderparamlist->end();
1007 while (pshere != psend)
1008 {
1009 cerr << text_t2ascii << "param=" << (*pshere).param;
1010 cerr << " spec=" << (int)((*pshere).spec) << "\n";
1011 ++pshere;
1012 }
1013#endif
1014}
1015
1016
1017// overrides (or sets) a macro for the current page.
1018// returns 0 if there was no error,
1019// -1 if it redefined a macro
1020// -4 if no macroname was supplied
1021int displayclass::setmacro (const text_t &macroname,
1022 const text_t &package,
1023 const text_t &macrovalue)
1024{
1025 // make sure a macroname was supplied
1026 if (macroname.empty()) return -4;
1027
1028 // make package "Global" if it doesn't point to anything yet
1029 if (package.empty())
1030 return currentmacros->setmacro(defaultpackage, macroname, "memory", macrovalue);
1031
1032 // set the macro
1033 return currentmacros->setmacro(package, macroname, "memory", macrovalue);
1034}
1035
1036int displayclass::setmacroif (const text_t &macro_to_set_and_test,
1037 const text_t &macro_to_set_if_macro_not_set,
1038 const text_t &macroname,
1039 const text_t &package) {
1040
1041 if (macroname.empty()) return -4;
1042 if (macro_to_set_and_test.empty()) return -40;
1043
1044 text_t temp;
1045 expandstring(defaultpackage, macro_to_set_and_test, temp);
1046
1047 if (package.empty()) {
1048 if (temp == macro_to_set_and_test) return currentmacros->setmacro(defaultpackage, macroname, "memory", macro_to_set_if_macro_not_set);
1049 return currentmacros->setmacro(defaultpackage, macroname, "memory", macro_to_set_and_test);
1050 } else {
1051 if (temp == macro_to_set_and_test) return currentmacros->setmacro(package, macroname, "memory", macro_to_set_if_macro_not_set);
1052 return currentmacros->setmacro(package, macroname, "memory", macro_to_set_and_test);
1053
1054 }
1055}
1056
1057void displayclass::expandstring (const text_t &package, const text_t &inputtext,
1058 text_t &outputtext, int recursiondepth)
1059{
1060 text_t macroname, macropackage, macroargs;
1061 text_t::const_iterator tthere = inputtext.begin();
1062 text_t::const_iterator ttend = inputtext.end();
1063 unsigned short c = '\0';
1064 int openbrackets=0;
1065
1066 if (package.empty()) expandstring(defaultpackage, inputtext, outputtext, recursiondepth);
1067
1068 outputtext.clear();
1069 // use one-character lookahead
1070 if (tthere != ttend) c = (*tthere);
1071 while (tthere != ttend)
1072 {
1073 if (c == '\\')
1074 {
1075 // found an escape character
1076 c = my_ttnextchar (tthere, ttend); // skip the escape character
1077 if (tthere != ttend)
1078 {
1079 if (c == 'n') outputtext.push_back('\n');
1080 else if (c == 't') outputtext.push_back('\t');
1081 else outputtext.push_back(c);
1082 }
1083 c = my_ttnextchar (tthere, ttend);
1084
1085 }
1086 else if (c == '_')
1087 {
1088 // might have found the start of a macro
1089
1090 // embedded macros (macros within the names
1091 // of other macros) are no longer supported -- Rodger.
1092
1093 // save the current state (in case we need to back out)
1094 text_t::const_iterator savedtthere = tthere;
1095 unsigned short savedc = c;
1096
1097 macroname.clear();
1098 macropackage = package;
1099 macroargs.clear();
1100
1101 c = my_ttnextchar (tthere, ttend); // skip the '_'
1102
1103 // get the macroname
1104 while (tthere != ttend && (!is_unicode_space(c)) &&
1105 (c != '\\') && (c != '_') &&(c != ':') &&
1106 (macroname.size() < 80))
1107 {
1108 macroname.push_back((unsigned char)c);
1109 c = my_ttnextchar (tthere, ttend);
1110 }
1111
1112 if (c == ':')
1113 {
1114 // we actually had the macro package
1115 c = my_ttnextchar (tthere, ttend);
1116 macropackage = macroname;
1117 macroname.clear ();
1118
1119 // get the macro name (honest!)
1120 while ((tthere != ttend) && (!is_unicode_space(c)) &&
1121 (c != '\\') && (c != '_') &&(c != ':') &&
1122 (macroname.size() < 80))
1123 {
1124 macroname.push_back((unsigned char)c);
1125 c = my_ttnextchar (tthere, ttend);
1126 }
1127 }
1128
1129 if ((tthere != ttend) && (c == '_') &&
1130 (macroname.size() > 0))
1131 {
1132 // found a macro!!!! (maybe)
1133 c = my_ttnextchar (tthere, ttend); // skip the '_'
1134
1135 // get the parameters (if there are any)
1136 if ((tthere != ttend) && (c == '('))
1137 {
1138 c = my_ttnextchar (tthere, ttend); // skip '('
1139 ++openbrackets;
1140 // have to be careful of quotes
1141 unsigned short quote = '\0';
1142 while (tthere != ttend)
1143 {
1144 if (c == '\\')
1145 {
1146 // have an escape character, save escape
1147 // character and next character as well
1148 macroargs.push_back (c);
1149 c = my_ttnextchar (tthere, ttend);
1150
1151 }
1152 else if (quote == '\0')
1153 {
1154 // not in a quote at the moment
1155 if (c == '(')
1156 {
1157 ++openbrackets;
1158 }
1159 else if (c == ')')
1160 {
1161 --openbrackets;
1162 if (openbrackets==0) {
1163 // found end of the arguments
1164 c = my_ttnextchar (tthere, ttend); // skip ')'
1165 break;
1166 } else {
1167 // nested brackets
1168 }
1169 }
1170 else if (c == '\'' || c == '\"')
1171 {
1172 // found a quote
1173 quote = c;
1174 }
1175
1176 }
1177 else
1178 {
1179 // we are in a quote
1180 if (c == quote) quote = '\0';
1181 }
1182
1183 // save this character
1184 if (tthere != ttend) macroargs.push_back (c);
1185 c = my_ttnextchar (tthere, ttend);
1186 } // while (tthere != ttend)
1187 }
1188
1189 //
1190 // we now have the macropackage, macroname, and macroargs
1191 //
1192
1193 // try to expand out this macro
1194 text_t expandedmacro;
1195 if (macro (macroname, macropackage.empty()?defaultpackage:macropackage, macroargs, expandedmacro, recursiondepth))
1196 {
1197 // append the expanded macro
1198 outputtext += expandedmacro;
1199 }
1200 else
1201 {
1202 // wasn't a macro
1203 tthere = savedtthere;
1204 c = savedc;
1205 outputtext.push_back(c); // the '_'
1206 c = my_ttnextchar (tthere, ttend);
1207 }
1208
1209
1210 }
1211 else
1212 {
1213 // wasn't a macro
1214 tthere = savedtthere;
1215 c = savedc;
1216 outputtext.push_back(c); // the '_'
1217 c = my_ttnextchar (tthere, ttend);
1218 }
1219
1220 }
1221 else
1222 {
1223 // nothing of interest
1224 outputtext.push_back(c); // the '_'
1225 c = my_ttnextchar (tthere, ttend);
1226 }
1227 } // end of while (tthere != ttend)
1228}
1229
1230ostream *displayclass::setlogout (ostream *thelogout) {
1231
1232 ostream *oldlogout = logout;
1233 logout = thelogout;
1234 return oldlogout;
1235}
1236
1237// protected methods for display
1238
1239// reloads any default macro files which have changed
1240// returns 0 no errors occurred
1241// -1 an error occurred while trying to load one of the files
1242int displayclass::checkdefaultmacrofiles ()
1243{
1244 // this isn't implemented yet
1245 return 0;
1246}
1247
1248// isdefaultmacro sees if there is an entry in the list of
1249// default macros with the given package and macro name
1250// returns 0 if no macros in the package or in the global package
1251// were found
1252// 1 if no macros in the package were found but a macro
1253// in the global package was found
1254// 2 if a macro in the given package was found
1255int displayclass::isdefaultmacro (const text_t &package, const text_t &macroname) {
1256 // make sure a macroname was supplied
1257 if (macroname.empty()) return 0;
1258
1259 if (!package.empty()) {
1260 // check the given package
1261 if (defaultmacros->macro_find(package, macroname) != NULL) return 2;
1262 }
1263
1264 // check the Global package
1265 if (defaultmacros->macro_find(defaultpackage, macroname) != NULL) return 1;
1266 // not found
1267 return 0;
1268}
1269
1270// havemacro sees if there is an entry in the list of macros or
1271// default macros with the given package and macro name
1272// returns 0 if no macros in the package or in the global package
1273// were found
1274// 1 if no macros in the package were found but a macro
1275// in the global package was found
1276// 2 if a macro in the given package was found
1277// 4 if no macros in the package were found but a macro
1278// in the global package was found in default macros
1279// 8 if a macro in the given package was found in default
1280int displayclass::havemacro(const text_t &package, const text_t &macroname)
1281{
1282 // make sure a macroname was supplied
1283 if (macroname.empty()) return 0;
1284
1285 // make package "Global" if it doesn't point to anything yet
1286 if (package.empty()) {
1287 // check the Global package in current macros list
1288 if (currentmacros->macro_find(defaultpackage, macroname) != NULL) return 1;
1289
1290 // check the Global package in default macros list
1291 if (defaultmacros->macro_find(defaultpackage, macroname) != NULL) return 4;
1292 } else {
1293 // check the given package in current macros list
1294 if (currentmacros->macro_find(package, macroname) != NULL) return 2;
1295
1296 // check the Global package in current macros list
1297 if (currentmacros->macro_find(defaultpackage, macroname) != NULL) return 1;
1298
1299 // check the given package in default macros list
1300 if (defaultmacros->macro_find(package, macroname) != NULL) return 8;
1301
1302 // check the Global package in default macros list
1303 if (defaultmacros->macro_find(defaultpackage, macroname) != NULL) return 4;
1304 }
1305
1306 // nothing was found
1307 return 0;
1308}
1309
1310
1311// setparammacro adds an entry to the list of default macros
1312// returns 0 if there was no error,
1313// -1 if it redefined a macro
1314// -2 if it hid a Global macro
1315// -3 if it redefined a macro and hid a Global macro
1316// -4 if no macroname was supplied
1317int displayclass::setparammacro (parammacros_t* macrotable,
1318 const text_t &package,
1319 const text_t &macroname,
1320 text_t macroparams,
1321 const text_t &filename,
1322 const text_t &macrovalue)
1323{
1324 // make sure a macroname was supplied
1325 if (macroname.empty()) return -4;
1326
1327 // put the parameters in a standard form
1328 paramhashtype paramhash;
1329 if (macroparams.empty()) macroparams = "ignore=yes";
1330 splitparams (macroparams, paramhash);
1331 joinparams (paramhash, macroparams);
1332
1333 // remember these parameters
1334 allparams.insert (macroparams);
1335
1336 // remember this filename (this part isn't finished yet -- Rodger).
1337
1338 // set the macro
1339 if (package.empty()) {
1340 return macrotable->setmacro(defaultpackage, macroname, macroparams, filename, macrovalue);
1341 } else {
1342 return macrotable->setmacro(package, macroname, macroparams, filename, macrovalue);
1343 }
1344}
1345
1346// setdefaultmacro adds an entry to the list of default macros
1347// returns 0 if there was no error,
1348// -1 if it redefined a macro
1349// -2 if it hid a Global macro
1350// -3 if it redefined a macro and hid a Global macro
1351// -4 if no macroname was supplied
1352int displayclass::setdefaultmacro (const text_t &package,
1353 const text_t &macroname,
1354 text_t macroparams,
1355 const text_t &filename,
1356 const text_t &macrovalue)
1357{
1358 return setparammacro(defaultmacros,package,macroname,
1359 macroparams,filename,macrovalue);
1360}
1361
1362// public function
1363int displayclass::setcollectionmacro(const text_t &package,
1364 const text_t &macroname,
1365 text_t mparams,
1366 const text_t &macrovalue)
1367{
1368 return setcollectionmacro(package, macroname, mparams, "memory", macrovalue);
1369}
1370
1371// setcollectionmacro adds an entry to the list of collection specific macros
1372// returns 0 if there was no error,
1373// -1 if it redefined a macro
1374// -2 if it hid a Global macro
1375// -3 if it redefined a macro and hid a Global macro
1376// -4 if no macroname was supplied
1377int displayclass::setcollectionmacro (const text_t &package,
1378 const text_t &macroname,
1379 text_t mparams,
1380 const text_t &filename,
1381 const text_t &macrovalue)
1382{
1383 return setparammacro(collectionmacros,package,macroname,mparams,
1384 filename,macrovalue);
1385}
1386
1387
1388// evaluates a boolean expression
1389// returns false if expr equals "" or "0".
1390// otherwise returns true *unless* expr is
1391// format "XXXX" eq/ne "dddd" in which case
1392// the two quoted strings are compared
1393bool displayclass::boolexpr (const text_t &package, const text_t &expr, int recursiondepth) {
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 // allow \" as start of exp as well as ", cos this is what the GLI writes out
1404 if (expr[0] != '\"' && !(expr[0] == '\\' && expr[1] == '"')) {
1405 return true;
1406 }
1407 // don't use expexpr while separating quoted parts of
1408 // expression just in case expanded out macros contain
1409 // quotes
1410 text_t::const_iterator here = expr.begin();
1411 text_t::const_iterator end = expr.end();
1412
1413 int quotecount = 0;
1414 text_t string1, expstring1;
1415 text_t string2, expstring2;
1416 text_t op;
1417 text_t combineop;
1418 bool result = false; // an empty string is false
1419 bool result2 = false;
1420
1421 while (here != end) {
1422 // get a comparison
1423 quotecount = 0;
1424 string1.clear();
1425 string2.clear();
1426 op.clear();
1427 while (here != end) {
1428 if (*here == '\\' && *(here+1) =='"') {
1429 ++quotecount; // found an escaped quote
1430 ++here;
1431 }
1432 else if (*here == '"') ++quotecount;
1433 else if (quotecount == 1) string1.push_back(*here);
1434 else if ((quotecount == 2) && !is_unicode_space (*here))
1435 op.push_back(*here);
1436 else if (quotecount == 3) string2.push_back(*here);
1437 else if (quotecount >= 4) break;
1438 ++here;
1439 }
1440
1441 expandstring (package, string1, expstring1, recursiondepth);
1442 expandstring (package, string2, expstring2, recursiondepth);
1443
1444 // get next result
1445 result2 = true; // any badly formatted string will return true
1446 if (op == "eq") result2 = (expstring1 == expstring2);
1447 else if (op == "ne") result2 = (expstring1 != expstring2);
1448 else if (op == "gt") result2 = (expstring1 > expstring2);
1449 else if (op == "ge") result2 = (expstring1 >= expstring2);
1450 else if (op == "lt") result2 = (expstring1 < expstring2);
1451 else if (op == "le") result2 = (expstring1 <= expstring2);
1452 else if (op == "==") result2 = (expstring1.getint() == expstring2.getint());
1453 else if (op == "!=") result2 = (expstring1.getint() != expstring2.getint());
1454 else if (op == ">") result2 = (expstring1.getint() > expstring2.getint());
1455 else if (op == ">=") result2 = (expstring1.getint() >= expstring2.getint());
1456 else if (op == "<") result2 = (expstring1.getint() < expstring2.getint());
1457 else if (op == "<=") result2 = (expstring1.getint() <= expstring2.getint());
1458 else if (op == "sw") result2 = (starts_with(expstring1,expstring2));
1459 else if (op == "ew") result2 = (ends_with(expstring1,expstring2));
1460
1461 // combine the results
1462 if (combineop == "&&") result = (result && result2);
1463 else if (combineop == "||") result = (result || result2);
1464 else result = result2;
1465
1466 // get next combination operator
1467 combineop.clear();
1468 while (here != end && *here != '"') {
1469 if (!is_unicode_space(*here)) combineop.push_back(*here);
1470 ++here;
1471 }
1472 }
1473
1474 return result;
1475}
1476
1477
1478mvalue* displayclass::macro_find (parammacros_t* macrotable,
1479 const text_t &packagename,
1480 const text_t &macroname)
1481{
1482 mvalue *macrovalue = NULL;
1483
1484 // next look in the named parammacros for the named package
1485 if (macrotable->macro_find (packagename, macroname) != NULL)
1486 {
1487 paramspeclist::iterator paramhere, paramend;
1488
1489 paramhere = orderparamlist->begin(); paramend = orderparamlist->end();
1490 while ((macrovalue == NULL) && (paramhere != paramend))
1491 {
1492 macrovalue = macrotable->parameter_find(packagename, macroname,
1493 (*paramhere).param);
1494 ++paramhere;
1495 }
1496 }
1497
1498 return macrovalue;
1499}
1500
1501
1502// (recursively) expand out a macro
1503// returns true if the macro was found
1504// false if the macro was not found
1505// macropackage should never be empty
1506bool displayclass::macro (const text_t &macroname,
1507 const text_t &macropackage,
1508 const text_t &macroparam,
1509 text_t &outputtext,
1510 int recursiondepth)
1511{
1512 assert(!macropackage.empty()); //???
1513 //outconvertclass text_t2ascii;
1514 text_tlist splitmacroparam;
1515 text_t::const_iterator hereit, endit;
1516 text_t aparam;
1517 unsigned short c = '\0', quote = '\0';
1518
1519 // cerr << "r: " << recursiondepth << "\n";
1520 // check for deep recursion
1521 if (recursiondepth >= MAXRECURSIONDEPTH)
1522 {
1523 if (logout != NULL)
1524 (*logout) << "Warning: deep recursion, limiting macro expansion\n";
1525 return false;
1526 }
1527
1528 // check the macropackage
1529 //if (macropackage.empty()) macropackage = "Global";
1530
1531 // get the parameters (but don't expand them)
1532 if (macroparam.size() > 0) {
1533 // set up for loop
1534 hereit = macroparam.begin();
1535 endit = macroparam.end();
1536 if (hereit != endit) c = (*hereit);
1537 int openbrackets=0; // keep track of bracket level eg _If_(1, _macro_(arg))
1538 // this allows commas inside brackets (as arguments)
1539 while (hereit != endit) {
1540 // get the next parameter
1541 aparam.clear();
1542 quote = '\0'; // not-quoted unless proven quoted
1543
1544 // ignore initial whitespace
1545 while ((hereit!=endit)&&is_unicode_space(c)) c=my_ttnextchar(hereit,endit);
1546
1547 // look for the end of the parameter
1548 while (hereit != endit) {
1549 if (c == '\\') {
1550 // found escape character, also get next character
1551 aparam.push_back(c);
1552 c = my_ttnextchar (hereit, endit);
1553 if (hereit != endit) aparam.push_back(c);
1554
1555 } else if (quote=='\0' && (c=='\'' /*|| c=='"'*/)) {
1556 // found a quoted section
1557 quote = c;
1558 if (openbrackets>0) aparam.push_back(c);
1559
1560 } else if (quote!='\0' && c==quote) {
1561 // found the end of a quote
1562 quote = '\0';
1563 if (openbrackets<0) aparam.push_back(c);
1564
1565 } else if (quote=='\0' && (c==',' || c==')') && openbrackets==0) {
1566 // found the end of a parameter
1567 c = my_ttnextchar (hereit, endit);
1568 break;
1569
1570 } else if (c=='(') {
1571 aparam.push_back(c);
1572 ++openbrackets;
1573 } else if (c==')') {
1574 --openbrackets;
1575 aparam.push_back(c);
1576 } else {
1577 // ordinary character
1578 aparam.push_back(c);
1579 }
1580
1581 c = my_ttnextchar (hereit, endit);
1582 }
1583
1584
1585 // add this parameter to the list
1586 splitmacroparam.push_back(aparam);
1587 }
1588 }
1589
1590 if (macroname == "If") {
1591 // get the condition, then clause and else clause
1592 text_tlist::iterator paramcond = splitmacroparam.begin();
1593 text_tlist::iterator paramend = splitmacroparam.end();
1594 text_tlist::iterator paramthen = paramend;
1595 text_tlist::iterator paramelse = paramend;
1596
1597 if (paramcond != paramend) {
1598 paramthen = paramcond;
1599 ++paramthen;
1600 }
1601 if (paramthen != paramend) {
1602 paramelse = paramthen;
1603 ++paramelse;
1604 }
1605
1606 // will always output something
1607 outputtext.clear();
1608
1609
1610 // expand out the first parameter
1611 if (paramcond != paramend) {
1612 /// text_t tmpoutput;
1613 /// expandstring (macropackage, *paramcond, tmpoutput, recursiondepth+1);
1614 /// lc (tmpoutput);
1615 // test the expanded string
1616 if (boolexpr (macropackage, *paramcond, recursiondepth+1)) {
1617 //(tmpoutput.size()) && (tmpoutput != "false") && (tmpoutput != "0")) {
1618 // true
1619 if (paramthen != paramend)
1620 expandstring (macropackage, *paramthen, outputtext, recursiondepth+1);
1621 } else {
1622 // false
1623 if (paramelse != paramend)
1624 expandstring (macropackage, *paramelse, outputtext, recursiondepth+1);
1625 }
1626 }
1627
1628 return true;
1629 }
1630
1631 // try and find this macro
1632
1633 // this list might be replaced by something a little more
1634 // sophisticated in the future.
1635 text_tlist packagelist;
1636 packagelist.push_back (macropackage);
1637 packagelist.push_back ("Global");
1638
1639 text_tlist::iterator packagehere = packagelist.begin();
1640 text_tlist::iterator packageend = packagelist.end();
1641 mvalue *macrovalue = NULL;
1642 while (packagehere != packageend)
1643 {
1644 // first look in the currentmacros
1645 macrovalue = currentmacros->macro_find(*packagehere, macroname);
1646 if (macrovalue != NULL)
1647 break;
1648
1649 macrovalue = currentmacros->macro_find("Style", macroname);
1650 if (macrovalue != NULL)
1651 break;
1652
1653 // look in the collection specific macros
1654 macrovalue = macro_find(collectionmacros,*packagehere,macroname);
1655 if (macrovalue != NULL)
1656 break;
1657
1658 // and again in the collection specific package "Style"
1659 macrovalue = macro_find(collectionmacros,(const char*)"Style",macroname);
1660 if (macrovalue != NULL)
1661 break;
1662
1663 // look in the default macros
1664 macrovalue = macro_find(defaultmacros,*packagehere,macroname);
1665 if (macrovalue != NULL)
1666 break;
1667
1668 // and again in the package "Style"
1669 macrovalue = macro_find(defaultmacros,(const char*)"Style",macroname);
1670 if (macrovalue != NULL)
1671 break;
1672 ++packagehere;
1673 }
1674
1675 if (macrovalue != NULL)
1676 {
1677 // we found a macro
1678
1679 // replace all the option macros (_1_, _2_, ...) in the value of this macro
1680 // and decide if we need to expand the value of this macro
1681 bool needsexpansion = false;
1682 text_t tempmacrovalue;
1683 tempmacrovalue.clear();
1684 hereit = macrovalue->value.begin();
1685 endit = macrovalue->value.end();
1686 if (hereit != endit) c = (*hereit);
1687 while (hereit != endit)
1688 {
1689 if (c == '\\')
1690 {
1691 // found an escape character
1692 needsexpansion = true;
1693 tempmacrovalue.push_back(c);
1694 c = my_ttnextchar (hereit, endit);
1695 if (hereit != endit) tempmacrovalue.push_back(c);
1696 c = my_ttnextchar (hereit, endit);
1697 }
1698 else if (c == '_')
1699 {
1700 text_t::const_iterator savedhereit = hereit;
1701
1702 // attempt to get a number
1703 int argnum = 0;
1704 unsigned short digc = my_ttnextchar (hereit, endit);
1705 while (digc >= '0' && digc <= '9') {
1706 argnum = argnum*10 + digc - '0';
1707 digc = my_ttnextchar (hereit, endit);
1708 }
1709 if (digc == '_' && argnum > 0) {
1710 // found an option macro, append the appropriate text
1711 text_tlist::iterator ttlisthere = splitmacroparam.begin();
1712 text_tlist::iterator ttlistend = splitmacroparam.end();
1713 while ((argnum > 1) && (ttlisthere != ttlistend)) {
1714 --argnum;
1715 ++ttlisthere;
1716 }
1717 if (ttlisthere != ttlistend) {
1718 tempmacrovalue.append(*ttlisthere);
1719 if (findchar((*ttlisthere).begin(), (*ttlisthere).end(), '_') != (*ttlisthere).end())
1720 needsexpansion = true;
1721 }
1722
1723 c = my_ttnextchar (hereit, endit);
1724
1725 } else {
1726 // wasn't a option macro
1727 needsexpansion = true;
1728 tempmacrovalue.push_back(c);
1729 hereit = savedhereit;
1730 c = my_ttnextchar (hereit, endit);
1731 }
1732 }
1733 else
1734 {
1735 tempmacrovalue.push_back(c);
1736 c = my_ttnextchar (hereit, endit);
1737 }
1738 }
1739
1740 // recursively replace this macro (if we need to)
1741 if (needsexpansion) {
1742 expandstring (*packagehere, tempmacrovalue, outputtext, recursiondepth+1);
1743 } else {
1744 outputtext += tempmacrovalue;
1745 }
1746
1747 return true;
1748 }
1749
1750 // GRB: deal with remaining code properly as it faults
1751 return false;
1752 outconvertclass text_t2ascii;
1753 if (logout != NULL) {
1754 (*logout) << text_t2ascii << "Warning: _" <<
1755 macropackage << ":" << macroname << "_ not found\n";
1756 }
1757
1758 return false; // didn't find the macro
1759}
1760
1761
1762void displayclass::printdefaultmacros ()
1763{
1764 defpackagemap::const_iterator packagemapit;
1765 defmacromap::const_iterator macromapit;
1766 defparammap::const_iterator parammapit;
1767 const mvalue *mvalueptr;
1768 outconvertclass text_t2ascii;
1769
1770 text_t packagename, macroname, macroparam;
1771
1772 for (packagemapit = defaultmacros->package_begin();
1773 packagemapit != defaultmacros->package_end();
1774 ++packagemapit)
1775 {
1776 packagename = (*packagemapit).first;
1777 // cerr << "***package : \"" << packagename << "\"\n";
1778
1779 for (macromapit = (*packagemapit).second.begin();
1780 macromapit != (*packagemapit).second.end();
1781 ++macromapit)
1782 {
1783 macroname = (*macromapit).first;
1784 // cerr << "***macroname : \"" << macroname << "\"\n";
1785
1786 for (parammapit = (*macromapit).second.begin();
1787 parammapit != (*macromapit).second.end();
1788 ++parammapit)
1789 {
1790 macroparam = (*parammapit).first;
1791 mvalueptr = &((*parammapit).second);
1792 cerr << text_t2ascii << "package : \"" << packagename << "\"\n";
1793 cerr << text_t2ascii << "macroname : \"" << macroname << "\"\n";
1794 cerr << text_t2ascii << "parameters: [" << macroparam << "]\n";
1795 cerr << text_t2ascii << "filename : \"" << mvalueptr->filename << "\"\n";
1796 cerr << text_t2ascii << "value : {" << mvalueptr->value << "}\n\n";
1797 }
1798 }
1799 }
1800}
1801
1802void displayclass::printallparams ()
1803{
1804 text_tset::iterator text_tsetit;
1805 outconvertclass text_t2ascii;
1806
1807 cerr << text_t2ascii << "** allparams\n";
1808 for (text_tsetit=allparams.begin();
1809 text_tsetit!=allparams.end();
1810 ++text_tsetit) {
1811 cerr << text_t2ascii << (*text_tsetit) << "\n";
1812 }
1813 cerr << text_t2ascii << "\n";
1814}
1815
1816
1817
1818/////////////////////////////////////
1819// stuff to do tricky output
1820/////////////////////////////////////
1821
1822displayclass &operator<< (outconvertclass &theoutc, displayclass &display)
1823{
1824 display.setconvertclass (&theoutc);
1825 return display;
1826}
1827
1828displayclass &operator<< (displayclass &display, const text_t &t)
1829{
1830 outconvertclass *theoutc = display.getconvertclass();
1831
1832 if (theoutc == NULL) return display;
1833
1834 text_t output;
1835 display.expandstring (t, output);
1836 (*theoutc) << output;
1837
1838 return display;
1839}
Note: See TracBrowser for help on using the repository browser.