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
= 72.0/2.51;
243 else if (strcmp(units
, "mm") == 0)
244 conversionFactor
= 72.0/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 ifstream
istr(filename
, ios::nocreate
| ios::in
);
406 if (istr
.bad()) return;
411 char sectionName
[100];
421 istr
.get(ch
); // Read past space
424 while (ch
!= '\n' && !istr
.eof())
431 TexReferences
.Put(label
, new TexRef(label
, file
, section
, sectionName
));
438 * Bibliography-handling code
442 void BibEatWhiteSpace(istream
& str
)
444 char ch
= str
.peek();
446 while (!str
.eof() && (ch
== ' ' || ch
== '\t' || ch
== 13 || ch
== 10 || ch
== EOF
))
451 if ((ch
== EOF
) || str
.eof()) return;
455 // Ignore end-of-line comments
456 if (ch
== '%' || ch
== ';' || ch
== '#')
460 while (ch
!= 10 && ch
!= 13 && !str
.eof())
465 BibEatWhiteSpace(str
);
469 // Read word up to { or , or space
470 void BibReadWord(istream
& istr
, char *buffer
)
474 char ch
= istr
.peek();
475 while (!istr
.eof() && ch
!= ' ' && ch
!= '{' && ch
!= '(' && ch
!= 13 && ch
!= 10 && ch
!= '\t' &&
476 ch
!= ',' && ch
!= '=')
486 // Read string (double-quoted or not) to end quote or EOL
487 void BibReadToEOL(istream
& istr
, char *buffer
)
491 char ch
= istr
.peek();
492 bool inQuotes
= FALSE
;
499 // If in quotes, read white space too. If not,
500 // stop at white space or comment.
501 while (!istr
.eof() && ch
!= 13 && ch
!= 10 && ch
!= '"' &&
502 (inQuotes
|| ((ch
!= ' ') && (ch
!= 9) &&
503 (ch
!= ';') && (ch
!= '%') && (ch
!= '#'))))
515 // Read }-terminated value, taking nested braces into account.
516 void BibReadValue(istream
& istr
, char *buffer
, bool ignoreBraces
= TRUE
,
517 bool quotesMayTerminate
= TRUE
)
522 char ch
= istr
.peek();
523 bool stopping
= FALSE
;
524 while (!istr
.eof() && !stopping
)
530 sprintf(buf
, "Sorry, value > 2000 chars in bib file at line %ld, terminating.", BibLine
);
531 wxFatalError(buf
, "Tex2RTF Fatal Error");
547 else if (quotesMayTerminate
&& ch
== '"')
554 if (!ignoreBraces
|| (ch
!= '{' && ch
!= '}'))
566 bool ReadBib(char *filename
)
569 ifstream
istr(filename
, ios::nocreate
| ios::in
);
570 if (istr
.bad()) return FALSE
;
574 OnInform("Reading .bib file...");
577 char fieldValue
[2000];
578 char recordType
[100];
580 char recordField
[100];
585 BibEatWhiteSpace(istr
);
589 sprintf(buf
, "Expected @: malformed bib file at line %ld (%s)", BibLine
, filename
);
593 BibReadWord(istr
, recordType
);
594 BibEatWhiteSpace(istr
);
596 if (ch
!= '{' && ch
!= '(')
598 sprintf(buf
, "Expected { or ( after record type: malformed .bib file at line %ld (%s)", BibLine
, filename
);
602 BibEatWhiteSpace(istr
);
603 if (StringMatch(recordType
, "string", FALSE
, TRUE
))
605 BibReadWord(istr
, recordType
);
606 BibEatWhiteSpace(istr
);
610 sprintf(buf
, "Expected = after string key: malformed .bib file at line %ld (%s)", BibLine
, filename
);
614 BibEatWhiteSpace(istr
);
616 if (ch
!= '"' && ch
!= '{')
618 sprintf(buf
, "Expected = after string key: malformed .bib file at line %ld (%s)", BibLine
, filename
);
622 BibReadValue(istr
, fieldValue
);
624 // Now put in hash table if necesary
625 if (!BibStringTable
.Get(recordType
))
626 BibStringTable
.Put(recordType
, (wxObject
*)copystring(fieldValue
));
628 // Read closing ) or }
629 BibEatWhiteSpace(istr
);
631 BibEatWhiteSpace(istr
);
635 BibReadWord(istr
, recordKey
);
637 BibEntry
*bibEntry
= new BibEntry
;
638 bibEntry
->key
= copystring(recordKey
);
639 bibEntry
->type
= copystring(recordType
);
641 bool moreRecords
= TRUE
;
642 while (moreRecords
&& !istr
.eof())
644 BibEatWhiteSpace(istr
);
646 if (ch
== '}' || ch
== ')')
652 BibEatWhiteSpace(istr
);
653 BibReadWord(istr
, recordField
);
654 BibEatWhiteSpace(istr
);
658 sprintf(buf
, "Expected = after field type: malformed .bib file at line %ld (%s)", BibLine
, filename
);
662 BibEatWhiteSpace(istr
);
664 if (ch
!= '{' && ch
!= '"')
667 BibReadWord(istr
, fieldValue
+1);
669 // If in the table of strings, replace with string from table.
670 char *s
= (char *)BibStringTable
.Get(fieldValue
);
673 strcpy(fieldValue
, s
);
677 BibReadValue(istr
, fieldValue
, TRUE
, (ch
== '"' ? TRUE
: FALSE
));
679 // Now we can add a field
680 if (StringMatch(recordField
, "author", FALSE
, TRUE
))
681 bibEntry
->author
= copystring(fieldValue
);
682 else if (StringMatch(recordField
, "key", FALSE
, TRUE
))
684 else if (StringMatch(recordField
, "annotate", FALSE
, TRUE
))
686 else if (StringMatch(recordField
, "abstract", FALSE
, TRUE
))
688 else if (StringMatch(recordField
, "edition", FALSE
, TRUE
))
690 else if (StringMatch(recordField
, "howpublished", FALSE
, TRUE
))
692 else if (StringMatch(recordField
, "note", FALSE
, TRUE
) || StringMatch(recordField
, "notes", FALSE
, TRUE
))
694 else if (StringMatch(recordField
, "series", FALSE
, TRUE
))
696 else if (StringMatch(recordField
, "type", FALSE
, TRUE
))
698 else if (StringMatch(recordField
, "keywords", FALSE
, TRUE
))
700 else if (StringMatch(recordField
, "editor", FALSE
, TRUE
) || StringMatch(recordField
, "editors", FALSE
, TRUE
))
701 bibEntry
->editor
= copystring(fieldValue
);
702 else if (StringMatch(recordField
, "title", FALSE
, TRUE
))
703 bibEntry
->title
= copystring(fieldValue
);
704 else if (StringMatch(recordField
, "booktitle", FALSE
, TRUE
))
705 bibEntry
->booktitle
= copystring(fieldValue
);
706 else if (StringMatch(recordField
, "journal", FALSE
, TRUE
))
707 bibEntry
->journal
= copystring(fieldValue
);
708 else if (StringMatch(recordField
, "volume", FALSE
, TRUE
))
709 bibEntry
->volume
= copystring(fieldValue
);
710 else if (StringMatch(recordField
, "number", FALSE
, TRUE
))
711 bibEntry
->number
= copystring(fieldValue
);
712 else if (StringMatch(recordField
, "year", FALSE
, TRUE
))
713 bibEntry
->year
= copystring(fieldValue
);
714 else if (StringMatch(recordField
, "month", FALSE
, TRUE
))
715 bibEntry
->month
= copystring(fieldValue
);
716 else if (StringMatch(recordField
, "pages", FALSE
, TRUE
))
717 bibEntry
->pages
= copystring(fieldValue
);
718 else if (StringMatch(recordField
, "publisher", FALSE
, TRUE
))
719 bibEntry
->publisher
= copystring(fieldValue
);
720 else if (StringMatch(recordField
, "address", FALSE
, TRUE
))
721 bibEntry
->address
= copystring(fieldValue
);
722 else if (StringMatch(recordField
, "institution", FALSE
, TRUE
) || StringMatch(recordField
, "school", FALSE
, TRUE
))
723 bibEntry
->institution
= copystring(fieldValue
);
724 else if (StringMatch(recordField
, "organization", FALSE
, TRUE
) || StringMatch(recordField
, "organisation", FALSE
, TRUE
))
725 bibEntry
->organization
= copystring(fieldValue
);
726 else if (StringMatch(recordField
, "comment", FALSE
, TRUE
) || StringMatch(recordField
, "comments", FALSE
, TRUE
))
727 bibEntry
->comment
= copystring(fieldValue
);
728 else if (StringMatch(recordField
, "annote", FALSE
, TRUE
))
729 bibEntry
->comment
= copystring(fieldValue
);
730 else if (StringMatch(recordField
, "chapter", FALSE
, TRUE
))
731 bibEntry
->chapter
= copystring(fieldValue
);
734 sprintf(buf
, "Unrecognised bib field type %s at line %ld (%s)", recordField
, BibLine
, filename
);
739 BibList
.Append(recordKey
, bibEntry
);
740 BibEatWhiteSpace(istr
);
746 void OutputBibItem(TexRef
*ref
, BibEntry
*bib
)
750 OnMacro(ltNUMBEREDBIBITEM
, 2, TRUE
);
751 OnArgument(ltNUMBEREDBIBITEM
, 1, TRUE
);
752 TexOutput(ref
->sectionNumber
);
753 OnArgument(ltNUMBEREDBIBITEM
, 1, FALSE
);
754 OnArgument(ltNUMBEREDBIBITEM
, 2, TRUE
);
757 OnMacro(ltBF
, 1, TRUE
);
758 OnArgument(ltBF
, 1, TRUE
);
760 TexOutput(bib
->author
);
761 OnArgument(ltBF
, 1, FALSE
);
762 OnMacro(ltBF
, 1, FALSE
);
763 if (bib
->author
&& (strlen(bib
->author
) > 0) && (bib
->author
[strlen(bib
->author
) - 1] != '.'))
770 TexOutput(bib
->year
);
775 TexOutput(bib
->month
);
778 if (bib
->year
|| bib
->month
)
781 if (StringMatch(bib
->type
, "article", FALSE
, TRUE
))
785 TexOutput(bib
->title
);
790 OnMacro(ltIT
, 1, TRUE
);
791 OnArgument(ltIT
, 1, TRUE
);
792 TexOutput(bib
->journal
);
793 OnArgument(ltIT
, 1, FALSE
);
794 OnMacro(ltIT
, 1, FALSE
);
799 OnMacro(ltBF
, 1, TRUE
);
800 OnArgument(ltBF
, 1, TRUE
);
801 TexOutput(bib
->volume
);
802 OnArgument(ltBF
, 1, FALSE
);
803 OnMacro(ltBF
, 1, FALSE
);
808 TexOutput(bib
->number
);
813 TexOutput(", pages ");
814 TexOutput(bib
->pages
);
818 else if (StringMatch(bib
->type
, "book", FALSE
, TRUE
) ||
819 StringMatch(bib
->type
, "unpublished", FALSE
, TRUE
) ||
820 StringMatch(bib
->type
, "manual", FALSE
, TRUE
) ||
821 StringMatch(bib
->type
, "phdthesis", FALSE
, TRUE
) ||
822 StringMatch(bib
->type
, "mastersthesis", FALSE
, TRUE
) ||
823 StringMatch(bib
->type
, "misc", FALSE
, TRUE
) ||
824 StringMatch(bib
->type
, "techreport", FALSE
, TRUE
) ||
825 StringMatch(bib
->type
, "booklet", FALSE
, TRUE
))
827 if (bib
->title
|| bib
->booktitle
)
829 OnMacro(ltIT
, 1, TRUE
);
830 OnArgument(ltIT
, 1, TRUE
);
831 TexOutput(bib
->title
? bib
->title
: bib
->booktitle
);
833 OnArgument(ltIT
, 1, FALSE
);
834 OnMacro(ltIT
, 1, FALSE
);
836 if (StringMatch(bib
->type
, "phdthesis", FALSE
, TRUE
))
837 TexOutput("PhD thesis. ");
838 if (StringMatch(bib
->type
, "techreport", FALSE
, TRUE
))
839 TexOutput("Technical report. ");
843 TexOutput(bib
->editor
);
846 if (bib
->institution
)
848 TexOutput(bib
->institution
);
851 if (bib
->organization
)
853 TexOutput(bib
->organization
);
858 TexOutput(bib
->publisher
);
863 TexOutput(bib
->address
);
867 else if (StringMatch(bib
->type
, "inbook", FALSE
, TRUE
) ||
868 StringMatch(bib
->type
, "inproceedings", FALSE
, TRUE
) ||
869 StringMatch(bib
->type
, "incollection", FALSE
, TRUE
) ||
870 StringMatch(bib
->type
, "conference", FALSE
, TRUE
))
874 TexOutput(bib
->title
);
878 TexOutput(", from ");
879 OnMacro(ltIT
, 1, TRUE
);
880 OnArgument(ltIT
, 1, TRUE
);
881 TexOutput(bib
->booktitle
);
883 OnArgument(ltIT
, 1, FALSE
);
884 OnMacro(ltIT
, 1, FALSE
);
889 TexOutput(bib
->editor
);
894 TexOutput(bib
->publisher
);
898 if (bib
->publisher
) TexOutput(", ");
900 TexOutput(bib
->address
);
902 if (bib
->publisher
|| bib
->address
)
908 OnMacro(ltBF
, 1, TRUE
);
909 OnArgument(ltBF
, 1, TRUE
);
910 TexOutput(bib
->volume
);
911 OnArgument(ltBF
, 1, FALSE
);
912 OnMacro(ltBF
, 1, FALSE
);
919 TexOutput(bib
->number
);
924 TexOutput(" Number ");
925 TexOutput(bib
->number
);
931 TexOutput(" Chap. "); TexOutput(bib
->chapter
);
935 if (bib
->chapter
) TexOutput(", pages ");
936 else TexOutput(" Pages ");
937 TexOutput(bib
->pages
);
941 OnArgument(ltNUMBEREDBIBITEM
, 2, FALSE
);
942 OnMacro(ltNUMBEREDBIBITEM
, 2, FALSE
);
948 ForceTopicName("bibliography");
949 FakeCurrentSection(ReferencesNameString
);
950 ForceTopicName(NULL
);
952 OnMacro(ltPAR
, 0, TRUE
);
953 OnMacro(ltPAR
, 0, FALSE
);
955 if ((convertMode
== TEX_RTF
) && !winHelp
)
957 OnMacro(ltPAR
, 0, TRUE
);
958 OnMacro(ltPAR
, 0, FALSE
);
961 wxNode
*node
= CitationList
.First();
964 char *citeKey
= (char *)node
->Data();
965 // wxNode *texNode = TexReferences.Find(citeKey);
966 TexRef
*ref
= (TexRef
*)TexReferences
.Get(citeKey
);
967 wxNode
*bibNode
= BibList
.Find(citeKey
);
970 BibEntry
*entry
= (BibEntry
*)bibNode
->Data();
971 OutputBibItem(ref
, entry
);
977 static int citeCount
= 1;
979 void ResolveBibReferences(void)
981 if (CitationList
.Number() > 0)
982 OnInform("Resolving bibliographic references...");
986 wxNode
*node
= CitationList
.First();
990 char *citeKey
= (char *)node
->Data();
991 // wxNode *texNode = TexReferences.Find(citeKey);
992 TexRef
*ref
= (TexRef
*)TexReferences
.Get(citeKey
);
993 wxNode
*bibNode
= BibList
.Find(citeKey
);
997 //BibEntry *entry = (BibEntry *)bibNode->Data();
998 if (ref
->sectionNumber
) delete[] ref
->sectionNumber
;
999 sprintf(buf
, "[%d]", citeCount
);
1000 ref
->sectionNumber
= copystring(buf
);
1005 sprintf(buf
, "Warning: bib ref %s not resolved.", citeKey
);
1008 node
= node
->Next();
1012 // Remember we need to resolve this citation
1013 void AddCitation(char *citeKey
)
1015 if (!CitationList
.Member(citeKey
))
1016 CitationList
.Add(citeKey
);
1018 if (!TexReferences
.Get(citeKey
))
1020 TexReferences
.Put(citeKey
, new TexRef(citeKey
, "??", NULL
));
1024 TexRef
*FindReference(char *key
)
1026 return (TexRef
*)TexReferences
.Get(key
);
1030 * Custom macro stuff
1034 bool StringTobool(char *val
)
1036 if (strncmp(val
, "yes", 3) == 0 || strncmp(val
, "YES", 3) == 0 ||
1037 strncmp(val
, "on", 2) == 0 || strncmp(val
, "ON", 2) == 0 ||
1038 strncmp(val
, "true", 4) == 0 || strncmp(val
, "TRUE", 4) == 0 ||
1039 strncmp(val
, "ok", 2) == 0 || strncmp(val
, "OK", 2) == 0 ||
1040 strncmp(val
, "1", 1) == 0)
1046 // Define a variable value from the .ini file
1047 char *RegisterSetting(char *settingName
, char *settingValue
, bool interactive
)
1049 static char errorCode
[100];
1050 strcpy(errorCode
, "OK");
1051 if (StringMatch(settingName
, "chapterName", FALSE
, TRUE
))
1053 delete[] ChapterNameString
;
1054 ChapterNameString
= copystring(settingValue
);
1056 else if (StringMatch(settingName
, "sectionName", FALSE
, TRUE
))
1058 delete[] SectionNameString
;
1059 SectionNameString
= copystring(settingValue
);
1061 else if (StringMatch(settingName
, "subsectionName", FALSE
, TRUE
))
1063 delete[] SubsectionNameString
;
1064 SubsectionNameString
= copystring(settingValue
);
1066 else if (StringMatch(settingName
, "subsubsectionName", FALSE
, TRUE
))
1068 delete[] SubsubsectionNameString
;
1069 SubsubsectionNameString
= copystring(settingValue
);
1071 else if (StringMatch(settingName
, "indexName", FALSE
, TRUE
))
1073 delete[] IndexNameString
;
1074 IndexNameString
= copystring(settingValue
);
1076 else if (StringMatch(settingName
, "contentsName", FALSE
, TRUE
))
1078 delete[] ContentsNameString
;
1079 ContentsNameString
= copystring(settingValue
);
1081 else if (StringMatch(settingName
, "glossaryName", FALSE
, TRUE
))
1083 delete[] GlossaryNameString
;
1084 GlossaryNameString
= copystring(settingValue
);
1086 else if (StringMatch(settingName
, "referencesName", FALSE
, TRUE
))
1088 delete[] ReferencesNameString
;
1089 ReferencesNameString
= copystring(settingValue
);
1091 else if (StringMatch(settingName
, "tablesName", FALSE
, TRUE
))
1093 delete[] TablesNameString
;
1094 TablesNameString
= copystring(settingValue
);
1096 else if (StringMatch(settingName
, "figuresName", FALSE
, TRUE
))
1098 delete[] FiguresNameString
;
1099 FiguresNameString
= copystring(settingValue
);
1101 else if (StringMatch(settingName
, "tableName", FALSE
, TRUE
))
1103 delete[] TableNameString
;
1104 TableNameString
= copystring(settingValue
);
1106 else if (StringMatch(settingName
, "figureName", FALSE
, TRUE
))
1108 delete[] FigureNameString
;
1109 FigureNameString
= copystring(settingValue
);
1111 else if (StringMatch(settingName
, "abstractName", FALSE
, TRUE
))
1113 delete[] AbstractNameString
;
1114 AbstractNameString
= copystring(settingValue
);
1116 else if (StringMatch(settingName
, "chapterFontSize", FALSE
, TRUE
))
1117 StringToInt(settingValue
, &chapterFont
);
1118 else if (StringMatch(settingName
, "sectionFontSize", FALSE
, TRUE
))
1119 StringToInt(settingValue
, §ionFont
);
1120 else if (StringMatch(settingName
, "subsectionFontSize", FALSE
, TRUE
))
1121 StringToInt(settingValue
, &subsectionFont
);
1122 else if (StringMatch(settingName
, "titleFontSize", FALSE
, TRUE
))
1123 StringToInt(settingValue
, &titleFont
);
1124 else if (StringMatch(settingName
, "authorFontSize", FALSE
, TRUE
))
1125 StringToInt(settingValue
, &authorFont
);
1126 else if (StringMatch(settingName
, "ignoreInput", FALSE
, TRUE
))
1127 IgnorableInputFiles
.Add(FileNameFromPath(settingValue
));
1128 else if (StringMatch(settingName
, "mirrorMargins", FALSE
, TRUE
))
1129 mirrorMargins
= StringTobool(settingValue
);
1130 else if (StringMatch(settingName
, "runTwice", FALSE
, TRUE
))
1131 runTwice
= StringTobool(settingValue
);
1132 else if (StringMatch(settingName
, "isInteractive", FALSE
, TRUE
))
1133 isInteractive
= StringTobool(settingValue
);
1134 else if (StringMatch(settingName
, "headerRule", FALSE
, TRUE
))
1135 headerRule
= StringTobool(settingValue
);
1136 else if (StringMatch(settingName
, "footerRule", FALSE
, TRUE
))
1137 footerRule
= StringTobool(settingValue
);
1138 else if (StringMatch(settingName
, "combineSubSections", FALSE
, TRUE
))
1139 combineSubSections
= StringTobool(settingValue
);
1140 else if (StringMatch(settingName
, "listLabelIndent", FALSE
, TRUE
))
1141 StringToInt(settingValue
, &labelIndentTab
);
1142 else if (StringMatch(settingName
, "listItemIndent", FALSE
, TRUE
))
1143 StringToInt(settingValue
, &itemIndentTab
);
1144 else if (StringMatch(settingName
, "useUpButton", FALSE
, TRUE
))
1145 useUpButton
= StringTobool(settingValue
);
1146 else if (StringMatch(settingName
, "useHeadingStyles", FALSE
, TRUE
))
1147 useHeadingStyles
= StringTobool(settingValue
);
1148 else if (StringMatch(settingName
, "useWord", FALSE
, TRUE
))
1149 useWord
= StringTobool(settingValue
);
1150 else if (StringMatch(settingName
, "contentsDepth", FALSE
, TRUE
))
1151 StringToInt(settingValue
, &contentsDepth
);
1152 else if (StringMatch(settingName
, "generateHPJ", FALSE
, TRUE
))
1153 generateHPJ
= StringTobool(settingValue
);
1154 else if (StringMatch(settingName
, "truncateFilenames", FALSE
, TRUE
))
1155 truncateFilenames
= StringTobool(settingValue
);
1156 else if (StringMatch(settingName
, "winHelpVersion", FALSE
, TRUE
))
1157 StringToInt(settingValue
, &winHelpVersion
);
1158 else if (StringMatch(settingName
, "winHelpContents", FALSE
, TRUE
))
1159 winHelpContents
= StringTobool(settingValue
);
1160 else if (StringMatch(settingName
, "htmlIndex", FALSE
, TRUE
))
1161 htmlIndex
= StringTobool(settingValue
);
1162 else if (StringMatch(settingName
, "htmlFrameContents", FALSE
, TRUE
))
1163 htmlFrameContents
= StringTobool(settingValue
);
1164 else if (StringMatch(settingName
, "upperCaseNames", FALSE
, TRUE
))
1165 upperCaseNames
= StringTobool(settingValue
);
1166 else if (StringMatch(settingName
, "winHelpTitle", FALSE
, TRUE
))
1169 delete[] winHelpTitle
;
1170 winHelpTitle
= copystring(settingValue
);
1172 else if (StringMatch(settingName
, "indexSubsections", FALSE
, TRUE
))
1173 indexSubsections
= StringTobool(settingValue
);
1174 else if (StringMatch(settingName
, "compatibility", FALSE
, TRUE
))
1175 compatibilityMode
= StringTobool(settingValue
);
1176 else if (StringMatch(settingName
, "defaultColumnWidth", FALSE
, TRUE
))
1178 StringToInt(settingValue
, &defaultTableColumnWidth
);
1179 defaultTableColumnWidth
= 20*defaultTableColumnWidth
;
1181 else if (StringMatch(settingName
, "bitmapMethod", FALSE
, TRUE
))
1183 if ((strcmp(settingValue
, "includepicture") != 0) && (strcmp(settingValue
, "hex") != 0) &&
1184 (strcmp(settingValue
, "import") != 0))
1187 OnError("Unknown bitmapMethod");
1188 strcpy(errorCode
, "Unknown bitmapMethod");
1192 delete[] bitmapMethod
;
1193 bitmapMethod
= copystring(settingValue
);
1196 else if (StringMatch(settingName
, "htmlBrowseButtons", FALSE
, TRUE
))
1198 if (strcmp(settingValue
, "none") == 0)
1199 htmlBrowseButtons
= HTML_BUTTONS_NONE
;
1200 else if (strcmp(settingValue
, "bitmap") == 0)
1201 htmlBrowseButtons
= HTML_BUTTONS_BITMAP
;
1202 else if (strcmp(settingValue
, "text") == 0)
1203 htmlBrowseButtons
= HTML_BUTTONS_TEXT
;
1207 OnInform("Initialisation file error: htmlBrowseButtons must be one of none, bitmap, or text.");
1208 strcpy(errorCode
, "Initialisation file error: htmlBrowseButtons must be one of none, bitmap, or text.");
1211 else if (StringMatch(settingName
, "backgroundImage", FALSE
, TRUE
))
1213 backgroundImageString
= copystring(settingValue
);
1215 else if (StringMatch(settingName
, "backgroundColour", FALSE
, TRUE
))
1217 delete[] backgroundColourString
;
1218 backgroundColourString
= copystring(settingValue
);
1220 else if (StringMatch(settingName
, "textColour", FALSE
, TRUE
))
1222 textColourString
= copystring(settingValue
);
1224 else if (StringMatch(settingName
, "linkColour", FALSE
, TRUE
))
1226 linkColourString
= copystring(settingValue
);
1228 else if (StringMatch(settingName
, "followedLinkColour", FALSE
, TRUE
))
1230 followedLinkColourString
= copystring(settingValue
);
1232 else if (StringMatch(settingName
, "conversionMode", FALSE
, TRUE
))
1234 if (StringMatch(settingValue
, "RTF", FALSE
, TRUE
))
1236 winHelp
= FALSE
; convertMode
= TEX_RTF
;
1238 else if (StringMatch(settingValue
, "WinHelp", FALSE
, TRUE
))
1240 winHelp
= TRUE
; convertMode
= TEX_RTF
;
1242 else if (StringMatch(settingValue
, "XLP", FALSE
, TRUE
) ||
1243 StringMatch(settingValue
, "wxHelp", FALSE
, TRUE
))
1245 convertMode
= TEX_XLP
;
1247 else if (StringMatch(settingValue
, "HTML", FALSE
, TRUE
))
1249 convertMode
= TEX_HTML
;
1254 OnInform("Initialisation file error: conversionMode must be one of\nRTF, WinHelp, XLP (or wxHelp), HTML.");
1255 strcpy(errorCode
, "Initialisation file error: conversionMode must be one of\nRTF, WinHelp, XLP (or wxHelp), HTML.");
1258 else if (StringMatch(settingName
, "documentFontSize", FALSE
, TRUE
))
1261 StringToInt(settingValue
, &n
);
1262 if (n
== 10 || n
== 11 || n
== 12)
1267 sprintf(buf
, "Initialisation file error: nonstandard document font size %d.", n
);
1270 strcpy(errorCode
, buf
);
1276 sprintf(buf
, "Initialisation file error: unrecognised setting %s.", settingName
);
1279 strcpy(errorCode
, buf
);
1284 bool ReadCustomMacros(char *filename
)
1286 ifstream
istr(filename
, ios::nocreate
| ios::in
);
1287 if (istr
.bad()) return FALSE
;
1289 CustomMacroList
.Clear();
1291 char macroName
[100];
1292 char macroBody
[1000];
1297 BibEatWhiteSpace(istr
);
1302 if (ch
!= '\\') // Not a macro definition, so must be NAME=VALUE
1304 char settingName
[100];
1305 settingName
[0] = ch
;
1306 BibReadWord(istr
, (settingName
+1));
1307 BibEatWhiteSpace(istr
);
1311 OnError("Expected = following name: malformed tex2rtf.ini file.");
1316 char settingValue
[200];
1317 BibEatWhiteSpace(istr
);
1318 BibReadToEOL(istr
, settingValue
);
1319 RegisterSetting(settingName
, settingValue
);
1324 BibReadWord(istr
, macroName
);
1325 BibEatWhiteSpace(istr
);
1329 OnError("Expected [ followed by number of arguments: malformed tex2rtf.ini file.");
1336 OnError("Expected ] following number of arguments: malformed tex2rtf.ini file.");
1339 BibEatWhiteSpace(istr
);
1343 OnError("Expected { followed by macro body: malformed tex2rtf.ini file.");
1346 CustomMacro
*macro
= new CustomMacro(macroName
, noArgs
, NULL
);
1347 BibReadValue(istr
, macroBody
, FALSE
, FALSE
); // Don't ignore extra braces
1348 if (strlen(macroBody
) > 0)
1349 macro
->macroBody
= copystring(macroBody
);
1351 BibEatWhiteSpace(istr
);
1352 CustomMacroList
.Append(macroName
, macro
);
1353 AddMacroDef(ltCUSTOM_MACRO
, macroName
, noArgs
);
1357 sprintf(mbuf
, "Read initialization file %s.", filename
);
1362 CustomMacro
*FindCustomMacro(char *name
)
1364 wxNode
*node
= CustomMacroList
.Find(name
);
1367 CustomMacro
*macro
= (CustomMacro
*)node
->Data();
1373 // Display custom macros
1374 void ShowCustomMacros(void)
1376 wxNode
*node
= CustomMacroList
.First();
1379 OnInform("No custom macros loaded.\n");
1386 CustomMacro
*macro
= (CustomMacro
*)node
->Data();
1387 sprintf(buf
, "\\%s[%d]\n {%s}", macro
->macroName
, macro
->noArgs
,
1388 macro
->macroBody
? macro
->macroBody
: "");
1390 node
= node
->Next();
1394 // Parse a string into several comma-separated fields
1395 char *ParseMultifieldString(char *allFields
, int *pos
)
1397 static char buffer
[300];
1399 int fieldIndex
= *pos
;
1400 int len
= strlen(allFields
);
1402 bool keepGoing
= TRUE
;
1403 while ((fieldIndex
<= len
) && keepGoing
)
1405 if (allFields
[fieldIndex
] == ' ')
1410 else if (allFields
[fieldIndex
] == ',')
1412 *pos
= fieldIndex
+ 1;
1415 else if (allFields
[fieldIndex
] == 0)
1417 *pos
= fieldIndex
+ 1;
1422 buffer
[i
] = allFields
[fieldIndex
];
1428 if (oldPos
== (*pos
))
1442 ColourTableEntry::ColourTableEntry(char *theName
, unsigned int r
, unsigned int g
, unsigned int b
)
1444 name
= copystring(theName
);
1450 ColourTableEntry::~ColourTableEntry(void)
1455 void AddColour(char *theName
, unsigned int r
, unsigned int g
, unsigned int b
)
1457 wxNode
*node
= ColourTable
.Find(theName
);
1460 ColourTableEntry
*entry
= (ColourTableEntry
*)node
->Data();
1461 if (entry
->red
== r
|| entry
->green
== g
|| entry
->blue
== b
)
1469 ColourTableEntry
*entry
= new ColourTableEntry(theName
, r
, g
, b
);
1470 ColourTable
.Append(theName
, entry
);
1473 int FindColourPosition(char *theName
)
1476 wxNode
*node
= ColourTable
.First();
1479 ColourTableEntry
*entry
= (ColourTableEntry
*)node
->Data();
1480 if (strcmp(theName
, entry
->name
) == 0)
1483 node
= node
->Next();
1488 // Converts e.g. "red" -> "#FF0000"
1489 extern void DecToHex(int, char *);
1490 bool FindColourHTMLString(char *theName
, char *buf
)
1493 wxNode
*node
= ColourTable
.First();
1496 ColourTableEntry
*entry
= (ColourTableEntry
*)node
->Data();
1497 if (strcmp(theName
, entry
->name
) == 0)
1502 DecToHex(entry
->red
, buf2
);
1504 DecToHex(entry
->green
, buf2
);
1506 DecToHex(entry
->blue
, buf2
);
1512 node
= node
->Next();
1518 void InitialiseColourTable(void)
1520 // \\red0\\green0\\blue0;
1521 AddColour("black", 0,0,0);
1523 // \\red0\\green0\\blue255;\\red0\\green255\\blue255;\n");
1524 AddColour("cyan", 0,255,255);
1526 // \\red0\\green255\\blue0;
1527 AddColour("green", 0,255,0);
1529 // \\red255\\green0\\blue255;
1530 AddColour("magenta", 255,0,255);
1532 // \\red255\\green0\\blue0;
1533 AddColour("red", 255,0,0);
1535 // \\red255\\green255\\blue0;
1536 AddColour("yellow", 255,255,0);
1538 // \\red255\\green255\\blue255;}");
1539 AddColour("white", 255,255,255);
1543 * The purpose of this is to reduce the number of times wxYield is
1544 * called, since under Windows this can slow things down.
1547 static int yieldCount
= 0;
1549 void Tex2RTFYield(bool force
)
1557 if (yieldCount
== 0)
1566 // In both RTF generation and HTML generation for wxHelp version 2,
1567 // we need to associate \indexed keywords with the current filename/topics.
1569 // Hash table for lists of keywords for topics (WinHelp).
1570 wxHashTable
TopicTable(wxKEY_STRING
);
1571 void AddKeyWordForTopic(char *topic
, char *entry
, char *filename
)
1573 TexTopic
*texTopic
= (TexTopic
*)TopicTable
.Get(topic
);
1576 texTopic
= new TexTopic(filename
);
1577 texTopic
->keywords
= new wxStringList
;
1578 TopicTable
.Put(topic
, texTopic
);
1581 if (!texTopic
->keywords
->Member(entry
))
1582 texTopic
->keywords
->Add(entry
);
1585 void ClearKeyWordTable(void)
1587 TopicTable
.BeginFind();
1588 wxNode
*node
= TopicTable
.Next();
1591 TexTopic
*texTopic
= (TexTopic
*)node
->Data();
1593 node
= TopicTable
.Next();
1600 * TexTopic structure
1603 TexTopic::TexTopic(char *f
)
1606 filename
= copystring(f
);
1609 hasChildren
= FALSE
;
1613 TexTopic::~TexTopic(void)
1621 // Convert case, according to upperCaseNames setting.
1622 char *ConvertCase(char *s
)
1624 static char buf
[256];
1625 int len
= strlen(s
);
1628 for (i
= 0; i
< len
; i
++)
1629 buf
[i
] = wxToUpper(s
[i
]);
1631 for (i
= 0; i
< len
; i
++)
1632 buf
[i
] = wxToLower(s
[i
]);