source: main/tags/2.52/gsdl/lib/display.cpp@ 25422

Last change on this file since 25422 was 7397, checked in by mdewsnip, 20 years ago

(Human Info) Use displayclass::defaultpackage instead of "Global".

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 47.9 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 != "Global") &&
133 (macro_find ("Global", 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
547
548
549inline unsigned short my_ttnextchar (text_t::const_iterator &here,
550 text_t::const_iterator &end)
551{
552 if (here != end)
553 {
554 here++;
555 if (here != end) return (*here);
556 }
557 return '\0';
558}
559
560
561
562/////////////////////////////////////
563// methods for display
564/////////////////////////////////////
565
566// public methods for display
567
568displayclass::displayclass ()
569{
570 defaultmacros = new parammacros_t;
571 collectionmacros = new parammacros_t;
572 defaultfiles = new fileinfomap;
573
574 orderparamlist = new paramspeclist;
575 currentmacros = new currentmacros_t;
576
577 outc = NULL;
578
579 logout = NULL;
580}
581
582
583displayclass::~displayclass ()
584{
585 delete defaultmacros;
586 delete collectionmacros;
587 delete defaultfiles;
588
589 delete orderparamlist;
590 delete currentmacros;
591}
592
593// setdefaultmacro adds an entry to the list of default macros
594// returns 0 if there was no error,
595// -1 if it redefined a macro
596// -2 if it hid a Global macro
597// -3 if it redefined a macro and hid a Global macro
598// -4 if no macroname was supplied
599int displayclass::setdefaultmacro (text_t package, const text_t &macroname,
600 text_t params, const text_t &macrovalue)
601{
602 // **** Looks to me like the last two parameters are the wrong way around!!!
603 return setdefaultmacro (macroname, package, params, macrovalue, "memory");
604}
605
606
607
608// as we are using one character lookahead the
609// value of line might be off by one.
610// the input file must be in the utf-8 or unicode format
611// initially for each file isunicode should be set to 0 and
612// bigendian should be set to 1
613// 0 will be returned when the end of the file has been found
614unsigned short my_uni_get (/*unistream*/ifstream &fin, int &line,
615 int &isunicode, int &bigendian) {
616 unsigned short c = 0;
617
618 if (isunicode) {
619 // unicode text
620 // get the next two characters
621 unsigned char c1 = 0, c2 = 0;
622
623 if (!fin.eof()) c1=fin.get();
624 if (!fin.eof()) c2=fin.get();
625 else c1 = 0;
626
627 // if they indicate the order get the next character
628 // otherwise just get these characters
629 if (c1 == 0xff && c2 == 0xfe) {
630 bigendian = 0;
631 c = my_uni_get (fin, line, isunicode, bigendian);
632 } else if (c1 == 0xfe && c2 == 0xff) {
633 bigendian = 1;
634 c = my_uni_get (fin, line, isunicode, bigendian);
635 } else c = (bigendian) ? (c1*256+c2) : (c2*256+c1);
636
637 } else {
638 // utf-8 text
639 // how many characters we get depends on what we find
640 unsigned char c1 = 0, c2 = 0, c3 = 0;
641 while (!fin.eof()) {
642 c1=fin.get();
643 if (c1 == 0xfe || c1 == 0xff) {
644 // switch to unicode
645 isunicode = 1;
646 if (!fin.eof()) c2=fin.get();
647
648 if (c1 == 0xff && c2 == 0xfe) bigendian = 0;
649 else bigendian = 1;
650
651 c = my_uni_get (fin, line, isunicode, bigendian);
652 break;
653
654 } else if (c1 <= 0x7f) {
655 // one byte character
656 c = c1;
657 break;
658
659 } else if (c1 >= 0xc0 && c1 <= 0xdf) {
660 // two byte character
661 if (!fin.eof()) c2=fin.get();
662 c = ((c1 & 0x1f) << 6) + (c2 & 0x3f);
663 break;
664
665 } else if (c1 >= 0xe0 && c1 <= 0xef) {
666 // three byte character
667 if (!fin.eof()) c2=fin.get();
668 if (!fin.eof()) c3=fin.get();
669 c = ((c1 & 0xf) << 12) + ((c2 & 0x3f) << 6) + (c3 & 0x3f);
670 break;
671 }
672
673 // if we get here there was an error in the file, we should
674 // be able to recover from it however, maybe the file is in
675 // another encoding
676 }
677 }
678
679 if (c == '\n') line++;
680 return c;
681}
682
683
684
685// loads a macro file into the specified macrotable datastructure
686// returns 0 if didn't need to load the file (it was already loaded)
687// 1 if was (re)loaded
688// -1 an error occurred while trying to load the file
689int displayclass::loadparammacros (parammacros_t* macrotable, text_t thisfilename) {
690 // convert the filename to a C string
691 char *filenamestr = thisfilename.getcstr();
692 outconvertclass text_t2ascii;
693
694 // see if we need to open this file -- this isn't implemented yet
695
696 // open the file
697 // unistream fin (filenamestr);
698 ifstream fin (filenamestr);
699
700 if (fin.fail()) return -1; // read failed
701
702 text_t package = "Global";
703 int line = 1;
704 int isunicode = 0, bigendian = 1;
705
706 // pre-fetch the next character
707 unsigned short c = my_uni_get(fin, line, isunicode, bigendian);
708
709 text_t macropackage, macroname, macroparameters, macrovalue;
710 int err; // for keeping track of whether an error occurred somewhere
711
712 while (!fin.eof()) {
713 // expect: white space, comment, "package", or macroname
714 if (is_unicode_space(c)) {
715 // found some white-space
716 c = my_uni_get(fin, line, isunicode, bigendian);
717
718 } else if (c == '#') {
719 // found the start of a comment
720 // skip all characters up to the end of the line
721 c = my_uni_get(fin, line, isunicode, bigendian); // skip the '#'
722 while (!fin.eof ()) {
723 if (c == '\n') break;
724 c = my_uni_get(fin, line, isunicode, bigendian);
725 }
726
727 } else if (c == 'p') {
728 // found the start of 'package' (hopefully)
729 // get everything up to the next space
730 text_t tmp;
731 while (!fin.eof() && my_isalpha(c)) {
732 tmp.push_back(c);
733 c = my_uni_get(fin, line, isunicode, bigendian);
734 }
735 // see if we have a package name
736 if (tmp == "package") {
737 // skip all white space
738 while (!fin.eof() && is_unicode_space(c))
739 c = my_uni_get(fin, line, isunicode, bigendian);
740
741 // get the package name
742 tmp.clear(); // init tmp
743 while (!fin.eof() && my_isalpha(c)) {
744 tmp.push_back(c);
745 c = my_uni_get(fin, line, isunicode, bigendian);
746 }
747 package = tmp;
748 if (package.empty()) package = "Global";
749
750 } else {
751 // error
752 if (logout != NULL) {
753 (*logout) << text_t2ascii << "Expected 'package' on line " << line
754 << " of " << thisfilename << "\n";
755 }
756 }
757
758 } else if (c == '_') {
759 // found the start of a macro (hopefully)
760 c = my_uni_get(fin, line, isunicode, bigendian); // skip the _
761
762 // init variables
763 err = 0;
764 macropackage = package;
765 macroname.clear(); // init macroname
766 macroparameters.clear(); // init macroname
767 macrovalue.clear(); // init macroname
768
769 // get the macro name
770 while ((!fin.eof()) && (!is_unicode_space(c)) &&
771 (c != '\\') && (c != '_') &&(c != ':') &&
772 (macroname.size() < 80)) {
773 macroname.push_back(c);
774 c = my_uni_get(fin, line, isunicode, bigendian);
775 }
776
777 if (c == ':') {
778 // we actually had the macro package
779 c = my_uni_get(fin, line, isunicode, bigendian); // skip :
780 macropackage = macroname;
781 macroname.clear ();
782
783 // get the macro name (honest!)
784 while ((!fin.eof()) && (!is_unicode_space(c)) &&
785 (c != '\\') && (c != '_') &&(c != ':') &&
786 (macroname.size() < 80)) {
787 macroname.push_back(c);
788 c = my_uni_get(fin, line, isunicode, bigendian);
789 }
790 }
791
792 if (!err && c == '_') {
793 c = my_uni_get(fin, line, isunicode, bigendian); // skip the _
794
795 // skip all white space
796 while (!fin.eof() && is_unicode_space(c)) c = my_uni_get(fin, line, isunicode, bigendian);
797 } else if (!err) err = 1;
798
799 // get the macro parameters (optional)
800 if (!err && c == '[') {
801 c = my_uni_get(fin, line, isunicode, bigendian); // skip the [
802 while ((!fin.eof()) && (c != '\n') && (c != '\\') && (c != ']')) {
803 macroparameters.push_back(c);
804 c = my_uni_get(fin, line, isunicode, bigendian);
805 }
806
807 if (c == ']') {
808 c = my_uni_get(fin, line, isunicode, bigendian); // skip the ]
809
810 // skip all white space
811 while (!fin.eof() && is_unicode_space(c)) c = my_uni_get(fin, line, isunicode, bigendian);
812 }
813 else if (!err) err = 2;
814 }
815
816 // get the macro value
817 if (!err && c == '{') {
818 c = my_uni_get(fin, line, isunicode, bigendian); // skip the {
819 while ((!fin.eof()) && (c != '}')) {
820 if (c == '\\') {
821 macrovalue.push_back(c); // keep the '\'
822 c = my_uni_get(fin, line, isunicode, bigendian); // store the *next* value regardless
823 if (!fin.eof()) macrovalue.push_back(c);
824 c = my_uni_get(fin, line, isunicode, bigendian);
825 }
826 macrovalue.push_back(c);
827 c = my_uni_get(fin, line, isunicode, bigendian);
828 }
829
830 if (c == '}') {
831 c = my_uni_get(fin, line, isunicode, bigendian); // skip the }
832
833 // define the macro
834
835 err = setparammacro (macrotable, macropackage, macroname, macroparameters,
836 thisfilename, macrovalue);
837 if ((err == -1 || err == -3) && logout != NULL) {
838 (*logout) << text_t2ascii << "Warning: redefinition of _" <<
839 package << ":" << macroname << "_[" << macroparameters <<
840 "] on line ";
841 (*logout) << line;
842 (*logout) << text_t2ascii << " of " << thisfilename << "\n";
843
844 } else if (err == -2 && logout != NULL) {
845 // I don't think we really want this warning since it's a very
846 // common occurance - Stefan.
847
848 // (*logout) << text_t2ascii << "Warning: _" <<
849 // package << ":" << macroname << "_[" << macroparameters <<
850 // "] on line ";
851 // (*logout) << line;
852 // (*logout) << text_t2ascii << " of " <<
853 // thisfilename << " hides a Global macro with the same name\n";
854
855 } else if (err == -4 && logout != NULL) {
856 (*logout) << text_t2ascii << "Error: macro name expected on line ";
857 (*logout) << line ;
858 (*logout) << text_t2ascii << " of " << thisfilename << "\n";
859 }
860
861 err = 0; // for the test below
862 }
863 else if (!err) err = 3;
864 }
865 else if (!err) err = 4;
866
867 if (err) {
868 // found an error, skip to the end of the line
869 if (logout != NULL) {
870 (*logout) << text_t2ascii << "Error: ";
871 if (err == 1) (*logout) << text_t2ascii << "'_'";
872 else if (err == 2) (*logout) << text_t2ascii << "']'";
873 else if (err == 3) (*logout) << text_t2ascii << "'}'";
874 else if (err == 4) (*logout) << text_t2ascii << "'{'";
875 (*logout) << text_t2ascii << " expected on line ";
876 (*logout) << line ;
877 (*logout) << text_t2ascii << " of " << thisfilename << "\n";
878 }
879 while (!fin.eof ()) {
880 if (c == '\n') break;
881 c = my_uni_get(fin, line, isunicode, bigendian);
882 }
883 }
884
885 } else {
886 // found an error, skip to the end of the line
887 if (logout != NULL) {
888 (*logout) << text_t2ascii << "Error: Unexpected input on line " << line
889 << " of " << thisfilename << "\n";
890 }
891 while (!fin.eof ()) {
892 if (c == '\n') break;
893 c = my_uni_get(fin, line, isunicode, bigendian);
894 }
895
896 }
897 }
898
899 fin.close ();
900
901 // free up memory
902 delete filenamestr;
903 return 0;
904}
905
906
907// loads a default macro file (if it isn't already loaded)
908// returns 0 if didn't need to load the file (it was already loaded)
909// 1 if was (re)loaded
910// -1 an error occurred while trying to load the file
911int displayclass::loaddefaultmacros (text_t thisfilename)
912{
913 return loadparammacros(defaultmacros,thisfilename);
914}
915
916// loads a collection specific macro file
917// returns 0 if didn't need to load the file (it was already loaded)
918// 1 if was (re)loaded
919// -1 an error occurred while trying to load the file
920int displayclass::loadcollectionmacros (text_t thisfilename)
921{
922 return loadparammacros(collectionmacros,thisfilename);
923}
924
925
926void displayclass::unloaddefaultmacros () {
927 defaultmacros->clear();
928}
929
930void displayclass::unloadcollectionmacros () {
931 collectionmacros->clear();
932}
933
934// prepares to create a page.
935void displayclass::openpage (const text_t &thispageparams,
936 const text_t &thisprecedence)
937{
938 // init variables
939 currentmacros->clear();
940
941 // reload any default macro files which have changed.
942 if (checkdefaultmacrofiles() < 0)
943 {
944 // print error message
945 }
946
947 setpageparams (thispageparams, thisprecedence);
948}
949
950
951// changes the parameters for the current page.
952void displayclass::setpageparams (text_t thispageparams,
953 text_t thisprecedence)
954{
955 paramhashtype thissplitparams, splittheseparams;
956 precedencetype precedhash;
957 text_tset::iterator text_tsetit;
958 paramspec tmpps;
959
960 precedence = thisprecedence;
961 calcprecedence(thisprecedence, precedhash);
962
963 splitparams(thispageparams, thissplitparams);
964 joinparams(thissplitparams, thispageparams);
965 params = thispageparams;
966
967 // get a list of parameters with their specificness
968 orderparamlist->erase(orderparamlist->begin(), orderparamlist->end());
969 for (text_tsetit=allparams.begin();
970 text_tsetit!=allparams.end();
971 text_tsetit++)
972 {
973 tmpps.param = (*text_tsetit);
974 splitparams(tmpps.param, splittheseparams);
975 tmpps.spec = getspecificness(thissplitparams, splittheseparams, precedhash);
976 if (tmpps.spec >= 0)
977 {
978 orderparamlist->push_back (tmpps);
979 }
980 }
981
982 // sort the list
983 sort(orderparamlist->begin(), orderparamlist->end());
984
985// paramspeclist::iterator pshere = orderparamlist->begin();
986// paramspeclist::iterator psend = orderparamlist->end();
987// while (pshere != psend)
988// {
989// cerr << text_t2ascii << "param=" << (*pshere).param;
990// cerr << " spec=" << (int)((*pshere).spec) << "\n";
991// pshere++;
992// }
993}
994
995
996// overrides (or sets) a macro for the current page.
997// returns 0 if there was no error,
998// -1 if it redefined a macro
999// -4 if no macroname was supplied
1000int displayclass::setmacro (const text_t &macroname,
1001 text_t package,
1002 const text_t &macrovalue)
1003{
1004 // make sure a macroname was supplied
1005 if (macroname.empty()) return -4;
1006
1007 // make package "Global" if it doesn't point to anything yet
1008 if (package.empty()) package = "Global";
1009
1010 // set the macro
1011 return currentmacros->setmacro(package, macroname, "memory", macrovalue);
1012}
1013
1014
1015void displayclass::expandstring (const text_t &inputtext, text_t &outputtext)
1016{
1017 expandstring("", inputtext, outputtext);
1018}
1019
1020
1021void displayclass::expandstring (text_t package, const text_t &inputtext,
1022 text_t &outputtext, int recursiondepth)
1023{
1024 text_t macroname, macropackage, macroargs;
1025 text_t::const_iterator tthere = inputtext.begin();
1026 text_t::const_iterator ttend = inputtext.end();
1027 unsigned short c = '\0';
1028 int openbrackets=0;
1029
1030 if (package.empty()) package = "Global";
1031
1032 outputtext.clear();
1033 // use one-character lookahead
1034 if (tthere != ttend) c = (*tthere);
1035 while (tthere != ttend)
1036 {
1037 if (c == '\\')
1038 {
1039 // found an escape character
1040 c = my_ttnextchar (tthere, ttend); // skip the escape character
1041 if (tthere != ttend)
1042 {
1043 if (c == 'n') outputtext.push_back('\n');
1044 else if (c == 't') outputtext.push_back('\t');
1045 else outputtext.push_back(c);
1046 }
1047 c = my_ttnextchar (tthere, ttend);
1048
1049 }
1050 else if (c == '_')
1051 {
1052 // might have found the start of a macro
1053
1054 // embedded macros (macros within the names
1055 // of other macros) are no longer supported -- Rodger.
1056
1057 // save the current state (in case we need to back out)
1058 text_t::const_iterator savedtthere = tthere;
1059 unsigned short savedc = c;
1060
1061 macroname.clear();
1062 macropackage = package;
1063 macroargs.clear();
1064
1065 c = my_ttnextchar (tthere, ttend); // skip the '_'
1066
1067 // get the macroname
1068 while (tthere != ttend && (!is_unicode_space(c)) &&
1069 (c != '\\') && (c != '_') &&(c != ':') &&
1070 (macroname.size() < 80))
1071 {
1072 macroname.push_back((unsigned char)c);
1073 c = my_ttnextchar (tthere, ttend);
1074 }
1075
1076 if (c == ':')
1077 {
1078 // we actually had the macro package
1079 c = my_ttnextchar (tthere, ttend);
1080 macropackage = macroname;
1081 macroname.clear ();
1082
1083 // get the macro name (honest!)
1084 while ((tthere != ttend) && (!is_unicode_space(c)) &&
1085 (c != '\\') && (c != '_') &&(c != ':') &&
1086 (macroname.size() < 80))
1087 {
1088 macroname.push_back((unsigned char)c);
1089 c = my_ttnextchar (tthere, ttend);
1090 }
1091 }
1092
1093 if ((tthere != ttend) && (c == '_') &&
1094 (macroname.size() > 0))
1095 {
1096 // found a macro!!!! (maybe)
1097 c = my_ttnextchar (tthere, ttend); // skip the '_'
1098
1099 // get the parameters (if there are any)
1100 if ((tthere != ttend) && (c == '('))
1101 {
1102 c = my_ttnextchar (tthere, ttend); // skip '('
1103 openbrackets++;
1104 // have to be careful of quotes
1105 unsigned short quote = '\0';
1106 while (tthere != ttend)
1107 {
1108 if (c == '\\')
1109 {
1110 // have an escape character, save escape
1111 // character and next character as well
1112 macroargs.push_back (c);
1113 c = my_ttnextchar (tthere, ttend);
1114
1115 }
1116 else if (quote == '\0')
1117 {
1118 // not in a quote at the moment
1119 if (c == '(')
1120 {
1121 openbrackets++;
1122//// macroargs.push_back (c);
1123//// c = my_ttnextchar (tthere, ttend);
1124 }
1125 else if (c == ')')
1126 {
1127 openbrackets--;
1128 if (openbrackets==0) {
1129 // found end of the arguments
1130 c = my_ttnextchar (tthere, ttend); // skip ')'
1131 break;
1132 } else {
1133 // nested brackets
1134 macroargs.push_back (c);
1135 c = my_ttnextchar (tthere, ttend);
1136 }
1137 }
1138 else if (c == '\'' || c == '\"')
1139 {
1140 // found a quote
1141 quote = c;
1142 }
1143
1144 }
1145 else
1146 {
1147 // we are in a quote
1148 if (c == quote) quote = '\0';
1149 }
1150
1151 // save this character
1152 if (tthere != ttend) macroargs.push_back (c);
1153 c = my_ttnextchar (tthere, ttend);
1154 } // while (tthere != ttend)
1155 }
1156
1157 //
1158 // we now have the macropackage, macroname, and macroargs
1159 //
1160
1161 // try to expand out this macro
1162 text_t expandedmacro;
1163 if (macro (macroname, macropackage, macroargs, expandedmacro, recursiondepth))
1164 {
1165 // append the expanded macro
1166 outputtext += expandedmacro;
1167 }
1168 else
1169 {
1170 // wasn't a macro
1171 tthere = savedtthere;
1172 c = savedc;
1173 outputtext.push_back(c); // the '_'
1174 c = my_ttnextchar (tthere, ttend);
1175 }
1176
1177
1178 }
1179 else
1180 {
1181 // wasn't a macro
1182 tthere = savedtthere;
1183 c = savedc;
1184 outputtext.push_back(c); // the '_'
1185 c = my_ttnextchar (tthere, ttend);
1186 }
1187
1188 }
1189 else
1190 {
1191 // nothing of interest
1192 outputtext.push_back(c); // the '_'
1193 c = my_ttnextchar (tthere, ttend);
1194 }
1195 } // end of while (tthere != ttend)
1196}
1197
1198ostream *displayclass::setlogout (ostream *thelogout) {
1199
1200 ostream *oldlogout = logout;
1201 logout = thelogout;
1202 return oldlogout;
1203}
1204
1205// protected methods for display
1206
1207// reloads any default macro files which have changed
1208// returns 0 no errors occurred
1209// -1 an error occurred while trying to load one of the files
1210int displayclass::checkdefaultmacrofiles ()
1211{
1212 // this isn't implemented yet
1213 return 0;
1214}
1215
1216// isdefaultmacro sees if there is an entry in the list of
1217// default macros with the given package and macro name
1218// returns 0 if no macros in the package or in the global package
1219// were found
1220// 1 if no macros in the package were found but a macro
1221// in the global package was found
1222// 2 if a macro in the given package was found
1223int displayclass::isdefaultmacro (text_t package, const text_t &macroname) {
1224 // make sure a macroname was supplied
1225 if (macroname.empty()) return 0;
1226
1227 // make package "Global" if it doesn't point to anything yet
1228 if (package.empty()) package = "Global";
1229
1230 // check the given package
1231 if (defaultmacros->macro_find(package, macroname) != NULL) return 2;
1232
1233 // check the Global package
1234 if (defaultmacros->macro_find("Global", macroname) != NULL) return 1;
1235
1236 return 0;
1237}
1238
1239
1240// setparammacro adds an entry to the list of default macros
1241// returns 0 if there was no error,
1242// -1 if it redefined a macro
1243// -2 if it hid a Global macro
1244// -3 if it redefined a macro and hid a Global macro
1245// -4 if no macroname was supplied
1246int displayclass::setparammacro (parammacros_t* macrotable,
1247 text_t package, const text_t &macroname,
1248 text_t params, const text_t &filename,
1249 const text_t &macrovalue)
1250{
1251 // make sure a macroname was supplied
1252 if (macroname.empty()) return -4;
1253
1254 // put the parameters in a standard form
1255 paramhashtype paramhash;
1256 if (params.empty()) params = "ignore=yes";
1257 splitparams (params, paramhash);
1258 joinparams (paramhash, params);
1259
1260 // make package "Global" if it doesn't point to anything yet
1261 if (package.empty()) package = "Global";
1262
1263 // remember these parameters
1264 allparams.insert (params);
1265
1266 // remember this filename (this part isn't finished yet -- Rodger).
1267
1268 // set the macro
1269 return macrotable->setmacro(package, macroname, params, filename, macrovalue);
1270}
1271
1272// setdefaultmacro adds an entry to the list of default macros
1273// returns 0 if there was no error,
1274// -1 if it redefined a macro
1275// -2 if it hid a Global macro
1276// -3 if it redefined a macro and hid a Global macro
1277// -4 if no macroname was supplied
1278int displayclass::setdefaultmacro (text_t package, const text_t &macroname,
1279 text_t params, const text_t &filename,
1280 const text_t &macrovalue)
1281{
1282 return setparammacro(defaultmacros,package,macroname,params,filename,macrovalue);
1283}
1284
1285
1286// setcollectionmacro adds an entry to the list of collection specific macros
1287// returns 0 if there was no error,
1288// -1 if it redefined a macro
1289// -2 if it hid a Global macro
1290// -3 if it redefined a macro and hid a Global macro
1291// -4 if no macroname was supplied
1292int displayclass::setcollectionmacro (text_t package, const text_t &macroname,
1293 text_t params, const text_t &filename,
1294 const text_t &macrovalue)
1295{
1296 return setparammacro(collectionmacros,package,macroname,params,filename,macrovalue);
1297}
1298
1299
1300// evaluates a boolean expression
1301// returns false if expr equals "" or "0".
1302// otherwise returns true *unless* expr is
1303// format "XXXX" eq/ne "dddd" in which case
1304// the two quoted strings are compared
1305bool displayclass::boolexpr (text_t package, const text_t &expr, int recursiondepth) {
1306
1307 if (expr.empty()) return false;
1308
1309 text_t expexpr;
1310 if (package.empty()) package = "Global";
1311 expandstring (package, expr, expexpr, recursiondepth);
1312
1313 if (expexpr.empty() || expexpr == "0") return false;
1314 if (expr[0] != '\"') return true;
1315
1316 // don't use expexpr while separating quoted parts of
1317 // expression just in case expanded out macros contain
1318 // quotes
1319 text_t::const_iterator here = expr.begin();
1320 text_t::const_iterator end = expr.end();
1321
1322 int quotecount = 0;
1323 text_t string1, expstring1;
1324 text_t string2, expstring2;
1325 text_t op;
1326 text_t combineop;
1327 bool result = false; // an empty string is false
1328 bool result2 = false;
1329
1330 while (here != end) {
1331 // get a comparison
1332 quotecount = 0;
1333 string1.clear();
1334 string2.clear();
1335 op.clear();
1336 while (here != end) {
1337 if (*here == '"') quotecount++;
1338 else if (quotecount == 1) string1.push_back(*here);
1339 else if ((quotecount == 2) && !is_unicode_space (*here))
1340 op.push_back(*here);
1341 else if (quotecount == 3) string2.push_back(*here);
1342 else if (quotecount >= 4) break;
1343 here ++;
1344 }
1345
1346 expandstring (package, string1, expstring1, recursiondepth);
1347 expandstring (package, string2, expstring2, recursiondepth);
1348
1349 // get next result
1350 result2 = true; // any badly formatted string will return true
1351 if (op == "eq") result2 = (expstring1 == expstring2);
1352 else if (op == "ne") result2 = (expstring1 != expstring2);
1353 else if (op == "gt") result2 = (expstring1 > expstring2);
1354 else if (op == "ge") result2 = (expstring1 >= expstring2);
1355 else if (op == "lt") result2 = (expstring1 < expstring2);
1356 else if (op == "le") result2 = (expstring1 <= expstring2);
1357 else if (op == "==") result2 = (expstring1.getint() == expstring2.getint());
1358 else if (op == "!=") result2 = (expstring1.getint() != expstring2.getint());
1359 else if (op == ">") result2 = (expstring1.getint() > expstring2.getint());
1360 else if (op == ">=") result2 = (expstring1.getint() >= expstring2.getint());
1361 else if (op == "<") result2 = (expstring1.getint() < expstring2.getint());
1362 else if (op == "<=") result2 = (expstring1.getint() <= expstring2.getint());
1363
1364 // combine the results
1365 if (combineop == "&&") result = (result && result2);
1366 else if (combineop == "||") result = (result || result2);
1367 else result = result2;
1368
1369 // get next combination operator
1370 combineop.clear();
1371 while (here != end && *here != '"') {
1372 if (!is_unicode_space(*here)) combineop.push_back(*here);
1373 here++;
1374 }
1375 }
1376
1377 return result;
1378}
1379
1380
1381mvalue* displayclass::macro_find (parammacros_t* macrotable, text_t packagename,
1382 const text_t &macroname)
1383{
1384 mvalue *macrovalue = NULL;
1385
1386 // next look in the named parammacros for the named package
1387 if (macrotable->macro_find (packagename, macroname) != NULL)
1388 {
1389 paramspeclist::iterator paramhere, paramend;
1390
1391 paramhere = orderparamlist->begin(); paramend = orderparamlist->end();
1392 while ((macrovalue == NULL) && (paramhere != paramend))
1393 {
1394 macrovalue = macrotable->parameter_find(packagename, macroname,
1395 (*paramhere).param);
1396 paramhere++;
1397 }
1398 }
1399
1400 return macrovalue;
1401}
1402
1403
1404// (recursively) expand out a macro
1405// returns true if the macro was found
1406// false if the macro was not found
1407bool displayclass::macro (const text_t &macroname, text_t macropackage,
1408 const text_t &macroparam, text_t &outputtext,
1409 int recursiondepth)
1410{
1411 outconvertclass text_t2ascii;
1412 text_tlist splitmacroparam;
1413 text_t::const_iterator hereit, endit;
1414 text_t aparam;
1415 unsigned short c = '\0', quote = '\0';
1416
1417 // cerr << "r: " << recursiondepth << "\n";
1418 // check for deep recursion
1419 if (recursiondepth >= MAXRECURSIONDEPTH)
1420 {
1421 if (logout != NULL)
1422 (*logout) << "Warning: deep recursion, limiting macro expansion\n";
1423 return false;
1424 }
1425
1426 // check the macropackage
1427 if (macropackage.empty()) macropackage = "Global";
1428
1429 // get the parameters (but don't expand them)
1430 if (macroparam.size() > 0) {
1431 // set up for loop
1432 hereit = macroparam.begin();
1433 endit = macroparam.end();
1434 if (hereit != endit) c = (*hereit);
1435 int openbrackets=0; // keep track of bracket level eg _If_(1, _macro_(arg))
1436 // this allows commas inside brackets (as arguments)
1437 while (hereit != endit) {
1438 // get the next parameter
1439 aparam.clear();
1440 quote = '\0'; // not-quoted unless proven quoted
1441
1442 // ignore initial whitespace
1443 while ((hereit!=endit)&&is_unicode_space(c)) c=my_ttnextchar(hereit,endit);
1444
1445 // look for the end of the parameter
1446 while (hereit != endit) {
1447 if (c == '\\') {
1448 // found escape character, also get next character
1449 aparam.push_back(c);
1450 c = my_ttnextchar (hereit, endit);
1451 if (hereit != endit) aparam.push_back(c);
1452
1453 } else if (quote=='\0' && (c=='\'' /*|| c=='"'*/)) {
1454 // found a quoted section
1455 quote = c;
1456 if (openbrackets>0) aparam.push_back(c);
1457
1458 } else if (quote!='\0' && c==quote) {
1459 // found the end of a quote
1460 quote = '\0';
1461 if (openbrackets<0) aparam.push_back(c);
1462
1463 } else if (quote=='\0' && (c==',' || c==')') && openbrackets==0) {
1464 // found the end of a parameter
1465 c = my_ttnextchar (hereit, endit);
1466 break;
1467
1468 } else if (c=='(') {
1469 aparam.push_back(c);
1470 openbrackets++;
1471 } else if (c==')') {
1472 openbrackets--;
1473 aparam.push_back(c);
1474 } else {
1475 // ordinary character
1476 aparam.push_back(c);
1477 }
1478
1479 c = my_ttnextchar (hereit, endit);
1480 }
1481
1482
1483 // add this parameter to the list
1484 splitmacroparam.push_back(aparam);
1485 }
1486 }
1487
1488 if (macroname == "If") {
1489 // get the condition, then clause and else clause
1490 text_tlist::iterator paramcond = splitmacroparam.begin();
1491 text_tlist::iterator paramend = splitmacroparam.end();
1492 text_tlist::iterator paramthen = paramend;
1493 text_tlist::iterator paramelse = paramend;
1494
1495 if (paramcond != paramend) {
1496 paramthen = paramcond;
1497 paramthen++;
1498 }
1499 if (paramthen != paramend) {
1500 paramelse = paramthen;
1501 paramelse++;
1502 }
1503
1504 // will always output something
1505 outputtext.clear();
1506
1507
1508 // expand out the first parameter
1509 if (paramcond != paramend) {
1510 /// text_t tmpoutput;
1511 /// expandstring (macropackage, *paramcond, tmpoutput, recursiondepth+1);
1512 /// lc (tmpoutput);
1513 // test the expanded string
1514 if (boolexpr (macropackage, *paramcond, recursiondepth+1)) {
1515 //(tmpoutput.size()) && (tmpoutput != "false") && (tmpoutput != "0")) {
1516 // true
1517 if (paramthen != paramend)
1518 expandstring (macropackage, *paramthen, outputtext, recursiondepth+1);
1519 } else {
1520 // false
1521 if (paramelse != paramend)
1522 expandstring (macropackage, *paramelse, outputtext, recursiondepth+1);
1523 }
1524 }
1525
1526 return true;
1527 }
1528
1529 // try and find this macro
1530
1531 // this list might be replaced by something a little more
1532 // sophisticated in the future.
1533 text_tlist packagelist;
1534 packagelist.push_back (macropackage);
1535 packagelist.push_back ("Global");
1536
1537 text_tlist::iterator packagehere = packagelist.begin();
1538 text_tlist::iterator packageend = packagelist.end();
1539 mvalue *macrovalue = NULL;
1540 while ((macrovalue == NULL) && (packagehere != packageend))
1541 {
1542 // first look in the currentmacros
1543 macrovalue = currentmacros->macro_find(*packagehere, macroname);
1544 if (macrovalue == NULL)
1545 macrovalue = currentmacros->macro_find("Style", macroname);
1546
1547 // look in the collection specific macros
1548 if (macrovalue == NULL) {
1549 macrovalue = macro_find(collectionmacros,*packagehere,macroname);
1550 }
1551
1552 // and again in the collection specific package "Style"
1553 if (macrovalue == NULL) {
1554 macrovalue = macro_find(collectionmacros,(char*)"Style",macroname);
1555 }
1556
1557 // look in the default macros
1558 if (macrovalue == NULL) {
1559 macrovalue = macro_find(defaultmacros,*packagehere,macroname);
1560 }
1561
1562 // and again in the package "Style"
1563 if (macrovalue == NULL) {
1564 macrovalue = macro_find(defaultmacros,(char*)"Style",macroname);
1565 }
1566
1567 if (macrovalue == NULL) packagehere++;
1568 }
1569
1570 if (macrovalue != NULL)
1571 {
1572 // we found a macro
1573
1574 // replace all the option macros (_1_, _2_, ...) in the value of this macro
1575 // and decide if we need to expand the value of this macro
1576 bool needsexpansion = false;
1577 text_t tempmacrovalue;
1578 tempmacrovalue.clear();
1579 hereit = macrovalue->value.begin();
1580 endit = macrovalue->value.end();
1581 if (hereit != endit) c = (*hereit);
1582 while (hereit != endit)
1583 {
1584 if (c == '\\')
1585 {
1586 // found an escape character
1587 needsexpansion = true;
1588 tempmacrovalue.push_back(c);
1589 c = my_ttnextchar (hereit, endit);
1590 if (hereit != endit) tempmacrovalue.push_back(c);
1591 c = my_ttnextchar (hereit, endit);
1592 }
1593 else if (c == '_')
1594 {
1595 text_t::const_iterator savedhereit = hereit;
1596
1597 // attempt to get a number
1598 int argnum = 0;
1599 unsigned short digc = my_ttnextchar (hereit, endit);
1600 while (digc >= '0' && digc <= '9') {
1601 argnum = argnum*10 + digc - '0';
1602 digc = my_ttnextchar (hereit, endit);
1603 }
1604 if (digc == '_' && argnum > 0) {
1605 // found an option macro, append the appropriate text
1606 text_tlist::iterator ttlisthere = splitmacroparam.begin();
1607 text_tlist::iterator ttlistend = splitmacroparam.end();
1608 while ((argnum > 1) && (ttlisthere != ttlistend)) {
1609 argnum--;
1610 ttlisthere++;
1611 }
1612 if (ttlisthere != ttlistend) {
1613 tempmacrovalue.append(*ttlisthere);
1614 if (findchar((*ttlisthere).begin(), (*ttlisthere).end(), '_') != (*ttlisthere).end())
1615 needsexpansion = true;
1616 }
1617
1618 c = my_ttnextchar (hereit, endit);
1619
1620 } else {
1621 // wasn't a option macro
1622 needsexpansion = true;
1623 tempmacrovalue.push_back(c);
1624 hereit = savedhereit;
1625 c = my_ttnextchar (hereit, endit);
1626 }
1627 }
1628 else
1629 {
1630 tempmacrovalue.push_back(c);
1631 c = my_ttnextchar (hereit, endit);
1632 }
1633 }
1634
1635 // recursively replace this macro (if we need to)
1636 if (needsexpansion) {
1637 expandstring (*packagehere, tempmacrovalue, outputtext, recursiondepth+1);
1638 } else {
1639 outputtext += tempmacrovalue;
1640 }
1641
1642 return true;
1643 }
1644
1645 // GRB: deal with remaining code properly as it faults
1646 return false;
1647
1648 if (logout != NULL) {
1649 (*logout) << text_t2ascii << "Warning: _" <<
1650 macropackage << ":" << macroname << "_ not found\n";
1651 }
1652
1653 return false; // didn't find the macro
1654}
1655
1656
1657void displayclass::printdefaultmacros ()
1658{
1659 defpackagemap::const_iterator packagemapit;
1660 defmacromap::const_iterator macromapit;
1661 defparammap::const_iterator parammapit;
1662 const mvalue *mvalueptr;
1663 outconvertclass text_t2ascii;
1664
1665 text_t packagename, macroname, macroparam;
1666
1667 for (packagemapit = defaultmacros->package_begin();
1668 packagemapit != defaultmacros->package_end();
1669 packagemapit++)
1670 {
1671 packagename = (*packagemapit).first;
1672 // cerr << "***package : \"" << packagename << "\"\n";
1673
1674 for (macromapit = (*packagemapit).second.begin();
1675 macromapit != (*packagemapit).second.end();
1676 macromapit++)
1677 {
1678 macroname = (*macromapit).first;
1679 // cerr << "***macroname : \"" << macroname << "\"\n";
1680
1681 for (parammapit = (*macromapit).second.begin();
1682 parammapit != (*macromapit).second.end();
1683 parammapit++)
1684 {
1685 macroparam = (*parammapit).first;
1686 mvalueptr = &((*parammapit).second);
1687 cerr << text_t2ascii << "package : \"" << packagename << "\"\n";
1688 cerr << text_t2ascii << "macroname : \"" << macroname << "\"\n";
1689 cerr << text_t2ascii << "parameters: [" << macroparam << "]\n";
1690 cerr << text_t2ascii << "filename : \"" << mvalueptr->filename << "\"\n";
1691 cerr << text_t2ascii << "value : {" << mvalueptr->value << "}\n\n";
1692 }
1693 }
1694 }
1695}
1696
1697void displayclass::printallparams ()
1698{
1699 text_tset::iterator text_tsetit;
1700 outconvertclass text_t2ascii;
1701
1702 cerr << text_t2ascii << "** allparams\n";
1703 for (text_tsetit=allparams.begin();
1704 text_tsetit!=allparams.end();
1705 text_tsetit++) {
1706 cerr << text_t2ascii << (*text_tsetit) << "\n";
1707 }
1708 cerr << text_t2ascii << "\n";
1709}
1710
1711
1712
1713/////////////////////////////////////
1714// stuff to do tricky output
1715/////////////////////////////////////
1716
1717displayclass &operator<< (outconvertclass &theoutc, displayclass &display)
1718{
1719 display.setconvertclass (&theoutc);
1720 return display;
1721}
1722
1723displayclass &operator<< (displayclass &display, const text_t &t)
1724{
1725 outconvertclass *theoutc = display.getconvertclass();
1726
1727 if (theoutc == NULL) return display;
1728
1729 text_t output;
1730 display.expandstring (t, output);
1731 (*theoutc) << output;
1732
1733 return display;
1734}
Note: See TracBrowser for help on using the repository browser.