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(const 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.", BibLine
);
546 wxLogError(buf
, "Tex2RTF Fatal Error");
563 else if (quotesMayTerminate
&& ch
== '"')
570 if (!ignoreBraces
|| (ch
!= '{' && ch
!= '}'))
582 bool ReadBib(char *filename
)
584 if (!wxFileExists(filename
))
588 wxSTD ifstream
istr(filename
, ios::in
);
589 if (istr
.bad()) return FALSE
;
593 OnInform("Reading .bib file...");
596 char fieldValue
[4000];
597 char recordType
[100];
599 char recordField
[100];
604 BibEatWhiteSpace(istr
);
608 sprintf(buf
, "Expected @: malformed bib file at line %ld (%s)", BibLine
, filename
);
612 BibReadWord(istr
, recordType
);
613 BibEatWhiteSpace(istr
);
615 if (ch
!= '{' && ch
!= '(')
617 sprintf(buf
, "Expected { or ( after record type: malformed .bib file at line %ld (%s)", BibLine
, filename
);
621 BibEatWhiteSpace(istr
);
622 if (StringMatch(recordType
, "string", FALSE
, TRUE
))
624 BibReadWord(istr
, recordType
);
625 BibEatWhiteSpace(istr
);
629 sprintf(buf
, "Expected = after string key: malformed .bib file at line %ld (%s)", BibLine
, filename
);
633 BibEatWhiteSpace(istr
);
635 if (ch
!= '"' && ch
!= '{')
637 sprintf(buf
, "Expected = after string key: malformed .bib file at line %ld (%s)", BibLine
, filename
);
641 BibReadValue(istr
, fieldValue
);
643 // Now put in hash table if necesary
644 if (!BibStringTable
.Get(recordType
))
645 BibStringTable
.Put(recordType
, (wxObject
*)copystring(fieldValue
));
647 // Read closing ) or }
648 BibEatWhiteSpace(istr
);
650 BibEatWhiteSpace(istr
);
654 BibReadWord(istr
, recordKey
);
656 BibEntry
*bibEntry
= new BibEntry
;
657 bibEntry
->key
= copystring(recordKey
);
658 bibEntry
->type
= copystring(recordType
);
660 bool moreRecords
= TRUE
;
661 while (moreRecords
&& !istr
.eof())
663 BibEatWhiteSpace(istr
);
665 if (ch
== '}' || ch
== ')')
671 BibEatWhiteSpace(istr
);
672 BibReadWord(istr
, recordField
);
673 BibEatWhiteSpace(istr
);
677 sprintf(buf
, "Expected = after field type: malformed .bib file at line %ld (%s)", BibLine
, filename
);
681 BibEatWhiteSpace(istr
);
683 if (ch
!= '{' && ch
!= '"')
686 BibReadWord(istr
, fieldValue
+1);
688 // If in the table of strings, replace with string from table.
689 char *s
= (char *)BibStringTable
.Get(fieldValue
);
692 strcpy(fieldValue
, s
);
696 BibReadValue(istr
, fieldValue
, TRUE
, (ch
== '"' ? TRUE
: FALSE
));
698 // Now we can add a field
699 if (StringMatch(recordField
, "author", FALSE
, TRUE
))
700 bibEntry
->author
= copystring(fieldValue
);
701 else if (StringMatch(recordField
, "key", FALSE
, TRUE
))
703 else if (StringMatch(recordField
, "annotate", FALSE
, TRUE
))
705 else if (StringMatch(recordField
, "abstract", FALSE
, TRUE
))
707 else if (StringMatch(recordField
, "edition", FALSE
, TRUE
))
709 else if (StringMatch(recordField
, "howpublished", FALSE
, TRUE
))
711 else if (StringMatch(recordField
, "note", FALSE
, TRUE
) || StringMatch(recordField
, "notes", FALSE
, TRUE
))
713 else if (StringMatch(recordField
, "series", FALSE
, TRUE
))
715 else if (StringMatch(recordField
, "type", FALSE
, TRUE
))
717 else if (StringMatch(recordField
, "keywords", FALSE
, TRUE
))
719 else if (StringMatch(recordField
, "editor", FALSE
, TRUE
) || StringMatch(recordField
, "editors", FALSE
, TRUE
))
720 bibEntry
->editor
= copystring(fieldValue
);
721 else if (StringMatch(recordField
, "title", FALSE
, TRUE
))
722 bibEntry
->title
= copystring(fieldValue
);
723 else if (StringMatch(recordField
, "booktitle", FALSE
, TRUE
))
724 bibEntry
->booktitle
= copystring(fieldValue
);
725 else if (StringMatch(recordField
, "journal", FALSE
, TRUE
))
726 bibEntry
->journal
= copystring(fieldValue
);
727 else if (StringMatch(recordField
, "volume", FALSE
, TRUE
))
728 bibEntry
->volume
= copystring(fieldValue
);
729 else if (StringMatch(recordField
, "number", FALSE
, TRUE
))
730 bibEntry
->number
= copystring(fieldValue
);
731 else if (StringMatch(recordField
, "year", FALSE
, TRUE
))
732 bibEntry
->year
= copystring(fieldValue
);
733 else if (StringMatch(recordField
, "month", FALSE
, TRUE
))
734 bibEntry
->month
= copystring(fieldValue
);
735 else if (StringMatch(recordField
, "pages", FALSE
, TRUE
))
736 bibEntry
->pages
= copystring(fieldValue
);
737 else if (StringMatch(recordField
, "publisher", FALSE
, TRUE
))
738 bibEntry
->publisher
= copystring(fieldValue
);
739 else if (StringMatch(recordField
, "address", FALSE
, TRUE
))
740 bibEntry
->address
= copystring(fieldValue
);
741 else if (StringMatch(recordField
, "institution", FALSE
, TRUE
) || StringMatch(recordField
, "school", FALSE
, TRUE
))
742 bibEntry
->institution
= copystring(fieldValue
);
743 else if (StringMatch(recordField
, "organization", FALSE
, TRUE
) || StringMatch(recordField
, "organisation", FALSE
, TRUE
))
744 bibEntry
->organization
= copystring(fieldValue
);
745 else if (StringMatch(recordField
, "comment", FALSE
, TRUE
) || StringMatch(recordField
, "comments", FALSE
, TRUE
))
746 bibEntry
->comment
= copystring(fieldValue
);
747 else if (StringMatch(recordField
, "annote", FALSE
, TRUE
))
748 bibEntry
->comment
= copystring(fieldValue
);
749 else if (StringMatch(recordField
, "chapter", FALSE
, TRUE
))
750 bibEntry
->chapter
= copystring(fieldValue
);
753 sprintf(buf
, "Unrecognised bib field type %s at line %ld (%s)", recordField
, BibLine
, filename
);
758 BibList
.Append(recordKey
, bibEntry
);
759 BibEatWhiteSpace(istr
);
765 void OutputBibItem(TexRef
*ref
, BibEntry
*bib
)
769 OnMacro(ltNUMBEREDBIBITEM
, 2, TRUE
);
770 OnArgument(ltNUMBEREDBIBITEM
, 1, TRUE
);
771 TexOutput(ref
->sectionNumber
);
772 OnArgument(ltNUMBEREDBIBITEM
, 1, FALSE
);
773 OnArgument(ltNUMBEREDBIBITEM
, 2, TRUE
);
776 OnMacro(ltBF
, 1, TRUE
);
777 OnArgument(ltBF
, 1, TRUE
);
779 TexOutput(bib
->author
);
780 OnArgument(ltBF
, 1, FALSE
);
781 OnMacro(ltBF
, 1, FALSE
);
782 if (bib
->author
&& (strlen(bib
->author
) > 0) && (bib
->author
[strlen(bib
->author
) - 1] != '.'))
789 TexOutput(bib
->year
);
794 TexOutput(bib
->month
);
797 if (bib
->year
|| bib
->month
)
800 if (StringMatch(bib
->type
, "article", FALSE
, TRUE
))
804 TexOutput(bib
->title
);
809 OnMacro(ltIT
, 1, TRUE
);
810 OnArgument(ltIT
, 1, TRUE
);
811 TexOutput(bib
->journal
);
812 OnArgument(ltIT
, 1, FALSE
);
813 OnMacro(ltIT
, 1, FALSE
);
818 OnMacro(ltBF
, 1, TRUE
);
819 OnArgument(ltBF
, 1, TRUE
);
820 TexOutput(bib
->volume
);
821 OnArgument(ltBF
, 1, FALSE
);
822 OnMacro(ltBF
, 1, FALSE
);
827 TexOutput(bib
->number
);
832 TexOutput(", pages ");
833 TexOutput(bib
->pages
);
837 else if (StringMatch(bib
->type
, "book", FALSE
, TRUE
) ||
838 StringMatch(bib
->type
, "unpublished", FALSE
, TRUE
) ||
839 StringMatch(bib
->type
, "manual", FALSE
, TRUE
) ||
840 StringMatch(bib
->type
, "phdthesis", FALSE
, TRUE
) ||
841 StringMatch(bib
->type
, "mastersthesis", FALSE
, TRUE
) ||
842 StringMatch(bib
->type
, "misc", FALSE
, TRUE
) ||
843 StringMatch(bib
->type
, "techreport", FALSE
, TRUE
) ||
844 StringMatch(bib
->type
, "booklet", FALSE
, TRUE
))
846 if (bib
->title
|| bib
->booktitle
)
848 OnMacro(ltIT
, 1, TRUE
);
849 OnArgument(ltIT
, 1, TRUE
);
850 TexOutput(bib
->title
? bib
->title
: bib
->booktitle
);
852 OnArgument(ltIT
, 1, FALSE
);
853 OnMacro(ltIT
, 1, FALSE
);
855 if (StringMatch(bib
->type
, "phdthesis", FALSE
, TRUE
))
856 TexOutput("PhD thesis. ");
857 if (StringMatch(bib
->type
, "techreport", FALSE
, TRUE
))
858 TexOutput("Technical report. ");
862 TexOutput(bib
->editor
);
865 if (bib
->institution
)
867 TexOutput(bib
->institution
);
870 if (bib
->organization
)
872 TexOutput(bib
->organization
);
877 TexOutput(bib
->publisher
);
882 TexOutput(bib
->address
);
886 else if (StringMatch(bib
->type
, "inbook", FALSE
, TRUE
) ||
887 StringMatch(bib
->type
, "inproceedings", FALSE
, TRUE
) ||
888 StringMatch(bib
->type
, "incollection", FALSE
, TRUE
) ||
889 StringMatch(bib
->type
, "conference", FALSE
, TRUE
))
893 TexOutput(bib
->title
);
897 TexOutput(", from ");
898 OnMacro(ltIT
, 1, TRUE
);
899 OnArgument(ltIT
, 1, TRUE
);
900 TexOutput(bib
->booktitle
);
902 OnArgument(ltIT
, 1, FALSE
);
903 OnMacro(ltIT
, 1, FALSE
);
908 TexOutput(bib
->editor
);
913 TexOutput(bib
->publisher
);
917 if (bib
->publisher
) TexOutput(", ");
919 TexOutput(bib
->address
);
921 if (bib
->publisher
|| bib
->address
)
927 OnMacro(ltBF
, 1, TRUE
);
928 OnArgument(ltBF
, 1, TRUE
);
929 TexOutput(bib
->volume
);
930 OnArgument(ltBF
, 1, FALSE
);
931 OnMacro(ltBF
, 1, FALSE
);
938 TexOutput(bib
->number
);
943 TexOutput(" Number ");
944 TexOutput(bib
->number
);
950 TexOutput(" Chap. "); TexOutput(bib
->chapter
);
954 if (bib
->chapter
) TexOutput(", pages ");
955 else TexOutput(" Pages ");
956 TexOutput(bib
->pages
);
960 OnArgument(ltNUMBEREDBIBITEM
, 2, FALSE
);
961 OnMacro(ltNUMBEREDBIBITEM
, 2, FALSE
);
967 ForceTopicName("bibliography");
968 FakeCurrentSection(ReferencesNameString
);
969 ForceTopicName(NULL
);
971 OnMacro(ltPAR
, 0, TRUE
);
972 OnMacro(ltPAR
, 0, FALSE
);
974 if ((convertMode
== TEX_RTF
) && !winHelp
)
976 OnMacro(ltPAR
, 0, TRUE
);
977 OnMacro(ltPAR
, 0, FALSE
);
980 wxNode
*node
= CitationList
.First();
983 char *citeKey
= (char *)node
->Data();
984 // wxNode *texNode = TexReferences.Find(citeKey);
985 TexRef
*ref
= (TexRef
*)TexReferences
.Get(citeKey
);
986 wxNode
*bibNode
= BibList
.Find(citeKey
);
989 BibEntry
*entry
= (BibEntry
*)bibNode
->Data();
990 OutputBibItem(ref
, entry
);
996 static int citeCount
= 1;
998 void ResolveBibReferences(void)
1000 if (CitationList
.Number() > 0)
1001 OnInform("Resolving bibliographic references...");
1005 wxNode
*node
= CitationList
.First();
1009 char *citeKey
= (char *)node
->Data();
1010 // wxNode *texNode = TexReferences.Find(citeKey);
1011 TexRef
*ref
= (TexRef
*)TexReferences
.Get(citeKey
);
1012 wxNode
*bibNode
= BibList
.Find(citeKey
);
1016 //BibEntry *entry = (BibEntry *)bibNode->Data();
1017 if (ref
->sectionNumber
) delete[] ref
->sectionNumber
;
1018 sprintf(buf
, "[%d]", citeCount
);
1019 ref
->sectionNumber
= copystring(buf
);
1024 sprintf(buf
, "Warning: bib ref %s not resolved.", citeKey
);
1027 node
= node
->Next();
1031 // Remember we need to resolve this citation
1032 void AddCitation(char *citeKey
)
1034 if (!CitationList
.Member(citeKey
))
1035 CitationList
.Add(citeKey
);
1037 if (!TexReferences
.Get(citeKey
))
1039 TexReferences
.Put(citeKey
, new TexRef(citeKey
, "??", NULL
));
1043 TexRef
*FindReference(char *key
)
1045 return (TexRef
*)TexReferences
.Get(key
);
1049 * Custom macro stuff
1053 bool StringTobool(char *val
)
1055 if (strncmp(val
, "yes", 3) == 0 || strncmp(val
, "YES", 3) == 0 ||
1056 strncmp(val
, "on", 2) == 0 || strncmp(val
, "ON", 2) == 0 ||
1057 strncmp(val
, "true", 4) == 0 || strncmp(val
, "TRUE", 4) == 0 ||
1058 strncmp(val
, "ok", 2) == 0 || strncmp(val
, "OK", 2) == 0 ||
1059 strncmp(val
, "1", 1) == 0)
1065 // Define a variable value from the .ini file
1066 char *RegisterSetting(char *settingName
, char *settingValue
, bool interactive
)
1068 static char errorCode
[100];
1069 strcpy(errorCode
, "OK");
1070 if (StringMatch(settingName
, "chapterName", FALSE
, TRUE
))
1072 delete[] ChapterNameString
;
1073 ChapterNameString
= copystring(settingValue
);
1075 else if (StringMatch(settingName
, "sectionName", FALSE
, TRUE
))
1077 delete[] SectionNameString
;
1078 SectionNameString
= copystring(settingValue
);
1080 else if (StringMatch(settingName
, "subsectionName", FALSE
, TRUE
))
1082 delete[] SubsectionNameString
;
1083 SubsectionNameString
= copystring(settingValue
);
1085 else if (StringMatch(settingName
, "subsubsectionName", FALSE
, TRUE
))
1087 delete[] SubsubsectionNameString
;
1088 SubsubsectionNameString
= copystring(settingValue
);
1090 else if (StringMatch(settingName
, "indexName", FALSE
, TRUE
))
1092 delete[] IndexNameString
;
1093 IndexNameString
= copystring(settingValue
);
1095 else if (StringMatch(settingName
, "contentsName", FALSE
, TRUE
))
1097 delete[] ContentsNameString
;
1098 ContentsNameString
= copystring(settingValue
);
1100 else if (StringMatch(settingName
, "glossaryName", FALSE
, TRUE
))
1102 delete[] GlossaryNameString
;
1103 GlossaryNameString
= copystring(settingValue
);
1105 else if (StringMatch(settingName
, "referencesName", FALSE
, TRUE
))
1107 delete[] ReferencesNameString
;
1108 ReferencesNameString
= copystring(settingValue
);
1110 else if (StringMatch(settingName
, "tablesName", FALSE
, TRUE
))
1112 delete[] TablesNameString
;
1113 TablesNameString
= copystring(settingValue
);
1115 else if (StringMatch(settingName
, "figuresName", FALSE
, TRUE
))
1117 delete[] FiguresNameString
;
1118 FiguresNameString
= copystring(settingValue
);
1120 else if (StringMatch(settingName
, "tableName", FALSE
, TRUE
))
1122 delete[] TableNameString
;
1123 TableNameString
= copystring(settingValue
);
1125 else if (StringMatch(settingName
, "figureName", FALSE
, TRUE
))
1127 delete[] FigureNameString
;
1128 FigureNameString
= copystring(settingValue
);
1130 else if (StringMatch(settingName
, "abstractName", FALSE
, TRUE
))
1132 delete[] AbstractNameString
;
1133 AbstractNameString
= copystring(settingValue
);
1135 else if (StringMatch(settingName
, "chapterFontSize", FALSE
, TRUE
))
1136 StringToInt(settingValue
, &chapterFont
);
1137 else if (StringMatch(settingName
, "sectionFontSize", FALSE
, TRUE
))
1138 StringToInt(settingValue
, §ionFont
);
1139 else if (StringMatch(settingName
, "subsectionFontSize", FALSE
, TRUE
))
1140 StringToInt(settingValue
, &subsectionFont
);
1141 else if (StringMatch(settingName
, "titleFontSize", FALSE
, TRUE
))
1142 StringToInt(settingValue
, &titleFont
);
1143 else if (StringMatch(settingName
, "authorFontSize", FALSE
, TRUE
))
1144 StringToInt(settingValue
, &authorFont
);
1145 else if (StringMatch(settingName
, "ignoreInput", FALSE
, TRUE
))
1146 IgnorableInputFiles
.Add(FileNameFromPath(settingValue
));
1147 else if (StringMatch(settingName
, "mirrorMargins", FALSE
, TRUE
))
1148 mirrorMargins
= StringTobool(settingValue
);
1149 else if (StringMatch(settingName
, "runTwice", FALSE
, TRUE
))
1150 runTwice
= StringTobool(settingValue
);
1151 else if (StringMatch(settingName
, "isInteractive", FALSE
, TRUE
))
1152 isInteractive
= StringTobool(settingValue
);
1153 else if (StringMatch(settingName
, "headerRule", FALSE
, TRUE
))
1154 headerRule
= StringTobool(settingValue
);
1155 else if (StringMatch(settingName
, "footerRule", FALSE
, TRUE
))
1156 footerRule
= StringTobool(settingValue
);
1157 else if (StringMatch(settingName
, "combineSubSections", FALSE
, TRUE
))
1158 combineSubSections
= StringTobool(settingValue
);
1159 else if (StringMatch(settingName
, "listLabelIndent", FALSE
, TRUE
))
1160 StringToInt(settingValue
, &labelIndentTab
);
1161 else if (StringMatch(settingName
, "listItemIndent", FALSE
, TRUE
))
1162 StringToInt(settingValue
, &itemIndentTab
);
1163 else if (StringMatch(settingName
, "useUpButton", FALSE
, TRUE
))
1164 useUpButton
= StringTobool(settingValue
);
1165 else if (StringMatch(settingName
, "useHeadingStyles", FALSE
, TRUE
))
1166 useHeadingStyles
= StringTobool(settingValue
);
1167 else if (StringMatch(settingName
, "useWord", FALSE
, TRUE
))
1168 useWord
= StringTobool(settingValue
);
1169 else if (StringMatch(settingName
, "contentsDepth", FALSE
, TRUE
))
1170 StringToInt(settingValue
, &contentsDepth
);
1171 else if (StringMatch(settingName
, "generateHPJ", FALSE
, TRUE
))
1172 generateHPJ
= StringTobool(settingValue
);
1173 else if (StringMatch(settingName
, "truncateFilenames", FALSE
, TRUE
))
1174 truncateFilenames
= StringTobool(settingValue
);
1175 else if (StringMatch(settingName
, "winHelpVersion", FALSE
, TRUE
))
1176 StringToInt(settingValue
, &winHelpVersion
);
1177 else if (StringMatch(settingName
, "winHelpContents", FALSE
, TRUE
))
1178 winHelpContents
= StringTobool(settingValue
);
1179 else if (StringMatch(settingName
, "htmlIndex", FALSE
, TRUE
))
1180 htmlIndex
= StringTobool(settingValue
);
1181 else if (StringMatch(settingName
, "htmlWorkshopFiles", FALSE
, TRUE
))
1182 htmlWorkshopFiles
= StringTobool(settingValue
);
1183 else if (StringMatch(settingName
, "htmlFrameContents", FALSE
, TRUE
))
1184 htmlFrameContents
= StringTobool(settingValue
);
1185 else if (StringMatch(settingName
, "upperCaseNames", FALSE
, TRUE
))
1186 upperCaseNames
= StringTobool(settingValue
);
1187 else if (StringMatch(settingName
, "ignoreBadRefs", FALSE
, TRUE
))
1188 ignoreBadRefs
= StringTobool(settingValue
);
1189 else if (StringMatch(settingName
, "htmlFaceName", FALSE
, TRUE
))
1191 delete[] htmlFaceName
;
1192 htmlFaceName
= copystring(settingValue
);
1194 else if (StringMatch(settingName
, "winHelpTitle", FALSE
, TRUE
))
1197 delete[] winHelpTitle
;
1198 winHelpTitle
= copystring(settingValue
);
1200 else if (StringMatch(settingName
, "indexSubsections", FALSE
, TRUE
))
1201 indexSubsections
= StringTobool(settingValue
);
1202 else if (StringMatch(settingName
, "compatibility", FALSE
, TRUE
))
1203 compatibilityMode
= StringTobool(settingValue
);
1204 else if (StringMatch(settingName
, "defaultColumnWidth", FALSE
, TRUE
))
1206 StringToInt(settingValue
, &defaultTableColumnWidth
);
1207 defaultTableColumnWidth
= 20*defaultTableColumnWidth
;
1209 else if (StringMatch(settingName
, "bitmapMethod", FALSE
, TRUE
))
1211 if ((strcmp(settingValue
, "includepicture") != 0) && (strcmp(settingValue
, "hex") != 0) &&
1212 (strcmp(settingValue
, "import") != 0))
1215 OnError("Unknown bitmapMethod");
1216 strcpy(errorCode
, "Unknown bitmapMethod");
1220 delete[] bitmapMethod
;
1221 bitmapMethod
= copystring(settingValue
);
1224 else if (StringMatch(settingName
, "htmlBrowseButtons", FALSE
, TRUE
))
1226 if (strcmp(settingValue
, "none") == 0)
1227 htmlBrowseButtons
= HTML_BUTTONS_NONE
;
1228 else if (strcmp(settingValue
, "bitmap") == 0)
1229 htmlBrowseButtons
= HTML_BUTTONS_BITMAP
;
1230 else if (strcmp(settingValue
, "text") == 0)
1231 htmlBrowseButtons
= HTML_BUTTONS_TEXT
;
1235 OnInform("Initialisation file error: htmlBrowseButtons must be one of none, bitmap, or text.");
1236 strcpy(errorCode
, "Initialisation file error: htmlBrowseButtons must be one of none, bitmap, or text.");
1239 else if (StringMatch(settingName
, "backgroundImage", FALSE
, TRUE
))
1241 backgroundImageString
= copystring(settingValue
);
1243 else if (StringMatch(settingName
, "backgroundColour", FALSE
, TRUE
))
1245 delete[] backgroundColourString
;
1246 backgroundColourString
= copystring(settingValue
);
1248 else if (StringMatch(settingName
, "textColour", FALSE
, TRUE
))
1250 textColourString
= copystring(settingValue
);
1252 else if (StringMatch(settingName
, "linkColour", FALSE
, TRUE
))
1254 linkColourString
= copystring(settingValue
);
1256 else if (StringMatch(settingName
, "followedLinkColour", FALSE
, TRUE
))
1258 followedLinkColourString
= copystring(settingValue
);
1260 else if (StringMatch(settingName
, "conversionMode", FALSE
, TRUE
))
1262 if (StringMatch(settingValue
, "RTF", FALSE
, TRUE
))
1264 winHelp
= FALSE
; convertMode
= TEX_RTF
;
1266 else if (StringMatch(settingValue
, "WinHelp", FALSE
, TRUE
))
1268 winHelp
= TRUE
; convertMode
= TEX_RTF
;
1270 else if (StringMatch(settingValue
, "XLP", FALSE
, TRUE
) ||
1271 StringMatch(settingValue
, "wxHelp", FALSE
, TRUE
))
1273 convertMode
= TEX_XLP
;
1275 else if (StringMatch(settingValue
, "HTML", FALSE
, TRUE
))
1277 convertMode
= TEX_HTML
;
1282 OnInform("Initialisation file error: conversionMode must be one of\nRTF, WinHelp, XLP (or wxHelp), HTML.");
1283 strcpy(errorCode
, "Initialisation file error: conversionMode must be one of\nRTF, WinHelp, XLP (or wxHelp), HTML.");
1286 else if (StringMatch(settingName
, "documentFontSize", FALSE
, TRUE
))
1289 StringToInt(settingValue
, &n
);
1290 if (n
== 10 || n
== 11 || n
== 12)
1295 sprintf(buf
, "Initialisation file error: nonstandard document font size %d.", n
);
1298 strcpy(errorCode
, buf
);
1304 sprintf(buf
, "Initialisation file error: unrecognised setting %s.", settingName
);
1307 strcpy(errorCode
, buf
);
1312 bool ReadCustomMacros(char *filename
)
1314 if (!wxFileExists(filename
))
1317 wxSTD ifstream
istr(filename
, ios::in
);
1319 if (istr
.bad()) return FALSE
;
1321 CustomMacroList
.Clear();
1323 char macroName
[100];
1324 char macroBody
[1000];
1329 BibEatWhiteSpace(istr
);
1334 if (ch
!= '\\') // Not a macro definition, so must be NAME=VALUE
1336 char settingName
[100];
1337 settingName
[0] = ch
;
1338 BibReadWord(istr
, (settingName
+1));
1339 BibEatWhiteSpace(istr
);
1343 OnError("Expected = following name: malformed tex2rtf.ini file.");
1348 char settingValue
[200];
1349 BibEatWhiteSpace(istr
);
1350 BibReadToEOL(istr
, settingValue
);
1351 RegisterSetting(settingName
, settingValue
);
1356 BibReadWord(istr
, macroName
);
1357 BibEatWhiteSpace(istr
);
1361 OnError("Expected [ followed by number of arguments: malformed tex2rtf.ini file.");
1368 OnError("Expected ] following number of arguments: malformed tex2rtf.ini file.");
1371 BibEatWhiteSpace(istr
);
1375 OnError("Expected { followed by macro body: malformed tex2rtf.ini file.");
1378 CustomMacro
*macro
= new CustomMacro(macroName
, noArgs
, NULL
);
1379 BibReadValue(istr
, macroBody
, FALSE
, FALSE
); // Don't ignore extra braces
1380 if (strlen(macroBody
) > 0)
1381 macro
->macroBody
= copystring(macroBody
);
1383 BibEatWhiteSpace(istr
);
1384 CustomMacroList
.Append(macroName
, macro
);
1385 AddMacroDef(ltCUSTOM_MACRO
, macroName
, noArgs
);
1389 sprintf(mbuf
, "Read initialization file %s.", filename
);
1394 CustomMacro
*FindCustomMacro(char *name
)
1396 wxNode
*node
= CustomMacroList
.Find(name
);
1399 CustomMacro
*macro
= (CustomMacro
*)node
->Data();
1405 // Display custom macros
1406 void ShowCustomMacros(void)
1408 wxNode
*node
= CustomMacroList
.First();
1411 OnInform("No custom macros loaded.\n");
1418 CustomMacro
*macro
= (CustomMacro
*)node
->Data();
1419 sprintf(buf
, "\\%s[%d]\n {%s}", macro
->macroName
, macro
->noArgs
,
1420 macro
->macroBody
? macro
->macroBody
: "");
1422 node
= node
->Next();
1426 // Parse a string into several comma-separated fields
1427 char *ParseMultifieldString(char *allFields
, int *pos
)
1429 static char buffer
[300];
1431 int fieldIndex
= *pos
;
1432 int len
= strlen(allFields
);
1434 bool keepGoing
= TRUE
;
1435 while ((fieldIndex
<= len
) && keepGoing
)
1437 if (allFields
[fieldIndex
] == ' ')
1442 else if (allFields
[fieldIndex
] == ',')
1444 *pos
= fieldIndex
+ 1;
1447 else if (allFields
[fieldIndex
] == 0)
1449 *pos
= fieldIndex
+ 1;
1454 buffer
[i
] = allFields
[fieldIndex
];
1460 if (oldPos
== (*pos
))
1474 ColourTableEntry::ColourTableEntry(const char *theName
, unsigned int r
, unsigned int g
, unsigned int b
)
1476 name
= copystring(theName
);
1482 ColourTableEntry::~ColourTableEntry(void)
1487 void AddColour(const char *theName
, unsigned int r
, unsigned int g
, unsigned int b
)
1489 wxNode
*node
= ColourTable
.Find(theName
);
1492 ColourTableEntry
*entry
= (ColourTableEntry
*)node
->Data();
1493 if (entry
->red
== r
|| entry
->green
== g
|| entry
->blue
== b
)
1501 ColourTableEntry
*entry
= new ColourTableEntry(theName
, r
, g
, b
);
1502 ColourTable
.Append(theName
, entry
);
1505 int FindColourPosition(char *theName
)
1508 wxNode
*node
= ColourTable
.First();
1511 ColourTableEntry
*entry
= (ColourTableEntry
*)node
->Data();
1512 if (strcmp(theName
, entry
->name
) == 0)
1515 node
= node
->Next();
1520 // Converts e.g. "red" -> "#FF0000"
1521 extern void DecToHex(int, char *);
1522 bool FindColourHTMLString(char *theName
, char *buf
)
1525 wxNode
*node
= ColourTable
.First();
1528 ColourTableEntry
*entry
= (ColourTableEntry
*)node
->Data();
1529 if (strcmp(theName
, entry
->name
) == 0)
1534 DecToHex(entry
->red
, buf2
);
1536 DecToHex(entry
->green
, buf2
);
1538 DecToHex(entry
->blue
, buf2
);
1544 node
= node
->Next();
1550 void InitialiseColourTable(void)
1552 // \\red0\\green0\\blue0;
1553 AddColour("black", 0,0,0);
1555 // \\red0\\green0\\blue255;\\red0\\green255\\blue255;\n");
1556 AddColour("cyan", 0,255,255);
1558 // \\red0\\green255\\blue0;
1559 AddColour("green", 0,255,0);
1561 // \\red255\\green0\\blue255;
1562 AddColour("magenta", 255,0,255);
1564 // \\red255\\green0\\blue0;
1565 AddColour("red", 255,0,0);
1567 // \\red255\\green255\\blue0;
1568 AddColour("yellow", 255,255,0);
1570 // \\red255\\green255\\blue255;}");
1571 AddColour("white", 255,255,255);
1575 * The purpose of this is to reduce the number of times wxYield is
1576 * called, since under Windows this can slow things down.
1579 static int yieldCount
= 0;
1581 void Tex2RTFYield(bool force
)
1589 if (yieldCount
== 0)
1599 // In both RTF generation and HTML generation for wxHelp version 2,
1600 // we need to associate \indexed keywords with the current filename/topics.
1602 // Hash table for lists of keywords for topics (WinHelp).
1603 wxHashTable
TopicTable(wxKEY_STRING
);
1604 void AddKeyWordForTopic(char *topic
, char *entry
, char *filename
)
1606 TexTopic
*texTopic
= (TexTopic
*)TopicTable
.Get(topic
);
1609 texTopic
= new TexTopic(filename
);
1610 texTopic
->keywords
= new wxStringList
;
1611 TopicTable
.Put(topic
, texTopic
);
1614 if (!texTopic
->keywords
->Member(entry
))
1615 texTopic
->keywords
->Add(entry
);
1618 void ClearKeyWordTable(void)
1620 TopicTable
.BeginFind();
1621 wxNode
*node
= TopicTable
.Next();
1624 TexTopic
*texTopic
= (TexTopic
*)node
->Data();
1626 node
= TopicTable
.Next();
1633 * TexTopic structure
1636 TexTopic::TexTopic(char *f
)
1639 filename
= copystring(f
);
1642 hasChildren
= FALSE
;
1646 TexTopic::~TexTopic(void)
1654 // Convert case, according to upperCaseNames setting.
1655 char *ConvertCase(char *s
)
1657 static char buf
[256];
1658 int len
= strlen(s
);
1661 for (i
= 0; i
< len
; i
++)
1662 buf
[i
] = toupper(s
[i
]);
1664 for (i
= 0; i
< len
; i
++)
1665 buf
[i
] = tolower(s
[i
]);