]> git.saurik.com Git - wxWidgets.git/blob - utils/tex2rtf/src/tex2rtf.cpp
Small fixes for Tex2RTF but alas, we still get memory problems.
[wxWidgets.git] / utils / tex2rtf / src / tex2rtf.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: tex2rtf.cpp
3 // Purpose: Converts Latex to linear/WinHelp RTF, HTML, wxHelp.
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
24 #include "wx/wx.h"
25 #endif
26
27 #ifndef NO_GUI
28 #include <wx/help.h>
29 #include <wx/timer.h>
30 #endif
31
32 #if defined(NO_GUI) || defined(__UNIX__)
33 #if wxUSE_IOSTREAMH
34 #include <iostream.h>
35 #include <fstream.h>
36 #else
37 #include <iostream>
38 #include <fstream>
39 #endif
40 #endif
41
42 #include <ctype.h>
43 #include <stdlib.h>
44 #include "tex2any.h"
45 #include "tex2rtf.h"
46 #include "rtfutils.h"
47
48 #if (defined(__WXGTK__) || defined(__WXMOTIF__)) && !defined(NO_GUI)
49 #include "tex2rtf.xpm"
50 #endif
51
52 const float versionNo = 2.0;
53
54 TexChunk *currentMember = NULL;
55 bool startedSections = FALSE;
56 char *contentsString = NULL;
57 bool suppressNameDecoration = FALSE;
58 bool OkToClose = TRUE;
59 int passNumber = 1;
60
61 #ifndef NO_GUI
62
63 #if wxUSE_HELP
64 wxHelpController *HelpInstance = NULL;
65 #endif // wxUSE_HELP
66
67 #ifdef __WXMSW__
68 static char *ipc_buffer = NULL;
69 static char Tex2RTFLastStatus[100];
70 Tex2RTFServer *TheTex2RTFServer = NULL;
71 #endif
72 #endif
73
74 char *bulletFile = NULL;
75
76 FILE *Contents = NULL; // Contents page
77 FILE *Chapters = NULL; // Chapters (WinHelp RTF) or rest of file (linear RTF)
78 FILE *Sections = NULL;
79 FILE *Subsections = NULL;
80 FILE *Subsubsections = NULL;
81 FILE *Popups = NULL;
82 FILE *WinHelpContentsFile = NULL;
83
84 char *InputFile = NULL;
85 char *OutputFile = NULL;
86 char *MacroFile = copystring("tex2rtf.ini");
87
88 char *FileRoot = NULL;
89 char *ContentsName = NULL; // Contents page from last time around
90 char *TmpContentsName = NULL; // Current contents page
91 char *TmpFrameContentsName = NULL; // Current frame contents page
92 char *WinHelpContentsFileName = NULL; // WinHelp .cnt file
93 char *RefName = NULL; // Reference file name
94
95 char *RTFCharset = copystring("ansi");
96
97 #ifdef __WXMSW__
98 int BufSize = 100; // Size of buffer in K
99 #else
100 int BufSize = 500;
101 #endif
102
103 bool Go(void);
104 void ShowOptions(void);
105
106 #ifdef NO_GUI
107
108 extern char *wxBuffer; // we must init it, otherwise tex2rtf will crash
109
110 int main(int argc, char **argv)
111 #else
112 wxMenuBar *menuBar = NULL;
113 MyFrame *frame = NULL;
114
115 // DECLARE_APP(MyApp)
116 IMPLEMENT_APP(MyApp)
117
118 // `Main program' equivalent, creating windows and returning main app frame
119 bool MyApp::OnInit()
120 #endif
121 {
122 // Use default list of macros defined in tex2any.cc
123 DefineDefaultMacros();
124 AddMacroDef(ltHARDY, "hardy", 0);
125
126 FileRoot = new char[300];
127 ContentsName = new char[300];
128 TmpContentsName = new char[300];
129 TmpFrameContentsName = new char[300];
130 WinHelpContentsFileName = new char[300];
131 RefName = new char[300];
132
133 int n = 1;
134
135 // Read input/output files
136 if (argc > 1)
137 {
138 if (argv[1][0] != '-')
139 {
140 InputFile = argv[1];
141 n ++;
142
143 if (argc > 2)
144 {
145 if (argv[2][0] != '-')
146 {
147 OutputFile = argv[2];
148 n ++;
149 }
150 }
151 }
152 }
153
154 #ifdef NO_GUI
155 wxBuffer = new char[1500];
156 // this is done in wxApp, but NO_GUI version doesn't call it :-(
157
158 if (!InputFile || !OutputFile)
159 {
160 cout << "Tex2RTF: input or output file is missing.\n";
161 ShowOptions();
162 exit(1);
163 }
164
165 #endif
166 if (InputFile)
167 {
168 TexPathList.EnsureFileAccessible(InputFile);
169 }
170 if (!InputFile || !OutputFile)
171 isInteractive = TRUE;
172
173 for (int i = n; i < argc;)
174 {
175 if (strcmp(argv[i], "-winhelp") == 0)
176 {
177 i ++;
178 convertMode = TEX_RTF;
179 winHelp = TRUE;
180 }
181 #ifndef NO_GUI
182 else if (strcmp(argv[i], "-interactive") == 0)
183 {
184 i ++;
185 isInteractive = TRUE;
186 }
187 #endif
188 else if (strcmp(argv[i], "-sync") == 0) // Don't yield
189 {
190 i ++;
191 isSync = TRUE;
192 }
193 else if (strcmp(argv[i], "-rtf") == 0)
194 {
195 i ++;
196 convertMode = TEX_RTF;
197 }
198 else if (strcmp(argv[i], "-html") == 0)
199 {
200 i ++;
201 convertMode = TEX_HTML;
202 }
203 else if (strcmp(argv[i], "-xlp") == 0)
204 {
205 i ++;
206 convertMode = TEX_XLP;
207 }
208 else if (strcmp(argv[i], "-twice") == 0)
209 {
210 i ++;
211 runTwice = TRUE;
212 }
213 else if (strcmp(argv[i], "-macros") == 0)
214 {
215 i ++;
216 if (i < argc)
217 {
218 MacroFile = copystring(argv[i]);
219 i ++;
220 }
221 }
222 else if (strcmp(argv[i], "-bufsize") == 0)
223 {
224 i ++;
225 if (i < argc)
226 {
227 BufSize = atoi(argv[i]);
228 i ++;
229 }
230 }
231 else if (strcmp(argv[i], "-charset") == 0)
232 {
233 i ++;
234 if (i < argc)
235 {
236 char *s = argv[i];
237 i ++;
238 if (strcmp(s, "ansi") == 0 || strcmp(s, "pc") == 0 || strcmp(s, "mac") == 0 ||
239 strcmp(s, "pca") == 0)
240 RTFCharset = copystring(s);
241 else
242 {
243 OnError("Incorrect argument for -charset");
244 return FALSE;
245 }
246 }
247 }
248 else
249 {
250 char buf[100];
251 sprintf(buf, "Invalid switch %s.\n", argv[i]);
252 OnError(buf);
253 i++;
254 #ifdef NO_GUI
255 ShowOptions();
256 exit(1);
257 #endif
258 return FALSE;
259 }
260 }
261
262 #if defined(__WXMSW__) && !defined(NO_GUI)
263 wxDDEInitialize();
264 Tex2RTFLastStatus[0] = 0; // DDE connection return value
265 TheTex2RTFServer = new Tex2RTFServer;
266 TheTex2RTFServer->Create("TEX2RTF");
267 #endif
268
269 #if defined(__WXMSW__) && defined(__WIN16__)
270 // Limit to max Windows array size
271 if (BufSize > 64) BufSize = 64;
272 #endif
273
274 TexInitialize(BufSize);
275 ResetContentsLevels(0);
276
277 #ifndef NO_GUI
278
279 if (isInteractive)
280 {
281 char buf[100];
282
283 // Create the main frame window
284 frame = new MyFrame(NULL, -1, "Tex2RTF", wxPoint(-1, -1), wxSize(400, 300));
285 frame->CreateStatusBar(2);
286
287 // Give it an icon
288 // TODO: uncomment this when we have tex2rtf.xpm
289 frame->SetIcon(wxICON(tex2rtf));
290
291 if (InputFile)
292 {
293 sprintf(buf, "Tex2RTF [%s]", FileNameFromPath(InputFile));
294 frame->SetTitle(buf);
295 }
296
297 // Make a menubar
298 wxMenu *file_menu = new wxMenu;
299 file_menu->Append(TEX_GO, "&Go", "Run converter");
300 file_menu->Append(TEX_SET_INPUT, "Set &Input File", "Set the LaTeX input file");
301 file_menu->Append(TEX_SET_OUTPUT, "Set &Output File", "Set the output file");
302 file_menu->AppendSeparator();
303 file_menu->Append(TEX_VIEW_LATEX, "View &LaTeX File", "View the LaTeX input file");
304 file_menu->Append(TEX_VIEW_OUTPUT, "View Output &File", "View output file");
305 file_menu->Append(TEX_SAVE_FILE, "&Save log file", "Save displayed text into file");
306 file_menu->AppendSeparator();
307 file_menu->Append(TEX_QUIT, "E&xit", "Exit Tex2RTF");
308
309 wxMenu *macro_menu = new wxMenu;
310
311 macro_menu->Append(TEX_LOAD_CUSTOM_MACROS, "&Load Custom Macros", "Load custom LaTeX macro file");
312 macro_menu->Append(TEX_VIEW_CUSTOM_MACROS, "View &Custom Macros", "View custom LaTeX macros");
313
314 wxMenu *mode_menu = new wxMenu;
315
316 mode_menu->Append(TEX_MODE_RTF, "Output linear &RTF", "Wordprocessor-compatible RTF");
317 mode_menu->Append(TEX_MODE_WINHELP, "Output &WinHelp RTF", "WinHelp-compatible RTF");
318 mode_menu->Append(TEX_MODE_HTML, "Output &HTML", "HTML World Wide Web hypertext file");
319 mode_menu->Append(TEX_MODE_XLP, "Output &XLP", "wxHelp hypertext help file");
320
321 wxMenu *help_menu = new wxMenu;
322
323 help_menu->Append(TEX_HELP, "&Help", "Tex2RTF Contents Page");
324 help_menu->Append(TEX_ABOUT, "&About Tex2RTF", "About Tex2RTF");
325
326 menuBar = new wxMenuBar;
327 menuBar->Append(file_menu, "&File");
328 menuBar->Append(macro_menu, "&Macros");
329 menuBar->Append(mode_menu, "&Conversion Mode");
330 menuBar->Append(help_menu, "&Help");
331
332 frame->SetMenuBar(menuBar);
333 frame->textWindow = new wxTextCtrl(frame, -1, "", wxPoint(-1, -1), wxSize(-1, -1), wxTE_READONLY|wxTE_MULTILINE);
334
335 (*frame->textWindow) << "Welcome to Julian Smart's LaTeX to RTF converter.\n";
336 // ShowOptions();
337
338 #if wxUSE_HELP
339 HelpInstance = new wxHelpController();
340 HelpInstance->Initialize("tex2rtf");
341 #endif // wxUSE_HELP
342
343 /*
344 * Read macro/initialisation file
345 *
346 */
347
348 wxString path;
349 if ((path = TexPathList.FindValidPath(MacroFile)) != "")
350 ReadCustomMacros((char*) (const char*) path);
351
352 strcpy(buf, "In ");
353
354 if (winHelp && (convertMode == TEX_RTF))
355 strcat(buf, "WinHelp RTF");
356 else if (!winHelp && (convertMode == TEX_RTF))
357 strcat(buf, "linear RTF");
358 else if (convertMode == TEX_HTML) strcat(buf, "HTML");
359 else if (convertMode == TEX_XLP) strcat(buf, "XLP");
360 strcat(buf, " mode.");
361 frame->SetStatusText(buf, 1);
362
363 frame->Show(TRUE);
364 return TRUE;
365 }
366 else
367 #endif // NO_GUI
368 {
369 /*
370 * Read macro/initialisation file
371 *
372 */
373
374 wxString path;
375 if ((path = TexPathList.FindValidPath(MacroFile)) != "")
376 ReadCustomMacros((char*) (const char*) path);
377
378 Go();
379 if (runTwice) Go();
380 #ifdef NO_GUI
381 return 0;
382 #else
383 return NULL;
384 #endif
385 }
386
387 #ifndef NO_GUI
388 // Return the main frame window
389 return TRUE;
390 #else
391 delete[] wxBuffer;
392 return FALSE;
393 #endif
394 }
395
396 #ifndef NO_GUI
397 int MyApp::OnExit()
398 {
399 wxNode *node = CustomMacroList.First();
400 while (node)
401 {
402 CustomMacro *macro = (CustomMacro *)node->Data();
403 delete macro;
404 delete node;
405 node = CustomMacroList.First();
406 }
407 MacroDefs.BeginFind();
408 node = MacroDefs.Next();
409 while (node)
410 {
411 TexMacroDef* def = (TexMacroDef*) node->Data();
412 delete def;
413 node = MacroDefs.Next();
414 }
415 MacroDefs.Clear();
416 #ifdef __WXMSW__
417 delete TheTex2RTFServer;
418 wxDDECleanUp();
419 #endif
420
421 #if wxUSE_HELP
422 delete HelpInstance;
423 #endif // wxUSE_HELP
424
425 // TODO: this simulates zero-memory leaks!
426 // Otherwise there are just too many...
427 #ifndef __WXGTK__
428 wxDebugContext::SetCheckpoint();
429 #endif
430
431 return 0;
432 }
433 #endif
434 void ShowOptions(void)
435 {
436 char buf[100];
437 sprintf(buf, "Tex2RTF version %.2f", versionNo);
438 OnInform(buf);
439 OnInform("Usage: tex2rtf [input] [output] [switches]\n");
440 OnInform("where valid switches are");
441 OnInform(" -interactive");
442 OnInform(" -bufsize <size in K>");
443 OnInform(" -charset <pc | pca | ansi | mac> (default ansi)");
444 OnInform(" -twice");
445 OnInform(" -sync");
446 OnInform(" -macros <filename>");
447 OnInform(" -winhelp");
448 OnInform(" -rtf");
449 OnInform(" -html");
450 OnInform(" -xlp\n");
451 }
452
453 #ifndef NO_GUI
454
455 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
456 EVT_CLOSE(MyFrame::OnCloseWindow)
457 EVT_MENU(TEX_QUIT, MyFrame::OnExit)
458 EVT_MENU(TEX_GO, MyFrame::OnGo)
459 EVT_MENU(TEX_SET_INPUT, MyFrame::OnSetInput)
460 EVT_MENU(TEX_SET_OUTPUT, MyFrame::OnSetOutput)
461 EVT_MENU(TEX_SAVE_FILE, MyFrame::OnSaveFile)
462 EVT_MENU(TEX_VIEW_LATEX, MyFrame::OnViewLatex)
463 EVT_MENU(TEX_VIEW_OUTPUT, MyFrame::OnViewOutput)
464 EVT_MENU(TEX_VIEW_CUSTOM_MACROS, MyFrame::OnShowMacros)
465 EVT_MENU(TEX_LOAD_CUSTOM_MACROS, MyFrame::OnLoadMacros)
466 EVT_MENU(TEX_MODE_RTF, MyFrame::OnModeRTF)
467 EVT_MENU(TEX_MODE_WINHELP, MyFrame::OnModeWinHelp)
468 EVT_MENU(TEX_MODE_HTML, MyFrame::OnModeHTML)
469 EVT_MENU(TEX_MODE_XLP, MyFrame::OnModeXLP)
470 EVT_MENU(TEX_HELP, MyFrame::OnHelp)
471 EVT_MENU(TEX_ABOUT, MyFrame::OnAbout)
472 END_EVENT_TABLE()
473
474 // My frame constructor
475 MyFrame::MyFrame(wxFrame *frame, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size):
476 wxFrame(frame, id, title, pos, size)
477 {}
478
479 void MyFrame::OnCloseWindow(wxCloseEvent& event)
480 {
481 if (!stopRunning && !OkToClose)
482 {
483 stopRunning = TRUE;
484 runTwice = FALSE;
485 return;
486 }
487 else if (OkToClose)
488 {
489 this->Destroy();
490 }
491 }
492
493 void MyFrame::OnExit(wxCommandEvent& event)
494 {
495 this->Destroy();
496 }
497
498 void MyFrame::OnGo(wxCommandEvent& event)
499 {
500 menuBar->EnableTop(0, FALSE);
501 menuBar->EnableTop(1, FALSE);
502 menuBar->EnableTop(2, FALSE);
503 menuBar->EnableTop(3, FALSE);
504 textWindow->Clear();
505 Tex2RTFYield(TRUE);
506 Go();
507
508 if (runTwice)
509 {
510 Tex2RTFYield(TRUE);
511 Go();
512 }
513 menuBar->EnableTop(0, TRUE);
514 menuBar->EnableTop(1, TRUE);
515 menuBar->EnableTop(2, TRUE);
516 menuBar->EnableTop(3, TRUE);
517 }
518
519 void MyFrame::OnSetInput(wxCommandEvent& event)
520 {
521 ChooseInputFile(TRUE);
522 }
523
524 void MyFrame::OnSetOutput(wxCommandEvent& event)
525 {
526 ChooseOutputFile(TRUE);
527 }
528
529 void MyFrame::OnSaveFile(wxCommandEvent& event)
530 {
531 wxString s = wxFileSelector("Save text to file", "", "", "txt", "*.txt");
532 if (s != "")
533 {
534 textWindow->SaveFile(s);
535 char buf[350];
536 sprintf(buf, "Saved text to %s", (const char*) s);
537 frame->SetStatusText(buf, 0);
538 }
539 }
540
541 void MyFrame::OnViewOutput(wxCommandEvent& event)
542 {
543 ChooseOutputFile();
544 if (OutputFile && wxFileExists(OutputFile))
545 {
546 textWindow->LoadFile(OutputFile);
547 char buf[300];
548 wxString str(wxFileNameFromPath(OutputFile));
549 sprintf(buf, "Tex2RTF [%s]", (const char*) str);
550 frame->SetTitle(buf);
551 }
552 }
553
554 void MyFrame::OnViewLatex(wxCommandEvent& event)
555 {
556 ChooseInputFile();
557 if (InputFile && wxFileExists(InputFile))
558 {
559 textWindow->LoadFile(InputFile);
560 char buf[300];
561 wxString str(wxFileNameFromPath(OutputFile));
562 sprintf(buf, "Tex2RTF [%s]", (const char*) str);
563 frame->SetTitle(buf);
564 }
565 }
566
567 void MyFrame::OnLoadMacros(wxCommandEvent& event)
568 {
569 textWindow->Clear();
570 wxString s = wxFileSelector("Choose custom macro file", wxPathOnly(MacroFile), wxFileNameFromPath(MacroFile), "ini", "*.ini");
571 if (s != "" && wxFileExists(s))
572 {
573 MacroFile = copystring(s);
574 ReadCustomMacros((char*) (const char*) s);
575 ShowCustomMacros();
576 }
577 }
578
579 void MyFrame::OnShowMacros(wxCommandEvent& event)
580 {
581 textWindow->Clear();
582 Tex2RTFYield(TRUE);
583 ShowCustomMacros();
584 }
585
586 void MyFrame::OnModeRTF(wxCommandEvent& event)
587 {
588 convertMode = TEX_RTF;
589 winHelp = FALSE;
590 InputFile = NULL;
591 OutputFile = NULL;
592 SetStatusText("In linear RTF mode.", 1);
593 }
594
595 void MyFrame::OnModeWinHelp(wxCommandEvent& event)
596 {
597 convertMode = TEX_RTF;
598 winHelp = TRUE;
599 InputFile = NULL;
600 OutputFile = NULL;
601 SetStatusText("In WinHelp RTF mode.", 1);
602 }
603
604 void MyFrame::OnModeHTML(wxCommandEvent& event)
605 {
606 convertMode = TEX_HTML;
607 winHelp = FALSE;
608 InputFile = NULL;
609 OutputFile = NULL;
610 SetStatusText("In HTML mode.", 1);
611 }
612
613 void MyFrame::OnModeXLP(wxCommandEvent& event)
614 {
615 convertMode = TEX_XLP;
616 InputFile = NULL;
617 OutputFile = NULL;
618 SetStatusText("In XLP mode.", 1);
619 }
620
621 void MyFrame::OnHelp(wxCommandEvent& event)
622 {
623 #if wxUSE_HELP
624 HelpInstance->LoadFile();
625 HelpInstance->DisplayContents();
626 #endif // wxUSE_HELP
627 }
628
629 void MyFrame::OnAbout(wxCommandEvent& event)
630 {
631 char buf[300];
632 #ifdef __WIN32__
633 char *platform = " (32-bit)";
634 #else
635 #ifdef __WXMSW__
636 char *platform = " (16-bit)";
637 #else
638 char *platform = "";
639 #endif
640 #endif
641 sprintf(buf, "Tex2RTF Version %.2f%s\nLaTeX to RTF, WinHelp, HTML and wxHelp Conversion\n\n(c) Julian Smart 1999", versionNo, platform);
642 wxMessageBox(buf, "About Tex2RTF");
643 }
644
645 void ChooseInputFile(bool force)
646 {
647 if (force || !InputFile)
648 {
649 wxString s = wxFileSelector("Choose LaTeX input file", wxPathOnly(InputFile), wxFileNameFromPath(InputFile), "tex", "*.tex");
650 if (s != "")
651 {
652 // Different file, so clear index entries.
653 ClearKeyWordTable();
654 ResetContentsLevels(0);
655 passNumber = 1;
656 char buf[300];
657 InputFile = copystring(s);
658 wxString str = wxFileNameFromPath(InputFile);
659 sprintf(buf, "Tex2RTF [%s]", (const char*) str);
660 frame->SetTitle(buf);
661 OutputFile = NULL;
662 }
663 }
664 }
665
666 void ChooseOutputFile(bool force)
667 {
668 char extensionBuf[10];
669 char wildBuf[10];
670 strcpy(wildBuf, "*.");
671 wxString path;
672 if (OutputFile)
673 path = wxPathOnly(OutputFile);
674 else if (InputFile)
675 path = wxPathOnly(InputFile);
676
677 switch (convertMode)
678 {
679 case TEX_RTF:
680 {
681 strcpy(extensionBuf, "rtf");
682 strcat(wildBuf, "rtf");
683 break;
684 }
685 case TEX_XLP:
686 {
687 strcpy(extensionBuf, "xlp");
688 strcat(wildBuf, "xlp");
689 break;
690 }
691 case TEX_HTML:
692 {
693 #if defined(__WXMSW__) && defined(__WIN16__)
694 strcpy(extensionBuf, "htm");
695 strcat(wildBuf, "htm");
696 #else
697 strcpy(extensionBuf, "html");
698 strcat(wildBuf, "html");
699 #endif
700 break;
701 }
702 }
703 if (force || !OutputFile)
704 {
705 wxString s = wxFileSelector("Choose output file", path, wxFileNameFromPath(OutputFile),
706 extensionBuf, wildBuf);
707 if (s != "")
708 OutputFile = copystring(s);
709 }
710 }
711 #endif
712
713 bool Go(void)
714 {
715 #ifndef NO_GUI
716 ChooseInputFile();
717 ChooseOutputFile();
718 #endif
719
720 if (!InputFile || !OutputFile)
721 return FALSE;
722
723 #ifndef NO_GUI
724 if (isInteractive)
725 {
726 char buf[300];
727 wxString str = wxFileNameFromPath(InputFile);
728
729 sprintf(buf, "Tex2RTF [%s]", (const char*) str);
730 frame->SetTitle(buf);
731 }
732
733 wxStartTimer();
734 #endif
735
736 // Find extension-less filename
737 strcpy(FileRoot, OutputFile);
738 StripExtension(FileRoot);
739
740 if (truncateFilenames && convertMode == TEX_HTML)
741 {
742 // Truncate to five characters. This ensures that
743 // we can generate DOS filenames such as thing999. But 1000 files
744 // may not be enough, of course...
745 char* sName = wxFileNameFromPath( FileRoot); // this Julian's method is non-destructive reference
746
747 if(sName)
748 if(strlen( sName) > 5)
749 sName[5] = '\0'; // that should do!
750 }
751
752 sprintf(ContentsName, "%s.con", FileRoot);
753 sprintf(TmpContentsName, "%s.cn1", FileRoot);
754 sprintf(TmpFrameContentsName, "%s.frc", FileRoot);
755 sprintf(WinHelpContentsFileName, "%s.cnt", FileRoot);
756 sprintf(RefName, "%s.ref", FileRoot);
757
758 TexPathList.EnsureFileAccessible(InputFile);
759 if (!bulletFile)
760 {
761 wxString s = TexPathList.FindValidPath("bullet.bmp");
762 if (s != "")
763 {
764 wxString str = wxFileNameFromPath(s);
765 bulletFile = copystring(str);
766 }
767 }
768
769 if (wxFileExists(RefName))
770 ReadTexReferences(RefName);
771
772 bool success = FALSE;
773
774 if (InputFile && OutputFile)
775 {
776 if (!FileExists(InputFile))
777 {
778 OnError("Cannot open input file!");
779 TexCleanUp();
780 return FALSE;
781 }
782 #ifndef NO_GUI
783 if (isInteractive)
784 {
785 char buf[50];
786 sprintf(buf, "Working, pass %d...", passNumber);
787 frame->SetStatusText(buf);
788 }
789 #endif
790 OkToClose = FALSE;
791 OnInform("Reading LaTeX file...");
792 TexLoadFile(InputFile);
793
794 switch (convertMode)
795 {
796 case TEX_RTF:
797 {
798 success = RTFGo();
799 break;
800 }
801 case TEX_XLP:
802 {
803 success = XLPGo();
804 break;
805 }
806 case TEX_HTML:
807 {
808 success = HTMLGo();
809 break;
810 }
811 }
812 }
813 if (stopRunning)
814 {
815 OnInform("*** Aborted by user.");
816 success = FALSE;
817 stopRunning = FALSE;
818 }
819
820 if (success)
821 {
822 WriteTexReferences(RefName);
823 TexCleanUp();
824 startedSections = FALSE;
825
826 char buf[100];
827 #ifndef NO_GUI
828 long tim = wxGetElapsedTime();
829 sprintf(buf, "Finished in %ld seconds.", (long)(tim/1000.0));
830 OnInform(buf);
831 if (isInteractive)
832 {
833 sprintf(buf, "Done, %d %s.", passNumber, (passNumber > 1) ? "passes" : "pass");
834 frame->SetStatusText(buf);
835 }
836 #else
837 sprintf(buf, "Done, %d %s.", passNumber, (passNumber > 1) ? "passes" : "pass");
838 OnInform(buf);
839 #endif
840 passNumber ++;
841 OkToClose = TRUE;
842 return TRUE;
843 }
844
845 TexCleanUp();
846 startedSections = FALSE;
847
848 OnInform("Sorry, unsuccessful.");
849 OkToClose = TRUE;
850 return FALSE;
851 }
852
853 void OnError(char *msg)
854 {
855 #ifdef NO_GUI
856 cerr << "Error: " << msg << "\n";
857 cerr.flush();
858 #else
859 if (isInteractive && frame)
860 (*frame->textWindow) << "Error: " << msg << "\n";
861 else
862 #ifdef __UNIX__
863 {
864 cerr << "Error: " << msg << "\n";
865 cerr.flush();
866 }
867 #endif
868 #ifdef __WXMSW__
869 wxError(msg);
870 #endif
871 Tex2RTFYield(TRUE);
872 #endif // NO_GUI
873 }
874
875 void OnInform(char *msg)
876 {
877 #ifdef NO_GUI
878 cout << msg << "\n";
879 cout.flush();
880 #else
881 if (isInteractive && frame)
882 (*frame->textWindow) << msg << "\n";
883 else
884 #ifdef __WXMSW__
885 {
886 cout << msg << "\n";
887 cout.flush();
888 }
889 #endif
890 #ifdef __WXMSW__
891 {}
892 #endif
893 if (isInteractive)
894 {
895 Tex2RTFYield(TRUE);
896 }
897 #endif // NO_GUI
898 }
899
900 void OnMacro(int macroId, int no_args, bool start)
901 {
902 switch (convertMode)
903 {
904 case TEX_RTF:
905 {
906 RTFOnMacro(macroId, no_args, start);
907 break;
908 }
909 case TEX_XLP:
910 {
911 XLPOnMacro(macroId, no_args, start);
912 break;
913 }
914 case TEX_HTML:
915 {
916 HTMLOnMacro(macroId, no_args, start);
917 break;
918 }
919 }
920 }
921
922 bool OnArgument(int macroId, int arg_no, bool start)
923 {
924 switch (convertMode)
925 {
926 case TEX_RTF:
927 {
928 return RTFOnArgument(macroId, arg_no, start);
929 break;
930 }
931 case TEX_XLP:
932 {
933 return XLPOnArgument(macroId, arg_no, start);
934 break;
935 }
936 case TEX_HTML:
937 {
938 return HTMLOnArgument(macroId, arg_no, start);
939 break;
940 }
941 }
942 return TRUE;
943 }
944
945 /*
946 * DDE Stuff
947 */
948 #if defined(__WXMSW__) && !defined(NO_GUI)
949
950 /*
951 * Server
952 */
953
954 wxConnectionBase *Tex2RTFServer::OnAcceptConnection(const wxString& topic)
955 {
956 if (topic == "TEX2RTF")
957 {
958 if (!ipc_buffer)
959 ipc_buffer = new char[1000];
960
961 return new Tex2RTFConnection(ipc_buffer, 4000);
962 }
963 else
964 return NULL;
965 }
966
967 /*
968 * Connection
969 */
970
971 Tex2RTFConnection::Tex2RTFConnection(char *buf, int size):wxDDEConnection(buf, size)
972 {
973 }
974
975 Tex2RTFConnection::~Tex2RTFConnection(void)
976 {
977 }
978
979 bool SplitCommand(char *data, char *firstArg, char *secondArg)
980 {
981 firstArg[0] = 0;
982 secondArg[0] = 0;
983 int i = 0;
984 int len = strlen(data);
985 bool stop = FALSE;
986 // Find first argument (command name)
987 while (!stop)
988 {
989 if (data[i] == ' ' || data[i] == 0)
990 stop = TRUE;
991 else
992 {
993 firstArg[i] = data[i];
994 i ++;
995 }
996 }
997 firstArg[i] = 0;
998 if (data[i] == ' ')
999 {
1000 // Find second argument
1001 i ++;
1002 int j = 0;
1003 while (data[i] != 0)
1004 {
1005 secondArg[j] = data[i];
1006 i ++;
1007 j ++;
1008 }
1009 secondArg[j] = 0;
1010 }
1011 return TRUE;
1012 }
1013
1014 bool Tex2RTFConnection::OnExecute(const wxString& topic, char *data, int size, int format)
1015 {
1016 strcpy(Tex2RTFLastStatus, "OK");
1017
1018 char firstArg[50];
1019 char secondArg[300];
1020 if (SplitCommand(data, firstArg, secondArg))
1021 {
1022 bool hasArg = (strlen(secondArg) > 0);
1023 if (strcmp(firstArg, "INPUT") == 0 && hasArg)
1024 {
1025 if (InputFile) delete[] InputFile;
1026 InputFile = copystring(secondArg);
1027 if (frame)
1028 {
1029 char buf[100];
1030 wxString str = wxFileNameFromPath(InputFile);
1031 sprintf(buf, "Tex2RTF [%s]", (const char*) str);
1032 frame->SetTitle(buf);
1033 }
1034 }
1035 else if (strcmp(firstArg, "OUTPUT") == 0 && hasArg)
1036 {
1037 if (OutputFile) delete[] OutputFile;
1038 OutputFile = copystring(secondArg);
1039 }
1040 else if (strcmp(firstArg, "GO") == 0)
1041 {
1042 strcpy(Tex2RTFLastStatus, "WORKING");
1043 if (!Go())
1044 strcpy(Tex2RTFLastStatus, "CONVERSION ERROR");
1045 else
1046 strcpy(Tex2RTFLastStatus, "OK");
1047 }
1048 else if (strcmp(firstArg, "EXIT") == 0)
1049 {
1050 if (frame) frame->Close();
1051 }
1052 else if (strcmp(firstArg, "MINIMIZE") == 0 || strcmp(firstArg, "ICONIZE") == 0)
1053 {
1054 if (frame)
1055 frame->Iconize(TRUE);
1056 }
1057 else if (strcmp(firstArg, "SHOW") == 0 || strcmp(firstArg, "RESTORE") == 0)
1058 {
1059 if (frame)
1060 {
1061 frame->Iconize(FALSE);
1062 frame->Show(TRUE);
1063 }
1064 }
1065 else
1066 {
1067 // Try for a setting
1068 strcpy(Tex2RTFLastStatus, RegisterSetting(firstArg, secondArg, FALSE));
1069 #ifndef NO_GUI
1070 if (frame && strcmp(firstArg, "conversionMode") == 0)
1071 {
1072 char buf[100];
1073 strcpy(buf, "In ");
1074
1075 if (winHelp && (convertMode == TEX_RTF))
1076 strcat(buf, "WinHelp RTF");
1077 else if (!winHelp && (convertMode == TEX_RTF))
1078 strcat(buf, "linear RTF");
1079 else if (convertMode == TEX_HTML) strcat(buf, "HTML");
1080 else if (convertMode == TEX_XLP) strcat(buf, "XLP");
1081 strcat(buf, " mode.");
1082 frame->SetStatusText(buf, 1);
1083 }
1084 #endif
1085 }
1086 }
1087 return TRUE;
1088 }
1089
1090 char *Tex2RTFConnection::OnRequest(const wxString& topic, const wxString& item, int *size, int format)
1091 {
1092 return Tex2RTFLastStatus;
1093 }
1094
1095 #endif
1096
1097 #ifndef NO_GUI
1098 #ifndef __WXGTK__
1099 //void wxObject::Dump(ostream& str)
1100 //{
1101 // if (GetClassInfo() && GetClassInfo()->GetClassName())
1102 // str << GetClassInfo()->GetClassName();
1103 // else
1104 // str << "unknown object class";
1105 //}
1106 #endif
1107 #endif