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"
45 #if !WXWIN_COMPATIBILITY_2_4
46 static inline wxChar
* copystring(const wxChar
* s
)
47 { return wxStrcpy(new wxChar
[wxStrlen(s
) + 1], s
); }
48 static inline void StringToInt (const wxChar
*s
, int *number
)
50 if (s
&& *s
&& number
)
51 *number
= (int) wxStrtol (s
, (wxChar
**) NULL
, 10);
55 wxHashTable
TexReferences(wxKEY_STRING
);
56 wxList
BibList(wxKEY_STRING
);
57 wxStringList CitationList
;
58 wxList
ColourTable(wxKEY_STRING
);
59 wxHashTable
BibStringTable(wxKEY_STRING
);
60 wxList
CustomMacroList(wxKEY_STRING
);
61 TexChunk
*currentSection
= NULL
;
62 char *fakeCurrentSection
= NULL
;
64 static long BibLine
= 1;
66 void OutputCurrentSection(void)
68 if (fakeCurrentSection
)
69 TexOutput(fakeCurrentSection
);
70 else if (currentSection
)
71 TraverseChildrenFromChunk(currentSection
);
74 // Nasty but the way things are done now, necessary,
75 // in order to output a chunk properly to a string (macros and all).
76 void OutputCurrentSectionToString(char *buf
)
78 if (fakeCurrentSection
)
79 strcpy(buf
, fakeCurrentSection
);
81 OutputChunkToString(currentSection
, buf
);
84 void OutputChunkToString(TexChunk
*chunk
, char *buf
)
86 FILE *tempfd
= fopen("tmp.tmp", "w");
90 FILE *old1
= CurrentOutput1
;
91 FILE *old2
= CurrentOutput2
;
93 CurrentOutput1
= tempfd
;
94 CurrentOutput2
= NULL
;
96 TraverseChildrenFromChunk(chunk
);
98 CurrentOutput1
= old1
;
99 CurrentOutput2
= old2
;
103 // Read from file into string
104 tempfd
= fopen("tmp.tmp", "r");
123 wxRemoveFile("tmp.tmp");
126 // Called by Tex2Any to simulate a section
127 void FakeCurrentSection(char *fakeSection
, bool addToContents
)
129 currentSection
= NULL
;
130 if (fakeCurrentSection
) delete[] fakeCurrentSection
;
131 fakeCurrentSection
= copystring(fakeSection
);
133 if (DocumentStyle
== LATEX_ARTICLE
)
135 int mac
= ltSECTIONHEADING
;
137 mac
= ltSECTIONHEADINGSTAR
;
138 OnMacro(mac
, 0, TRUE
);
139 OnMacro(mac
, 0, FALSE
);
143 int mac
= ltCHAPTERHEADING
;
145 mac
= ltCHAPTERHEADINGSTAR
;
146 OnMacro(mac
, 0, TRUE
);
147 OnMacro(mac
, 0, FALSE
);
149 if (fakeCurrentSection
) delete[] fakeCurrentSection
;
150 fakeCurrentSection
= NULL
;
153 // Look for \label macro, use this ref name if found or
154 // make up a topic name otherwise.
155 static long topicCounter
= 0;
157 void ResetTopicCounter(void)
162 static char *forceTopicName
= NULL
;
164 void ForceTopicName(const char *name
)
167 delete[] forceTopicName
;
169 forceTopicName
= copystring(name
);
171 forceTopicName
= NULL
;
174 char *FindTopicName(TexChunk
*chunk
)
177 return forceTopicName
;
179 char *topicName
= NULL
;
180 static char topicBuf
[100];
182 if (chunk
&& (chunk
->type
== CHUNK_TYPE_MACRO
) &&
183 (chunk
->macroId
== ltLABEL
))
185 wxNode
*node
= chunk
->children
.First();
188 TexChunk
*child
= (TexChunk
*)node
->Data();
189 if (child
->type
== CHUNK_TYPE_ARG
)
191 wxNode
*snode
= child
->children
.First();
194 TexChunk
*schunk
= (TexChunk
*)snode
->Data();
195 if (schunk
->type
== CHUNK_TYPE_STRING
)
196 topicName
= schunk
->value
;
205 sprintf(topicBuf
, "topic%ld", topicCounter
);
212 * Simulate argument data, so we can 'drive' clients which implement
213 * certain basic formatting behaviour.
214 * Snag is that some save a TexChunk, so don't use yet...
218 void StartSimulateArgument(char *data
)
220 strcpy(currentArgData
, data
);
224 void EndSimulateArgument(void)
230 * Parse and convert unit arguments to points
234 int ParseUnitArgument(char *unitArg
)
236 float conversionFactor
= 1.0;
237 float unitValue
= 0.0;
238 int len
= strlen(unitArg
);
239 // Get rid of any accidentally embedded commands
240 for (int i
= 0; i
< len
; i
++)
241 if (unitArg
[i
] == '\\')
243 len
= strlen(unitArg
);
245 if (unitArg
&& (len
> 0) && (isdigit(unitArg
[0]) || unitArg
[0] == '-'))
247 sscanf(unitArg
, "%f", &unitValue
);
251 units
[0] = unitArg
[len
-2];
252 units
[1] = unitArg
[len
-1];
254 if (strcmp(units
, "in") == 0)
255 conversionFactor
= 72.0;
256 else if (strcmp(units
, "cm") == 0)
257 conversionFactor
= (float)72.0/(float)2.51;
258 else if (strcmp(units
, "mm") == 0)
259 conversionFactor
= (float)72.0/(float)25.1;
260 else if (strcmp(units
, "pt") == 0)
261 conversionFactor
= 1;
263 return (int)(unitValue
*conversionFactor
);
269 * Strip off any extension (dot something) from end of file,
270 * IF one exists. Inserts zero into buffer.
274 void StripExtension(char *buffer
)
276 int len
= strlen(buffer
);
280 if (buffer
[i
] == '.')
294 void SetFontSizes(int pointSize
)
346 void AddTexRef(char *name
, char *file
, char *sectionName
,
347 int chapter
, int section
, int subsection
, int subsubsection
)
349 TexRef
*texRef
= (TexRef
*)TexReferences
.Get(name
);
350 if (texRef
) TexReferences
.Delete(name
);
357 strcat(buf, sectionName);
364 sprintf(buf2
, "%d", chapter
);
373 sprintf(buf2
, "%d", section
);
380 sprintf(buf2
, "%d", subsection
);
387 sprintf(buf2
, "%d", subsubsection
);
390 char *tmp
= ((strlen(buf
) > 0) ? buf
: (char *)NULL
);
391 TexReferences
.Put(name
, new TexRef(name
, file
, tmp
, sectionName
));
394 void WriteTexReferences(char *filename
)
396 wxSTD ofstream
ostr(filename
);
397 if (ostr
.bad()) return;
400 TexReferences
.BeginFind();
401 wxNode
*node
= TexReferences
.Next();
405 TexRef
*ref
= (TexRef
*)node
->Data();
406 ostr
<< ref
->refLabel
<< " " << (ref
->refFile
? ref
->refFile
: "??") << " ";
407 ostr
<< (ref
->sectionName
? ref
->sectionName
: "??") << " ";
408 ostr
<< (ref
->sectionNumber
? ref
->sectionNumber
: "??") << "\n";
409 if (!ref
->sectionNumber
|| (strcmp(ref
->sectionNumber
, "??") == 0 && strcmp(ref
->sectionName
, "??") == 0))
411 sprintf(buf
, "Warning: reference %s not resolved.", ref
->refLabel
);
414 node
= TexReferences
.Next();
418 void ReadTexReferences(char *filename
)
420 if (!wxFileExists(filename
))
423 wxSTD ifstream
istr(filename
, wxSTD
ios::in
);
425 if (istr
.bad()) return;
430 char sectionName
[100];
440 istr
.get(ch
); // Read past space
443 while (ch
!= '\n' && !istr
.eof())
451 // gt - needed to trick the hash table "TexReferences" into deleting the key
452 // strings it creates in the Put() function, but not the item that is
453 // created here, as that is destroyed elsewhere. Without doing this, there
454 // were massive memory leaks
455 TexReferences
.DeleteContents(TRUE
);
456 TexReferences
.Put(label
, new TexRef(label
, file
, section
, sectionName
));
457 TexReferences
.DeleteContents(FALSE
);
464 * Bibliography-handling code
468 void BibEatWhiteSpace(wxSTD istream
& str
)
470 char ch
= str
.peek();
472 while (!str
.eof() && (ch
== ' ' || ch
== '\t' || ch
== 13 || ch
== 10 || ch
== EOF
))
477 if ((ch
== EOF
) || str
.eof()) return;
481 // Ignore end-of-line comments
482 if (ch
== '%' || ch
== ';' || ch
== '#')
486 while (ch
!= 10 && ch
!= 13 && !str
.eof())
491 BibEatWhiteSpace(str
);
495 // Read word up to { or , or space
496 void BibReadWord(wxSTD istream
& istr
, char *buffer
)
500 char ch
= istr
.peek();
501 while (!istr
.eof() && ch
!= ' ' && ch
!= '{' && ch
!= '(' && ch
!= 13 && ch
!= 10 && ch
!= '\t' &&
502 ch
!= ',' && ch
!= '=')
512 // Read string (double-quoted or not) to end quote or EOL
513 void BibReadToEOL(wxSTD istream
& istr
, char *buffer
)
517 char ch
= istr
.peek();
518 bool inQuotes
= FALSE
;
525 // If in quotes, read white space too. If not,
526 // stop at white space or comment.
527 while (!istr
.eof() && ch
!= 13 && ch
!= 10 && ch
!= '"' &&
528 (inQuotes
|| ((ch
!= ' ') && (ch
!= 9) &&
529 (ch
!= ';') && (ch
!= '%') && (ch
!= '#'))))
541 // Read }-terminated value, taking nested braces into account.
542 void BibReadValue(wxSTD istream
& istr
, char *buffer
, bool ignoreBraces
= TRUE
,
543 bool quotesMayTerminate
= TRUE
)
548 char ch
= istr
.peek();
549 bool stopping
= FALSE
;
550 while (!istr
.eof() && !stopping
)
556 sprintf(buf
, "Sorry, value > 4000 chars in bib file at line %ld.", BibLine
);
557 wxLogError(buf
, "Tex2RTF Fatal Error");
574 else if (quotesMayTerminate
&& ch
== '"')
581 if (!ignoreBraces
|| (ch
!= '{' && ch
!= '}'))
593 bool ReadBib(char *filename
)
595 if (!wxFileExists(filename
))
599 wxSTD ifstream
istr(filename
, wxSTD
ios::in
);
600 if (istr
.bad()) return FALSE
;
604 OnInform("Reading .bib file...");
607 char fieldValue
[4000];
608 char recordType
[100];
610 char recordField
[100];
615 BibEatWhiteSpace(istr
);
619 sprintf(buf
, "Expected @: malformed bib file at line %ld (%s)", BibLine
, filename
);
623 BibReadWord(istr
, recordType
);
624 BibEatWhiteSpace(istr
);
626 if (ch
!= '{' && ch
!= '(')
628 sprintf(buf
, "Expected { or ( after record type: malformed .bib file at line %ld (%s)", BibLine
, filename
);
632 BibEatWhiteSpace(istr
);
633 if (StringMatch(recordType
, "string", FALSE
, TRUE
))
635 BibReadWord(istr
, recordType
);
636 BibEatWhiteSpace(istr
);
640 sprintf(buf
, "Expected = after string key: malformed .bib file at line %ld (%s)", BibLine
, filename
);
644 BibEatWhiteSpace(istr
);
646 if (ch
!= '"' && ch
!= '{')
648 sprintf(buf
, "Expected = after string key: malformed .bib file at line %ld (%s)", BibLine
, filename
);
652 BibReadValue(istr
, fieldValue
);
654 // Now put in hash table if necesary
655 if (!BibStringTable
.Get(recordType
))
656 BibStringTable
.Put(recordType
, (wxObject
*)copystring(fieldValue
));
658 // Read closing ) or }
659 BibEatWhiteSpace(istr
);
661 BibEatWhiteSpace(istr
);
665 BibReadWord(istr
, recordKey
);
667 BibEntry
*bibEntry
= new BibEntry
;
668 bibEntry
->key
= copystring(recordKey
);
669 bibEntry
->type
= copystring(recordType
);
671 bool moreRecords
= TRUE
;
672 while (moreRecords
&& !istr
.eof())
674 BibEatWhiteSpace(istr
);
676 if (ch
== '}' || ch
== ')')
682 BibEatWhiteSpace(istr
);
683 BibReadWord(istr
, recordField
);
684 BibEatWhiteSpace(istr
);
688 sprintf(buf
, "Expected = after field type: malformed .bib file at line %ld (%s)", BibLine
, filename
);
692 BibEatWhiteSpace(istr
);
694 if (ch
!= '{' && ch
!= '"')
697 BibReadWord(istr
, fieldValue
+1);
699 // If in the table of strings, replace with string from table.
700 char *s
= (char *)BibStringTable
.Get(fieldValue
);
703 strcpy(fieldValue
, s
);
707 BibReadValue(istr
, fieldValue
, TRUE
, (ch
== '"' ? TRUE
: FALSE
));
709 // Now we can add a field
710 if (StringMatch(recordField
, "author", FALSE
, TRUE
))
711 bibEntry
->author
= copystring(fieldValue
);
712 else if (StringMatch(recordField
, "key", FALSE
, TRUE
))
714 else if (StringMatch(recordField
, "annotate", FALSE
, TRUE
))
716 else if (StringMatch(recordField
, "abstract", FALSE
, TRUE
))
718 else if (StringMatch(recordField
, "edition", FALSE
, TRUE
))
720 else if (StringMatch(recordField
, "howpublished", FALSE
, TRUE
))
722 else if (StringMatch(recordField
, "note", FALSE
, TRUE
) || StringMatch(recordField
, "notes", FALSE
, TRUE
))
724 else if (StringMatch(recordField
, "series", FALSE
, TRUE
))
726 else if (StringMatch(recordField
, "type", FALSE
, TRUE
))
728 else if (StringMatch(recordField
, "keywords", FALSE
, TRUE
))
730 else if (StringMatch(recordField
, "editor", FALSE
, TRUE
) || StringMatch(recordField
, "editors", FALSE
, TRUE
))
731 bibEntry
->editor
= copystring(fieldValue
);
732 else if (StringMatch(recordField
, "title", FALSE
, TRUE
))
733 bibEntry
->title
= copystring(fieldValue
);
734 else if (StringMatch(recordField
, "booktitle", FALSE
, TRUE
))
735 bibEntry
->booktitle
= copystring(fieldValue
);
736 else if (StringMatch(recordField
, "journal", FALSE
, TRUE
))
737 bibEntry
->journal
= copystring(fieldValue
);
738 else if (StringMatch(recordField
, "volume", FALSE
, TRUE
))
739 bibEntry
->volume
= copystring(fieldValue
);
740 else if (StringMatch(recordField
, "number", FALSE
, TRUE
))
741 bibEntry
->number
= copystring(fieldValue
);
742 else if (StringMatch(recordField
, "year", FALSE
, TRUE
))
743 bibEntry
->year
= copystring(fieldValue
);
744 else if (StringMatch(recordField
, "month", FALSE
, TRUE
))
745 bibEntry
->month
= copystring(fieldValue
);
746 else if (StringMatch(recordField
, "pages", FALSE
, TRUE
))
747 bibEntry
->pages
= copystring(fieldValue
);
748 else if (StringMatch(recordField
, "publisher", FALSE
, TRUE
))
749 bibEntry
->publisher
= copystring(fieldValue
);
750 else if (StringMatch(recordField
, "address", FALSE
, TRUE
))
751 bibEntry
->address
= copystring(fieldValue
);
752 else if (StringMatch(recordField
, "institution", FALSE
, TRUE
) || StringMatch(recordField
, "school", FALSE
, TRUE
))
753 bibEntry
->institution
= copystring(fieldValue
);
754 else if (StringMatch(recordField
, "organization", FALSE
, TRUE
) || StringMatch(recordField
, "organisation", FALSE
, TRUE
))
755 bibEntry
->organization
= copystring(fieldValue
);
756 else if (StringMatch(recordField
, "comment", FALSE
, TRUE
) || StringMatch(recordField
, "comments", FALSE
, TRUE
))
757 bibEntry
->comment
= copystring(fieldValue
);
758 else if (StringMatch(recordField
, "annote", FALSE
, TRUE
))
759 bibEntry
->comment
= copystring(fieldValue
);
760 else if (StringMatch(recordField
, "chapter", FALSE
, TRUE
))
761 bibEntry
->chapter
= copystring(fieldValue
);
764 sprintf(buf
, "Unrecognised bib field type %s at line %ld (%s)", recordField
, BibLine
, filename
);
769 BibList
.Append(recordKey
, bibEntry
);
770 BibEatWhiteSpace(istr
);
776 void OutputBibItem(TexRef
*ref
, BibEntry
*bib
)
780 OnMacro(ltNUMBEREDBIBITEM
, 2, TRUE
);
781 OnArgument(ltNUMBEREDBIBITEM
, 1, TRUE
);
782 TexOutput(ref
->sectionNumber
);
783 OnArgument(ltNUMBEREDBIBITEM
, 1, FALSE
);
784 OnArgument(ltNUMBEREDBIBITEM
, 2, TRUE
);
787 OnMacro(ltBF
, 1, TRUE
);
788 OnArgument(ltBF
, 1, TRUE
);
790 TexOutput(bib
->author
);
791 OnArgument(ltBF
, 1, FALSE
);
792 OnMacro(ltBF
, 1, FALSE
);
793 if (bib
->author
&& (strlen(bib
->author
) > 0) && (bib
->author
[strlen(bib
->author
) - 1] != '.'))
800 TexOutput(bib
->year
);
805 TexOutput(bib
->month
);
808 if (bib
->year
|| bib
->month
)
811 if (StringMatch(bib
->type
, "article", FALSE
, TRUE
))
815 TexOutput(bib
->title
);
820 OnMacro(ltIT
, 1, TRUE
);
821 OnArgument(ltIT
, 1, TRUE
);
822 TexOutput(bib
->journal
);
823 OnArgument(ltIT
, 1, FALSE
);
824 OnMacro(ltIT
, 1, FALSE
);
829 OnMacro(ltBF
, 1, TRUE
);
830 OnArgument(ltBF
, 1, TRUE
);
831 TexOutput(bib
->volume
);
832 OnArgument(ltBF
, 1, FALSE
);
833 OnMacro(ltBF
, 1, FALSE
);
838 TexOutput(bib
->number
);
843 TexOutput(", pages ");
844 TexOutput(bib
->pages
);
848 else if (StringMatch(bib
->type
, "book", FALSE
, TRUE
) ||
849 StringMatch(bib
->type
, "unpublished", FALSE
, TRUE
) ||
850 StringMatch(bib
->type
, "manual", FALSE
, TRUE
) ||
851 StringMatch(bib
->type
, "phdthesis", FALSE
, TRUE
) ||
852 StringMatch(bib
->type
, "mastersthesis", FALSE
, TRUE
) ||
853 StringMatch(bib
->type
, "misc", FALSE
, TRUE
) ||
854 StringMatch(bib
->type
, "techreport", FALSE
, TRUE
) ||
855 StringMatch(bib
->type
, "booklet", FALSE
, TRUE
))
857 if (bib
->title
|| bib
->booktitle
)
859 OnMacro(ltIT
, 1, TRUE
);
860 OnArgument(ltIT
, 1, TRUE
);
861 TexOutput(bib
->title
? bib
->title
: bib
->booktitle
);
863 OnArgument(ltIT
, 1, FALSE
);
864 OnMacro(ltIT
, 1, FALSE
);
866 if (StringMatch(bib
->type
, "phdthesis", FALSE
, TRUE
))
867 TexOutput("PhD thesis. ");
868 if (StringMatch(bib
->type
, "techreport", FALSE
, TRUE
))
869 TexOutput("Technical report. ");
873 TexOutput(bib
->editor
);
876 if (bib
->institution
)
878 TexOutput(bib
->institution
);
881 if (bib
->organization
)
883 TexOutput(bib
->organization
);
888 TexOutput(bib
->publisher
);
893 TexOutput(bib
->address
);
897 else if (StringMatch(bib
->type
, "inbook", FALSE
, TRUE
) ||
898 StringMatch(bib
->type
, "inproceedings", FALSE
, TRUE
) ||
899 StringMatch(bib
->type
, "incollection", FALSE
, TRUE
) ||
900 StringMatch(bib
->type
, "conference", FALSE
, TRUE
))
904 TexOutput(bib
->title
);
908 TexOutput(", from ");
909 OnMacro(ltIT
, 1, TRUE
);
910 OnArgument(ltIT
, 1, TRUE
);
911 TexOutput(bib
->booktitle
);
913 OnArgument(ltIT
, 1, FALSE
);
914 OnMacro(ltIT
, 1, FALSE
);
919 TexOutput(bib
->editor
);
924 TexOutput(bib
->publisher
);
928 if (bib
->publisher
) TexOutput(", ");
930 TexOutput(bib
->address
);
932 if (bib
->publisher
|| bib
->address
)
938 OnMacro(ltBF
, 1, TRUE
);
939 OnArgument(ltBF
, 1, TRUE
);
940 TexOutput(bib
->volume
);
941 OnArgument(ltBF
, 1, FALSE
);
942 OnMacro(ltBF
, 1, FALSE
);
949 TexOutput(bib
->number
);
954 TexOutput(" Number ");
955 TexOutput(bib
->number
);
961 TexOutput(" Chap. "); TexOutput(bib
->chapter
);
965 if (bib
->chapter
) TexOutput(", pages ");
966 else TexOutput(" Pages ");
967 TexOutput(bib
->pages
);
971 OnArgument(ltNUMBEREDBIBITEM
, 2, FALSE
);
972 OnMacro(ltNUMBEREDBIBITEM
, 2, FALSE
);
978 ForceTopicName("bibliography");
979 FakeCurrentSection(ReferencesNameString
);
980 ForceTopicName(NULL
);
982 OnMacro(ltPAR
, 0, TRUE
);
983 OnMacro(ltPAR
, 0, FALSE
);
985 if ((convertMode
== TEX_RTF
) && !winHelp
)
987 OnMacro(ltPAR
, 0, TRUE
);
988 OnMacro(ltPAR
, 0, FALSE
);
991 wxNode
*node
= CitationList
.First();
994 char *citeKey
= (char *)node
->Data();
995 // wxNode *texNode = TexReferences.Find(citeKey);
996 TexRef
*ref
= (TexRef
*)TexReferences
.Get(citeKey
);
997 wxNode
*bibNode
= BibList
.Find(citeKey
);
1000 BibEntry
*entry
= (BibEntry
*)bibNode
->Data();
1001 OutputBibItem(ref
, entry
);
1003 node
= node
->Next();
1007 static int citeCount
= 1;
1009 void ResolveBibReferences(void)
1011 if (CitationList
.Number() > 0)
1012 OnInform("Resolving bibliographic references...");
1016 wxNode
*node
= CitationList
.First();
1020 char *citeKey
= (char *)node
->Data();
1021 // wxNode *texNode = TexReferences.Find(citeKey);
1022 TexRef
*ref
= (TexRef
*)TexReferences
.Get(citeKey
);
1023 wxNode
*bibNode
= BibList
.Find(citeKey
);
1027 //BibEntry *entry = (BibEntry *)bibNode->Data();
1028 if (ref
->sectionNumber
) delete[] ref
->sectionNumber
;
1029 sprintf(buf
, "[%d]", citeCount
);
1030 ref
->sectionNumber
= copystring(buf
);
1035 sprintf(buf
, "Warning: bib ref %s not resolved.", citeKey
);
1038 node
= node
->Next();
1042 // Remember we need to resolve this citation
1043 void AddCitation(char *citeKey
)
1045 if (!CitationList
.Member(citeKey
))
1046 CitationList
.Add(citeKey
);
1048 if (!TexReferences
.Get(citeKey
))
1050 TexReferences
.Put(citeKey
, new TexRef(citeKey
, "??", NULL
));
1054 TexRef
*FindReference(char *key
)
1056 return (TexRef
*)TexReferences
.Get(key
);
1060 * Custom macro stuff
1064 bool StringTobool(char *val
)
1066 if (strncmp(val
, "yes", 3) == 0 || strncmp(val
, "YES", 3) == 0 ||
1067 strncmp(val
, "on", 2) == 0 || strncmp(val
, "ON", 2) == 0 ||
1068 strncmp(val
, "true", 4) == 0 || strncmp(val
, "TRUE", 4) == 0 ||
1069 strncmp(val
, "ok", 2) == 0 || strncmp(val
, "OK", 2) == 0 ||
1070 strncmp(val
, "1", 1) == 0)
1076 // Define a variable value from the .ini file
1077 char *RegisterSetting(char *settingName
, char *settingValue
, bool interactive
)
1079 static char errorCode
[100];
1080 strcpy(errorCode
, "OK");
1081 if (StringMatch(settingName
, "chapterName", FALSE
, TRUE
))
1083 delete[] ChapterNameString
;
1084 ChapterNameString
= copystring(settingValue
);
1086 else if (StringMatch(settingName
, "sectionName", FALSE
, TRUE
))
1088 delete[] SectionNameString
;
1089 SectionNameString
= copystring(settingValue
);
1091 else if (StringMatch(settingName
, "subsectionName", FALSE
, TRUE
))
1093 delete[] SubsectionNameString
;
1094 SubsectionNameString
= copystring(settingValue
);
1096 else if (StringMatch(settingName
, "subsubsectionName", FALSE
, TRUE
))
1098 delete[] SubsubsectionNameString
;
1099 SubsubsectionNameString
= copystring(settingValue
);
1101 else if (StringMatch(settingName
, "indexName", FALSE
, TRUE
))
1103 delete[] IndexNameString
;
1104 IndexNameString
= copystring(settingValue
);
1106 else if (StringMatch(settingName
, "contentsName", FALSE
, TRUE
))
1108 delete[] ContentsNameString
;
1109 ContentsNameString
= copystring(settingValue
);
1111 else if (StringMatch(settingName
, "glossaryName", FALSE
, TRUE
))
1113 delete[] GlossaryNameString
;
1114 GlossaryNameString
= copystring(settingValue
);
1116 else if (StringMatch(settingName
, "referencesName", FALSE
, TRUE
))
1118 delete[] ReferencesNameString
;
1119 ReferencesNameString
= copystring(settingValue
);
1121 else if (StringMatch(settingName
, "tablesName", FALSE
, TRUE
))
1123 delete[] TablesNameString
;
1124 TablesNameString
= copystring(settingValue
);
1126 else if (StringMatch(settingName
, "figuresName", FALSE
, TRUE
))
1128 delete[] FiguresNameString
;
1129 FiguresNameString
= copystring(settingValue
);
1131 else if (StringMatch(settingName
, "tableName", FALSE
, TRUE
))
1133 delete[] TableNameString
;
1134 TableNameString
= copystring(settingValue
);
1136 else if (StringMatch(settingName
, "figureName", FALSE
, TRUE
))
1138 delete[] FigureNameString
;
1139 FigureNameString
= copystring(settingValue
);
1141 else if (StringMatch(settingName
, "abstractName", FALSE
, TRUE
))
1143 delete[] AbstractNameString
;
1144 AbstractNameString
= copystring(settingValue
);
1146 else if (StringMatch(settingName
, "chapterFontSize", FALSE
, TRUE
))
1147 StringToInt(settingValue
, &chapterFont
);
1148 else if (StringMatch(settingName
, "sectionFontSize", FALSE
, TRUE
))
1149 StringToInt(settingValue
, §ionFont
);
1150 else if (StringMatch(settingName
, "subsectionFontSize", FALSE
, TRUE
))
1151 StringToInt(settingValue
, &subsectionFont
);
1152 else if (StringMatch(settingName
, "titleFontSize", FALSE
, TRUE
))
1153 StringToInt(settingValue
, &titleFont
);
1154 else if (StringMatch(settingName
, "authorFontSize", FALSE
, TRUE
))
1155 StringToInt(settingValue
, &authorFont
);
1156 else if (StringMatch(settingName
, "ignoreInput", FALSE
, TRUE
))
1157 IgnorableInputFiles
.Add(wxFileNameFromPath(settingValue
));
1158 else if (StringMatch(settingName
, "mirrorMargins", FALSE
, TRUE
))
1159 mirrorMargins
= StringTobool(settingValue
);
1160 else if (StringMatch(settingName
, "runTwice", FALSE
, TRUE
))
1161 runTwice
= StringTobool(settingValue
);
1162 else if (StringMatch(settingName
, "isInteractive", FALSE
, TRUE
))
1163 isInteractive
= StringTobool(settingValue
);
1164 else if (StringMatch(settingName
, "headerRule", FALSE
, TRUE
))
1165 headerRule
= StringTobool(settingValue
);
1166 else if (StringMatch(settingName
, "footerRule", FALSE
, TRUE
))
1167 footerRule
= StringTobool(settingValue
);
1168 else if (StringMatch(settingName
, "combineSubSections", FALSE
, TRUE
))
1169 combineSubSections
= StringTobool(settingValue
);
1170 else if (StringMatch(settingName
, "listLabelIndent", FALSE
, TRUE
))
1171 StringToInt(settingValue
, &labelIndentTab
);
1172 else if (StringMatch(settingName
, "listItemIndent", FALSE
, TRUE
))
1173 StringToInt(settingValue
, &itemIndentTab
);
1174 else if (StringMatch(settingName
, "useUpButton", FALSE
, TRUE
))
1175 useUpButton
= StringTobool(settingValue
);
1176 else if (StringMatch(settingName
, "useHeadingStyles", FALSE
, TRUE
))
1177 useHeadingStyles
= StringTobool(settingValue
);
1178 else if (StringMatch(settingName
, "useWord", FALSE
, TRUE
))
1179 useWord
= StringTobool(settingValue
);
1180 else if (StringMatch(settingName
, "contentsDepth", FALSE
, TRUE
))
1181 StringToInt(settingValue
, &contentsDepth
);
1182 else if (StringMatch(settingName
, "generateHPJ", FALSE
, TRUE
))
1183 generateHPJ
= StringTobool(settingValue
);
1184 else if (StringMatch(settingName
, "truncateFilenames", FALSE
, TRUE
))
1185 truncateFilenames
= StringTobool(settingValue
);
1186 else if (StringMatch(settingName
, "winHelpVersion", FALSE
, TRUE
))
1187 StringToInt(settingValue
, &winHelpVersion
);
1188 else if (StringMatch(settingName
, "winHelpContents", FALSE
, TRUE
))
1189 winHelpContents
= StringTobool(settingValue
);
1190 else if (StringMatch(settingName
, "htmlIndex", FALSE
, TRUE
))
1191 htmlIndex
= StringTobool(settingValue
);
1192 else if (StringMatch(settingName
, "htmlWorkshopFiles", FALSE
, TRUE
))
1193 htmlWorkshopFiles
= StringTobool(settingValue
);
1194 else if (StringMatch(settingName
, "htmlFrameContents", FALSE
, TRUE
))
1195 htmlFrameContents
= StringTobool(settingValue
);
1196 else if (StringMatch(settingName
, "htmlStylesheet", FALSE
, TRUE
))
1198 if (htmlStylesheet
) delete[] htmlStylesheet
;
1199 htmlStylesheet
= copystring(settingValue
);
1201 else if (StringMatch(settingName
, "upperCaseNames", FALSE
, TRUE
))
1202 upperCaseNames
= StringTobool(settingValue
);
1203 else if (StringMatch(settingName
, "ignoreBadRefs", FALSE
, TRUE
))
1204 ignoreBadRefs
= StringTobool(settingValue
);
1205 else if (StringMatch(settingName
, "htmlFaceName", FALSE
, TRUE
))
1207 delete[] htmlFaceName
;
1208 htmlFaceName
= copystring(settingValue
);
1210 else if (StringMatch(settingName
, "winHelpTitle", FALSE
, TRUE
))
1213 delete[] winHelpTitle
;
1214 winHelpTitle
= copystring(settingValue
);
1216 else if (StringMatch(settingName
, "indexSubsections", FALSE
, TRUE
))
1217 indexSubsections
= StringTobool(settingValue
);
1218 else if (StringMatch(settingName
, "compatibility", FALSE
, TRUE
))
1219 compatibilityMode
= StringTobool(settingValue
);
1220 else if (StringMatch(settingName
, "defaultColumnWidth", FALSE
, TRUE
))
1222 StringToInt(settingValue
, &defaultTableColumnWidth
);
1223 defaultTableColumnWidth
= 20*defaultTableColumnWidth
;
1225 else if (StringMatch(settingName
, "bitmapMethod", FALSE
, TRUE
))
1227 if ((strcmp(settingValue
, "includepicture") != 0) && (strcmp(settingValue
, "hex") != 0) &&
1228 (strcmp(settingValue
, "import") != 0))
1231 OnError("Unknown bitmapMethod");
1232 strcpy(errorCode
, "Unknown bitmapMethod");
1236 delete[] bitmapMethod
;
1237 bitmapMethod
= copystring(settingValue
);
1240 else if (StringMatch(settingName
, "htmlBrowseButtons", FALSE
, TRUE
))
1242 if (strcmp(settingValue
, "none") == 0)
1243 htmlBrowseButtons
= HTML_BUTTONS_NONE
;
1244 else if (strcmp(settingValue
, "bitmap") == 0)
1245 htmlBrowseButtons
= HTML_BUTTONS_BITMAP
;
1246 else if (strcmp(settingValue
, "text") == 0)
1247 htmlBrowseButtons
= HTML_BUTTONS_TEXT
;
1251 OnInform("Initialisation file error: htmlBrowseButtons must be one of none, bitmap, or text.");
1252 strcpy(errorCode
, "Initialisation file error: htmlBrowseButtons must be one of none, bitmap, or text.");
1255 else if (StringMatch(settingName
, "backgroundImage", FALSE
, TRUE
))
1257 backgroundImageString
= copystring(settingValue
);
1259 else if (StringMatch(settingName
, "backgroundColour", FALSE
, TRUE
))
1261 delete[] backgroundColourString
;
1262 backgroundColourString
= copystring(settingValue
);
1264 else if (StringMatch(settingName
, "textColour", FALSE
, TRUE
))
1266 textColourString
= copystring(settingValue
);
1268 else if (StringMatch(settingName
, "linkColour", FALSE
, TRUE
))
1270 linkColourString
= copystring(settingValue
);
1272 else if (StringMatch(settingName
, "followedLinkColour", FALSE
, TRUE
))
1274 followedLinkColourString
= copystring(settingValue
);
1276 else if (StringMatch(settingName
, "conversionMode", FALSE
, TRUE
))
1278 if (StringMatch(settingValue
, "RTF", FALSE
, TRUE
))
1280 winHelp
= FALSE
; convertMode
= TEX_RTF
;
1282 else if (StringMatch(settingValue
, "WinHelp", FALSE
, TRUE
))
1284 winHelp
= TRUE
; convertMode
= TEX_RTF
;
1286 else if (StringMatch(settingValue
, "XLP", FALSE
, TRUE
) ||
1287 StringMatch(settingValue
, "wxHelp", FALSE
, TRUE
))
1289 convertMode
= TEX_XLP
;
1291 else if (StringMatch(settingValue
, "HTML", FALSE
, TRUE
))
1293 convertMode
= TEX_HTML
;
1298 OnInform("Initialisation file error: conversionMode must be one of\nRTF, WinHelp, XLP (or wxHelp), HTML.");
1299 strcpy(errorCode
, "Initialisation file error: conversionMode must be one of\nRTF, WinHelp, XLP (or wxHelp), HTML.");
1302 else if (StringMatch(settingName
, "documentFontSize", FALSE
, TRUE
))
1305 StringToInt(settingValue
, &n
);
1306 if (n
== 10 || n
== 11 || n
== 12)
1311 sprintf(buf
, "Initialisation file error: nonstandard document font size %d.", n
);
1314 strcpy(errorCode
, buf
);
1320 sprintf(buf
, "Initialisation file error: unrecognised setting %s.", settingName
);
1323 strcpy(errorCode
, buf
);
1328 bool ReadCustomMacros(char *filename
)
1330 if (!wxFileExists(filename
))
1333 wxSTD ifstream
istr(filename
, wxSTD
ios::in
);
1335 if (istr
.bad()) return FALSE
;
1337 CustomMacroList
.Clear();
1339 char macroName
[100];
1340 char macroBody
[1000];
1345 BibEatWhiteSpace(istr
);
1350 if (ch
!= '\\') // Not a macro definition, so must be NAME=VALUE
1352 char settingName
[100];
1353 settingName
[0] = ch
;
1354 BibReadWord(istr
, (settingName
+1));
1355 BibEatWhiteSpace(istr
);
1359 OnError("Expected = following name: malformed tex2rtf.ini file.");
1364 char settingValue
[200];
1365 BibEatWhiteSpace(istr
);
1366 BibReadToEOL(istr
, settingValue
);
1367 RegisterSetting(settingName
, settingValue
);
1372 BibReadWord(istr
, macroName
);
1373 BibEatWhiteSpace(istr
);
1377 OnError("Expected [ followed by number of arguments: malformed tex2rtf.ini file.");
1384 OnError("Expected ] following number of arguments: malformed tex2rtf.ini file.");
1387 BibEatWhiteSpace(istr
);
1391 OnError("Expected { followed by macro body: malformed tex2rtf.ini file.");
1394 CustomMacro
*macro
= new CustomMacro(macroName
, noArgs
, NULL
);
1395 BibReadValue(istr
, macroBody
, FALSE
, FALSE
); // Don't ignore extra braces
1396 if (strlen(macroBody
) > 0)
1397 macro
->macroBody
= copystring(macroBody
);
1399 BibEatWhiteSpace(istr
);
1400 CustomMacroList
.Append(macroName
, macro
);
1401 AddMacroDef(ltCUSTOM_MACRO
, macroName
, noArgs
);
1405 sprintf(mbuf
, "Read initialization file %s.", filename
);
1410 CustomMacro
*FindCustomMacro(char *name
)
1412 wxNode
*node
= CustomMacroList
.Find(name
);
1415 CustomMacro
*macro
= (CustomMacro
*)node
->Data();
1421 // Display custom macros
1422 void ShowCustomMacros(void)
1424 wxNode
*node
= CustomMacroList
.First();
1427 OnInform("No custom macros loaded.\n");
1434 CustomMacro
*macro
= (CustomMacro
*)node
->Data();
1435 sprintf(buf
, "\\%s[%d]\n {%s}", macro
->macroName
, macro
->noArgs
,
1436 macro
->macroBody
? macro
->macroBody
: "");
1438 node
= node
->Next();
1442 // Parse a string into several comma-separated fields
1443 char *ParseMultifieldString(char *allFields
, int *pos
)
1445 static char buffer
[300];
1447 int fieldIndex
= *pos
;
1448 int len
= strlen(allFields
);
1450 bool keepGoing
= TRUE
;
1451 while ((fieldIndex
<= len
) && keepGoing
)
1453 if (allFields
[fieldIndex
] == ' ')
1458 else if (allFields
[fieldIndex
] == ',')
1460 *pos
= fieldIndex
+ 1;
1463 else if (allFields
[fieldIndex
] == 0)
1465 *pos
= fieldIndex
+ 1;
1470 buffer
[i
] = allFields
[fieldIndex
];
1476 if (oldPos
== (*pos
))
1490 ColourTableEntry::ColourTableEntry(const char *theName
, unsigned int r
, unsigned int g
, unsigned int b
)
1492 name
= copystring(theName
);
1498 ColourTableEntry::~ColourTableEntry(void)
1503 void AddColour(const char *theName
, unsigned int r
, unsigned int g
, unsigned int b
)
1505 wxNode
*node
= ColourTable
.Find(theName
);
1508 ColourTableEntry
*entry
= (ColourTableEntry
*)node
->Data();
1509 if (entry
->red
== r
|| entry
->green
== g
|| entry
->blue
== b
)
1517 ColourTableEntry
*entry
= new ColourTableEntry(theName
, r
, g
, b
);
1518 ColourTable
.Append(theName
, entry
);
1521 int FindColourPosition(char *theName
)
1524 wxNode
*node
= ColourTable
.First();
1527 ColourTableEntry
*entry
= (ColourTableEntry
*)node
->Data();
1528 if (strcmp(theName
, entry
->name
) == 0)
1531 node
= node
->Next();
1536 // Converts e.g. "red" -> "#FF0000"
1537 extern void DecToHex(int, char *);
1538 bool FindColourHTMLString(char *theName
, char *buf
)
1541 wxNode
*node
= ColourTable
.First();
1544 ColourTableEntry
*entry
= (ColourTableEntry
*)node
->Data();
1545 if (strcmp(theName
, entry
->name
) == 0)
1550 DecToHex(entry
->red
, buf2
);
1552 DecToHex(entry
->green
, buf2
);
1554 DecToHex(entry
->blue
, buf2
);
1560 node
= node
->Next();
1566 void InitialiseColourTable(void)
1568 // \\red0\\green0\\blue0;
1569 AddColour("black", 0,0,0);
1571 // \\red0\\green0\\blue255;\\red0\\green255\\blue255;\n");
1572 AddColour("cyan", 0,255,255);
1574 // \\red0\\green255\\blue0;
1575 AddColour("green", 0,255,0);
1577 // \\red255\\green0\\blue255;
1578 AddColour("magenta", 255,0,255);
1580 // \\red255\\green0\\blue0;
1581 AddColour("red", 255,0,0);
1583 // \\red255\\green255\\blue0;
1584 AddColour("yellow", 255,255,0);
1586 // \\red255\\green255\\blue255;}");
1587 AddColour("white", 255,255,255);
1591 * The purpose of this is to reduce the number of times wxYield is
1592 * called, since under Windows this can slow things down.
1595 void Tex2RTFYield(bool force
)
1598 static int yieldCount
= 0;
1605 if (yieldCount
== 0)
1615 // In both RTF generation and HTML generation for wxHelp version 2,
1616 // we need to associate \indexed keywords with the current filename/topics.
1618 // Hash table for lists of keywords for topics (WinHelp).
1619 wxHashTable
TopicTable(wxKEY_STRING
);
1620 void AddKeyWordForTopic(char *topic
, char *entry
, char *filename
)
1622 TexTopic
*texTopic
= (TexTopic
*)TopicTable
.Get(topic
);
1625 texTopic
= new TexTopic(filename
);
1626 texTopic
->keywords
= new wxStringList
;
1627 TopicTable
.Put(topic
, texTopic
);
1630 if (!texTopic
->keywords
->Member(entry
))
1631 texTopic
->keywords
->Add(entry
);
1634 void ClearKeyWordTable(void)
1636 TopicTable
.BeginFind();
1637 wxNode
*node
= TopicTable
.Next();
1640 TexTopic
*texTopic
= (TexTopic
*)node
->Data();
1642 node
= TopicTable
.Next();
1649 * TexTopic structure
1652 TexTopic::TexTopic(char *f
)
1655 filename
= copystring(f
);
1658 hasChildren
= FALSE
;
1662 TexTopic::~TexTopic(void)
1670 // Convert case, according to upperCaseNames setting.
1671 char *ConvertCase(char *s
)
1673 static char buf
[256];
1674 int len
= strlen(s
);
1677 for (i
= 0; i
< len
; i
++)
1678 buf
[i
] = toupper(s
[i
]);
1680 for (i
= 0; i
< len
; i
++)
1681 buf
[i
] = tolower(s
[i
]);
1686 #if !WXWIN_COMPATIBILITY_2
1687 // if substring is TRUE, search for str1 in str2
1688 bool StringMatch(const wxChar
*str1
, const wxChar
*str2
, bool subString
,
1693 wxString
Sstr1(str1
);
1694 wxString
Sstr2(str2
);
1700 return Sstr2
.Index(Sstr1
) != wxNOT_FOUND
;
1703 return exact
? wxString(str2
).Cmp(str1
) == 0 :
1704 wxString(str2
).CmpNoCase(str1
) == 0;