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"
40 wxHashTable
TexReferences(wxKEY_STRING
);
41 wxList
BibList(wxKEY_STRING
);
42 wxStringList CitationList
;
43 wxList
ColourTable(wxKEY_STRING
);
44 wxHashTable
BibStringTable(wxKEY_STRING
);
45 wxList
CustomMacroList(wxKEY_STRING
);
46 TexChunk
*currentSection
= NULL
;
47 char *fakeCurrentSection
= NULL
;
49 static long BibLine
= 1;
51 void OutputCurrentSection(void)
53 if (fakeCurrentSection
)
54 TexOutput(fakeCurrentSection
);
55 else if (currentSection
)
56 TraverseChildrenFromChunk(currentSection
);
59 // Nasty but the way things are done now, necessary,
60 // in order to output a chunk properly to a string (macros and all).
61 void OutputCurrentSectionToString(char *buf
)
63 if (fakeCurrentSection
)
64 strcpy(buf
, fakeCurrentSection
);
66 OutputChunkToString(currentSection
, buf
);
69 void OutputChunkToString(TexChunk
*chunk
, char *buf
)
71 FILE *tempfd
= fopen("tmp.tmp", "w");
75 FILE *old1
= CurrentOutput1
;
76 FILE *old2
= CurrentOutput2
;
78 CurrentOutput1
= tempfd
;
79 CurrentOutput2
= NULL
;
81 TraverseChildrenFromChunk(chunk
);
83 CurrentOutput1
= old1
;
84 CurrentOutput2
= old2
;
88 // Read from file into string
89 tempfd
= fopen("tmp.tmp", "r");
108 wxRemoveFile("tmp.tmp");
111 // Called by Tex2Any to simulate a section
112 void FakeCurrentSection(char *fakeSection
, bool addToContents
)
114 currentSection
= NULL
;
115 if (fakeCurrentSection
) delete[] fakeCurrentSection
;
116 fakeCurrentSection
= copystring(fakeSection
);
118 if (DocumentStyle
== LATEX_ARTICLE
)
120 int mac
= ltSECTIONHEADING
;
122 mac
= ltSECTIONHEADINGSTAR
;
123 OnMacro(mac
, 0, TRUE
);
124 OnMacro(mac
, 0, FALSE
);
128 int mac
= ltCHAPTERHEADING
;
130 mac
= ltCHAPTERHEADINGSTAR
;
131 OnMacro(mac
, 0, TRUE
);
132 OnMacro(mac
, 0, FALSE
);
134 if (fakeCurrentSection
) delete[] fakeCurrentSection
;
135 fakeCurrentSection
= NULL
;
138 // Look for \label macro, use this ref name if found or
139 // make up a topic name otherwise.
140 static long topicCounter
= 0;
142 void ResetTopicCounter(void)
147 static char *forceTopicName
= NULL
;
149 void ForceTopicName(char *name
)
152 delete[] forceTopicName
;
154 forceTopicName
= copystring(name
);
156 forceTopicName
= NULL
;
159 char *FindTopicName(TexChunk
*chunk
)
162 return forceTopicName
;
164 char *topicName
= NULL
;
165 static char topicBuf
[100];
167 if (chunk
&& (chunk
->type
== CHUNK_TYPE_MACRO
) &&
168 (chunk
->macroId
== ltLABEL
))
170 wxNode
*node
= chunk
->children
.First();
173 TexChunk
*child
= (TexChunk
*)node
->Data();
174 if (child
->type
== CHUNK_TYPE_ARG
)
176 wxNode
*snode
= child
->children
.First();
179 TexChunk
*schunk
= (TexChunk
*)snode
->Data();
180 if (schunk
->type
== CHUNK_TYPE_STRING
)
181 topicName
= schunk
->value
;
190 sprintf(topicBuf
, "topic%ld", topicCounter
);
197 * Simulate argument data, so we can 'drive' clients which implement
198 * certain basic formatting behaviour.
199 * Snag is that some save a TexChunk, so don't use yet...
203 void StartSimulateArgument(char *data
)
205 strcpy(currentArgData
, data
);
209 void EndSimulateArgument(void)
215 * Parse and convert unit arguments to points
219 int ParseUnitArgument(char *unitArg
)
221 float conversionFactor
= 1.0;
222 float unitValue
= 0.0;
223 int len
= strlen(unitArg
);
224 // Get rid of any accidentally embedded commands
225 for (int i
= 0; i
< len
; i
++)
226 if (unitArg
[i
] == '\\')
228 len
= strlen(unitArg
);
230 if (unitArg
&& (len
> 0) && (isdigit(unitArg
[0]) || unitArg
[0] == '-'))
232 sscanf(unitArg
, "%f", &unitValue
);
236 units
[0] = unitArg
[len
-2];
237 units
[1] = unitArg
[len
-1];
239 if (strcmp(units
, "in") == 0)
240 conversionFactor
= 72.0;
241 else if (strcmp(units
, "cm") == 0)
242 conversionFactor
= (float)72.0/(float)2.51;
243 else if (strcmp(units
, "mm") == 0)
244 conversionFactor
= (float)72.0/(float)25.1;
245 else if (strcmp(units
, "pt") == 0)
246 conversionFactor
= 1;
248 return (int)(unitValue
*conversionFactor
);
254 * Strip off any extension (dot something) from end of file,
255 * IF one exists. Inserts zero into buffer.
259 void StripExtension(char *buffer
)
261 int len
= strlen(buffer
);
265 if (buffer
[i
] == '.')
279 void SetFontSizes(int pointSize
)
331 void AddTexRef(char *name
, char *file
, char *sectionName
,
332 int chapter
, int section
, int subsection
, int subsubsection
)
334 TexRef
*texRef
= (TexRef
*)TexReferences
.Get(name
);
335 if (texRef
) TexReferences
.Delete(name
);
342 strcat(buf, sectionName);
349 sprintf(buf2
, "%d", chapter
);
358 sprintf(buf2
, "%d", section
);
365 sprintf(buf2
, "%d", subsection
);
372 sprintf(buf2
, "%d", subsubsection
);
375 char *tmp
= ((strlen(buf
) > 0) ? buf
: (char *)NULL
);
376 TexReferences
.Put(name
, new TexRef(name
, file
, tmp
, sectionName
));
379 void WriteTexReferences(char *filename
)
381 ofstream
ostr(filename
);
382 if (ostr
.bad()) return;
385 TexReferences
.BeginFind();
386 wxNode
*node
= TexReferences
.Next();
390 TexRef
*ref
= (TexRef
*)node
->Data();
391 ostr
<< ref
->refLabel
<< " " << (ref
->refFile
? ref
->refFile
: "??") << " ";
392 ostr
<< (ref
->sectionName
? ref
->sectionName
: "??") << " ";
393 ostr
<< (ref
->sectionNumber
? ref
->sectionNumber
: "??") << "\n";
394 if (!ref
->sectionNumber
|| (strcmp(ref
->sectionNumber
, "??") == 0 && strcmp(ref
->sectionName
, "??") == 0))
396 sprintf(buf
, "Warning: reference %s not resolved.", ref
->refLabel
);
399 node
= TexReferences
.Next();
403 void ReadTexReferences(char *filename
)
405 if (!wxFileExists(filename
))
408 ifstream
istr(filename
, ios::in
);
410 if (istr
.bad()) return;
415 char sectionName
[100];
425 istr
.get(ch
); // Read past space
428 while (ch
!= '\n' && !istr
.eof())
436 // gt - needed to trick the hash table "TexReferences" into deleting the key
437 // strings it creates in the Put() function, but not the item that is
438 // created here, as that is destroyed elsewhere. Without doing this, there
439 // were massive memory leaks
440 TexReferences
.DeleteContents(TRUE
);
441 TexReferences
.Put(label
, new TexRef(label
, file
, section
, sectionName
));
442 TexReferences
.DeleteContents(FALSE
);
449 * Bibliography-handling code
453 void BibEatWhiteSpace(istream
& str
)
455 char ch
= str
.peek();
457 while (!str
.eof() && (ch
== ' ' || ch
== '\t' || ch
== 13 || ch
== 10 || ch
== EOF
))
462 if ((ch
== EOF
) || str
.eof()) return;
466 // Ignore end-of-line comments
467 if (ch
== '%' || ch
== ';' || ch
== '#')
471 while (ch
!= 10 && ch
!= 13 && !str
.eof())
476 BibEatWhiteSpace(str
);
480 // Read word up to { or , or space
481 void BibReadWord(istream
& istr
, char *buffer
)
485 char ch
= istr
.peek();
486 while (!istr
.eof() && ch
!= ' ' && ch
!= '{' && ch
!= '(' && ch
!= 13 && ch
!= 10 && ch
!= '\t' &&
487 ch
!= ',' && ch
!= '=')
497 // Read string (double-quoted or not) to end quote or EOL
498 void BibReadToEOL(istream
& istr
, char *buffer
)
502 char ch
= istr
.peek();
503 bool inQuotes
= FALSE
;
510 // If in quotes, read white space too. If not,
511 // stop at white space or comment.
512 while (!istr
.eof() && ch
!= 13 && ch
!= 10 && ch
!= '"' &&
513 (inQuotes
|| ((ch
!= ' ') && (ch
!= 9) &&
514 (ch
!= ';') && (ch
!= '%') && (ch
!= '#'))))
526 // Read }-terminated value, taking nested braces into account.
527 void BibReadValue(istream
& istr
, char *buffer
, bool ignoreBraces
= TRUE
,
528 bool quotesMayTerminate
= TRUE
)
533 char ch
= istr
.peek();
534 bool stopping
= FALSE
;
535 while (!istr
.eof() && !stopping
)
541 sprintf(buf
, "Sorry, value > 4000 chars in bib file at line %ld, terminating.", BibLine
);
542 wxFatalError(buf
, "Tex2RTF Fatal Error");
558 else if (quotesMayTerminate
&& ch
== '"')
565 if (!ignoreBraces
|| (ch
!= '{' && ch
!= '}'))
577 bool ReadBib(char *filename
)
579 if (!wxFileExists(filename
))
583 ifstream
istr(filename
, ios::in
);
584 if (istr
.bad()) return FALSE
;
588 OnInform("Reading .bib file...");
591 char fieldValue
[4000];
592 char recordType
[100];
594 char recordField
[100];
599 BibEatWhiteSpace(istr
);
603 sprintf(buf
, "Expected @: malformed bib file at line %ld (%s)", BibLine
, filename
);
607 BibReadWord(istr
, recordType
);
608 BibEatWhiteSpace(istr
);
610 if (ch
!= '{' && ch
!= '(')
612 sprintf(buf
, "Expected { or ( after record type: malformed .bib file at line %ld (%s)", BibLine
, filename
);
616 BibEatWhiteSpace(istr
);
617 if (StringMatch(recordType
, "string", FALSE
, TRUE
))
619 BibReadWord(istr
, recordType
);
620 BibEatWhiteSpace(istr
);
624 sprintf(buf
, "Expected = after string key: malformed .bib file at line %ld (%s)", BibLine
, filename
);
628 BibEatWhiteSpace(istr
);
630 if (ch
!= '"' && ch
!= '{')
632 sprintf(buf
, "Expected = after string key: malformed .bib file at line %ld (%s)", BibLine
, filename
);
636 BibReadValue(istr
, fieldValue
);
638 // Now put in hash table if necesary
639 if (!BibStringTable
.Get(recordType
))
640 BibStringTable
.Put(recordType
, (wxObject
*)copystring(fieldValue
));
642 // Read closing ) or }
643 BibEatWhiteSpace(istr
);
645 BibEatWhiteSpace(istr
);
649 BibReadWord(istr
, recordKey
);
651 BibEntry
*bibEntry
= new BibEntry
;
652 bibEntry
->key
= copystring(recordKey
);
653 bibEntry
->type
= copystring(recordType
);
655 bool moreRecords
= TRUE
;
656 while (moreRecords
&& !istr
.eof())
658 BibEatWhiteSpace(istr
);
660 if (ch
== '}' || ch
== ')')
666 BibEatWhiteSpace(istr
);
667 BibReadWord(istr
, recordField
);
668 BibEatWhiteSpace(istr
);
672 sprintf(buf
, "Expected = after field type: malformed .bib file at line %ld (%s)", BibLine
, filename
);
676 BibEatWhiteSpace(istr
);
678 if (ch
!= '{' && ch
!= '"')
681 BibReadWord(istr
, fieldValue
+1);
683 // If in the table of strings, replace with string from table.
684 char *s
= (char *)BibStringTable
.Get(fieldValue
);
687 strcpy(fieldValue
, s
);
691 BibReadValue(istr
, fieldValue
, TRUE
, (ch
== '"' ? TRUE
: FALSE
));
693 // Now we can add a field
694 if (StringMatch(recordField
, "author", FALSE
, TRUE
))
695 bibEntry
->author
= copystring(fieldValue
);
696 else if (StringMatch(recordField
, "key", FALSE
, TRUE
))
698 else if (StringMatch(recordField
, "annotate", FALSE
, TRUE
))
700 else if (StringMatch(recordField
, "abstract", FALSE
, TRUE
))
702 else if (StringMatch(recordField
, "edition", FALSE
, TRUE
))
704 else if (StringMatch(recordField
, "howpublished", FALSE
, TRUE
))
706 else if (StringMatch(recordField
, "note", FALSE
, TRUE
) || StringMatch(recordField
, "notes", FALSE
, TRUE
))
708 else if (StringMatch(recordField
, "series", FALSE
, TRUE
))
710 else if (StringMatch(recordField
, "type", FALSE
, TRUE
))
712 else if (StringMatch(recordField
, "keywords", FALSE
, TRUE
))
714 else if (StringMatch(recordField
, "editor", FALSE
, TRUE
) || StringMatch(recordField
, "editors", FALSE
, TRUE
))
715 bibEntry
->editor
= copystring(fieldValue
);
716 else if (StringMatch(recordField
, "title", FALSE
, TRUE
))
717 bibEntry
->title
= copystring(fieldValue
);
718 else if (StringMatch(recordField
, "booktitle", FALSE
, TRUE
))
719 bibEntry
->booktitle
= copystring(fieldValue
);
720 else if (StringMatch(recordField
, "journal", FALSE
, TRUE
))
721 bibEntry
->journal
= copystring(fieldValue
);
722 else if (StringMatch(recordField
, "volume", FALSE
, TRUE
))
723 bibEntry
->volume
= copystring(fieldValue
);
724 else if (StringMatch(recordField
, "number", FALSE
, TRUE
))
725 bibEntry
->number
= copystring(fieldValue
);
726 else if (StringMatch(recordField
, "year", FALSE
, TRUE
))
727 bibEntry
->year
= copystring(fieldValue
);
728 else if (StringMatch(recordField
, "month", FALSE
, TRUE
))
729 bibEntry
->month
= copystring(fieldValue
);
730 else if (StringMatch(recordField
, "pages", FALSE
, TRUE
))
731 bibEntry
->pages
= copystring(fieldValue
);
732 else if (StringMatch(recordField
, "publisher", FALSE
, TRUE
))
733 bibEntry
->publisher
= copystring(fieldValue
);
734 else if (StringMatch(recordField
, "address", FALSE
, TRUE
))
735 bibEntry
->address
= copystring(fieldValue
);
736 else if (StringMatch(recordField
, "institution", FALSE
, TRUE
) || StringMatch(recordField
, "school", FALSE
, TRUE
))
737 bibEntry
->institution
= copystring(fieldValue
);
738 else if (StringMatch(recordField
, "organization", FALSE
, TRUE
) || StringMatch(recordField
, "organisation", FALSE
, TRUE
))
739 bibEntry
->organization
= copystring(fieldValue
);
740 else if (StringMatch(recordField
, "comment", FALSE
, TRUE
) || StringMatch(recordField
, "comments", FALSE
, TRUE
))
741 bibEntry
->comment
= copystring(fieldValue
);
742 else if (StringMatch(recordField
, "annote", FALSE
, TRUE
))
743 bibEntry
->comment
= copystring(fieldValue
);
744 else if (StringMatch(recordField
, "chapter", FALSE
, TRUE
))
745 bibEntry
->chapter
= copystring(fieldValue
);
748 sprintf(buf
, "Unrecognised bib field type %s at line %ld (%s)", recordField
, BibLine
, filename
);
753 BibList
.Append(recordKey
, bibEntry
);
754 BibEatWhiteSpace(istr
);
760 void OutputBibItem(TexRef
*ref
, BibEntry
*bib
)
764 OnMacro(ltNUMBEREDBIBITEM
, 2, TRUE
);
765 OnArgument(ltNUMBEREDBIBITEM
, 1, TRUE
);
766 TexOutput(ref
->sectionNumber
);
767 OnArgument(ltNUMBEREDBIBITEM
, 1, FALSE
);
768 OnArgument(ltNUMBEREDBIBITEM
, 2, TRUE
);
771 OnMacro(ltBF
, 1, TRUE
);
772 OnArgument(ltBF
, 1, TRUE
);
774 TexOutput(bib
->author
);
775 OnArgument(ltBF
, 1, FALSE
);
776 OnMacro(ltBF
, 1, FALSE
);
777 if (bib
->author
&& (strlen(bib
->author
) > 0) && (bib
->author
[strlen(bib
->author
) - 1] != '.'))
784 TexOutput(bib
->year
);
789 TexOutput(bib
->month
);
792 if (bib
->year
|| bib
->month
)
795 if (StringMatch(bib
->type
, "article", FALSE
, TRUE
))
799 TexOutput(bib
->title
);
804 OnMacro(ltIT
, 1, TRUE
);
805 OnArgument(ltIT
, 1, TRUE
);
806 TexOutput(bib
->journal
);
807 OnArgument(ltIT
, 1, FALSE
);
808 OnMacro(ltIT
, 1, FALSE
);
813 OnMacro(ltBF
, 1, TRUE
);
814 OnArgument(ltBF
, 1, TRUE
);
815 TexOutput(bib
->volume
);
816 OnArgument(ltBF
, 1, FALSE
);
817 OnMacro(ltBF
, 1, FALSE
);
822 TexOutput(bib
->number
);
827 TexOutput(", pages ");
828 TexOutput(bib
->pages
);
832 else if (StringMatch(bib
->type
, "book", FALSE
, TRUE
) ||
833 StringMatch(bib
->type
, "unpublished", FALSE
, TRUE
) ||
834 StringMatch(bib
->type
, "manual", FALSE
, TRUE
) ||
835 StringMatch(bib
->type
, "phdthesis", FALSE
, TRUE
) ||
836 StringMatch(bib
->type
, "mastersthesis", FALSE
, TRUE
) ||
837 StringMatch(bib
->type
, "misc", FALSE
, TRUE
) ||
838 StringMatch(bib
->type
, "techreport", FALSE
, TRUE
) ||
839 StringMatch(bib
->type
, "booklet", FALSE
, TRUE
))
841 if (bib
->title
|| bib
->booktitle
)
843 OnMacro(ltIT
, 1, TRUE
);
844 OnArgument(ltIT
, 1, TRUE
);
845 TexOutput(bib
->title
? bib
->title
: bib
->booktitle
);
847 OnArgument(ltIT
, 1, FALSE
);
848 OnMacro(ltIT
, 1, FALSE
);
850 if (StringMatch(bib
->type
, "phdthesis", FALSE
, TRUE
))
851 TexOutput("PhD thesis. ");
852 if (StringMatch(bib
->type
, "techreport", FALSE
, TRUE
))
853 TexOutput("Technical report. ");
857 TexOutput(bib
->editor
);
860 if (bib
->institution
)
862 TexOutput(bib
->institution
);
865 if (bib
->organization
)
867 TexOutput(bib
->organization
);
872 TexOutput(bib
->publisher
);
877 TexOutput(bib
->address
);
881 else if (StringMatch(bib
->type
, "inbook", FALSE
, TRUE
) ||
882 StringMatch(bib
->type
, "inproceedings", FALSE
, TRUE
) ||
883 StringMatch(bib
->type
, "incollection", FALSE
, TRUE
) ||
884 StringMatch(bib
->type
, "conference", FALSE
, TRUE
))
888 TexOutput(bib
->title
);
892 TexOutput(", from ");
893 OnMacro(ltIT
, 1, TRUE
);
894 OnArgument(ltIT
, 1, TRUE
);
895 TexOutput(bib
->booktitle
);
897 OnArgument(ltIT
, 1, FALSE
);
898 OnMacro(ltIT
, 1, FALSE
);
903 TexOutput(bib
->editor
);
908 TexOutput(bib
->publisher
);
912 if (bib
->publisher
) TexOutput(", ");
914 TexOutput(bib
->address
);
916 if (bib
->publisher
|| bib
->address
)
922 OnMacro(ltBF
, 1, TRUE
);
923 OnArgument(ltBF
, 1, TRUE
);
924 TexOutput(bib
->volume
);
925 OnArgument(ltBF
, 1, FALSE
);
926 OnMacro(ltBF
, 1, FALSE
);
933 TexOutput(bib
->number
);
938 TexOutput(" Number ");
939 TexOutput(bib
->number
);
945 TexOutput(" Chap. "); TexOutput(bib
->chapter
);
949 if (bib
->chapter
) TexOutput(", pages ");
950 else TexOutput(" Pages ");
951 TexOutput(bib
->pages
);
955 OnArgument(ltNUMBEREDBIBITEM
, 2, FALSE
);
956 OnMacro(ltNUMBEREDBIBITEM
, 2, FALSE
);
962 ForceTopicName("bibliography");
963 FakeCurrentSection(ReferencesNameString
);
964 ForceTopicName(NULL
);
966 OnMacro(ltPAR
, 0, TRUE
);
967 OnMacro(ltPAR
, 0, FALSE
);
969 if ((convertMode
== TEX_RTF
) && !winHelp
)
971 OnMacro(ltPAR
, 0, TRUE
);
972 OnMacro(ltPAR
, 0, FALSE
);
975 wxNode
*node
= CitationList
.First();
978 char *citeKey
= (char *)node
->Data();
979 // wxNode *texNode = TexReferences.Find(citeKey);
980 TexRef
*ref
= (TexRef
*)TexReferences
.Get(citeKey
);
981 wxNode
*bibNode
= BibList
.Find(citeKey
);
984 BibEntry
*entry
= (BibEntry
*)bibNode
->Data();
985 OutputBibItem(ref
, entry
);
991 static int citeCount
= 1;
993 void ResolveBibReferences(void)
995 if (CitationList
.Number() > 0)
996 OnInform("Resolving bibliographic references...");
1000 wxNode
*node
= CitationList
.First();
1004 char *citeKey
= (char *)node
->Data();
1005 // wxNode *texNode = TexReferences.Find(citeKey);
1006 TexRef
*ref
= (TexRef
*)TexReferences
.Get(citeKey
);
1007 wxNode
*bibNode
= BibList
.Find(citeKey
);
1011 //BibEntry *entry = (BibEntry *)bibNode->Data();
1012 if (ref
->sectionNumber
) delete[] ref
->sectionNumber
;
1013 sprintf(buf
, "[%d]", citeCount
);
1014 ref
->sectionNumber
= copystring(buf
);
1019 sprintf(buf
, "Warning: bib ref %s not resolved.", citeKey
);
1022 node
= node
->Next();
1026 // Remember we need to resolve this citation
1027 void AddCitation(char *citeKey
)
1029 if (!CitationList
.Member(citeKey
))
1030 CitationList
.Add(citeKey
);
1032 if (!TexReferences
.Get(citeKey
))
1034 TexReferences
.Put(citeKey
, new TexRef(citeKey
, "??", NULL
));
1038 TexRef
*FindReference(char *key
)
1040 return (TexRef
*)TexReferences
.Get(key
);
1044 * Custom macro stuff
1048 bool StringTobool(char *val
)
1050 if (strncmp(val
, "yes", 3) == 0 || strncmp(val
, "YES", 3) == 0 ||
1051 strncmp(val
, "on", 2) == 0 || strncmp(val
, "ON", 2) == 0 ||
1052 strncmp(val
, "true", 4) == 0 || strncmp(val
, "TRUE", 4) == 0 ||
1053 strncmp(val
, "ok", 2) == 0 || strncmp(val
, "OK", 2) == 0 ||
1054 strncmp(val
, "1", 1) == 0)
1060 // Define a variable value from the .ini file
1061 char *RegisterSetting(char *settingName
, char *settingValue
, bool interactive
)
1063 static char errorCode
[100];
1064 strcpy(errorCode
, "OK");
1065 if (StringMatch(settingName
, "chapterName", FALSE
, TRUE
))
1067 delete[] ChapterNameString
;
1068 ChapterNameString
= copystring(settingValue
);
1070 else if (StringMatch(settingName
, "sectionName", FALSE
, TRUE
))
1072 delete[] SectionNameString
;
1073 SectionNameString
= copystring(settingValue
);
1075 else if (StringMatch(settingName
, "subsectionName", FALSE
, TRUE
))
1077 delete[] SubsectionNameString
;
1078 SubsectionNameString
= copystring(settingValue
);
1080 else if (StringMatch(settingName
, "subsubsectionName", FALSE
, TRUE
))
1082 delete[] SubsubsectionNameString
;
1083 SubsubsectionNameString
= copystring(settingValue
);
1085 else if (StringMatch(settingName
, "indexName", FALSE
, TRUE
))
1087 delete[] IndexNameString
;
1088 IndexNameString
= copystring(settingValue
);
1090 else if (StringMatch(settingName
, "contentsName", FALSE
, TRUE
))
1092 delete[] ContentsNameString
;
1093 ContentsNameString
= copystring(settingValue
);
1095 else if (StringMatch(settingName
, "glossaryName", FALSE
, TRUE
))
1097 delete[] GlossaryNameString
;
1098 GlossaryNameString
= copystring(settingValue
);
1100 else if (StringMatch(settingName
, "referencesName", FALSE
, TRUE
))
1102 delete[] ReferencesNameString
;
1103 ReferencesNameString
= copystring(settingValue
);
1105 else if (StringMatch(settingName
, "tablesName", FALSE
, TRUE
))
1107 delete[] TablesNameString
;
1108 TablesNameString
= copystring(settingValue
);
1110 else if (StringMatch(settingName
, "figuresName", FALSE
, TRUE
))
1112 delete[] FiguresNameString
;
1113 FiguresNameString
= copystring(settingValue
);
1115 else if (StringMatch(settingName
, "tableName", FALSE
, TRUE
))
1117 delete[] TableNameString
;
1118 TableNameString
= copystring(settingValue
);
1120 else if (StringMatch(settingName
, "figureName", FALSE
, TRUE
))
1122 delete[] FigureNameString
;
1123 FigureNameString
= copystring(settingValue
);
1125 else if (StringMatch(settingName
, "abstractName", FALSE
, TRUE
))
1127 delete[] AbstractNameString
;
1128 AbstractNameString
= copystring(settingValue
);
1130 else if (StringMatch(settingName
, "chapterFontSize", FALSE
, TRUE
))
1131 StringToInt(settingValue
, &chapterFont
);
1132 else if (StringMatch(settingName
, "sectionFontSize", FALSE
, TRUE
))
1133 StringToInt(settingValue
, §ionFont
);
1134 else if (StringMatch(settingName
, "subsectionFontSize", FALSE
, TRUE
))
1135 StringToInt(settingValue
, &subsectionFont
);
1136 else if (StringMatch(settingName
, "titleFontSize", FALSE
, TRUE
))
1137 StringToInt(settingValue
, &titleFont
);
1138 else if (StringMatch(settingName
, "authorFontSize", FALSE
, TRUE
))
1139 StringToInt(settingValue
, &authorFont
);
1140 else if (StringMatch(settingName
, "ignoreInput", FALSE
, TRUE
))
1141 IgnorableInputFiles
.Add(FileNameFromPath(settingValue
));
1142 else if (StringMatch(settingName
, "mirrorMargins", FALSE
, TRUE
))
1143 mirrorMargins
= StringTobool(settingValue
);
1144 else if (StringMatch(settingName
, "runTwice", FALSE
, TRUE
))
1145 runTwice
= StringTobool(settingValue
);
1146 else if (StringMatch(settingName
, "isInteractive", FALSE
, TRUE
))
1147 isInteractive
= StringTobool(settingValue
);
1148 else if (StringMatch(settingName
, "headerRule", FALSE
, TRUE
))
1149 headerRule
= StringTobool(settingValue
);
1150 else if (StringMatch(settingName
, "footerRule", FALSE
, TRUE
))
1151 footerRule
= StringTobool(settingValue
);
1152 else if (StringMatch(settingName
, "combineSubSections", FALSE
, TRUE
))
1153 combineSubSections
= StringTobool(settingValue
);
1154 else if (StringMatch(settingName
, "listLabelIndent", FALSE
, TRUE
))
1155 StringToInt(settingValue
, &labelIndentTab
);
1156 else if (StringMatch(settingName
, "listItemIndent", FALSE
, TRUE
))
1157 StringToInt(settingValue
, &itemIndentTab
);
1158 else if (StringMatch(settingName
, "useUpButton", FALSE
, TRUE
))
1159 useUpButton
= StringTobool(settingValue
);
1160 else if (StringMatch(settingName
, "useHeadingStyles", FALSE
, TRUE
))
1161 useHeadingStyles
= StringTobool(settingValue
);
1162 else if (StringMatch(settingName
, "useWord", FALSE
, TRUE
))
1163 useWord
= StringTobool(settingValue
);
1164 else if (StringMatch(settingName
, "contentsDepth", FALSE
, TRUE
))
1165 StringToInt(settingValue
, &contentsDepth
);
1166 else if (StringMatch(settingName
, "generateHPJ", FALSE
, TRUE
))
1167 generateHPJ
= StringTobool(settingValue
);
1168 else if (StringMatch(settingName
, "truncateFilenames", FALSE
, TRUE
))
1169 truncateFilenames
= StringTobool(settingValue
);
1170 else if (StringMatch(settingName
, "winHelpVersion", FALSE
, TRUE
))
1171 StringToInt(settingValue
, &winHelpVersion
);
1172 else if (StringMatch(settingName
, "winHelpContents", FALSE
, TRUE
))
1173 winHelpContents
= StringTobool(settingValue
);
1174 else if (StringMatch(settingName
, "htmlIndex", FALSE
, TRUE
))
1175 htmlIndex
= StringTobool(settingValue
);
1176 else if (StringMatch(settingName
, "htmlWorkshopFiles", FALSE
, TRUE
))
1177 htmlWorkshopFiles
= StringTobool(settingValue
);
1178 else if (StringMatch(settingName
, "htmlFrameContents", FALSE
, TRUE
))
1179 htmlFrameContents
= StringTobool(settingValue
);
1180 else if (StringMatch(settingName
, "upperCaseNames", FALSE
, TRUE
))
1181 upperCaseNames
= StringTobool(settingValue
);
1182 else if (StringMatch(settingName
, "winHelpTitle", FALSE
, TRUE
))
1185 delete[] winHelpTitle
;
1186 winHelpTitle
= copystring(settingValue
);
1188 else if (StringMatch(settingName
, "indexSubsections", FALSE
, TRUE
))
1189 indexSubsections
= StringTobool(settingValue
);
1190 else if (StringMatch(settingName
, "compatibility", FALSE
, TRUE
))
1191 compatibilityMode
= StringTobool(settingValue
);
1192 else if (StringMatch(settingName
, "defaultColumnWidth", FALSE
, TRUE
))
1194 StringToInt(settingValue
, &defaultTableColumnWidth
);
1195 defaultTableColumnWidth
= 20*defaultTableColumnWidth
;
1197 else if (StringMatch(settingName
, "bitmapMethod", FALSE
, TRUE
))
1199 if ((strcmp(settingValue
, "includepicture") != 0) && (strcmp(settingValue
, "hex") != 0) &&
1200 (strcmp(settingValue
, "import") != 0))
1203 OnError("Unknown bitmapMethod");
1204 strcpy(errorCode
, "Unknown bitmapMethod");
1208 delete[] bitmapMethod
;
1209 bitmapMethod
= copystring(settingValue
);
1212 else if (StringMatch(settingName
, "htmlBrowseButtons", FALSE
, TRUE
))
1214 if (strcmp(settingValue
, "none") == 0)
1215 htmlBrowseButtons
= HTML_BUTTONS_NONE
;
1216 else if (strcmp(settingValue
, "bitmap") == 0)
1217 htmlBrowseButtons
= HTML_BUTTONS_BITMAP
;
1218 else if (strcmp(settingValue
, "text") == 0)
1219 htmlBrowseButtons
= HTML_BUTTONS_TEXT
;
1223 OnInform("Initialisation file error: htmlBrowseButtons must be one of none, bitmap, or text.");
1224 strcpy(errorCode
, "Initialisation file error: htmlBrowseButtons must be one of none, bitmap, or text.");
1227 else if (StringMatch(settingName
, "backgroundImage", FALSE
, TRUE
))
1229 backgroundImageString
= copystring(settingValue
);
1231 else if (StringMatch(settingName
, "backgroundColour", FALSE
, TRUE
))
1233 delete[] backgroundColourString
;
1234 backgroundColourString
= copystring(settingValue
);
1236 else if (StringMatch(settingName
, "textColour", FALSE
, TRUE
))
1238 textColourString
= copystring(settingValue
);
1240 else if (StringMatch(settingName
, "linkColour", FALSE
, TRUE
))
1242 linkColourString
= copystring(settingValue
);
1244 else if (StringMatch(settingName
, "followedLinkColour", FALSE
, TRUE
))
1246 followedLinkColourString
= copystring(settingValue
);
1248 else if (StringMatch(settingName
, "conversionMode", FALSE
, TRUE
))
1250 if (StringMatch(settingValue
, "RTF", FALSE
, TRUE
))
1252 winHelp
= FALSE
; convertMode
= TEX_RTF
;
1254 else if (StringMatch(settingValue
, "WinHelp", FALSE
, TRUE
))
1256 winHelp
= TRUE
; convertMode
= TEX_RTF
;
1258 else if (StringMatch(settingValue
, "XLP", FALSE
, TRUE
) ||
1259 StringMatch(settingValue
, "wxHelp", FALSE
, TRUE
))
1261 convertMode
= TEX_XLP
;
1263 else if (StringMatch(settingValue
, "HTML", FALSE
, TRUE
))
1265 convertMode
= TEX_HTML
;
1270 OnInform("Initialisation file error: conversionMode must be one of\nRTF, WinHelp, XLP (or wxHelp), HTML.");
1271 strcpy(errorCode
, "Initialisation file error: conversionMode must be one of\nRTF, WinHelp, XLP (or wxHelp), HTML.");
1274 else if (StringMatch(settingName
, "documentFontSize", FALSE
, TRUE
))
1277 StringToInt(settingValue
, &n
);
1278 if (n
== 10 || n
== 11 || n
== 12)
1283 sprintf(buf
, "Initialisation file error: nonstandard document font size %d.", n
);
1286 strcpy(errorCode
, buf
);
1292 sprintf(buf
, "Initialisation file error: unrecognised setting %s.", settingName
);
1295 strcpy(errorCode
, buf
);
1300 bool ReadCustomMacros(char *filename
)
1302 if (!wxFileExists(filename
))
1305 ifstream
istr(filename
, ios::in
);
1307 if (istr
.bad()) return FALSE
;
1309 CustomMacroList
.Clear();
1311 char macroName
[100];
1312 char macroBody
[1000];
1317 BibEatWhiteSpace(istr
);
1322 if (ch
!= '\\') // Not a macro definition, so must be NAME=VALUE
1324 char settingName
[100];
1325 settingName
[0] = ch
;
1326 BibReadWord(istr
, (settingName
+1));
1327 BibEatWhiteSpace(istr
);
1331 OnError("Expected = following name: malformed tex2rtf.ini file.");
1336 char settingValue
[200];
1337 BibEatWhiteSpace(istr
);
1338 BibReadToEOL(istr
, settingValue
);
1339 RegisterSetting(settingName
, settingValue
);
1344 BibReadWord(istr
, macroName
);
1345 BibEatWhiteSpace(istr
);
1349 OnError("Expected [ followed by number of arguments: malformed tex2rtf.ini file.");
1356 OnError("Expected ] following number of arguments: malformed tex2rtf.ini file.");
1359 BibEatWhiteSpace(istr
);
1363 OnError("Expected { followed by macro body: malformed tex2rtf.ini file.");
1366 CustomMacro
*macro
= new CustomMacro(macroName
, noArgs
, NULL
);
1367 BibReadValue(istr
, macroBody
, FALSE
, FALSE
); // Don't ignore extra braces
1368 if (strlen(macroBody
) > 0)
1369 macro
->macroBody
= copystring(macroBody
);
1371 BibEatWhiteSpace(istr
);
1372 CustomMacroList
.Append(macroName
, macro
);
1373 AddMacroDef(ltCUSTOM_MACRO
, macroName
, noArgs
);
1377 sprintf(mbuf
, "Read initialization file %s.", filename
);
1382 CustomMacro
*FindCustomMacro(char *name
)
1384 wxNode
*node
= CustomMacroList
.Find(name
);
1387 CustomMacro
*macro
= (CustomMacro
*)node
->Data();
1393 // Display custom macros
1394 void ShowCustomMacros(void)
1396 wxNode
*node
= CustomMacroList
.First();
1399 OnInform("No custom macros loaded.\n");
1406 CustomMacro
*macro
= (CustomMacro
*)node
->Data();
1407 sprintf(buf
, "\\%s[%d]\n {%s}", macro
->macroName
, macro
->noArgs
,
1408 macro
->macroBody
? macro
->macroBody
: "");
1410 node
= node
->Next();
1414 // Parse a string into several comma-separated fields
1415 char *ParseMultifieldString(char *allFields
, int *pos
)
1417 static char buffer
[300];
1419 int fieldIndex
= *pos
;
1420 int len
= strlen(allFields
);
1422 bool keepGoing
= TRUE
;
1423 while ((fieldIndex
<= len
) && keepGoing
)
1425 if (allFields
[fieldIndex
] == ' ')
1430 else if (allFields
[fieldIndex
] == ',')
1432 *pos
= fieldIndex
+ 1;
1435 else if (allFields
[fieldIndex
] == 0)
1437 *pos
= fieldIndex
+ 1;
1442 buffer
[i
] = allFields
[fieldIndex
];
1448 if (oldPos
== (*pos
))
1462 ColourTableEntry::ColourTableEntry(char *theName
, unsigned int r
, unsigned int g
, unsigned int b
)
1464 name
= copystring(theName
);
1470 ColourTableEntry::~ColourTableEntry(void)
1475 void AddColour(char *theName
, unsigned int r
, unsigned int g
, unsigned int b
)
1477 wxNode
*node
= ColourTable
.Find(theName
);
1480 ColourTableEntry
*entry
= (ColourTableEntry
*)node
->Data();
1481 if (entry
->red
== r
|| entry
->green
== g
|| entry
->blue
== b
)
1489 ColourTableEntry
*entry
= new ColourTableEntry(theName
, r
, g
, b
);
1490 ColourTable
.Append(theName
, entry
);
1493 int FindColourPosition(char *theName
)
1496 wxNode
*node
= ColourTable
.First();
1499 ColourTableEntry
*entry
= (ColourTableEntry
*)node
->Data();
1500 if (strcmp(theName
, entry
->name
) == 0)
1503 node
= node
->Next();
1508 // Converts e.g. "red" -> "#FF0000"
1509 extern void DecToHex(int, char *);
1510 bool FindColourHTMLString(char *theName
, char *buf
)
1513 wxNode
*node
= ColourTable
.First();
1516 ColourTableEntry
*entry
= (ColourTableEntry
*)node
->Data();
1517 if (strcmp(theName
, entry
->name
) == 0)
1522 DecToHex(entry
->red
, buf2
);
1524 DecToHex(entry
->green
, buf2
);
1526 DecToHex(entry
->blue
, buf2
);
1532 node
= node
->Next();
1538 void InitialiseColourTable(void)
1540 // \\red0\\green0\\blue0;
1541 AddColour("black", 0,0,0);
1543 // \\red0\\green0\\blue255;\\red0\\green255\\blue255;\n");
1544 AddColour("cyan", 0,255,255);
1546 // \\red0\\green255\\blue0;
1547 AddColour("green", 0,255,0);
1549 // \\red255\\green0\\blue255;
1550 AddColour("magenta", 255,0,255);
1552 // \\red255\\green0\\blue0;
1553 AddColour("red", 255,0,0);
1555 // \\red255\\green255\\blue0;
1556 AddColour("yellow", 255,255,0);
1558 // \\red255\\green255\\blue255;}");
1559 AddColour("white", 255,255,255);
1563 * The purpose of this is to reduce the number of times wxYield is
1564 * called, since under Windows this can slow things down.
1567 static int yieldCount
= 0;
1569 void Tex2RTFYield(bool force
)
1577 if (yieldCount
== 0)
1587 // In both RTF generation and HTML generation for wxHelp version 2,
1588 // we need to associate \indexed keywords with the current filename/topics.
1590 // Hash table for lists of keywords for topics (WinHelp).
1591 wxHashTable
TopicTable(wxKEY_STRING
);
1592 void AddKeyWordForTopic(char *topic
, char *entry
, char *filename
)
1594 TexTopic
*texTopic
= (TexTopic
*)TopicTable
.Get(topic
);
1597 texTopic
= new TexTopic(filename
);
1598 texTopic
->keywords
= new wxStringList
;
1599 TopicTable
.Put(topic
, texTopic
);
1602 if (!texTopic
->keywords
->Member(entry
))
1603 texTopic
->keywords
->Add(entry
);
1606 void ClearKeyWordTable(void)
1608 TopicTable
.BeginFind();
1609 wxNode
*node
= TopicTable
.Next();
1612 TexTopic
*texTopic
= (TexTopic
*)node
->Data();
1614 node
= TopicTable
.Next();
1621 * TexTopic structure
1624 TexTopic::TexTopic(char *f
)
1627 filename
= copystring(f
);
1630 hasChildren
= FALSE
;
1634 TexTopic::~TexTopic(void)
1642 // Convert case, according to upperCaseNames setting.
1643 char *ConvertCase(char *s
)
1645 static char buf
[256];
1646 int len
= strlen(s
);
1649 for (i
= 0; i
< len
; i
++)
1650 buf
[i
] = toupper(s
[i
]);
1652 for (i
= 0; i
< len
; i
++)
1653 buf
[i
] = tolower(s
[i
]);