]>
Commit | Line | Data |
---|---|---|
9a29912f JS |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: texutils.cpp | |
3 | // Purpose: Miscellaneous utilities | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 7.9.93 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma implementation | |
14 | #endif | |
15 | ||
16 | // For compilers that support precompilation, includes "wx.h". | |
17 | #include "wx/wxprec.h" | |
18 | ||
19 | #ifdef __BORLANDC__ | |
20 | #pragma hdrstop | |
21 | #endif | |
22 | ||
23 | #ifndef WX_PRECOMP | |
ea172e69 | 24 | #include "wx/log.h" |
9a29912f JS |
25 | #endif |
26 | ||
ddc4f3b5 | 27 | #include "wx/app.h" |
5aa5c1e4 | 28 | #include "wx/hash.h" |
9a29912f | 29 | |
1a464ed9 JS |
30 | #ifdef new |
31 | #undef new | |
32 | #endif | |
33 | ||
9a29912f JS |
34 | #if wxUSE_IOSTREAMH |
35 | #include <iostream.h> | |
36 | #include <fstream.h> | |
37 | #else | |
38 | #include <iostream> | |
39 | #include <fstream> | |
2b5f62a0 | 40 | using namespace std; |
9a29912f JS |
41 | #endif |
42 | ||
43 | #include <ctype.h> | |
44 | #include "tex2any.h" | |
45 | ||
ea172e69 MB |
46 | #if !WXWIN_COMPATIBILITY_2_4 |
47 | static inline wxChar* copystring(const wxChar* s) | |
48 | { return wxStrcpy(new wxChar[wxStrlen(s) + 1], s); } | |
49 | static inline void StringToInt (const wxChar *s, int *number) | |
50 | { | |
51 | if (s && *s && number) | |
52 | *number = (int) wxStrtol (s, (wxChar **) NULL, 10); | |
53 | } | |
54 | #endif | |
55 | ||
9a29912f JS |
56 | wxHashTable TexReferences(wxKEY_STRING); |
57 | wxList BibList(wxKEY_STRING); | |
58 | wxStringList CitationList; | |
59 | wxList ColourTable(wxKEY_STRING); | |
60 | wxHashTable BibStringTable(wxKEY_STRING); | |
61 | wxList CustomMacroList(wxKEY_STRING); | |
62 | TexChunk *currentSection = NULL; | |
6c155d33 | 63 | wxChar *fakeCurrentSection = NULL; |
9a29912f JS |
64 | |
65 | static long BibLine = 1; | |
66 | ||
67 | void OutputCurrentSection(void) | |
68 | { | |
69 | if (fakeCurrentSection) | |
70 | TexOutput(fakeCurrentSection); | |
71 | else if (currentSection) | |
72 | TraverseChildrenFromChunk(currentSection); | |
73 | } | |
74 | ||
75 | // Nasty but the way things are done now, necessary, | |
76 | // in order to output a chunk properly to a string (macros and all). | |
6c155d33 | 77 | void OutputCurrentSectionToString(wxChar *buf) |
9a29912f JS |
78 | { |
79 | if (fakeCurrentSection) | |
6c155d33 | 80 | wxStrcpy(buf, fakeCurrentSection); |
9a29912f JS |
81 | else |
82 | OutputChunkToString(currentSection, buf); | |
83 | } | |
84 | ||
6c155d33 | 85 | void OutputChunkToString(TexChunk *chunk, wxChar *buf) |
9a29912f | 86 | { |
6c155d33 | 87 | FILE *tempfd = wxFopen(_T("tmp.tmp"), _T("w")); |
9a29912f JS |
88 | if (!tempfd) |
89 | return; | |
90 | ||
91 | FILE *old1 = CurrentOutput1; | |
92 | FILE *old2 = CurrentOutput2; | |
93 | ||
94 | CurrentOutput1 = tempfd; | |
95 | CurrentOutput2 = NULL; | |
96 | ||
97 | TraverseChildrenFromChunk(chunk); | |
98 | ||
99 | CurrentOutput1 = old1; | |
100 | CurrentOutput2 = old2; | |
101 | ||
102 | fclose(tempfd); | |
103 | ||
104 | // Read from file into string | |
6c155d33 | 105 | tempfd = wxFopen(_T("tmp.tmp"), _T("r")); |
9a29912f JS |
106 | if (!tempfd) |
107 | return; | |
108 | ||
109 | buf[0] = 0; | |
110 | int ch = -2; | |
111 | int i = 0; | |
112 | while (ch != EOF) | |
113 | { | |
114 | ch = getc(tempfd); | |
115 | if (ch == EOF) | |
116 | buf[i] = 0; | |
117 | else | |
118 | { | |
119 | buf[i] = ch; | |
120 | i ++; | |
121 | } | |
122 | } | |
123 | fclose(tempfd); | |
6c155d33 | 124 | wxRemoveFile(_T("tmp.tmp")); |
9a29912f JS |
125 | } |
126 | ||
127 | // Called by Tex2Any to simulate a section | |
6c155d33 | 128 | void FakeCurrentSection(wxChar *fakeSection, bool addToContents) |
9a29912f JS |
129 | { |
130 | currentSection = NULL; | |
131 | if (fakeCurrentSection) delete[] fakeCurrentSection; | |
132 | fakeCurrentSection = copystring(fakeSection); | |
133 | ||
134 | if (DocumentStyle == LATEX_ARTICLE) | |
135 | { | |
136 | int mac = ltSECTIONHEADING; | |
137 | if (!addToContents) | |
138 | mac = ltSECTIONHEADINGSTAR; | |
139 | OnMacro(mac, 0, TRUE); | |
140 | OnMacro(mac, 0, FALSE); | |
141 | } | |
142 | else | |
143 | { | |
144 | int mac = ltCHAPTERHEADING; | |
145 | if (!addToContents) | |
146 | mac = ltCHAPTERHEADINGSTAR; | |
147 | OnMacro(mac, 0, TRUE); | |
148 | OnMacro(mac, 0, FALSE); | |
149 | } | |
150 | if (fakeCurrentSection) delete[] fakeCurrentSection; | |
151 | fakeCurrentSection = NULL; | |
152 | } | |
153 | ||
154 | // Look for \label macro, use this ref name if found or | |
155 | // make up a topic name otherwise. | |
156 | static long topicCounter = 0; | |
157 | ||
158 | void ResetTopicCounter(void) | |
159 | { | |
160 | topicCounter = 0; | |
161 | } | |
162 | ||
6c155d33 | 163 | static wxChar *forceTopicName = NULL; |
9a29912f | 164 | |
6c155d33 | 165 | void ForceTopicName(const wxChar *name) |
9a29912f JS |
166 | { |
167 | if (forceTopicName) | |
168 | delete[] forceTopicName; | |
169 | if (name) | |
170 | forceTopicName = copystring(name); | |
171 | else | |
172 | forceTopicName = NULL; | |
173 | } | |
174 | ||
6c155d33 | 175 | wxChar *FindTopicName(TexChunk *chunk) |
9a29912f JS |
176 | { |
177 | if (forceTopicName) | |
178 | return forceTopicName; | |
179 | ||
6c155d33 JS |
180 | wxChar *topicName = NULL; |
181 | static wxChar topicBuf[100]; | |
9a29912f JS |
182 | |
183 | if (chunk && (chunk->type == CHUNK_TYPE_MACRO) && | |
184 | (chunk->macroId == ltLABEL)) | |
185 | { | |
ddc4f3b5 | 186 | wxNode *node = chunk->children.GetFirst(); |
9a29912f JS |
187 | if (node) |
188 | { | |
ddc4f3b5 | 189 | TexChunk *child = (TexChunk *)node->GetData(); |
9a29912f JS |
190 | if (child->type == CHUNK_TYPE_ARG) |
191 | { | |
ddc4f3b5 | 192 | wxNode *snode = child->children.GetFirst(); |
9a29912f JS |
193 | if (snode) |
194 | { | |
ddc4f3b5 | 195 | TexChunk *schunk = (TexChunk *)snode->GetData(); |
9a29912f JS |
196 | if (schunk->type == CHUNK_TYPE_STRING) |
197 | topicName = schunk->value; | |
198 | } | |
199 | } | |
200 | } | |
201 | } | |
202 | if (topicName) | |
203 | return topicName; | |
204 | else | |
205 | { | |
6c155d33 | 206 | wxSprintf(topicBuf, _T("topic%ld"), topicCounter); |
9a29912f JS |
207 | topicCounter ++; |
208 | return topicBuf; | |
209 | } | |
210 | } | |
211 | ||
212 | /* | |
213 | * Simulate argument data, so we can 'drive' clients which implement | |
214 | * certain basic formatting behaviour. | |
215 | * Snag is that some save a TexChunk, so don't use yet... | |
216 | * | |
217 | */ | |
218 | ||
6c155d33 | 219 | void StartSimulateArgument(wxChar *data) |
9a29912f | 220 | { |
6c155d33 | 221 | wxStrcpy(currentArgData, data); |
9a29912f JS |
222 | haveArgData = TRUE; |
223 | } | |
224 | ||
225 | void EndSimulateArgument(void) | |
226 | { | |
227 | haveArgData = FALSE; | |
228 | } | |
229 | ||
230 | /* | |
231 | * Parse and convert unit arguments to points | |
232 | * | |
233 | */ | |
234 | ||
6c155d33 | 235 | int ParseUnitArgument(wxChar *unitArg) |
9a29912f JS |
236 | { |
237 | float conversionFactor = 1.0; | |
238 | float unitValue = 0.0; | |
6c155d33 | 239 | int len = wxStrlen(unitArg); |
9a29912f JS |
240 | // Get rid of any accidentally embedded commands |
241 | for (int i = 0; i < len; i++) | |
242 | if (unitArg[i] == '\\') | |
243 | unitArg[i] = 0; | |
6c155d33 | 244 | len = wxStrlen(unitArg); |
9a29912f JS |
245 | |
246 | if (unitArg && (len > 0) && (isdigit(unitArg[0]) || unitArg[0] == '-')) | |
247 | { | |
6c155d33 | 248 | wxSscanf(unitArg, _T("%f"), &unitValue); |
9a29912f JS |
249 | if (len > 1) |
250 | { | |
6c155d33 | 251 | wxChar units[3]; |
9a29912f JS |
252 | units[0] = unitArg[len-2]; |
253 | units[1] = unitArg[len-1]; | |
254 | units[2] = 0; | |
6c155d33 | 255 | if (wxStrcmp(units, _T("in")) == 0) |
9a29912f | 256 | conversionFactor = 72.0; |
6c155d33 | 257 | else if (wxStrcmp(units, _T("cm")) == 0) |
f90e5ed2 | 258 | conversionFactor = (float)72.0/(float)2.51; |
6c155d33 | 259 | else if (wxStrcmp(units, _T("mm")) == 0) |
f90e5ed2 | 260 | conversionFactor = (float)72.0/(float)25.1; |
6c155d33 | 261 | else if (wxStrcmp(units, _T("pt")) == 0) |
9a29912f JS |
262 | conversionFactor = 1; |
263 | } | |
264 | return (int)(unitValue*conversionFactor); | |
265 | } | |
266 | else return 0; | |
267 | } | |
268 | ||
269 | /* | |
270 | * Strip off any extension (dot something) from end of file, | |
271 | * IF one exists. Inserts zero into buffer. | |
272 | * | |
273 | */ | |
274 | ||
6c155d33 | 275 | void StripExtension(wxChar *buffer) |
9a29912f | 276 | { |
6c155d33 | 277 | int len = wxStrlen(buffer); |
9a29912f JS |
278 | int i = len-1; |
279 | while (i > 0) | |
280 | { | |
281 | if (buffer[i] == '.') | |
282 | { | |
283 | buffer[i] = 0; | |
284 | break; | |
285 | } | |
286 | i --; | |
287 | } | |
288 | } | |
289 | ||
290 | /* | |
291 | * Latex font setting | |
292 | * | |
293 | */ | |
294 | ||
295 | void SetFontSizes(int pointSize) | |
296 | { | |
297 | switch (pointSize) | |
298 | { | |
299 | case 12: | |
300 | { | |
301 | normalFont = 12; | |
302 | smallFont = 10; | |
303 | tinyFont = 8; | |
304 | largeFont1 = 14; | |
305 | LargeFont2 = 16; | |
306 | LARGEFont3 = 20; | |
307 | hugeFont1 = 24; | |
308 | HugeFont2 = 28; | |
309 | HUGEFont3 = 32; | |
310 | break; | |
311 | } | |
312 | case 11: | |
313 | { | |
314 | normalFont = 11; | |
315 | smallFont = 9; | |
316 | tinyFont = 7; | |
317 | largeFont1 = 13; | |
318 | LargeFont2 = 16; | |
319 | LARGEFont3 = 19; | |
320 | hugeFont1 = 22; | |
321 | HugeFont2 = 26; | |
322 | HUGEFont3 = 30; | |
323 | break; | |
324 | } | |
325 | case 10: | |
326 | { | |
327 | normalFont = 10; | |
328 | smallFont = 8; | |
329 | tinyFont = 6; | |
330 | largeFont1 = 12; | |
331 | LargeFont2 = 14; | |
332 | LARGEFont3 = 18; | |
333 | hugeFont1 = 20; | |
334 | HugeFont2 = 24; | |
335 | HUGEFont3 = 28; | |
336 | break; | |
337 | } | |
338 | } | |
339 | } | |
340 | ||
341 | ||
342 | /* | |
343 | * Latex references | |
344 | * | |
345 | */ | |
346 | ||
6c155d33 | 347 | void AddTexRef(wxChar *name, wxChar *file, wxChar *sectionName, |
9a29912f JS |
348 | int chapter, int section, int subsection, int subsubsection) |
349 | { | |
350 | TexRef *texRef = (TexRef *)TexReferences.Get(name); | |
351 | if (texRef) TexReferences.Delete(name); | |
352 | ||
6c155d33 | 353 | wxChar buf[100]; |
9a29912f JS |
354 | buf[0] = 0; |
355 | /* | |
356 | if (sectionName) | |
357 | { | |
6c155d33 JS |
358 | wxStrcat(buf, sectionName); |
359 | wxStrcat(buf, " "); | |
9a29912f JS |
360 | } |
361 | */ | |
362 | if (chapter) | |
363 | { | |
6c155d33 JS |
364 | wxChar buf2[10]; |
365 | wxSprintf(buf2, _T("%d"), chapter); | |
366 | wxStrcat(buf, buf2); | |
9a29912f JS |
367 | } |
368 | if (section) | |
369 | { | |
6c155d33 | 370 | wxChar buf2[10]; |
9a29912f | 371 | if (chapter) |
6c155d33 | 372 | wxStrcat(buf, _T(".")); |
9a29912f | 373 | |
6c155d33 JS |
374 | wxSprintf(buf2, _T("%d"), section); |
375 | wxStrcat(buf, buf2); | |
9a29912f JS |
376 | } |
377 | if (subsection) | |
378 | { | |
6c155d33 JS |
379 | wxChar buf2[10]; |
380 | wxStrcat(buf, _T(".")); | |
381 | wxSprintf(buf2, _T("%d"), subsection); | |
382 | wxStrcat(buf, buf2); | |
9a29912f JS |
383 | } |
384 | if (subsubsection) | |
385 | { | |
6c155d33 JS |
386 | wxChar buf2[10]; |
387 | wxStrcat(buf, _T(".")); | |
388 | wxSprintf(buf2, _T("%d"), subsubsection); | |
389 | wxStrcat(buf, buf2); | |
9a29912f | 390 | } |
6c155d33 | 391 | wxChar *tmp = ((wxStrlen(buf) > 0) ? buf : (wxChar *)NULL); |
9a29912f JS |
392 | TexReferences.Put(name, new TexRef(name, file, tmp, sectionName)); |
393 | } | |
394 | ||
6c155d33 | 395 | void WriteTexReferences(wxChar *filename) |
9a29912f | 396 | { |
dd107c50 | 397 | wxSTD ofstream ostr(filename); |
9a29912f | 398 | if (ostr.bad()) return; |
6c155d33 | 399 | wxChar buf[200]; |
9a29912f JS |
400 | |
401 | TexReferences.BeginFind(); | |
402 | wxNode *node = TexReferences.Next(); | |
403 | while (node) | |
404 | { | |
405 | Tex2RTFYield(); | |
ddc4f3b5 | 406 | TexRef *ref = (TexRef *)node->GetData(); |
6c155d33 JS |
407 | ostr << ref->refLabel << _T(" ") << (ref->refFile ? ref->refFile : _T("??")) << _T(" "); |
408 | ostr << (ref->sectionName ? ref->sectionName : _T("??")) << _T(" "); | |
409 | ostr << (ref->sectionNumber ? ref->sectionNumber : _T("??")) << _T("\n"); | |
410 | if (!ref->sectionNumber || (wxStrcmp(ref->sectionNumber, _T("??")) == 0 && wxStrcmp(ref->sectionName, _T("??")) == 0)) | |
9a29912f | 411 | { |
6c155d33 | 412 | wxSprintf(buf, _T("Warning: reference %s not resolved."), ref->refLabel); |
9a29912f JS |
413 | OnInform(buf); |
414 | } | |
415 | node = TexReferences.Next(); | |
416 | } | |
417 | } | |
418 | ||
6c155d33 | 419 | void ReadTexReferences(wxChar *filename) |
9a29912f | 420 | { |
dbda9e86 JS |
421 | if (!wxFileExists(filename)) |
422 | return; | |
423 | ||
e969b63b | 424 | wxSTD ifstream istr(filename, wxSTD ios::in); |
dbda9e86 | 425 | |
9a29912f JS |
426 | if (istr.bad()) return; |
427 | ||
6c155d33 JS |
428 | wxChar label[100]; |
429 | wxChar file[400]; | |
430 | wxChar section[100]; | |
431 | wxChar sectionName[100]; | |
9a29912f JS |
432 | |
433 | while (!istr.eof()) | |
434 | { | |
435 | istr >> label; | |
436 | if (!istr.eof()) | |
437 | { | |
438 | istr >> file; | |
439 | istr >> sectionName; | |
440 | char ch; | |
441 | istr.get(ch); // Read past space | |
442 | istr.get(ch); | |
443 | int i = 0; | |
444 | while (ch != '\n' && !istr.eof()) | |
445 | { | |
446 | section[i] = ch; | |
447 | i ++; | |
448 | istr.get(ch); | |
449 | } | |
450 | section[i] = 0; | |
3924dd22 GT |
451 | |
452 | // gt - needed to trick the hash table "TexReferences" into deleting the key | |
453 | // strings it creates in the Put() function, but not the item that is | |
454 | // created here, as that is destroyed elsewhere. Without doing this, there | |
455 | // were massive memory leaks | |
456 | TexReferences.DeleteContents(TRUE); | |
9a29912f | 457 | TexReferences.Put(label, new TexRef(label, file, section, sectionName)); |
3924dd22 | 458 | TexReferences.DeleteContents(FALSE); |
9a29912f JS |
459 | } |
460 | } | |
461 | } | |
462 | ||
463 | ||
464 | /* | |
465 | * Bibliography-handling code | |
466 | * | |
467 | */ | |
468 | ||
dd107c50 | 469 | void BibEatWhiteSpace(wxSTD istream& str) |
9a29912f JS |
470 | { |
471 | char ch = str.peek(); | |
472 | ||
473 | while (!str.eof() && (ch == ' ' || ch == '\t' || ch == 13 || ch == 10 || ch == EOF)) | |
474 | { | |
475 | if (ch == 10) | |
476 | BibLine ++; | |
477 | str.get(ch); | |
478 | if ((ch == EOF) || str.eof()) return; | |
479 | ch = str.peek(); | |
480 | } | |
481 | ||
482 | // Ignore end-of-line comments | |
483 | if (ch == '%' || ch == ';' || ch == '#') | |
484 | { | |
485 | str.get(ch); | |
486 | ch = str.peek(); | |
487 | while (ch != 10 && ch != 13 && !str.eof()) | |
488 | { | |
489 | str.get(ch); | |
490 | ch = str.peek(); | |
491 | } | |
492 | BibEatWhiteSpace(str); | |
493 | } | |
494 | } | |
495 | ||
496 | // Read word up to { or , or space | |
6c155d33 | 497 | void BibReadWord(wxSTD istream& istr, wxChar *buffer) |
9a29912f JS |
498 | { |
499 | int i = 0; | |
500 | buffer[i] = 0; | |
501 | char ch = istr.peek(); | |
502 | while (!istr.eof() && ch != ' ' && ch != '{' && ch != '(' && ch != 13 && ch != 10 && ch != '\t' && | |
503 | ch != ',' && ch != '=') | |
504 | { | |
505 | istr.get(ch); | |
506 | buffer[i] = ch; | |
507 | i ++; | |
508 | ch = istr.peek(); | |
509 | } | |
510 | buffer[i] = 0; | |
511 | } | |
512 | ||
513 | // Read string (double-quoted or not) to end quote or EOL | |
6c155d33 | 514 | void BibReadToEOL(wxSTD istream& istr, wxChar *buffer) |
9a29912f JS |
515 | { |
516 | int i = 0; | |
517 | buffer[i] = 0; | |
6c155d33 | 518 | wxChar ch = istr.peek(); |
9a29912f | 519 | bool inQuotes = FALSE; |
6c155d33 | 520 | if (ch == _T('"')) |
9a29912f JS |
521 | { |
522 | istr.get(ch); | |
523 | ch = istr.peek(); | |
524 | inQuotes = TRUE; | |
525 | } | |
526 | // If in quotes, read white space too. If not, | |
527 | // stop at white space or comment. | |
6c155d33 JS |
528 | while (!istr.eof() && ch != 13 && ch != 10 && ch != _T('"') && |
529 | (inQuotes || ((ch != _T(' ')) && (ch != 9) && | |
530 | (ch != _T(';')) && (ch != _T('%')) && (ch != _T('#'))))) | |
9a29912f JS |
531 | { |
532 | istr.get(ch); | |
533 | buffer[i] = ch; | |
534 | i ++; | |
535 | ch = istr.peek(); | |
536 | } | |
537 | if (ch == '"') | |
538 | istr.get(ch); | |
539 | buffer[i] = 0; | |
540 | } | |
541 | ||
542 | // Read }-terminated value, taking nested braces into account. | |
6c155d33 | 543 | void BibReadValue(wxSTD istream& istr, wxChar *buffer, bool ignoreBraces = TRUE, |
9a29912f JS |
544 | bool quotesMayTerminate = TRUE) |
545 | { | |
546 | int braceCount = 1; | |
547 | int i = 0; | |
548 | buffer[i] = 0; | |
549 | char ch = istr.peek(); | |
550 | bool stopping = FALSE; | |
551 | while (!istr.eof() && !stopping) | |
552 | { | |
553 | // i ++; | |
08c6402a | 554 | if (i >= 4000) |
9a29912f | 555 | { |
6c155d33 JS |
556 | wxChar buf[100]; |
557 | wxSprintf(buf, _T("Sorry, value > 4000 chars in bib file at line %ld."), BibLine); | |
11611a62 JS |
558 | wxLogError(buf, "Tex2RTF Fatal Error"); |
559 | return; | |
9a29912f JS |
560 | } |
561 | istr.get(ch); | |
562 | ||
563 | if (ch == '{') | |
564 | braceCount ++; | |
565 | ||
566 | if (ch == '}') | |
567 | { | |
568 | braceCount --; | |
569 | if (braceCount == 0) | |
570 | { | |
571 | stopping = TRUE; | |
572 | break; | |
573 | } | |
574 | } | |
575 | else if (quotesMayTerminate && ch == '"') | |
576 | { | |
577 | stopping = TRUE; | |
578 | break; | |
579 | } | |
580 | if (!stopping) | |
581 | { | |
582 | if (!ignoreBraces || (ch != '{' && ch != '}')) | |
583 | { | |
584 | buffer[i] = ch; | |
585 | i ++; | |
586 | } | |
587 | } | |
588 | if (ch == 10) | |
589 | BibLine ++; | |
590 | } | |
591 | buffer[i] = 0; | |
6c155d33 | 592 | wxUnusedVar(stopping); |
9a29912f JS |
593 | } |
594 | ||
6c155d33 | 595 | bool ReadBib(wxChar *filename) |
9a29912f | 596 | { |
dbda9e86 JS |
597 | if (!wxFileExists(filename)) |
598 | return FALSE; | |
599 | ||
6c155d33 | 600 | wxChar buf[300]; |
e969b63b | 601 | wxSTD ifstream istr(filename, wxSTD ios::in); |
9a29912f JS |
602 | if (istr.bad()) return FALSE; |
603 | ||
604 | BibLine = 1; | |
605 | ||
6c155d33 | 606 | OnInform(_T("Reading .bib file...")); |
9a29912f JS |
607 | |
608 | char ch; | |
6c155d33 JS |
609 | wxChar fieldValue[4000]; |
610 | wxChar recordType[100]; | |
611 | wxChar recordKey[100]; | |
612 | wxChar recordField[100]; | |
9a29912f JS |
613 | while (!istr.eof()) |
614 | { | |
615 | Tex2RTFYield(); | |
616 | ||
617 | BibEatWhiteSpace(istr); | |
618 | istr.get(ch); | |
619 | if (ch != '@') | |
620 | { | |
6c155d33 | 621 | wxSprintf(buf, _T("Expected @: malformed bib file at line %ld (%s)"), BibLine, filename); |
9a29912f JS |
622 | OnError(buf); |
623 | return FALSE; | |
624 | } | |
625 | BibReadWord(istr, recordType); | |
626 | BibEatWhiteSpace(istr); | |
627 | istr.get(ch); | |
628 | if (ch != '{' && ch != '(') | |
629 | { | |
6c155d33 | 630 | wxSprintf(buf, _T("Expected { or ( after record type: malformed .bib file at line %ld (%s)"), BibLine, filename); |
9a29912f JS |
631 | OnError(buf); |
632 | return FALSE; | |
633 | } | |
634 | BibEatWhiteSpace(istr); | |
6c155d33 | 635 | if (StringMatch(recordType, _T("string"), FALSE, TRUE)) |
9a29912f JS |
636 | { |
637 | BibReadWord(istr, recordType); | |
638 | BibEatWhiteSpace(istr); | |
639 | istr.get(ch); | |
640 | if (ch != '=') | |
641 | { | |
6c155d33 | 642 | wxSprintf(buf, _T("Expected = after string key: malformed .bib file at line %ld (%s)"), BibLine, filename); |
9a29912f JS |
643 | OnError(buf); |
644 | return FALSE; | |
645 | } | |
646 | BibEatWhiteSpace(istr); | |
647 | istr.get(ch); | |
648 | if (ch != '"' && ch != '{') | |
649 | { | |
6c155d33 | 650 | wxSprintf(buf, _T("Expected = after string key: malformed .bib file at line %ld (%s)"), BibLine, filename); |
9a29912f JS |
651 | OnError(buf); |
652 | return FALSE; | |
653 | } | |
654 | BibReadValue(istr, fieldValue); | |
655 | ||
656 | // Now put in hash table if necesary | |
657 | if (!BibStringTable.Get(recordType)) | |
658 | BibStringTable.Put(recordType, (wxObject *)copystring(fieldValue)); | |
659 | ||
660 | // Read closing ) or } | |
661 | BibEatWhiteSpace(istr); | |
662 | istr.get(ch); | |
663 | BibEatWhiteSpace(istr); | |
664 | } | |
665 | else | |
666 | { | |
667 | BibReadWord(istr, recordKey); | |
668 | ||
669 | BibEntry *bibEntry = new BibEntry; | |
670 | bibEntry->key = copystring(recordKey); | |
671 | bibEntry->type = copystring(recordType); | |
672 | ||
673 | bool moreRecords = TRUE; | |
674 | while (moreRecords && !istr.eof()) | |
675 | { | |
676 | BibEatWhiteSpace(istr); | |
677 | istr.get(ch); | |
678 | if (ch == '}' || ch == ')') | |
679 | { | |
680 | moreRecords = FALSE; | |
681 | } | |
682 | else if (ch == ',') | |
683 | { | |
684 | BibEatWhiteSpace(istr); | |
685 | BibReadWord(istr, recordField); | |
686 | BibEatWhiteSpace(istr); | |
687 | istr.get(ch); | |
688 | if (ch != '=') | |
689 | { | |
6c155d33 | 690 | wxSprintf(buf, _T("Expected = after field type: malformed .bib file at line %ld (%s)"), BibLine, filename); |
9a29912f JS |
691 | OnError(buf); |
692 | return FALSE; | |
693 | } | |
694 | BibEatWhiteSpace(istr); | |
695 | istr.get(ch); | |
696 | if (ch != '{' && ch != '"') | |
697 | { | |
698 | fieldValue[0] = ch; | |
699 | BibReadWord(istr, fieldValue+1); | |
700 | ||
701 | // If in the table of strings, replace with string from table. | |
6c155d33 | 702 | wxChar *s = (wxChar *)BibStringTable.Get(fieldValue); |
9a29912f JS |
703 | if (s) |
704 | { | |
6c155d33 | 705 | wxStrcpy(fieldValue, s); |
9a29912f JS |
706 | } |
707 | } | |
708 | else | |
6c155d33 | 709 | BibReadValue(istr, fieldValue, TRUE, (ch == _T('"') ? TRUE : FALSE)); |
9a29912f JS |
710 | |
711 | // Now we can add a field | |
6c155d33 | 712 | if (StringMatch(recordField, _T("author"), FALSE, TRUE)) |
9a29912f | 713 | bibEntry->author = copystring(fieldValue); |
6c155d33 | 714 | else if (StringMatch(recordField, _T("key"), FALSE, TRUE)) |
9a29912f | 715 | {} |
6c155d33 | 716 | else if (StringMatch(recordField, _T("annotate"), FALSE, TRUE)) |
9a29912f | 717 | {} |
6c155d33 | 718 | else if (StringMatch(recordField, _T("abstract"), FALSE, TRUE)) |
9a29912f | 719 | {} |
6c155d33 | 720 | else if (StringMatch(recordField, _T("edition"), FALSE, TRUE)) |
9a29912f | 721 | {} |
6c155d33 | 722 | else if (StringMatch(recordField, _T("howpublished"), FALSE, TRUE)) |
9a29912f | 723 | {} |
6c155d33 | 724 | else if (StringMatch(recordField, _T("note"), FALSE, TRUE) || StringMatch(recordField, _T("notes"), FALSE, TRUE)) |
9a29912f | 725 | {} |
6c155d33 | 726 | else if (StringMatch(recordField, _T("series"), FALSE, TRUE)) |
9a29912f | 727 | {} |
6c155d33 | 728 | else if (StringMatch(recordField, _T("type"), FALSE, TRUE)) |
9a29912f | 729 | {} |
6c155d33 | 730 | else if (StringMatch(recordField, _T("keywords"), FALSE, TRUE)) |
9a29912f | 731 | {} |
6c155d33 | 732 | else if (StringMatch(recordField, _T("editor"), FALSE, TRUE) || StringMatch(recordField, _T("editors"), FALSE, TRUE)) |
9a29912f | 733 | bibEntry->editor= copystring(fieldValue); |
6c155d33 | 734 | else if (StringMatch(recordField, _T("title"), FALSE, TRUE)) |
9a29912f | 735 | bibEntry->title= copystring(fieldValue); |
6c155d33 | 736 | else if (StringMatch(recordField, _T("booktitle"), FALSE, TRUE)) |
9a29912f | 737 | bibEntry->booktitle= copystring(fieldValue); |
6c155d33 | 738 | else if (StringMatch(recordField, _T("journal"), FALSE, TRUE)) |
9a29912f | 739 | bibEntry->journal= copystring(fieldValue); |
6c155d33 | 740 | else if (StringMatch(recordField, _T("volume"), FALSE, TRUE)) |
9a29912f | 741 | bibEntry->volume= copystring(fieldValue); |
6c155d33 | 742 | else if (StringMatch(recordField, _T("number"), FALSE, TRUE)) |
9a29912f | 743 | bibEntry->number= copystring(fieldValue); |
6c155d33 | 744 | else if (StringMatch(recordField, _T("year"), FALSE, TRUE)) |
9a29912f | 745 | bibEntry->year= copystring(fieldValue); |
6c155d33 | 746 | else if (StringMatch(recordField, _T("month"), FALSE, TRUE)) |
9a29912f | 747 | bibEntry->month= copystring(fieldValue); |
6c155d33 | 748 | else if (StringMatch(recordField, _T("pages"), FALSE, TRUE)) |
9a29912f | 749 | bibEntry->pages= copystring(fieldValue); |
6c155d33 | 750 | else if (StringMatch(recordField, _T("publisher"), FALSE, TRUE)) |
9a29912f | 751 | bibEntry->publisher= copystring(fieldValue); |
6c155d33 | 752 | else if (StringMatch(recordField, _T("address"), FALSE, TRUE)) |
9a29912f | 753 | bibEntry->address= copystring(fieldValue); |
6c155d33 | 754 | else if (StringMatch(recordField, _T("institution"), FALSE, TRUE) || StringMatch(recordField, _T("school"), FALSE, TRUE)) |
9a29912f | 755 | bibEntry->institution= copystring(fieldValue); |
6c155d33 | 756 | else if (StringMatch(recordField, _T("organization"), FALSE, TRUE) || StringMatch(recordField, _T("organisation"), FALSE, TRUE)) |
9a29912f | 757 | bibEntry->organization= copystring(fieldValue); |
6c155d33 | 758 | else if (StringMatch(recordField, _T("comment"), FALSE, TRUE) || StringMatch(recordField, _T("comments"), FALSE, TRUE)) |
9a29912f | 759 | bibEntry->comment= copystring(fieldValue); |
6c155d33 | 760 | else if (StringMatch(recordField, _T("annote"), FALSE, TRUE)) |
9a29912f | 761 | bibEntry->comment= copystring(fieldValue); |
6c155d33 | 762 | else if (StringMatch(recordField, _T("chapter"), FALSE, TRUE)) |
9a29912f JS |
763 | bibEntry->chapter= copystring(fieldValue); |
764 | else | |
765 | { | |
6c155d33 | 766 | wxSprintf(buf, _T("Unrecognised bib field type %s at line %ld (%s)"), recordField, BibLine, filename); |
9a29912f JS |
767 | OnError(buf); |
768 | } | |
769 | } | |
770 | } | |
771 | BibList.Append(recordKey, bibEntry); | |
772 | BibEatWhiteSpace(istr); | |
773 | } | |
774 | } | |
775 | return TRUE; | |
776 | } | |
777 | ||
778 | void OutputBibItem(TexRef *ref, BibEntry *bib) | |
779 | { | |
780 | Tex2RTFYield(); | |
781 | ||
782 | OnMacro(ltNUMBEREDBIBITEM, 2, TRUE); | |
783 | OnArgument(ltNUMBEREDBIBITEM, 1, TRUE); | |
784 | TexOutput(ref->sectionNumber); | |
785 | OnArgument(ltNUMBEREDBIBITEM, 1, FALSE); | |
786 | OnArgument(ltNUMBEREDBIBITEM, 2, TRUE); | |
787 | ||
6c155d33 | 788 | TexOutput(_T(" ")); |
9a29912f JS |
789 | OnMacro(ltBF, 1, TRUE); |
790 | OnArgument(ltBF, 1, TRUE); | |
791 | if (bib->author) | |
792 | TexOutput(bib->author); | |
793 | OnArgument(ltBF, 1, FALSE); | |
794 | OnMacro(ltBF, 1, FALSE); | |
6c155d33 JS |
795 | if (bib->author && (wxStrlen(bib->author) > 0) && (bib->author[wxStrlen(bib->author) - 1] != '.')) |
796 | TexOutput(_T(". ")); | |
9a29912f | 797 | else |
6c155d33 | 798 | TexOutput(_T(" ")); |
9a29912f JS |
799 | |
800 | if (bib->year) | |
801 | { | |
802 | TexOutput(bib->year); | |
803 | } | |
804 | if (bib->month) | |
805 | { | |
6c155d33 | 806 | TexOutput(_T(" (")); |
9a29912f | 807 | TexOutput(bib->month); |
6c155d33 | 808 | TexOutput(_T(")")); |
9a29912f JS |
809 | } |
810 | if (bib->year || bib->month) | |
6c155d33 | 811 | TexOutput(_T(". ")); |
9a29912f | 812 | |
6c155d33 | 813 | if (StringMatch(bib->type, _T("article"), FALSE, TRUE)) |
9a29912f JS |
814 | { |
815 | if (bib->title) | |
816 | { | |
817 | TexOutput(bib->title); | |
6c155d33 | 818 | TexOutput(_T(". ")); |
9a29912f JS |
819 | } |
820 | if (bib->journal) | |
821 | { | |
822 | OnMacro(ltIT, 1, TRUE); | |
823 | OnArgument(ltIT, 1, TRUE); | |
824 | TexOutput(bib->journal); | |
825 | OnArgument(ltIT, 1, FALSE); | |
826 | OnMacro(ltIT, 1, FALSE); | |
827 | } | |
828 | if (bib->volume) | |
829 | { | |
6c155d33 | 830 | TexOutput(_T(", ")); |
9a29912f JS |
831 | OnMacro(ltBF, 1, TRUE); |
832 | OnArgument(ltBF, 1, TRUE); | |
833 | TexOutput(bib->volume); | |
834 | OnArgument(ltBF, 1, FALSE); | |
835 | OnMacro(ltBF, 1, FALSE); | |
836 | } | |
837 | if (bib->number) | |
838 | { | |
6c155d33 | 839 | TexOutput(_T("(")); |
9a29912f | 840 | TexOutput(bib->number); |
6c155d33 | 841 | TexOutput(_T(")")); |
9a29912f JS |
842 | } |
843 | if (bib->pages) | |
844 | { | |
6c155d33 | 845 | TexOutput(_T(", pages ")); |
9a29912f JS |
846 | TexOutput(bib->pages); |
847 | } | |
6c155d33 | 848 | TexOutput(_T(".")); |
9a29912f | 849 | } |
6c155d33 JS |
850 | else if (StringMatch(bib->type, _T("book"), FALSE, TRUE) || |
851 | StringMatch(bib->type, _T("unpublished"), FALSE, TRUE) || | |
852 | StringMatch(bib->type, _T("manual"), FALSE, TRUE) || | |
853 | StringMatch(bib->type, _T("phdthesis"), FALSE, TRUE) || | |
854 | StringMatch(bib->type, _T("mastersthesis"), FALSE, TRUE) || | |
855 | StringMatch(bib->type, _T("misc"), FALSE, TRUE) || | |
856 | StringMatch(bib->type, _T("techreport"), FALSE, TRUE) || | |
857 | StringMatch(bib->type, _T("booklet"), FALSE, TRUE)) | |
9a29912f JS |
858 | { |
859 | if (bib->title || bib->booktitle) | |
860 | { | |
861 | OnMacro(ltIT, 1, TRUE); | |
862 | OnArgument(ltIT, 1, TRUE); | |
863 | TexOutput(bib->title ? bib->title : bib->booktitle); | |
6c155d33 | 864 | TexOutput(_T(". ")); |
9a29912f JS |
865 | OnArgument(ltIT, 1, FALSE); |
866 | OnMacro(ltIT, 1, FALSE); | |
867 | } | |
6c155d33 JS |
868 | if (StringMatch(bib->type, _T("phdthesis"), FALSE, TRUE)) |
869 | TexOutput(_T("PhD thesis. ")); | |
870 | if (StringMatch(bib->type, _T("techreport"), FALSE, TRUE)) | |
871 | TexOutput(_T("Technical report. ")); | |
9a29912f JS |
872 | if (bib->editor) |
873 | { | |
6c155d33 | 874 | TexOutput(_T("Ed. ")); |
9a29912f | 875 | TexOutput(bib->editor); |
6c155d33 | 876 | TexOutput(_T(". ")); |
9a29912f JS |
877 | } |
878 | if (bib->institution) | |
879 | { | |
880 | TexOutput(bib->institution); | |
6c155d33 | 881 | TexOutput(_T(". ")); |
9a29912f JS |
882 | } |
883 | if (bib->organization) | |
884 | { | |
885 | TexOutput(bib->organization); | |
6c155d33 | 886 | TexOutput(_T(". ")); |
9a29912f JS |
887 | } |
888 | if (bib->publisher) | |
889 | { | |
890 | TexOutput(bib->publisher); | |
6c155d33 | 891 | TexOutput(_T(". ")); |
9a29912f JS |
892 | } |
893 | if (bib->address) | |
894 | { | |
895 | TexOutput(bib->address); | |
6c155d33 | 896 | TexOutput(_T(". ")); |
9a29912f JS |
897 | } |
898 | } | |
6c155d33 JS |
899 | else if (StringMatch(bib->type, _T("inbook"), FALSE, TRUE) || |
900 | StringMatch(bib->type, _T("inproceedings"), FALSE, TRUE) || | |
901 | StringMatch(bib->type, _T("incollection"), FALSE, TRUE) || | |
902 | StringMatch(bib->type, _T("conference"), FALSE, TRUE)) | |
9a29912f JS |
903 | { |
904 | if (bib->title) | |
905 | { | |
906 | TexOutput(bib->title); | |
907 | } | |
908 | if (bib->booktitle) | |
909 | { | |
6c155d33 | 910 | TexOutput(_T(", from ")); |
9a29912f JS |
911 | OnMacro(ltIT, 1, TRUE); |
912 | OnArgument(ltIT, 1, TRUE); | |
913 | TexOutput(bib->booktitle); | |
6c155d33 | 914 | TexOutput(_T(".")); |
9a29912f JS |
915 | OnArgument(ltIT, 1, FALSE); |
916 | OnMacro(ltIT, 1, FALSE); | |
917 | } | |
918 | if (bib->editor) | |
919 | { | |
6c155d33 | 920 | TexOutput(_T(", ed. ")); |
9a29912f JS |
921 | TexOutput(bib->editor); |
922 | } | |
923 | if (bib->publisher) | |
924 | { | |
6c155d33 | 925 | TexOutput(_T(" ")); |
9a29912f JS |
926 | TexOutput(bib->publisher); |
927 | } | |
928 | if (bib->address) | |
929 | { | |
6c155d33 JS |
930 | if (bib->publisher) TexOutput(_T(", ")); |
931 | else TexOutput(_T(" ")); | |
9a29912f JS |
932 | TexOutput(bib->address); |
933 | } | |
934 | if (bib->publisher || bib->address) | |
6c155d33 | 935 | TexOutput(_T(".")); |
9a29912f JS |
936 | |
937 | if (bib->volume) | |
938 | { | |
6c155d33 | 939 | TexOutput(_T(" ")); |
9a29912f JS |
940 | OnMacro(ltBF, 1, TRUE); |
941 | OnArgument(ltBF, 1, TRUE); | |
942 | TexOutput(bib->volume); | |
943 | OnArgument(ltBF, 1, FALSE); | |
944 | OnMacro(ltBF, 1, FALSE); | |
945 | } | |
946 | if (bib->number) | |
947 | { | |
948 | if (bib->volume) | |
949 | { | |
6c155d33 | 950 | TexOutput(_T("(")); |
9a29912f | 951 | TexOutput(bib->number); |
6c155d33 | 952 | TexOutput(_T(").")); |
9a29912f JS |
953 | } |
954 | else | |
955 | { | |
6c155d33 | 956 | TexOutput(_T(" Number ")); |
9a29912f | 957 | TexOutput(bib->number); |
6c155d33 | 958 | TexOutput(_T(".")); |
9a29912f JS |
959 | } |
960 | } | |
961 | if (bib->chapter) | |
962 | { | |
6c155d33 JS |
963 | TexOutput(_T(" Chap. ")); |
964 | TexOutput(bib->chapter); | |
9a29912f JS |
965 | } |
966 | if (bib->pages) | |
967 | { | |
6c155d33 JS |
968 | if (bib->chapter) TexOutput(_T(", pages ")); |
969 | else TexOutput(_T(" Pages ")); | |
9a29912f | 970 | TexOutput(bib->pages); |
6c155d33 | 971 | TexOutput(_T(".")); |
9a29912f JS |
972 | } |
973 | } | |
974 | OnArgument(ltNUMBEREDBIBITEM, 2, FALSE); | |
975 | OnMacro(ltNUMBEREDBIBITEM, 2, FALSE); | |
976 | } | |
977 | ||
978 | void OutputBib(void) | |
979 | { | |
980 | // Write the heading | |
6c155d33 | 981 | ForceTopicName(_T("bibliography")); |
9a29912f JS |
982 | FakeCurrentSection(ReferencesNameString); |
983 | ForceTopicName(NULL); | |
984 | ||
985 | OnMacro(ltPAR, 0, TRUE); | |
986 | OnMacro(ltPAR, 0, FALSE); | |
987 | ||
988 | if ((convertMode == TEX_RTF) && !winHelp) | |
989 | { | |
990 | OnMacro(ltPAR, 0, TRUE); | |
991 | OnMacro(ltPAR, 0, FALSE); | |
992 | } | |
993 | ||
ddc4f3b5 | 994 | wxStringListNode *node = CitationList.GetFirst(); |
9a29912f JS |
995 | while (node) |
996 | { | |
6c155d33 | 997 | wxChar *citeKey = (wxChar *)node->GetData(); |
9a29912f JS |
998 | // wxNode *texNode = TexReferences.Find(citeKey); |
999 | TexRef *ref = (TexRef *)TexReferences.Get(citeKey); | |
1000 | wxNode *bibNode = BibList.Find(citeKey); | |
1001 | if (bibNode && ref) | |
1002 | { | |
ddc4f3b5 | 1003 | BibEntry *entry = (BibEntry *)bibNode->GetData(); |
9a29912f JS |
1004 | OutputBibItem(ref, entry); |
1005 | } | |
ddc4f3b5 | 1006 | node = node->GetNext(); |
9a29912f JS |
1007 | } |
1008 | } | |
1009 | ||
1010 | static int citeCount = 1; | |
1011 | ||
1012 | void ResolveBibReferences(void) | |
1013 | { | |
ddc4f3b5 | 1014 | if (CitationList.GetCount() > 0) |
6c155d33 | 1015 | OnInform(_T("Resolving bibliographic references...")); |
9a29912f JS |
1016 | |
1017 | citeCount = 1; | |
6c155d33 | 1018 | wxChar buf[200]; |
ddc4f3b5 | 1019 | wxStringListNode *node = CitationList.GetFirst(); |
9a29912f JS |
1020 | while (node) |
1021 | { | |
1022 | Tex2RTFYield(); | |
6c155d33 | 1023 | wxChar *citeKey = (wxChar *)node->GetData(); |
9a29912f JS |
1024 | // wxNode *texNode = TexReferences.Find(citeKey); |
1025 | TexRef *ref = (TexRef *)TexReferences.Get(citeKey); | |
1026 | wxNode *bibNode = BibList.Find(citeKey); | |
1027 | if (bibNode && ref) | |
1028 | { | |
1029 | // Unused Variable | |
ddc4f3b5 | 1030 | //BibEntry *entry = (BibEntry *)bibNode->GetData(); |
9a29912f | 1031 | if (ref->sectionNumber) delete[] ref->sectionNumber; |
6c155d33 | 1032 | wxSprintf(buf, _T("[%d]"), citeCount); |
9a29912f JS |
1033 | ref->sectionNumber = copystring(buf); |
1034 | citeCount ++; | |
1035 | } | |
1036 | else | |
1037 | { | |
6c155d33 | 1038 | wxSprintf(buf, _T("Warning: bib ref %s not resolved."), citeKey); |
9a29912f JS |
1039 | OnInform(buf); |
1040 | } | |
ddc4f3b5 | 1041 | node = node->GetNext(); |
9a29912f JS |
1042 | } |
1043 | } | |
1044 | ||
1045 | // Remember we need to resolve this citation | |
6c155d33 | 1046 | void AddCitation(wxChar *citeKey) |
9a29912f JS |
1047 | { |
1048 | if (!CitationList.Member(citeKey)) | |
1049 | CitationList.Add(citeKey); | |
1050 | ||
1051 | if (!TexReferences.Get(citeKey)) | |
1052 | { | |
6c155d33 | 1053 | TexReferences.Put(citeKey, new TexRef(citeKey, _T("??"), NULL)); |
9a29912f JS |
1054 | } |
1055 | } | |
1056 | ||
6c155d33 | 1057 | TexRef *FindReference(wxChar *key) |
9a29912f JS |
1058 | { |
1059 | return (TexRef *)TexReferences.Get(key); | |
1060 | } | |
1061 | ||
1062 | /* | |
1063 | * Custom macro stuff | |
1064 | * | |
1065 | */ | |
1066 | ||
6c155d33 | 1067 | bool StringTobool(wxChar *val) |
9a29912f | 1068 | { |
6c155d33 JS |
1069 | if (wxStrncmp(val, _T("yes"), 3) == 0 || wxStrncmp(val, _T("YES"), 3) == 0 || |
1070 | wxStrncmp(val, _T("on"), 2) == 0 || wxStrncmp(val, _T("ON"), 2) == 0 || | |
1071 | wxStrncmp(val, _T("true"), 4) == 0 || wxStrncmp(val, _T("TRUE"), 4) == 0 || | |
1072 | wxStrncmp(val, _T("ok"), 2) == 0 || wxStrncmp(val, _T("OK"), 2) == 0 || | |
1073 | wxStrncmp(val, _T("1"), 1) == 0) | |
9a29912f JS |
1074 | return TRUE; |
1075 | else | |
1076 | return FALSE; | |
1077 | } | |
1078 | ||
1079 | // Define a variable value from the .ini file | |
6c155d33 | 1080 | wxChar *RegisterSetting(wxChar *settingName, wxChar *settingValue, bool interactive) |
9a29912f | 1081 | { |
6c155d33 JS |
1082 | static wxChar errorCode[100]; |
1083 | wxStrcpy(errorCode, _T("OK")); | |
1084 | if (StringMatch(settingName, _T("chapterName"), FALSE, TRUE)) | |
9a29912f JS |
1085 | { |
1086 | delete[] ChapterNameString; | |
1087 | ChapterNameString = copystring(settingValue); | |
1088 | } | |
6c155d33 | 1089 | else if (StringMatch(settingName, _T("sectionName"), FALSE, TRUE)) |
9a29912f JS |
1090 | { |
1091 | delete[] SectionNameString; | |
1092 | SectionNameString = copystring(settingValue); | |
1093 | } | |
6c155d33 | 1094 | else if (StringMatch(settingName, _T("subsectionName"), FALSE, TRUE)) |
9a29912f JS |
1095 | { |
1096 | delete[] SubsectionNameString; | |
1097 | SubsectionNameString = copystring(settingValue); | |
1098 | } | |
6c155d33 | 1099 | else if (StringMatch(settingName, _T("subsubsectionName"), FALSE, TRUE)) |
9a29912f JS |
1100 | { |
1101 | delete[] SubsubsectionNameString; | |
1102 | SubsubsectionNameString = copystring(settingValue); | |
1103 | } | |
6c155d33 | 1104 | else if (StringMatch(settingName, _T("indexName"), FALSE, TRUE)) |
9a29912f JS |
1105 | { |
1106 | delete[] IndexNameString; | |
1107 | IndexNameString = copystring(settingValue); | |
1108 | } | |
6c155d33 | 1109 | else if (StringMatch(settingName, _T("contentsName"), FALSE, TRUE)) |
9a29912f JS |
1110 | { |
1111 | delete[] ContentsNameString; | |
1112 | ContentsNameString = copystring(settingValue); | |
1113 | } | |
6c155d33 | 1114 | else if (StringMatch(settingName, _T("glossaryName"), FALSE, TRUE)) |
9a29912f JS |
1115 | { |
1116 | delete[] GlossaryNameString; | |
1117 | GlossaryNameString = copystring(settingValue); | |
1118 | } | |
6c155d33 | 1119 | else if (StringMatch(settingName, _T("referencesName"), FALSE, TRUE)) |
9a29912f JS |
1120 | { |
1121 | delete[] ReferencesNameString; | |
1122 | ReferencesNameString = copystring(settingValue); | |
1123 | } | |
6c155d33 | 1124 | else if (StringMatch(settingName, _T("tablesName"), FALSE, TRUE)) |
9a29912f JS |
1125 | { |
1126 | delete[] TablesNameString; | |
1127 | TablesNameString = copystring(settingValue); | |
1128 | } | |
6c155d33 | 1129 | else if (StringMatch(settingName, _T("figuresName"), FALSE, TRUE)) |
9a29912f JS |
1130 | { |
1131 | delete[] FiguresNameString; | |
1132 | FiguresNameString = copystring(settingValue); | |
1133 | } | |
6c155d33 | 1134 | else if (StringMatch(settingName, _T("tableName"), FALSE, TRUE)) |
9a29912f JS |
1135 | { |
1136 | delete[] TableNameString; | |
1137 | TableNameString = copystring(settingValue); | |
1138 | } | |
6c155d33 | 1139 | else if (StringMatch(settingName, _T("figureName"), FALSE, TRUE)) |
9a29912f JS |
1140 | { |
1141 | delete[] FigureNameString; | |
1142 | FigureNameString = copystring(settingValue); | |
1143 | } | |
6c155d33 | 1144 | else if (StringMatch(settingName, _T("abstractName"), FALSE, TRUE)) |
9a29912f JS |
1145 | { |
1146 | delete[] AbstractNameString; | |
1147 | AbstractNameString = copystring(settingValue); | |
1148 | } | |
6c155d33 | 1149 | else if (StringMatch(settingName, _T("chapterFontSize"), FALSE, TRUE)) |
9a29912f | 1150 | StringToInt(settingValue, &chapterFont); |
6c155d33 | 1151 | else if (StringMatch(settingName, _T("sectionFontSize"), FALSE, TRUE)) |
9a29912f | 1152 | StringToInt(settingValue, §ionFont); |
6c155d33 | 1153 | else if (StringMatch(settingName, _T("subsectionFontSize"), FALSE, TRUE)) |
9a29912f | 1154 | StringToInt(settingValue, &subsectionFont); |
6c155d33 | 1155 | else if (StringMatch(settingName, _T("titleFontSize"), FALSE, TRUE)) |
9a29912f | 1156 | StringToInt(settingValue, &titleFont); |
6c155d33 | 1157 | else if (StringMatch(settingName, _T("authorFontSize"), FALSE, TRUE)) |
9a29912f | 1158 | StringToInt(settingValue, &authorFont); |
6c155d33 | 1159 | else if (StringMatch(settingName, _T("ignoreInput"), FALSE, TRUE)) |
2b5f62a0 | 1160 | IgnorableInputFiles.Add(wxFileNameFromPath(settingValue)); |
6c155d33 | 1161 | else if (StringMatch(settingName, _T("mirrorMargins"), FALSE, TRUE)) |
9a29912f | 1162 | mirrorMargins = StringTobool(settingValue); |
6c155d33 | 1163 | else if (StringMatch(settingName, _T("runTwice"), FALSE, TRUE)) |
9a29912f | 1164 | runTwice = StringTobool(settingValue); |
6c155d33 | 1165 | else if (StringMatch(settingName, _T("isInteractive"), FALSE, TRUE)) |
9a29912f | 1166 | isInteractive = StringTobool(settingValue); |
6c155d33 | 1167 | else if (StringMatch(settingName, _T("headerRule"), FALSE, TRUE)) |
9a29912f | 1168 | headerRule = StringTobool(settingValue); |
6c155d33 | 1169 | else if (StringMatch(settingName, _T("footerRule"), FALSE, TRUE)) |
9a29912f | 1170 | footerRule = StringTobool(settingValue); |
6c155d33 | 1171 | else if (StringMatch(settingName, _T("combineSubSections"), FALSE, TRUE)) |
9a29912f | 1172 | combineSubSections = StringTobool(settingValue); |
6c155d33 | 1173 | else if (StringMatch(settingName, _T("listLabelIndent"), FALSE, TRUE)) |
9a29912f | 1174 | StringToInt(settingValue, &labelIndentTab); |
6c155d33 | 1175 | else if (StringMatch(settingName, _T("listItemIndent"), FALSE, TRUE)) |
9a29912f | 1176 | StringToInt(settingValue, &itemIndentTab); |
6c155d33 | 1177 | else if (StringMatch(settingName, _T("useUpButton"), FALSE, TRUE)) |
9a29912f | 1178 | useUpButton = StringTobool(settingValue); |
6c155d33 | 1179 | else if (StringMatch(settingName, _T("useHeadingStyles"), FALSE, TRUE)) |
9a29912f | 1180 | useHeadingStyles = StringTobool(settingValue); |
6c155d33 | 1181 | else if (StringMatch(settingName, _T("useWord"), FALSE, TRUE)) |
9a29912f | 1182 | useWord = StringTobool(settingValue); |
6c155d33 | 1183 | else if (StringMatch(settingName, _T("contentsDepth"), FALSE, TRUE)) |
9a29912f | 1184 | StringToInt(settingValue, &contentsDepth); |
6c155d33 | 1185 | else if (StringMatch(settingName, _T("generateHPJ"), FALSE, TRUE)) |
9a29912f | 1186 | generateHPJ = StringTobool(settingValue); |
6c155d33 | 1187 | else if (StringMatch(settingName, _T("truncateFilenames"), FALSE, TRUE)) |
9a29912f | 1188 | truncateFilenames = StringTobool(settingValue); |
6c155d33 | 1189 | else if (StringMatch(settingName, _T("winHelpVersion"), FALSE, TRUE)) |
9a29912f | 1190 | StringToInt(settingValue, &winHelpVersion); |
6c155d33 | 1191 | else if (StringMatch(settingName, _T("winHelpContents"), FALSE, TRUE)) |
9a29912f | 1192 | winHelpContents = StringTobool(settingValue); |
6c155d33 | 1193 | else if (StringMatch(settingName, _T("htmlIndex"), FALSE, TRUE)) |
9a29912f | 1194 | htmlIndex = StringTobool(settingValue); |
6c155d33 | 1195 | else if (StringMatch(settingName, _T("htmlWorkshopFiles"), FALSE, TRUE)) |
14204c7a | 1196 | htmlWorkshopFiles = StringTobool(settingValue); |
6c155d33 | 1197 | else if (StringMatch(settingName, _T("htmlFrameContents"), FALSE, TRUE)) |
9a29912f | 1198 | htmlFrameContents = StringTobool(settingValue); |
6c155d33 | 1199 | else if (StringMatch(settingName, _T("htmlStylesheet"), FALSE, TRUE)) |
6d8b260c VS |
1200 | { |
1201 | if (htmlStylesheet) delete[] htmlStylesheet; | |
1202 | htmlStylesheet = copystring(settingValue); | |
1203 | } | |
6c155d33 | 1204 | else if (StringMatch(settingName, _T("upperCaseNames"), FALSE, TRUE)) |
9a29912f | 1205 | upperCaseNames = StringTobool(settingValue); |
6c155d33 | 1206 | else if (StringMatch(settingName, _T("ignoreBadRefs"), FALSE, TRUE)) |
bf16085d | 1207 | ignoreBadRefs = StringTobool(settingValue); |
6c155d33 | 1208 | else if (StringMatch(settingName, _T("htmlFaceName"), FALSE, TRUE)) |
d7d17624 JS |
1209 | { |
1210 | delete[] htmlFaceName; | |
1211 | htmlFaceName = copystring(settingValue); | |
1212 | } | |
6c155d33 | 1213 | else if (StringMatch(settingName, _T("winHelpTitle"), FALSE, TRUE)) |
9a29912f JS |
1214 | { |
1215 | if (winHelpTitle) | |
1216 | delete[] winHelpTitle; | |
1217 | winHelpTitle = copystring(settingValue); | |
1218 | } | |
6c155d33 | 1219 | else if (StringMatch(settingName, _T("indexSubsections"), FALSE, TRUE)) |
9a29912f | 1220 | indexSubsections = StringTobool(settingValue); |
6c155d33 | 1221 | else if (StringMatch(settingName, _T("compatibility"), FALSE, TRUE)) |
9a29912f | 1222 | compatibilityMode = StringTobool(settingValue); |
6c155d33 | 1223 | else if (StringMatch(settingName, _T("defaultColumnWidth"), FALSE, TRUE)) |
9a29912f JS |
1224 | { |
1225 | StringToInt(settingValue, &defaultTableColumnWidth); | |
1226 | defaultTableColumnWidth = 20*defaultTableColumnWidth; | |
1227 | } | |
6c155d33 | 1228 | else if (StringMatch(settingName, _T("bitmapMethod"), FALSE, TRUE)) |
9a29912f | 1229 | { |
6c155d33 JS |
1230 | if ((wxStrcmp(settingValue, _T("includepicture")) != 0) && (wxStrcmp(settingValue, _T("hex")) != 0) && |
1231 | (wxStrcmp(settingValue, _T("import")) != 0)) | |
9a29912f JS |
1232 | { |
1233 | if (interactive) | |
6c155d33 JS |
1234 | OnError(_T("Unknown bitmapMethod")); |
1235 | wxStrcpy(errorCode, _T("Unknown bitmapMethod")); | |
9a29912f JS |
1236 | } |
1237 | else | |
1238 | { | |
1239 | delete[] bitmapMethod; | |
1240 | bitmapMethod = copystring(settingValue); | |
1241 | } | |
1242 | } | |
6c155d33 | 1243 | else if (StringMatch(settingName, _T("htmlBrowseButtons"), FALSE, TRUE)) |
9a29912f | 1244 | { |
6c155d33 | 1245 | if (wxStrcmp(settingValue, _T("none")) == 0) |
9a29912f | 1246 | htmlBrowseButtons = HTML_BUTTONS_NONE; |
6c155d33 | 1247 | else if (wxStrcmp(settingValue, _T("bitmap")) == 0) |
9a29912f | 1248 | htmlBrowseButtons = HTML_BUTTONS_BITMAP; |
6c155d33 | 1249 | else if (wxStrcmp(settingValue, _T("text")) == 0) |
9a29912f JS |
1250 | htmlBrowseButtons = HTML_BUTTONS_TEXT; |
1251 | else | |
1252 | { | |
1253 | if (interactive) | |
6c155d33 JS |
1254 | OnInform(_T("Initialisation file error: htmlBrowseButtons must be one of none, bitmap, or text.")); |
1255 | wxStrcpy(errorCode, _T("Initialisation file error: htmlBrowseButtons must be one of none, bitmap, or text.")); | |
9a29912f JS |
1256 | } |
1257 | } | |
6c155d33 | 1258 | else if (StringMatch(settingName, _T("backgroundImage"), FALSE, TRUE)) |
9a29912f JS |
1259 | { |
1260 | backgroundImageString = copystring(settingValue); | |
1261 | } | |
6c155d33 | 1262 | else if (StringMatch(settingName, _T("backgroundColour"), FALSE, TRUE)) |
9a29912f JS |
1263 | { |
1264 | delete[] backgroundColourString; | |
1265 | backgroundColourString = copystring(settingValue); | |
1266 | } | |
6c155d33 | 1267 | else if (StringMatch(settingName, _T("textColour"), FALSE, TRUE)) |
9a29912f JS |
1268 | { |
1269 | textColourString = copystring(settingValue); | |
1270 | } | |
6c155d33 | 1271 | else if (StringMatch(settingName, _T("linkColour"), FALSE, TRUE)) |
9a29912f JS |
1272 | { |
1273 | linkColourString = copystring(settingValue); | |
1274 | } | |
6c155d33 | 1275 | else if (StringMatch(settingName, _T("followedLinkColour"), FALSE, TRUE)) |
9a29912f JS |
1276 | { |
1277 | followedLinkColourString = copystring(settingValue); | |
1278 | } | |
6c155d33 | 1279 | else if (StringMatch(settingName, _T("conversionMode"), FALSE, TRUE)) |
9a29912f | 1280 | { |
6c155d33 | 1281 | if (StringMatch(settingValue, _T("RTF"), FALSE, TRUE)) |
9a29912f JS |
1282 | { |
1283 | winHelp = FALSE; convertMode = TEX_RTF; | |
1284 | } | |
6c155d33 | 1285 | else if (StringMatch(settingValue, _T("WinHelp"), FALSE, TRUE)) |
9a29912f JS |
1286 | { |
1287 | winHelp = TRUE; convertMode = TEX_RTF; | |
1288 | } | |
6c155d33 JS |
1289 | else if (StringMatch(settingValue, _T("XLP"), FALSE, TRUE) || |
1290 | StringMatch(settingValue, _T("wxHelp"), FALSE, TRUE)) | |
9a29912f JS |
1291 | { |
1292 | convertMode = TEX_XLP; | |
1293 | } | |
6c155d33 | 1294 | else if (StringMatch(settingValue, _T("HTML"), FALSE, TRUE)) |
9a29912f JS |
1295 | { |
1296 | convertMode = TEX_HTML; | |
1297 | } | |
1298 | else | |
1299 | { | |
1300 | if (interactive) | |
6c155d33 JS |
1301 | OnInform(_T("Initialisation file error: conversionMode must be one of\nRTF, WinHelp, XLP (or wxHelp), HTML.")); |
1302 | wxStrcpy(errorCode, _T("Initialisation file error: conversionMode must be one of\nRTF, WinHelp, XLP (or wxHelp), HTML.")); | |
9a29912f JS |
1303 | } |
1304 | } | |
6c155d33 | 1305 | else if (StringMatch(settingName, _T("documentFontSize"), FALSE, TRUE)) |
9a29912f JS |
1306 | { |
1307 | int n; | |
1308 | StringToInt(settingValue, &n); | |
1309 | if (n == 10 || n == 11 || n == 12) | |
1310 | SetFontSizes(n); | |
1311 | else | |
1312 | { | |
6c155d33 JS |
1313 | wxChar buf[200]; |
1314 | wxSprintf(buf, _T("Initialisation file error: nonstandard document font size %d."), n); | |
9a29912f JS |
1315 | if (interactive) |
1316 | OnInform(buf); | |
6c155d33 | 1317 | wxStrcpy(errorCode, buf); |
9a29912f JS |
1318 | } |
1319 | } | |
1320 | else | |
1321 | { | |
6c155d33 JS |
1322 | wxChar buf[200]; |
1323 | wxSprintf(buf, _T("Initialisation file error: unrecognised setting %s."), settingName); | |
9a29912f JS |
1324 | if (interactive) |
1325 | OnInform(buf); | |
6c155d33 | 1326 | wxStrcpy(errorCode, buf); |
9a29912f JS |
1327 | } |
1328 | return errorCode; | |
1329 | } | |
1330 | ||
6c155d33 | 1331 | bool ReadCustomMacros(wxChar *filename) |
9a29912f | 1332 | { |
dbda9e86 JS |
1333 | if (!wxFileExists(filename)) |
1334 | return FALSE; | |
1335 | ||
e969b63b | 1336 | wxSTD ifstream istr(filename, wxSTD ios::in); |
dbda9e86 | 1337 | |
9a29912f JS |
1338 | if (istr.bad()) return FALSE; |
1339 | ||
1340 | CustomMacroList.Clear(); | |
1341 | char ch; | |
6c155d33 JS |
1342 | wxChar macroName[100]; |
1343 | wxChar macroBody[1000]; | |
9a29912f JS |
1344 | int noArgs; |
1345 | ||
1346 | while (!istr.eof()) | |
1347 | { | |
1348 | BibEatWhiteSpace(istr); | |
1349 | istr.get(ch); | |
1350 | if (istr.eof()) | |
1351 | break; | |
1352 | ||
1353 | if (ch != '\\') // Not a macro definition, so must be NAME=VALUE | |
1354 | { | |
6c155d33 | 1355 | wxChar settingName[100]; |
9a29912f JS |
1356 | settingName[0] = ch; |
1357 | BibReadWord(istr, (settingName+1)); | |
1358 | BibEatWhiteSpace(istr); | |
1359 | istr.get(ch); | |
1360 | if (ch != '=') | |
1361 | { | |
6c155d33 | 1362 | OnError(_T("Expected = following name: malformed tex2rtf.ini file.")); |
9a29912f JS |
1363 | return FALSE; |
1364 | } | |
1365 | else | |
1366 | { | |
6c155d33 | 1367 | wxChar settingValue[200]; |
9a29912f JS |
1368 | BibEatWhiteSpace(istr); |
1369 | BibReadToEOL(istr, settingValue); | |
1370 | RegisterSetting(settingName, settingValue); | |
1371 | } | |
1372 | } | |
1373 | else | |
1374 | { | |
1375 | BibReadWord(istr, macroName); | |
1376 | BibEatWhiteSpace(istr); | |
1377 | istr.get(ch); | |
1378 | if (ch != '[') | |
1379 | { | |
6c155d33 | 1380 | OnError(_T("Expected [ followed by number of arguments: malformed tex2rtf.ini file.")); |
9a29912f JS |
1381 | return FALSE; |
1382 | } | |
1383 | istr >> noArgs; | |
1384 | istr.get(ch); | |
1385 | if (ch != ']') | |
1386 | { | |
6c155d33 | 1387 | OnError(_T("Expected ] following number of arguments: malformed tex2rtf.ini file.")); |
9a29912f JS |
1388 | return FALSE; |
1389 | } | |
1390 | BibEatWhiteSpace(istr); | |
1391 | istr.get(ch); | |
1392 | if (ch != '{') | |
1393 | { | |
6c155d33 | 1394 | OnError(_T("Expected { followed by macro body: malformed tex2rtf.ini file.")); |
9a29912f JS |
1395 | return FALSE; |
1396 | } | |
1397 | CustomMacro *macro = new CustomMacro(macroName, noArgs, NULL); | |
1398 | BibReadValue(istr, macroBody, FALSE, FALSE); // Don't ignore extra braces | |
6c155d33 | 1399 | if (wxStrlen(macroBody) > 0) |
9a29912f JS |
1400 | macro->macroBody = copystring(macroBody); |
1401 | ||
1402 | BibEatWhiteSpace(istr); | |
1403 | CustomMacroList.Append(macroName, macro); | |
1404 | AddMacroDef(ltCUSTOM_MACRO, macroName, noArgs); | |
1405 | } | |
1406 | } | |
6c155d33 JS |
1407 | wxChar mbuf[200]; |
1408 | wxSprintf(mbuf, _T("Read initialization file %s."), filename); | |
9a29912f JS |
1409 | OnInform(mbuf); |
1410 | return TRUE; | |
1411 | } | |
1412 | ||
6c155d33 | 1413 | CustomMacro *FindCustomMacro(wxChar *name) |
9a29912f JS |
1414 | { |
1415 | wxNode *node = CustomMacroList.Find(name); | |
1416 | if (node) | |
1417 | { | |
ddc4f3b5 | 1418 | CustomMacro *macro = (CustomMacro *)node->GetData(); |
9a29912f JS |
1419 | return macro; |
1420 | } | |
1421 | return NULL; | |
1422 | } | |
1423 | ||
1424 | // Display custom macros | |
1425 | void ShowCustomMacros(void) | |
1426 | { | |
ddc4f3b5 | 1427 | wxNode *node = CustomMacroList.GetFirst(); |
9a29912f JS |
1428 | if (!node) |
1429 | { | |
6c155d33 | 1430 | OnInform(_T("No custom macros loaded.\n")); |
9a29912f JS |
1431 | return; |
1432 | } | |
1433 | ||
6c155d33 | 1434 | wxChar buf[400]; |
9a29912f JS |
1435 | while (node) |
1436 | { | |
ddc4f3b5 | 1437 | CustomMacro *macro = (CustomMacro *)node->GetData(); |
6c155d33 JS |
1438 | wxSprintf(buf, _T("\\%s[%d]\n {%s}"), macro->macroName, macro->noArgs, |
1439 | macro->macroBody ? macro->macroBody : _T("")); | |
9a29912f | 1440 | OnInform(buf); |
ddc4f3b5 | 1441 | node = node->GetNext(); |
9a29912f JS |
1442 | } |
1443 | } | |
1444 | ||
1445 | // Parse a string into several comma-separated fields | |
6c155d33 | 1446 | wxChar *ParseMultifieldString(wxChar *allFields, int *pos) |
9a29912f | 1447 | { |
6c155d33 | 1448 | static wxChar buffer[300]; |
9a29912f JS |
1449 | int i = 0; |
1450 | int fieldIndex = *pos; | |
6c155d33 | 1451 | int len = wxStrlen(allFields); |
9a29912f JS |
1452 | int oldPos = *pos; |
1453 | bool keepGoing = TRUE; | |
1454 | while ((fieldIndex <= len) && keepGoing) | |
1455 | { | |
6c155d33 | 1456 | if (allFields[fieldIndex] == _T(' ')) |
9a29912f JS |
1457 | { |
1458 | // Skip | |
1459 | fieldIndex ++; | |
1460 | } | |
6c155d33 | 1461 | else if (allFields[fieldIndex] == _T(',')) |
9a29912f JS |
1462 | { |
1463 | *pos = fieldIndex + 1; | |
1464 | keepGoing = FALSE; | |
1465 | } | |
1466 | else if (allFields[fieldIndex] == 0) | |
1467 | { | |
1468 | *pos = fieldIndex + 1; | |
1469 | keepGoing = FALSE; | |
1470 | } | |
1471 | else | |
1472 | { | |
1473 | buffer[i] = allFields[fieldIndex]; | |
1474 | fieldIndex ++; | |
1475 | i++; | |
1476 | } | |
1477 | } | |
1478 | buffer[i] = 0; | |
1479 | if (oldPos == (*pos)) | |
1480 | *pos = len + 1; | |
1481 | ||
1482 | if (i == 0) | |
1483 | return NULL; | |
1484 | else | |
1485 | return buffer; | |
1486 | } | |
1487 | ||
1488 | /* | |
1489 | * Colour tables | |
1490 | * | |
1491 | */ | |
1492 | ||
6c155d33 | 1493 | ColourTableEntry::ColourTableEntry(const wxChar *theName, unsigned int r, unsigned int g, unsigned int b) |
9a29912f JS |
1494 | { |
1495 | name = copystring(theName); | |
1496 | red = r; | |
1497 | green = g; | |
1498 | blue = b; | |
1499 | } | |
1500 | ||
1501 | ColourTableEntry::~ColourTableEntry(void) | |
1502 | { | |
1503 | delete[] name; | |
1504 | } | |
1505 | ||
6c155d33 | 1506 | void AddColour(const wxChar *theName, unsigned int r, unsigned int g, unsigned int b) |
9a29912f JS |
1507 | { |
1508 | wxNode *node = ColourTable.Find(theName); | |
1509 | if (node) | |
1510 | { | |
ddc4f3b5 | 1511 | ColourTableEntry *entry = (ColourTableEntry *)node->GetData(); |
9a29912f JS |
1512 | if (entry->red == r || entry->green == g || entry->blue == b) |
1513 | return; | |
1514 | else | |
1515 | { | |
1516 | delete entry; | |
1517 | delete node; | |
1518 | } | |
1519 | } | |
1520 | ColourTableEntry *entry = new ColourTableEntry(theName, r, g, b); | |
1521 | ColourTable.Append(theName, entry); | |
1522 | } | |
1523 | ||
6c155d33 | 1524 | int FindColourPosition(wxChar *theName) |
9a29912f JS |
1525 | { |
1526 | int i = 0; | |
ddc4f3b5 | 1527 | wxNode *node = ColourTable.GetFirst(); |
9a29912f JS |
1528 | while (node) |
1529 | { | |
ddc4f3b5 | 1530 | ColourTableEntry *entry = (ColourTableEntry *)node->GetData(); |
6c155d33 | 1531 | if (wxStrcmp(theName, entry->name) == 0) |
9a29912f JS |
1532 | return i; |
1533 | i ++; | |
ddc4f3b5 | 1534 | node = node->GetNext(); |
9a29912f JS |
1535 | } |
1536 | return -1; | |
1537 | } | |
1538 | ||
1539 | // Converts e.g. "red" -> "#FF0000" | |
6c155d33 JS |
1540 | extern void DecToHex(int, wxChar *); |
1541 | bool FindColourHTMLString(wxChar *theName, wxChar *buf) | |
9a29912f | 1542 | { |
ddc4f3b5 | 1543 | wxNode *node = ColourTable.GetFirst(); |
9a29912f JS |
1544 | while (node) |
1545 | { | |
ddc4f3b5 | 1546 | ColourTableEntry *entry = (ColourTableEntry *)node->GetData(); |
6c155d33 | 1547 | if (wxStrcmp(theName, entry->name) == 0) |
9a29912f | 1548 | { |
6c155d33 | 1549 | wxStrcpy(buf, _T("#")); |
9a29912f | 1550 | |
6c155d33 | 1551 | wxChar buf2[3]; |
9a29912f | 1552 | DecToHex(entry->red, buf2); |
6c155d33 | 1553 | wxStrcat(buf, buf2); |
9a29912f | 1554 | DecToHex(entry->green, buf2); |
6c155d33 | 1555 | wxStrcat(buf, buf2); |
9a29912f | 1556 | DecToHex(entry->blue, buf2); |
6c155d33 | 1557 | wxStrcat(buf, buf2); |
9a29912f JS |
1558 | |
1559 | return TRUE; | |
1560 | } | |
ddc4f3b5 | 1561 | node = node->GetNext(); |
9a29912f JS |
1562 | } |
1563 | return FALSE; | |
1564 | } | |
1565 | ||
1566 | ||
1567 | void InitialiseColourTable(void) | |
1568 | { | |
1569 | // \\red0\\green0\\blue0; | |
6c155d33 | 1570 | AddColour(_T("black"), 0,0,0); |
9a29912f JS |
1571 | |
1572 | // \\red0\\green0\\blue255;\\red0\\green255\\blue255;\n"); | |
6c155d33 | 1573 | AddColour(_T("cyan"), 0,255,255); |
9a29912f JS |
1574 | |
1575 | // \\red0\\green255\\blue0; | |
6c155d33 | 1576 | AddColour(_T("green"), 0,255,0); |
9a29912f JS |
1577 | |
1578 | // \\red255\\green0\\blue255; | |
6c155d33 | 1579 | AddColour(_T("magenta"), 255,0,255); |
9a29912f JS |
1580 | |
1581 | // \\red255\\green0\\blue0; | |
6c155d33 | 1582 | AddColour(_T("red"), 255,0,0); |
9a29912f JS |
1583 | |
1584 | // \\red255\\green255\\blue0; | |
6c155d33 | 1585 | AddColour(_T("yellow"), 255,255,0); |
9a29912f JS |
1586 | |
1587 | // \\red255\\green255\\blue255;}"); | |
6c155d33 | 1588 | AddColour(_T("white"), 255,255,255); |
9a29912f JS |
1589 | } |
1590 | ||
1591 | /* | |
1592 | * The purpose of this is to reduce the number of times wxYield is | |
1593 | * called, since under Windows this can slow things down. | |
1594 | */ | |
9a29912f JS |
1595 | |
1596 | void Tex2RTFYield(bool force) | |
1597 | { | |
1598 | #ifdef __WXMSW__ | |
dda2e4fd | 1599 | static int yieldCount = 0; |
9a29912f | 1600 | |
dda2e4fd GD |
1601 | if (isSync) |
1602 | return; | |
1603 | ||
1604 | if (force) | |
1605 | yieldCount = 0; | |
1606 | if (yieldCount == 0) | |
1607 | { | |
1608 | if (wxTheApp) | |
1609 | wxYield(); | |
1610 | yieldCount = 10; | |
1611 | } | |
1612 | yieldCount --; | |
9a29912f JS |
1613 | #endif |
1614 | } | |
1615 | ||
1616 | // In both RTF generation and HTML generation for wxHelp version 2, | |
1617 | // we need to associate \indexed keywords with the current filename/topics. | |
1618 | ||
1619 | // Hash table for lists of keywords for topics (WinHelp). | |
1620 | wxHashTable TopicTable(wxKEY_STRING); | |
6c155d33 | 1621 | void AddKeyWordForTopic(wxChar *topic, wxChar *entry, wxChar *filename) |
9a29912f JS |
1622 | { |
1623 | TexTopic *texTopic = (TexTopic *)TopicTable.Get(topic); | |
1624 | if (!texTopic) | |
1625 | { | |
1626 | texTopic = new TexTopic(filename); | |
1627 | texTopic->keywords = new wxStringList; | |
1628 | TopicTable.Put(topic, texTopic); | |
1629 | } | |
1630 | ||
1631 | if (!texTopic->keywords->Member(entry)) | |
1632 | texTopic->keywords->Add(entry); | |
1633 | } | |
1634 | ||
1635 | void ClearKeyWordTable(void) | |
1636 | { | |
1637 | TopicTable.BeginFind(); | |
1638 | wxNode *node = TopicTable.Next(); | |
1639 | while (node) | |
1640 | { | |
ddc4f3b5 | 1641 | TexTopic *texTopic = (TexTopic *)node->GetData(); |
9a29912f JS |
1642 | delete texTopic; |
1643 | node = TopicTable.Next(); | |
1644 | } | |
1645 | TopicTable.Clear(); | |
1646 | } | |
1647 | ||
1648 | ||
1649 | /* | |
1650 | * TexTopic structure | |
1651 | */ | |
1652 | ||
6c155d33 | 1653 | TexTopic::TexTopic(wxChar *f) |
9a29912f JS |
1654 | { |
1655 | if (f) | |
1656 | filename = copystring(f); | |
1657 | else | |
1658 | filename = NULL; | |
1659 | hasChildren = FALSE; | |
1660 | keywords = NULL; | |
1661 | } | |
1662 | ||
1663 | TexTopic::~TexTopic(void) | |
1664 | { | |
1665 | if (keywords) | |
1666 | delete keywords; | |
1667 | if (filename) | |
1668 | delete[] filename; | |
1669 | } | |
1670 | ||
1671 | // Convert case, according to upperCaseNames setting. | |
6c155d33 | 1672 | wxChar *ConvertCase(wxChar *s) |
9a29912f | 1673 | { |
6c155d33 JS |
1674 | static wxChar buf[256]; |
1675 | int len = wxStrlen(s); | |
9a29912f JS |
1676 | int i; |
1677 | if (upperCaseNames) | |
1678 | for (i = 0; i < len; i ++) | |
6c155d33 | 1679 | buf[i] = wxToupper(s[i]); |
9a29912f JS |
1680 | else |
1681 | for (i = 0; i < len; i ++) | |
6c155d33 | 1682 | buf[i] = wxTolower(s[i]); |
9a29912f JS |
1683 | buf[i] = 0; |
1684 | return buf; | |
1685 | } | |
2b5f62a0 VZ |
1686 | |
1687 | #if !WXWIN_COMPATIBILITY_2 | |
1688 | // if substring is TRUE, search for str1 in str2 | |
1689 | bool StringMatch(const wxChar *str1, const wxChar *str2, bool subString, | |
1690 | bool exact) | |
1691 | { | |
1692 | if (subString) | |
1693 | { | |
1694 | wxString Sstr1(str1); | |
1695 | wxString Sstr2(str2); | |
1696 | if (!exact) | |
1697 | { | |
1698 | Sstr1.MakeUpper(); | |
1699 | Sstr2.MakeUpper(); | |
1700 | } | |
6a205442 | 1701 | return Sstr2.Index(Sstr1) != (size_t)wxNOT_FOUND; |
2b5f62a0 VZ |
1702 | } |
1703 | else | |
1704 | return exact ? wxString(str2).Cmp(str1) == 0 : | |
1705 | wxString(str2).CmpNoCase(str1) == 0; | |
1706 | } | |
1707 | #endif |