1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Miscellaneous utilities
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
44 wxHashTable
TexReferences(wxKEY_STRING
);
45 wxList
BibList(wxKEY_STRING
);
46 wxStringList CitationList
;
47 wxList
ColourTable(wxKEY_STRING
);
48 wxHashTable
BibStringTable(wxKEY_STRING
);
49 wxList
CustomMacroList(wxKEY_STRING
);
50 TexChunk
*currentSection
= NULL
;
51 char *fakeCurrentSection
= NULL
;
53 static long BibLine
= 1;
55 void OutputCurrentSection(void)
57 if (fakeCurrentSection
)
58 TexOutput(fakeCurrentSection
);
59 else if (currentSection
)
60 TraverseChildrenFromChunk(currentSection
);
63 // Nasty but the way things are done now, necessary,
64 // in order to output a chunk properly to a string (macros and all).
65 void OutputCurrentSectionToString(char *buf
)
67 if (fakeCurrentSection
)
68 strcpy(buf
, fakeCurrentSection
);
70 OutputChunkToString(currentSection
, buf
);
73 void OutputChunkToString(TexChunk
*chunk
, char *buf
)
75 FILE *tempfd
= fopen("tmp.tmp", "w");
79 FILE *old1
= CurrentOutput1
;
80 FILE *old2
= CurrentOutput2
;
82 CurrentOutput1
= tempfd
;
83 CurrentOutput2
= NULL
;
85 TraverseChildrenFromChunk(chunk
);
87 CurrentOutput1
= old1
;
88 CurrentOutput2
= old2
;
92 // Read from file into string
93 tempfd
= fopen("tmp.tmp", "r");
112 wxRemoveFile("tmp.tmp");
115 // Called by Tex2Any to simulate a section
116 void FakeCurrentSection(char *fakeSection
, bool addToContents
)
118 currentSection
= NULL
;
119 if (fakeCurrentSection
) delete[] fakeCurrentSection
;
120 fakeCurrentSection
= copystring(fakeSection
);
122 if (DocumentStyle
== LATEX_ARTICLE
)
124 int mac
= ltSECTIONHEADING
;
126 mac
= ltSECTIONHEADINGSTAR
;
127 OnMacro(mac
, 0, TRUE
);
128 OnMacro(mac
, 0, FALSE
);
132 int mac
= ltCHAPTERHEADING
;
134 mac
= ltCHAPTERHEADINGSTAR
;
135 OnMacro(mac
, 0, TRUE
);
136 OnMacro(mac
, 0, FALSE
);
138 if (fakeCurrentSection
) delete[] fakeCurrentSection
;
139 fakeCurrentSection
= NULL
;
142 // Look for \label macro, use this ref name if found or
143 // make up a topic name otherwise.
144 static long topicCounter
= 0;
146 void ResetTopicCounter(void)
151 static char *forceTopicName
= NULL
;
153 void ForceTopicName(char *name
)
156 delete[] forceTopicName
;
158 forceTopicName
= copystring(name
);
160 forceTopicName
= NULL
;
163 char *FindTopicName(TexChunk
*chunk
)
166 return forceTopicName
;
168 char *topicName
= NULL
;
169 static char topicBuf
[100];
171 if (chunk
&& (chunk
->type
== CHUNK_TYPE_MACRO
) &&
172 (chunk
->macroId
== ltLABEL
))
174 wxNode
*node
= chunk
->children
.First();
177 TexChunk
*child
= (TexChunk
*)node
->Data();
178 if (child
->type
== CHUNK_TYPE_ARG
)
180 wxNode
*snode
= child
->children
.First();
183 TexChunk
*schunk
= (TexChunk
*)snode
->Data();
184 if (schunk
->type
== CHUNK_TYPE_STRING
)
185 topicName
= schunk
->value
;
194 sprintf(topicBuf
, "topic%ld", topicCounter
);
201 * Simulate argument data, so we can 'drive' clients which implement
202 * certain basic formatting behaviour.
203 * Snag is that some save a TexChunk, so don't use yet...
207 void StartSimulateArgument(char *data
)
209 strcpy(currentArgData
, data
);
213 void EndSimulateArgument(void)
219 * Parse and convert unit arguments to points
223 int ParseUnitArgument(char *unitArg
)
225 float conversionFactor
= 1.0;
226 float unitValue
= 0.0;
227 int len
= strlen(unitArg
);
228 // Get rid of any accidentally embedded commands
229 for (int i
= 0; i
< len
; i
++)
230 if (unitArg
[i
] == '\\')
232 len
= strlen(unitArg
);
234 if (unitArg
&& (len
> 0) && (isdigit(unitArg
[0]) || unitArg
[0] == '-'))
236 sscanf(unitArg
, "%f", &unitValue
);
240 units
[0] = unitArg
[len
-2];
241 units
[1] = unitArg
[len
-1];
243 if (strcmp(units
, "in") == 0)
244 conversionFactor
= 72.0;
245 else if (strcmp(units
, "cm") == 0)
246 conversionFactor
= (float)72.0/(float)2.51;
247 else if (strcmp(units
, "mm") == 0)
248 conversionFactor
= (float)72.0/(float)25.1;
249 else if (strcmp(units
, "pt") == 0)
250 conversionFactor
= 1;
252 return (int)(unitValue
*conversionFactor
);
258 * Strip off any extension (dot something) from end of file,
259 * IF one exists. Inserts zero into buffer.
263 void StripExtension(char *buffer
)
265 int len
= strlen(buffer
);
269 if (buffer
[i
] == '.')
283 void SetFontSizes(int pointSize
)
335 void AddTexRef(char *name
, char *file
, char *sectionName
,
336 int chapter
, int section
, int subsection
, int subsubsection
)
338 TexRef
*texRef
= (TexRef
*)TexReferences
.Get(name
);
339 if (texRef
) TexReferences
.Delete(name
);
346 strcat(buf, sectionName);
353 sprintf(buf2
, "%d", chapter
);
362 sprintf(buf2
, "%d", section
);
369 sprintf(buf2
, "%d", subsection
);
376 sprintf(buf2
, "%d", subsubsection
);
379 char *tmp
= ((strlen(buf
) > 0) ? buf
: (char *)NULL
);
380 TexReferences
.Put(name
, new TexRef(name
, file
, tmp
, sectionName
));
383 void WriteTexReferences(char *filename
)
385 wxSTD ofstream
ostr(filename
);
386 if (ostr
.bad()) return;
389 TexReferences
.BeginFind();
390 wxNode
*node
= TexReferences
.Next();
394 TexRef
*ref
= (TexRef
*)node
->Data();
395 ostr
<< ref
->refLabel
<< " " << (ref
->refFile
? ref
->refFile
: "??") << " ";
396 ostr
<< (ref
->sectionName
? ref
->sectionName
: "??") << " ";
397 ostr
<< (ref
->sectionNumber
? ref
->sectionNumber
: "??") << "\n";
398 if (!ref
->sectionNumber
|| (strcmp(ref
->sectionNumber
, "??") == 0 && strcmp(ref
->sectionName
, "??") == 0))
400 sprintf(buf
, "Warning: reference %s not resolved.", ref
->refLabel
);
403 node
= TexReferences
.Next();
407 void ReadTexReferences(char *filename
)
409 if (!wxFileExists(filename
))
412 wxSTD ifstream
istr(filename
, ios::in
);
414 if (istr
.bad()) return;
419 char sectionName
[100];
429 istr
.get(ch
); // Read past space
432 while (ch
!= '\n' && !istr
.eof())
440 // gt - needed to trick the hash table "TexReferences" into deleting the key
441 // strings it creates in the Put() function, but not the item that is
442 // created here, as that is destroyed elsewhere. Without doing this, there
443 // were massive memory leaks
444 TexReferences
.DeleteContents(TRUE
);
445 TexReferences
.Put(label
, new TexRef(label
, file
, section
, sectionName
));
446 TexReferences
.DeleteContents(FALSE
);
453 * Bibliography-handling code
457 void BibEatWhiteSpace(wxSTD istream
& str
)
459 char ch
= str
.peek();
461 while (!str
.eof() && (ch
== ' ' || ch
== '\t' || ch
== 13 || ch
== 10 || ch
== EOF
))
466 if ((ch
== EOF
) || str
.eof()) return;
470 // Ignore end-of-line comments
471 if (ch
== '%' || ch
== ';' || ch
== '#')
475 while (ch
!= 10 && ch
!= 13 && !str
.eof())
480 BibEatWhiteSpace(str
);
484 // Read word up to { or , or space
485 void BibReadWord(wxSTD istream
& istr
, char *buffer
)
489 char ch
= istr
.peek();
490 while (!istr
.eof() && ch
!= ' ' && ch
!= '{' && ch
!= '(' && ch
!= 13 && ch
!= 10 && ch
!= '\t' &&
491 ch
!= ',' && ch
!= '=')
501 // Read string (double-quoted or not) to end quote or EOL
502 void BibReadToEOL(wxSTD istream
& istr
, char *buffer
)
506 char ch
= istr
.peek();
507 bool inQuotes
= FALSE
;
514 // If in quotes, read white space too. If not,
515 // stop at white space or comment.
516 while (!istr
.eof() && ch
!= 13 && ch
!= 10 && ch
!= '"' &&
517 (inQuotes
|| ((ch
!= ' ') && (ch
!= 9) &&
518 (ch
!= ';') && (ch
!= '%') && (ch
!= '#'))))
530 // Read }-terminated value, taking nested braces into account.
531 void BibReadValue(wxSTD istream
& istr
, char *buffer
, bool ignoreBraces
= TRUE
,
532 bool quotesMayTerminate
= TRUE
)
537 char ch
= istr
.peek();
538 bool stopping
= FALSE
;
539 while (!istr
.eof() && !stopping
)
545 sprintf(buf
, "Sorry, value > 4000 chars in bib file at line %ld, terminating.", BibLine
);
546 wxFatalError(buf
, "Tex2RTF Fatal Error");
562 else if (quotesMayTerminate
&& ch
== '"')
569 if (!ignoreBraces
|| (ch
!= '{' && ch
!= '}'))
581 bool ReadBib(char *filename
)
583 if (!wxFileExists(filename
))
587 wxSTD ifstream
istr(filename
, ios::in
);
588 if (istr
.bad()) return FALSE
;
592 OnInform("Reading .bib file...");
595 char fieldValue
[4000];
596 char recordType
[100];
598 char recordField
[100];
603 BibEatWhiteSpace(istr
);
607 sprintf(buf
, "Expected @: malformed bib file at line %ld (%s)", BibLine
, filename
);
611 BibReadWord(istr
, recordType
);
612 BibEatWhiteSpace(istr
);
614 if (ch
!= '{' && ch
!= '(')
616 sprintf(buf
, "Expected { or ( after record type: malformed .bib file at line %ld (%s)", BibLine
, filename
);
620 BibEatWhiteSpace(istr
);
621 if (StringMatch(recordType
, "string", FALSE
, TRUE
))
623 BibReadWord(istr
, recordType
);
624 BibEatWhiteSpace(istr
);
628 sprintf(buf
, "Expected = after string key: malformed .bib file at line %ld (%s)", BibLine
, filename
);
632 BibEatWhiteSpace(istr
);
634 if (ch
!= '"' && ch
!= '{')
636 sprintf(buf
, "Expected = after string key: malformed .bib file at line %ld (%s)", BibLine
, filename
);
640 BibReadValue(istr
, fieldValue
);
642 // Now put in hash table if necesary
643 if (!BibStringTable
.Get(recordType
))
644 BibStringTable
.Put(recordType
, (wxObject
*)copystring(fieldValue
));
646 // Read closing ) or }
647 BibEatWhiteSpace(istr
);
649 BibEatWhiteSpace(istr
);
653 BibReadWord(istr
, recordKey
);
655 BibEntry
*bibEntry
= new BibEntry
;
656 bibEntry
->key
= copystring(recordKey
);
657 bibEntry
->type
= copystring(recordType
);
659 bool moreRecords
= TRUE
;
660 while (moreRecords
&& !istr
.eof())
662 BibEatWhiteSpace(istr
);
664 if (ch
== '}' || ch
== ')')
670 BibEatWhiteSpace(istr
);
671 BibReadWord(istr
, recordField
);
672 BibEatWhiteSpace(istr
);
676 sprintf(buf
, "Expected = after field type: malformed .bib file at line %ld (%s)", BibLine
, filename
);
680 BibEatWhiteSpace(istr
);
682 if (ch
!= '{' && ch
!= '"')
685 BibReadWord(istr
, fieldValue
+1);
687 // If in the table of strings, replace with string from table.
688 char *s
= (char *)BibStringTable
.Get(fieldValue
);
691 strcpy(fieldValue
, s
);
695 BibReadValue(istr
, fieldValue
, TRUE
, (ch
== '"' ? TRUE
: FALSE
));
697 // Now we can add a field
698 if (StringMatch(recordField
, "author", FALSE
, TRUE
))
699 bibEntry
->author
= copystring(fieldValue
);
700 else if (StringMatch(recordField
, "key", FALSE
, TRUE
))
702 else if (StringMatch(recordField
, "annotate", FALSE
, TRUE
))
704 else if (StringMatch(recordField
, "abstract", FALSE
, TRUE
))
706 else if (StringMatch(recordField
, "edition", FALSE
, TRUE
))
708 else if (StringMatch(recordField
, "howpublished", FALSE
, TRUE
))
710 else if (StringMatch(recordField
, "note", FALSE
, TRUE
) || StringMatch(recordField
, "notes", FALSE
, TRUE
))
712 else if (StringMatch(recordField
, "series", FALSE
, TRUE
))
714 else if (StringMatch(recordField
, "type", FALSE
, TRUE
))
716 else if (StringMatch(recordField
, "keywords", FALSE
, TRUE
))
718 else if (StringMatch(recordField
, "editor", FALSE
, TRUE
) || StringMatch(recordField
, "editors", FALSE
, TRUE
))
719 bibEntry
->editor
= copystring(fieldValue
);
720 else if (StringMatch(recordField
, "title", FALSE
, TRUE
))
721 bibEntry
->title
= copystring(fieldValue
);
722 else if (StringMatch(recordField
, "booktitle", FALSE
, TRUE
))
723 bibEntry
->booktitle
= copystring(fieldValue
);
724 else if (StringMatch(recordField
, "journal", FALSE
, TRUE
))
725 bibEntry
->journal
= copystring(fieldValue
);
726 else if (StringMatch(recordField
, "volume", FALSE
, TRUE
))
727 bibEntry
->volume
= copystring(fieldValue
);
728 else if (StringMatch(recordField
, "number", FALSE
, TRUE
))
729 bibEntry
->number
= copystring(fieldValue
);
730 else if (StringMatch(recordField
, "year", FALSE
, TRUE
))
731 bibEntry
->year
= copystring(fieldValue
);
732 else if (StringMatch(recordField
, "month", FALSE
, TRUE
))
733 bibEntry
->month
= copystring(fieldValue
);
734 else if (StringMatch(recordField
, "pages", FALSE
, TRUE
))
735 bibEntry
->pages
= copystring(fieldValue
);
736 else if (StringMatch(recordField
, "publisher", FALSE
, TRUE
))
737 bibEntry
->publisher
= copystring(fieldValue
);
738 else if (StringMatch(recordField
, "address", FALSE
, TRUE
))
739 bibEntry
->address
= copystring(fieldValue
);
740 else if (StringMatch(recordField
, "institution", FALSE
, TRUE
) || StringMatch(recordField
, "school", FALSE
, TRUE
))
741 bibEntry
->institution
= copystring(fieldValue
);
742 else if (StringMatch(recordField
, "organization", FALSE
, TRUE
) || StringMatch(recordField
, "organisation", FALSE
, TRUE
))
743 bibEntry
->organization
= copystring(fieldValue
);
744 else if (StringMatch(recordField
, "comment", FALSE
, TRUE
) || StringMatch(recordField
, "comments", FALSE
, TRUE
))
745 bibEntry
->comment
= copystring(fieldValue
);
746 else if (StringMatch(recordField
, "annote", FALSE
, TRUE
))
747 bibEntry
->comment
= copystring(fieldValue
);
748 else if (StringMatch(recordField
, "chapter", FALSE
, TRUE
))
749 bibEntry
->chapter
= copystring(fieldValue
);
752 sprintf(buf
, "Unrecognised bib field type %s at line %ld (%s)", recordField
, BibLine
, filename
);
757 BibList
.Append(recordKey
, bibEntry
);
758 BibEatWhiteSpace(istr
);
764 void OutputBibItem(TexRef
*ref
, BibEntry
*bib
)
768 OnMacro(ltNUMBEREDBIBITEM
, 2, TRUE
);
769 OnArgument(ltNUMBEREDBIBITEM
, 1, TRUE
);
770 TexOutput(ref
->sectionNumber
);
771 OnArgument(ltNUMBEREDBIBITEM
, 1, FALSE
);
772 OnArgument(ltNUMBEREDBIBITEM
, 2, TRUE
);
775 OnMacro(ltBF
, 1, TRUE
);
776 OnArgument(ltBF
, 1, TRUE
);
778 TexOutput(bib
->author
);
779 OnArgument(ltBF
, 1, FALSE
);
780 OnMacro(ltBF
, 1, FALSE
);
781 if (bib
->author
&& (strlen(bib
->author
) > 0) && (bib
->author
[strlen(bib
->author
) - 1] != '.'))
788 TexOutput(bib
->year
);
793 TexOutput(bib
->month
);
796 if (bib
->year
|| bib
->month
)
799 if (StringMatch(bib
->type
, "article", FALSE
, TRUE
))
803 TexOutput(bib
->title
);
808 OnMacro(ltIT
, 1, TRUE
);
809 OnArgument(ltIT
, 1, TRUE
);
810 TexOutput(bib
->journal
);
811 OnArgument(ltIT
, 1, FALSE
);
812 OnMacro(ltIT
, 1, FALSE
);
817 OnMacro(ltBF
, 1, TRUE
);
818 OnArgument(ltBF
, 1, TRUE
);
819 TexOutput(bib
->volume
);
820 OnArgument(ltBF
, 1, FALSE
);
821 OnMacro(ltBF
, 1, FALSE
);
826 TexOutput(bib
->number
);
831 TexOutput(", pages ");
832 TexOutput(bib
->pages
);
836 else if (StringMatch(bib
->type
, "book", FALSE
, TRUE
) ||
837 StringMatch(bib
->type
, "unpublished", FALSE
, TRUE
) ||
838 StringMatch(bib
->type
, "manual", FALSE
, TRUE
) ||
839 StringMatch(bib
->type
, "phdthesis", FALSE
, TRUE
) ||
840 StringMatch(bib
->type
, "mastersthesis", FALSE
, TRUE
) ||
841 StringMatch(bib
->type
, "misc", FALSE
, TRUE
) ||
842 StringMatch(bib
->type
, "techreport", FALSE
, TRUE
) ||
843 StringMatch(bib
->type
, "booklet", FALSE
, TRUE
))
845 if (bib
->title
|| bib
->booktitle
)
847 OnMacro(ltIT
, 1, TRUE
);
848 OnArgument(ltIT
, 1, TRUE
);
849 TexOutput(bib
->title
? bib
->title
: bib
->booktitle
);
851 OnArgument(ltIT
, 1, FALSE
);
852 OnMacro(ltIT
, 1, FALSE
);
854 if (StringMatch(bib
->type
, "phdthesis", FALSE
, TRUE
))
855 TexOutput("PhD thesis. ");
856 if (StringMatch(bib
->type
, "techreport", FALSE
, TRUE
))
857 TexOutput("Technical report. ");
861 TexOutput(bib
->editor
);
864 if (bib
->institution
)
866 TexOutput(bib
->institution
);
869 if (bib
->organization
)
871 TexOutput(bib
->organization
);
876 TexOutput(bib
->publisher
);
881 TexOutput(bib
->address
);
885 else if (StringMatch(bib
->type
, "inbook", FALSE
, TRUE
) ||
886 StringMatch(bib
->type
, "inproceedings", FALSE
, TRUE
) ||
887 StringMatch(bib
->type
, "incollection", FALSE
, TRUE
) ||
888 StringMatch(bib
->type
, "conference", FALSE
, TRUE
))
892 TexOutput(bib
->title
);
896 TexOutput(", from ");
897 OnMacro(ltIT
, 1, TRUE
);
898 OnArgument(ltIT
, 1, TRUE
);
899 TexOutput(bib
->booktitle
);
901 OnArgument(ltIT
, 1, FALSE
);
902 OnMacro(ltIT
, 1, FALSE
);
907 TexOutput(bib
->editor
);
912 TexOutput(bib
->publisher
);
916 if (bib
->publisher
) TexOutput(", ");
918 TexOutput(bib
->address
);
920 if (bib
->publisher
|| bib
->address
)
926 OnMacro(ltBF
, 1, TRUE
);
927 OnArgument(ltBF
, 1, TRUE
);
928 TexOutput(bib
->volume
);
929 OnArgument(ltBF
, 1, FALSE
);
930 OnMacro(ltBF
, 1, FALSE
);
937 TexOutput(bib
->number
);
942 TexOutput(" Number ");
943 TexOutput(bib
->number
);
949 TexOutput(" Chap. "); TexOutput(bib
->chapter
);
953 if (bib
->chapter
) TexOutput(", pages ");
954 else TexOutput(" Pages ");
955 TexOutput(bib
->pages
);
959 OnArgument(ltNUMBEREDBIBITEM
, 2, FALSE
);
960 OnMacro(ltNUMBEREDBIBITEM
, 2, FALSE
);
966 ForceTopicName("bibliography");
967 FakeCurrentSection(ReferencesNameString
);
968 ForceTopicName(NULL
);
970 OnMacro(ltPAR
, 0, TRUE
);
971 OnMacro(ltPAR
, 0, FALSE
);
973 if ((convertMode
== TEX_RTF
) && !winHelp
)
975 OnMacro(ltPAR
, 0, TRUE
);
976 OnMacro(ltPAR
, 0, FALSE
);
979 wxNode
*node
= CitationList
.First();
982 char *citeKey
= (char *)node
->Data();
983 // wxNode *texNode = TexReferences.Find(citeKey);
984 TexRef
*ref
= (TexRef
*)TexReferences
.Get(citeKey
);
985 wxNode
*bibNode
= BibList
.Find(citeKey
);
988 BibEntry
*entry
= (BibEntry
*)bibNode
->Data();
989 OutputBibItem(ref
, entry
);
995 static int citeCount
= 1;
997 void ResolveBibReferences(void)
999 if (CitationList
.Number() > 0)
1000 OnInform("Resolving bibliographic references...");
1004 wxNode
*node
= CitationList
.First();
1008 char *citeKey
= (char *)node
->Data();
1009 // wxNode *texNode = TexReferences.Find(citeKey);
1010 TexRef
*ref
= (TexRef
*)TexReferences
.Get(citeKey
);
1011 wxNode
*bibNode
= BibList
.Find(citeKey
);
1015 //BibEntry *entry = (BibEntry *)bibNode->Data();
1016 if (ref
->sectionNumber
) delete[] ref
->sectionNumber
;
1017 sprintf(buf
, "[%d]", citeCount
);
1018 ref
->sectionNumber
= copystring(buf
);
1023 sprintf(buf
, "Warning: bib ref %s not resolved.", citeKey
);
1026 node
= node
->Next();
1030 // Remember we need to resolve this citation
1031 void AddCitation(char *citeKey
)
1033 if (!CitationList
.Member(citeKey
))
1034 CitationList
.Add(citeKey
);
1036 if (!TexReferences
.Get(citeKey
))
1038 TexReferences
.Put(citeKey
, new TexRef(citeKey
, "??", NULL
));
1042 TexRef
*FindReference(char *key
)
1044 return (TexRef
*)TexReferences
.Get(key
);
1048 * Custom macro stuff
1052 bool StringTobool(char *val
)
1054 if (strncmp(val
, "yes", 3) == 0 || strncmp(val
, "YES", 3) == 0 ||
1055 strncmp(val
, "on", 2) == 0 || strncmp(val
, "ON", 2) == 0 ||
1056 strncmp(val
, "true", 4) == 0 || strncmp(val
, "TRUE", 4) == 0 ||
1057 strncmp(val
, "ok", 2) == 0 || strncmp(val
, "OK", 2) == 0 ||
1058 strncmp(val
, "1", 1) == 0)
1064 // Define a variable value from the .ini file
1065 char *RegisterSetting(char *settingName
, char *settingValue
, bool interactive
)
1067 static char errorCode
[100];
1068 strcpy(errorCode
, "OK");
1069 if (StringMatch(settingName
, "chapterName", FALSE
, TRUE
))
1071 delete[] ChapterNameString
;
1072 ChapterNameString
= copystring(settingValue
);
1074 else if (StringMatch(settingName
, "sectionName", FALSE
, TRUE
))
1076 delete[] SectionNameString
;
1077 SectionNameString
= copystring(settingValue
);
1079 else if (StringMatch(settingName
, "subsectionName", FALSE
, TRUE
))
1081 delete[] SubsectionNameString
;
1082 SubsectionNameString
= copystring(settingValue
);
1084 else if (StringMatch(settingName
, "subsubsectionName", FALSE
, TRUE
))
1086 delete[] SubsubsectionNameString
;
1087 SubsubsectionNameString
= copystring(settingValue
);
1089 else if (StringMatch(settingName
, "indexName", FALSE
, TRUE
))
1091 delete[] IndexNameString
;
1092 IndexNameString
= copystring(settingValue
);
1094 else if (StringMatch(settingName
, "contentsName", FALSE
, TRUE
))
1096 delete[] ContentsNameString
;
1097 ContentsNameString
= copystring(settingValue
);
1099 else if (StringMatch(settingName
, "glossaryName", FALSE
, TRUE
))
1101 delete[] GlossaryNameString
;
1102 GlossaryNameString
= copystring(settingValue
);
1104 else if (StringMatch(settingName
, "referencesName", FALSE
, TRUE
))
1106 delete[] ReferencesNameString
;
1107 ReferencesNameString
= copystring(settingValue
);
1109 else if (StringMatch(settingName
, "tablesName", FALSE
, TRUE
))
1111 delete[] TablesNameString
;
1112 TablesNameString
= copystring(settingValue
);
1114 else if (StringMatch(settingName
, "figuresName", FALSE
, TRUE
))
1116 delete[] FiguresNameString
;
1117 FiguresNameString
= copystring(settingValue
);
1119 else if (StringMatch(settingName
, "tableName", FALSE
, TRUE
))
1121 delete[] TableNameString
;
1122 TableNameString
= copystring(settingValue
);
1124 else if (StringMatch(settingName
, "figureName", FALSE
, TRUE
))
1126 delete[] FigureNameString
;
1127 FigureNameString
= copystring(settingValue
);
1129 else if (StringMatch(settingName
, "abstractName", FALSE
, TRUE
))
1131 delete[] AbstractNameString
;
1132 AbstractNameString
= copystring(settingValue
);
1134 else if (StringMatch(settingName
, "chapterFontSize", FALSE
, TRUE
))
1135 StringToInt(settingValue
, &chapterFont
);
1136 else if (StringMatch(settingName
, "sectionFontSize", FALSE
, TRUE
))
1137 StringToInt(settingValue
, §ionFont
);
1138 else if (StringMatch(settingName
, "subsectionFontSize", FALSE
, TRUE
))
1139 StringToInt(settingValue
, &subsectionFont
);
1140 else if (StringMatch(settingName
, "titleFontSize", FALSE
, TRUE
))
1141 StringToInt(settingValue
, &titleFont
);
1142 else if (StringMatch(settingName
, "authorFontSize", FALSE
, TRUE
))
1143 StringToInt(settingValue
, &authorFont
);
1144 else if (StringMatch(settingName
, "ignoreInput", FALSE
, TRUE
))
1145 IgnorableInputFiles
.Add(FileNameFromPath(settingValue
));
1146 else if (StringMatch(settingName
, "mirrorMargins", FALSE
, TRUE
))
1147 mirrorMargins
= StringTobool(settingValue
);
1148 else if (StringMatch(settingName
, "runTwice", FALSE
, TRUE
))
1149 runTwice
= StringTobool(settingValue
);
1150 else if (StringMatch(settingName
, "isInteractive", FALSE
, TRUE
))
1151 isInteractive
= StringTobool(settingValue
);
1152 else if (StringMatch(settingName
, "headerRule", FALSE
, TRUE
))
1153 headerRule
= StringTobool(settingValue
);
1154 else if (StringMatch(settingName
, "footerRule", FALSE
, TRUE
))
1155 footerRule
= StringTobool(settingValue
);
1156 else if (StringMatch(settingName
, "combineSubSections", FALSE
, TRUE
))
1157 combineSubSections
= StringTobool(settingValue
);
1158 else if (StringMatch(settingName
, "listLabelIndent", FALSE
, TRUE
))
1159 StringToInt(settingValue
, &labelIndentTab
);
1160 else if (StringMatch(settingName
, "listItemIndent", FALSE
, TRUE
))
1161 StringToInt(settingValue
, &itemIndentTab
);
1162 else if (StringMatch(settingName
, "useUpButton", FALSE
, TRUE
))
1163 useUpButton
= StringTobool(settingValue
);
1164 else if (StringMatch(settingName
, "useHeadingStyles", FALSE
, TRUE
))
1165 useHeadingStyles
= StringTobool(settingValue
);
1166 else if (StringMatch(settingName
, "useWord", FALSE
, TRUE
))
1167 useWord
= StringTobool(settingValue
);
1168 else if (StringMatch(settingName
, "contentsDepth", FALSE
, TRUE
))
1169 StringToInt(settingValue
, &contentsDepth
);
1170 else if (StringMatch(settingName
, "generateHPJ", FALSE
, TRUE
))
1171 generateHPJ
= StringTobool(settingValue
);
1172 else if (StringMatch(settingName
, "truncateFilenames", FALSE
, TRUE
))
1173 truncateFilenames
= StringTobool(settingValue
);
1174 else if (StringMatch(settingName
, "winHelpVersion", FALSE
, TRUE
))
1175 StringToInt(settingValue
, &winHelpVersion
);
1176 else if (StringMatch(settingName
, "winHelpContents", FALSE
, TRUE
))
1177 winHelpContents
= StringTobool(settingValue
);
1178 else if (StringMatch(settingName
, "htmlIndex", FALSE
, TRUE
))
1179 htmlIndex
= StringTobool(settingValue
);
1180 else if (StringMatch(settingName
, "htmlWorkshopFiles", FALSE
, TRUE
))
1181 htmlWorkshopFiles
= StringTobool(settingValue
);
1182 else if (StringMatch(settingName
, "htmlFrameContents", FALSE
, TRUE
))
1183 htmlFrameContents
= StringTobool(settingValue
);
1184 else if (StringMatch(settingName
, "upperCaseNames", FALSE
, TRUE
))
1185 upperCaseNames
= StringTobool(settingValue
);
1186 else if (StringMatch(settingName
, "winHelpTitle", FALSE
, TRUE
))
1189 delete[] winHelpTitle
;
1190 winHelpTitle
= copystring(settingValue
);
1192 else if (StringMatch(settingName
, "indexSubsections", FALSE
, TRUE
))
1193 indexSubsections
= StringTobool(settingValue
);
1194 else if (StringMatch(settingName
, "compatibility", FALSE
, TRUE
))
1195 compatibilityMode
= StringTobool(settingValue
);
1196 else if (StringMatch(settingName
, "defaultColumnWidth", FALSE
, TRUE
))
1198 StringToInt(settingValue
, &defaultTableColumnWidth
);
1199 defaultTableColumnWidth
= 20*defaultTableColumnWidth
;
1201 else if (StringMatch(settingName
, "bitmapMethod", FALSE
, TRUE
))
1203 if ((strcmp(settingValue
, "includepicture") != 0) && (strcmp(settingValue
, "hex") != 0) &&
1204 (strcmp(settingValue
, "import") != 0))
1207 OnError("Unknown bitmapMethod");
1208 strcpy(errorCode
, "Unknown bitmapMethod");
1212 delete[] bitmapMethod
;
1213 bitmapMethod
= copystring(settingValue
);
1216 else if (StringMatch(settingName
, "htmlBrowseButtons", FALSE
, TRUE
))
1218 if (strcmp(settingValue
, "none") == 0)
1219 htmlBrowseButtons
= HTML_BUTTONS_NONE
;
1220 else if (strcmp(settingValue
, "bitmap") == 0)
1221 htmlBrowseButtons
= HTML_BUTTONS_BITMAP
;
1222 else if (strcmp(settingValue
, "text") == 0)
1223 htmlBrowseButtons
= HTML_BUTTONS_TEXT
;
1227 OnInform("Initialisation file error: htmlBrowseButtons must be one of none, bitmap, or text.");
1228 strcpy(errorCode
, "Initialisation file error: htmlBrowseButtons must be one of none, bitmap, or text.");
1231 else if (StringMatch(settingName
, "backgroundImage", FALSE
, TRUE
))
1233 backgroundImageString
= copystring(settingValue
);
1235 else if (StringMatch(settingName
, "backgroundColour", FALSE
, TRUE
))
1237 delete[] backgroundColourString
;
1238 backgroundColourString
= copystring(settingValue
);
1240 else if (StringMatch(settingName
, "textColour", FALSE
, TRUE
))
1242 textColourString
= copystring(settingValue
);
1244 else if (StringMatch(settingName
, "linkColour", FALSE
, TRUE
))
1246 linkColourString
= copystring(settingValue
);
1248 else if (StringMatch(settingName
, "followedLinkColour", FALSE
, TRUE
))
1250 followedLinkColourString
= copystring(settingValue
);
1252 else if (StringMatch(settingName
, "conversionMode", FALSE
, TRUE
))
1254 if (StringMatch(settingValue
, "RTF", FALSE
, TRUE
))
1256 winHelp
= FALSE
; convertMode
= TEX_RTF
;
1258 else if (StringMatch(settingValue
, "WinHelp", FALSE
, TRUE
))
1260 winHelp
= TRUE
; convertMode
= TEX_RTF
;
1262 else if (StringMatch(settingValue
, "XLP", FALSE
, TRUE
) ||
1263 StringMatch(settingValue
, "wxHelp", FALSE
, TRUE
))
1265 convertMode
= TEX_XLP
;
1267 else if (StringMatch(settingValue
, "HTML", FALSE
, TRUE
))
1269 convertMode
= TEX_HTML
;
1274 OnInform("Initialisation file error: conversionMode must be one of\nRTF, WinHelp, XLP (or wxHelp), HTML.");
1275 strcpy(errorCode
, "Initialisation file error: conversionMode must be one of\nRTF, WinHelp, XLP (or wxHelp), HTML.");
1278 else if (StringMatch(settingName
, "documentFontSize", FALSE
, TRUE
))
1281 StringToInt(settingValue
, &n
);
1282 if (n
== 10 || n
== 11 || n
== 12)
1287 sprintf(buf
, "Initialisation file error: nonstandard document font size %d.", n
);
1290 strcpy(errorCode
, buf
);
1296 sprintf(buf
, "Initialisation file error: unrecognised setting %s.", settingName
);
1299 strcpy(errorCode
, buf
);
1304 bool ReadCustomMacros(char *filename
)
1306 if (!wxFileExists(filename
))
1309 wxSTD ifstream
istr(filename
, ios::in
);
1311 if (istr
.bad()) return FALSE
;
1313 CustomMacroList
.Clear();
1315 char macroName
[100];
1316 char macroBody
[1000];
1321 BibEatWhiteSpace(istr
);
1326 if (ch
!= '\\') // Not a macro definition, so must be NAME=VALUE
1328 char settingName
[100];
1329 settingName
[0] = ch
;
1330 BibReadWord(istr
, (settingName
+1));
1331 BibEatWhiteSpace(istr
);
1335 OnError("Expected = following name: malformed tex2rtf.ini file.");
1340 char settingValue
[200];
1341 BibEatWhiteSpace(istr
);
1342 BibReadToEOL(istr
, settingValue
);
1343 RegisterSetting(settingName
, settingValue
);
1348 BibReadWord(istr
, macroName
);
1349 BibEatWhiteSpace(istr
);
1353 OnError("Expected [ followed by number of arguments: malformed tex2rtf.ini file.");
1360 OnError("Expected ] following number of arguments: malformed tex2rtf.ini file.");
1363 BibEatWhiteSpace(istr
);
1367 OnError("Expected { followed by macro body: malformed tex2rtf.ini file.");
1370 CustomMacro
*macro
= new CustomMacro(macroName
, noArgs
, NULL
);
1371 BibReadValue(istr
, macroBody
, FALSE
, FALSE
); // Don't ignore extra braces
1372 if (strlen(macroBody
) > 0)
1373 macro
->macroBody
= copystring(macroBody
);
1375 BibEatWhiteSpace(istr
);
1376 CustomMacroList
.Append(macroName
, macro
);
1377 AddMacroDef(ltCUSTOM_MACRO
, macroName
, noArgs
);
1381 sprintf(mbuf
, "Read initialization file %s.", filename
);
1386 CustomMacro
*FindCustomMacro(char *name
)
1388 wxNode
*node
= CustomMacroList
.Find(name
);
1391 CustomMacro
*macro
= (CustomMacro
*)node
->Data();
1397 // Display custom macros
1398 void ShowCustomMacros(void)
1400 wxNode
*node
= CustomMacroList
.First();
1403 OnInform("No custom macros loaded.\n");
1410 CustomMacro
*macro
= (CustomMacro
*)node
->Data();
1411 sprintf(buf
, "\\%s[%d]\n {%s}", macro
->macroName
, macro
->noArgs
,
1412 macro
->macroBody
? macro
->macroBody
: "");
1414 node
= node
->Next();
1418 // Parse a string into several comma-separated fields
1419 char *ParseMultifieldString(char *allFields
, int *pos
)
1421 static char buffer
[300];
1423 int fieldIndex
= *pos
;
1424 int len
= strlen(allFields
);
1426 bool keepGoing
= TRUE
;
1427 while ((fieldIndex
<= len
) && keepGoing
)
1429 if (allFields
[fieldIndex
] == ' ')
1434 else if (allFields
[fieldIndex
] == ',')
1436 *pos
= fieldIndex
+ 1;
1439 else if (allFields
[fieldIndex
] == 0)
1441 *pos
= fieldIndex
+ 1;
1446 buffer
[i
] = allFields
[fieldIndex
];
1452 if (oldPos
== (*pos
))
1466 ColourTableEntry::ColourTableEntry(char *theName
, unsigned int r
, unsigned int g
, unsigned int b
)
1468 name
= copystring(theName
);
1474 ColourTableEntry::~ColourTableEntry(void)
1479 void AddColour(char *theName
, unsigned int r
, unsigned int g
, unsigned int b
)
1481 wxNode
*node
= ColourTable
.Find(theName
);
1484 ColourTableEntry
*entry
= (ColourTableEntry
*)node
->Data();
1485 if (entry
->red
== r
|| entry
->green
== g
|| entry
->blue
== b
)
1493 ColourTableEntry
*entry
= new ColourTableEntry(theName
, r
, g
, b
);
1494 ColourTable
.Append(theName
, entry
);
1497 int FindColourPosition(char *theName
)
1500 wxNode
*node
= ColourTable
.First();
1503 ColourTableEntry
*entry
= (ColourTableEntry
*)node
->Data();
1504 if (strcmp(theName
, entry
->name
) == 0)
1507 node
= node
->Next();
1512 // Converts e.g. "red" -> "#FF0000"
1513 extern void DecToHex(int, char *);
1514 bool FindColourHTMLString(char *theName
, char *buf
)
1517 wxNode
*node
= ColourTable
.First();
1520 ColourTableEntry
*entry
= (ColourTableEntry
*)node
->Data();
1521 if (strcmp(theName
, entry
->name
) == 0)
1526 DecToHex(entry
->red
, buf2
);
1528 DecToHex(entry
->green
, buf2
);
1530 DecToHex(entry
->blue
, buf2
);
1536 node
= node
->Next();
1542 void InitialiseColourTable(void)
1544 // \\red0\\green0\\blue0;
1545 AddColour("black", 0,0,0);
1547 // \\red0\\green0\\blue255;\\red0\\green255\\blue255;\n");
1548 AddColour("cyan", 0,255,255);
1550 // \\red0\\green255\\blue0;
1551 AddColour("green", 0,255,0);
1553 // \\red255\\green0\\blue255;
1554 AddColour("magenta", 255,0,255);
1556 // \\red255\\green0\\blue0;
1557 AddColour("red", 255,0,0);
1559 // \\red255\\green255\\blue0;
1560 AddColour("yellow", 255,255,0);
1562 // \\red255\\green255\\blue255;}");
1563 AddColour("white", 255,255,255);
1567 * The purpose of this is to reduce the number of times wxYield is
1568 * called, since under Windows this can slow things down.
1571 static int yieldCount
= 0;
1573 void Tex2RTFYield(bool force
)
1581 if (yieldCount
== 0)
1591 // In both RTF generation and HTML generation for wxHelp version 2,
1592 // we need to associate \indexed keywords with the current filename/topics.
1594 // Hash table for lists of keywords for topics (WinHelp).
1595 wxHashTable
TopicTable(wxKEY_STRING
);
1596 void AddKeyWordForTopic(char *topic
, char *entry
, char *filename
)
1598 TexTopic
*texTopic
= (TexTopic
*)TopicTable
.Get(topic
);
1601 texTopic
= new TexTopic(filename
);
1602 texTopic
->keywords
= new wxStringList
;
1603 TopicTable
.Put(topic
, texTopic
);
1606 if (!texTopic
->keywords
->Member(entry
))
1607 texTopic
->keywords
->Add(entry
);
1610 void ClearKeyWordTable(void)
1612 TopicTable
.BeginFind();
1613 wxNode
*node
= TopicTable
.Next();
1616 TexTopic
*texTopic
= (TexTopic
*)node
->Data();
1618 node
= TopicTable
.Next();
1625 * TexTopic structure
1628 TexTopic::TexTopic(char *f
)
1631 filename
= copystring(f
);
1634 hasChildren
= FALSE
;
1638 TexTopic::~TexTopic(void)
1646 // Convert case, according to upperCaseNames setting.
1647 char *ConvertCase(char *s
)
1649 static char buf
[256];
1650 int len
= strlen(s
);
1653 for (i
= 0; i
< len
; i
++)
1654 buf
[i
] = toupper(s
[i
]);
1656 for (i
= 0; i
< len
; i
++)
1657 buf
[i
] = tolower(s
[i
]);