]> git.saurik.com Git - wxWidgets.git/blobdiff - utils/tex2rtf/src/rtfutils.cpp
check parameters after options check sothat usign -version doesn't result in an error
[wxWidgets.git] / utils / tex2rtf / src / rtfutils.cpp
index bc033398274cba6ea3e4ba495c8c9ac2fa91270a..6dc426cdba6b2b9fbf70ec44cedab80abfa04cb6 100644 (file)
 // Licence:     wxWindows licence
 /////////////////////////////////////////////////////////////////////////////
 
-#ifdef __GNUG__
-#pragma implementation
-#endif
-
 // For compilers that support precompilation, includes "wx.h".
 #include "wx/wxprec.h"
 
@@ -240,6 +236,7 @@ void WriteWinHelpContentsFileLine(wxChar *topicName, wxChar *xitle, int level)
   wxChar title[255];
   int s=0;
   int d=0;
+  // assuming iso-8859-1 here even in Unicode build (FIXME?)
   while ( (xitle[s]!=0)&&(d<255) )
   {
     wxChar ch=wxChar(xitle[s]&0xff);
@@ -248,12 +245,12 @@ void WriteWinHelpContentsFileLine(wxChar *topicName, wxChar *xitle, int level)
       wxChar ch2=wxChar(xitle[s+2]&0xff);
       wxChar ch3=wxChar(xitle[s+3]&0xff);
       s+=4; // next character
-      if ((ch1==0x27)&&(ch2==0x66)&&(ch3==0x36)) { title[d++]=_T('ö');  }
-      if ((ch1==0x27)&&(ch2==0x65)&&(ch3==0x34)) { title[d++]=_T('ä');  }
-      if ((ch1==0x27)&&(ch2==0x66)&&(ch3==0x63)) { title[d++]=_T('ü');  }
-      if ((ch1==0x27)&&(ch2==0x64)&&(ch3==0x36)) { title[d++]=_T('Ö');  }
-      if ((ch1==0x27)&&(ch2==0x63)&&(ch3==0x34)) { title[d++]=_T('Ä');  }
-      if ((ch1==0x27)&&(ch2==0x64)&&(ch3==0x63)) { title[d++]=_T('Ü');  }
+      if ((ch1==0x27)&&(ch2==0x66)&&(ch3==0x36)) { title[d++]=wxChar('ö');  }
+      if ((ch1==0x27)&&(ch2==0x65)&&(ch3==0x34)) { title[d++]=wxChar('ä');  }
+      if ((ch1==0x27)&&(ch2==0x66)&&(ch3==0x63)) { title[d++]=wxChar('ü');  }
+      if ((ch1==0x27)&&(ch2==0x64)&&(ch3==0x36)) { title[d++]=wxChar('Ö');  }
+      if ((ch1==0x27)&&(ch2==0x63)&&(ch3==0x34)) { title[d++]=wxChar('Ä');  }
+      if ((ch1==0x27)&&(ch2==0x64)&&(ch3==0x63)) { title[d++]=wxChar('Ü');  }
     } else {
       title[d++]=ch;
       s++;
@@ -520,65 +517,65 @@ void OutputNumberStyle(wxChar *numberStyle)
  * Write a Windows help project file
  */
 
-bool WriteHPJ(wxChar *filename)
+bool WriteHPJ(const wxString& filename)
 {
-  wxChar hpjFilename[256];
-  wxChar helpFile[50];
-  wxChar rtfFile[50];
-  wxStrcpy(hpjFilename, filename);
-  StripExtension(hpjFilename);
-  wxStrcat(hpjFilename, _T(".hpj"));
-
-  wxStrcpy(helpFile, wxFileNameFromPath(filename));
-  StripExtension(helpFile);
-  wxStrcpy(rtfFile, helpFile);
-  wxStrcat(helpFile, _T(".hlp"));
-  wxStrcat(rtfFile, _T(".rtf"));
-
-  FILE *fd = wxFopen(hpjFilename, _T("w"));
-  if (!fd)
-    return false;
-
-  wxChar *helpTitle = winHelpTitle;
-  if (!helpTitle)
-    helpTitle = _T("Untitled");
+    wxChar hpjFilename[256];
+    wxChar helpFile[50];
+    wxChar rtfFile[50];
+    wxStrcpy(hpjFilename, filename);
+    StripExtension(hpjFilename);
+    wxStrcat(hpjFilename, _T(".hpj"));
+
+    wxStrcpy(helpFile, wxFileNameFromPath(filename));
+    StripExtension(helpFile);
+    wxStrcpy(rtfFile, helpFile);
+    wxStrcat(helpFile, _T(".hlp"));
+    wxStrcat(rtfFile, _T(".rtf"));
+
+    FILE *fd = wxFopen(hpjFilename, _T("w"));
+    if (!fd)
+        return false;
 
-  wxString thePath = wxPathOnly(InputFile);
-  if (thePath.IsEmpty())
-    thePath = _T(".");
-  wxFprintf(fd, _T("[OPTIONS]\n"));
-  wxFprintf(fd, _T("BMROOT=%s ; Assume that bitmaps are where the source is\n"), thePath.c_str());
-  wxFprintf(fd, _T("TITLE=%s\n"), helpTitle);
-  wxFprintf(fd, _T("CONTENTS=Contents\n"));
+    wxChar *helpTitle = winHelpTitle;
+    if (!helpTitle)
+        helpTitle = _T("Untitled");
 
-  if (winHelpVersion > 3)
-  {
-    wxFprintf(fd, _T("; COMPRESS=12 Hall Zeck ; Max compression, but needs lots of memory\n"));
-    wxFprintf(fd, _T("COMPRESS=8 Zeck\n"));
-    wxFprintf(fd, _T("LCID=0x809 0x0 0x0 ;English (British)\n"));
-    wxFprintf(fd, _T("HLP=.\\%s.hlp\n"), wxFileNameFromPath(FileRoot));
-  }
-  else
-  {
-    wxFprintf(fd, _T("COMPRESS=HIGH\n"));
-  }
-  wxFprintf(fd, _T("\n"));
+    wxString thePath = wxPathOnly(InputFile);
+    if (thePath.empty())
+        thePath = _T(".");
+    wxFprintf(fd, _T("[OPTIONS]\n"));
+    wxFprintf(fd, _T("BMROOT=%s ; Assume that bitmaps are where the source is\n"), thePath.c_str());
+    wxFprintf(fd, _T("TITLE=%s\n"), helpTitle);
+    wxFprintf(fd, _T("CONTENTS=Contents\n"));
 
-  if (winHelpVersion > 3)
-  {
-    wxFprintf(fd, _T("[WINDOWS]\n"));
-    wxFprintf(fd, _T("Main=\"\",(553,102,400,600),20736,(r14876671),(r12632256),f3\n"));
+    if (winHelpVersion > 3)
+    {
+        wxFprintf(fd, _T("; COMPRESS=12 Hall Zeck ; Max compression, but needs lots of memory\n"));
+        wxFprintf(fd, _T("COMPRESS=8 Zeck\n"));
+        wxFprintf(fd, _T("LCID=0x809 0x0 0x0 ;English (British)\n"));
+        wxFprintf(fd, _T("HLP=.\\%s.hlp\n"), wxFileNameFromPath(FileRoot));
+    }
+    else
+    {
+        wxFprintf(fd, _T("COMPRESS=HIGH\n"));
+    }
     wxFprintf(fd, _T("\n"));
-  }
 
-  wxFprintf(fd, _T("[FILES]\n%s\n\n"), rtfFile);
-  wxFprintf(fd, _T("[CONFIG]\n"));
-  if (useUpButton)
-    wxFprintf(fd, _T("CreateButton(\"Up\", \"&Up\", \"JumpId(`%s', `Contents')\")\n"), helpFile);
-  wxFprintf(fd, _T("BrowseButtons()\n\n"));
-  wxFprintf(fd, _T("[MAP]\n\n[BITMAPS]\n\n"));
-  fclose(fd);
-  return true;
+    if (winHelpVersion > 3)
+    {
+        wxFprintf(fd, _T("[WINDOWS]\n"));
+        wxFprintf(fd, _T("Main=\"\",(553,102,400,600),20736,(r14876671),(r12632256),f3\n"));
+        wxFprintf(fd, _T("\n"));
+    }
+
+    wxFprintf(fd, _T("[FILES]\n%s\n\n"), rtfFile);
+    wxFprintf(fd, _T("[CONFIG]\n"));
+    if (useUpButton)
+        wxFprintf(fd, _T("CreateButton(\"Up\", \"&Up\", \"JumpId(`%s', `Contents')\")\n"), helpFile);
+    wxFprintf(fd, _T("BrowseButtons()\n\n"));
+    wxFprintf(fd, _T("[MAP]\n\n[BITMAPS]\n\n"));
+    fclose(fd);
+    return true;
 }
 
 
@@ -670,7 +667,7 @@ void ProcessText2RTF(TexChunk *chunk)
       i += 1;
       changed = true;
     }
-    else if (inVerbatim && (ch == '{' || ch == '}')) // Escape the curley bracket
+    else if (inVerbatim && (ch == '{' || ch == '}')) // Escape the curly bracket
     {
       BigBuffer[ptr] = '\\'; ptr ++;
       BigBuffer[ptr] = ch; ptr ++;
@@ -5149,200 +5146,197 @@ bool RTFOnArgument(int macroId, int arg_no, bool start)
 
 bool RTFGo(void)
 {
-  if (stopRunning)
-      return false;
+    if (stopRunning)
+        return false;
 
-  // Reset variables
-  indentLevel = 0;
-  forbidParindent = 0;
-  contentsLineSection = NULL;
-  contentsLineValue = NULL;
-  descriptionItemArg = NULL;
-  inTabular = false;
-  inTable = false;
-  inFigure = false;
-  startRows = false;
-  tableVerticalLineLeft = false;
-  tableVerticalLineRight = false;
-  noColumns = 0;
-  startedSections = false;
-  inVerbatim = false;
-  browseId = 0;
-
-  if (InputFile && OutputFile)
-  {
-    // Do some RTF-specific transformations on all the strings,
-    // recursively
-    Text2RTF(GetTopLevelChunk());
-
-    Contents = wxFopen(TmpContentsName, _T("w"));
-    Chapters = wxFopen(_T("chapters.rtf"), _T("w"));
-    if (winHelp)
-    {
-      Sections = wxFopen(_T("sections.rtf"), _T("w"));
-      Subsections = wxFopen(_T("subsections.rtf"), _T("w"));
-      Subsubsections = wxFopen(_T("subsubsections.rtf"), _T("w"));
-      Popups = wxFopen(_T("popups.rtf"), _T("w"));
-      if (winHelpContents)
-      {
-        WinHelpContentsFile = wxFopen(WinHelpContentsFileName, _T("w"));
-        if (WinHelpContentsFile)
-          wxFprintf(WinHelpContentsFile, _T(":Base %s.hlp\n"), wxFileNameFromPath(FileRoot));
-      }
+    // Reset variables
+    indentLevel = 0;
+    forbidParindent = 0;
+    contentsLineSection = NULL;
+    contentsLineValue = NULL;
+    descriptionItemArg = NULL;
+    inTabular = false;
+    inTable = false;
+    inFigure = false;
+    startRows = false;
+    tableVerticalLineLeft = false;
+    tableVerticalLineRight = false;
+    noColumns = 0;
+    startedSections = false;
+    inVerbatim = false;
+    browseId = 0;
+
+    if (!InputFile.empty() && !OutputFile.empty())
+    {
+        // Do some RTF-specific transformations on all the strings,
+        // recursively
+        Text2RTF(GetTopLevelChunk());
+
+        Contents = wxFopen(TmpContentsName, _T("w"));
+        Chapters = wxFopen(_T("chapters.rtf"), _T("w"));
+        if (winHelp)
+        {
+            Sections = wxFopen(_T("sections.rtf"), _T("w"));
+            Subsections = wxFopen(_T("subsections.rtf"), _T("w"));
+            Subsubsections = wxFopen(_T("subsubsections.rtf"), _T("w"));
+            Popups = wxFopen(_T("popups.rtf"), _T("w"));
+            if (winHelpContents)
+            {
+                WinHelpContentsFile = wxFopen(WinHelpContentsFileName, _T("w"));
+                if (WinHelpContentsFile)
+                    wxFprintf(WinHelpContentsFile, _T(":Base %s.hlp\n"), wxFileNameFromPath(FileRoot));
+            }
 
-      if (!Sections || !Subsections || !Subsubsections || !Popups || (winHelpContents && !WinHelpContentsFile))
-      {
-        OnError(_T("Ouch! Could not open temporary file(s) for writing."));
-        return false;
-      }
-    }
-    if (!Contents || !Chapters)
-    {
-      OnError(_T("Ouch! Could not open temporary file(s) for writing."));
-      return false;
-    }
+            if (!Sections || !Subsections || !Subsubsections || !Popups || (winHelpContents && !WinHelpContentsFile))
+            {
+                OnError(_T("Ouch! Could not open temporary file(s) for writing."));
+                return false;
+            }
+        }
+        if (!Contents || !Chapters)
+        {
+            OnError(_T("Ouch! Could not open temporary file(s) for writing."));
+            return false;
+        }
 
-    if (winHelp)
-    {
-      wxFprintf(Chapters, _T("\n#{\\footnote Contents}\n"));
-      wxFprintf(Chapters, _T("${\\footnote Contents}\n"));
-      wxFprintf(Chapters, _T("+{\\footnote %s}\n"), GetBrowseString());
-      wxFprintf(Chapters, _T("K{\\footnote {K} %s}\n"), ContentsNameString);
-      wxFprintf(Chapters, _T("!{\\footnote DisableButton(\"Up\")}\n"));
-    }
-    if (!winHelp)
-    {
-      wxFprintf(Chapters, _T("\\titlepg\n"));
-      wxFprintf(Contents, _T("\\par\\pard\\pgnrestart\\sect\\titlepg"));
-    }
+        if (winHelp)
+        {
+            wxFprintf(Chapters, _T("\n#{\\footnote Contents}\n"));
+            wxFprintf(Chapters, _T("${\\footnote Contents}\n"));
+            wxFprintf(Chapters, _T("+{\\footnote %s}\n"), GetBrowseString());
+            wxFprintf(Chapters, _T("K{\\footnote {K} %s}\n"), ContentsNameString);
+            wxFprintf(Chapters, _T("!{\\footnote DisableButton(\"Up\")}\n"));
+        }
+        if (!winHelp)
+        {
+            wxFprintf(Chapters, _T("\\titlepg\n"));
+            wxFprintf(Contents, _T("\\par\\pard\\pgnrestart\\sect\\titlepg"));
+        }
 
-    // In WinHelp, Contents title takes font of title.
-    // In linear RTF, same as chapter headings.
-    wxFprintf(Contents, _T("{\\b\\fs%d %s}\\par\\par\\pard\n\n"),
-      (winHelp ? titleFont : chapterFont)*2, ContentsNameString);
+        // In WinHelp, Contents title takes font of title.
+        // In linear RTF, same as chapter headings.
+        wxFprintf(Contents, _T("{\\b\\fs%d %s}\\par\\par\\pard\n\n"),
+            (winHelp ? titleFont : chapterFont)*2, ContentsNameString);
 
-    // By default, Swiss, 11 point.
-    wxFprintf(Chapters, _T("\\f2\\fs22\n"));
+        // By default, Swiss, 11 point.
+        wxFprintf(Chapters, _T("\\f2\\fs22\n"));
 
-    PushEnvironmentStyle(_T("\\f2\\fs22\\sa200"));
+        PushEnvironmentStyle(_T("\\f2\\fs22\\sa200"));
 
-    SetCurrentOutput(Chapters);
+        SetCurrentOutput(Chapters);
 
-    if (stopRunning)
-        return false;
+        if (stopRunning)
+            return false;
 
-    OnInform(_T("Converting..."));
+        OnInform(_T("Converting..."));
 
-    TraverseDocument();
+        TraverseDocument();
 
-    FILE *Header = wxFopen(_T("header.rtf"), _T("w"));
-    if (!Header)
-    {
-      OnError(_T("Ouch! Could not open temporary file header.rtf for writing."));
-      return false;
-    }
-    WriteRTFHeader(Header);
-    fclose(Header);
+        FILE *Header = wxFopen(_T("header.rtf"), _T("w"));
+        if (!Header)
+        {
+            OnError(_T("Ouch! Could not open temporary file header.rtf for writing."));
+            return false;
+        }
+        WriteRTFHeader(Header);
+        fclose(Header);
 
-    PopEnvironmentStyle();
+        PopEnvironmentStyle();
 
-    Tex2RTFYield(true);
-    if (winHelp)
-    {
-//      wxFprintf(Contents, _T("\\page\n"));
-      wxFprintf(Chapters, _T("\\page\n"));
-      wxFprintf(Sections, _T("\\page\n"));
-      wxFprintf(Subsections, _T("\\page\n"));
-      wxFprintf(Subsubsections, _T("\\page\n\n"));
-      wxFprintf(Popups, _T("\\page\n}\n"));
-    }
+        Tex2RTFYield(true);
+        if (winHelp)
+        {
+//          wxFprintf(Contents, _T("\\page\n"));
+            wxFprintf(Chapters, _T("\\page\n"));
+            wxFprintf(Sections, _T("\\page\n"));
+            wxFprintf(Subsections, _T("\\page\n"));
+            wxFprintf(Subsubsections, _T("\\page\n\n"));
+            wxFprintf(Popups, _T("\\page\n}\n"));
+        }
 
-//    TexOutput(_T("\n\\info{\\doccomm Document created by Julian Smart's Tex2RTF.}\n"));
-    if (!winHelp)
-      TexOutput(_T("}\n"));
-    fclose(Contents); Contents = NULL;
-    fclose(Chapters); Chapters = NULL;
-    if (winHelp)
-    {
-      fclose(Sections); Sections = NULL;
-      fclose(Subsections); Subsections = NULL;
-      fclose(Subsubsections); Subsubsections = NULL;
-      fclose(Popups); Popups = NULL;
-      if (winHelpContents)
-      {
-        fclose(WinHelpContentsFile); WinHelpContentsFile = NULL;
-      }
-    }
+//      TexOutput(_T("\n\\info{\\doccomm Document created by Julian Smart's Tex2RTF.}\n"));
+        if (!winHelp)
+            TexOutput(_T("}\n"));
+        fclose(Contents); Contents = NULL;
+        fclose(Chapters); Chapters = NULL;
+        if (winHelp)
+        {
+            fclose(Sections); Sections = NULL;
+            fclose(Subsections); Subsections = NULL;
+            fclose(Subsubsections); Subsubsections = NULL;
+            fclose(Popups); Popups = NULL;
+            if (winHelpContents)
+            {
+                fclose(WinHelpContentsFile); WinHelpContentsFile = NULL;
+            }
+        }
 
-    if (winHelp)
-    {
-      wxConcatFiles(_T("header.rtf"), _T("chapters.rtf"), _T("tmp1.rtf"));
-      Tex2RTFYield(true);
-      wxConcatFiles(_T("tmp1.rtf"), _T("sections.rtf"), _T("tmp2.rtf"));
-      Tex2RTFYield(true);
-      wxConcatFiles(_T("tmp2.rtf"), _T("subsections.rtf"), _T("tmp3.rtf"));
-      Tex2RTFYield(true);
-      wxConcatFiles(_T("tmp3.rtf"), _T("subsubsections.rtf"), _T("tmp4.rtf"));
-      Tex2RTFYield(true);
-      wxConcatFiles(_T("tmp4.rtf"), _T("popups.rtf"), OutputFile);
-      Tex2RTFYield(true);
-
-      wxRemoveFile(_T("tmp1.rtf"));
-      wxRemoveFile(_T("tmp2.rtf"));
-      wxRemoveFile(_T("tmp3.rtf"));
-      wxRemoveFile(_T("tmp4.rtf"));
-    }
-    else
-    {
-      wxConcatFiles(_T("header.rtf"), _T("chapters.rtf"), _T("tmp1.rtf"));
-      Tex2RTFYield(true);
-      if (wxFileExists(OutputFile))
-          wxRemoveFile(OutputFile);
+        if (winHelp)
+        {
+            wxConcatFiles(_T("header.rtf"), _T("chapters.rtf"), _T("tmp1.rtf"));
+            Tex2RTFYield(true);
+            wxConcatFiles(_T("tmp1.rtf"), _T("sections.rtf"), _T("tmp2.rtf"));
+            Tex2RTFYield(true);
+            wxConcatFiles(_T("tmp2.rtf"), _T("subsections.rtf"), _T("tmp3.rtf"));
+            Tex2RTFYield(true);
+            wxConcatFiles(_T("tmp3.rtf"), _T("subsubsections.rtf"), _T("tmp4.rtf"));
+            Tex2RTFYield(true);
+            wxConcatFiles(_T("tmp4.rtf"), _T("popups.rtf"), OutputFile);
+            Tex2RTFYield(true);
+
+            wxRemoveFile(_T("tmp1.rtf"));
+            wxRemoveFile(_T("tmp2.rtf"));
+            wxRemoveFile(_T("tmp3.rtf"));
+            wxRemoveFile(_T("tmp4.rtf"));
+        }
+        else
+        {
+            wxConcatFiles(_T("header.rtf"), _T("chapters.rtf"), _T("tmp1.rtf"));
+            Tex2RTFYield(true);
+            if (wxFileExists(OutputFile))
+                wxRemoveFile(OutputFile);
 
-      wxChar *cwdStr;
-      cwdStr = wxGetWorkingDirectory();
+            wxString cwdStr = wxGetCwd();
 
-      wxString outputDirStr;
-      outputDirStr = wxPathOnly(OutputFile);
+            wxString outputDirStr = wxPathOnly(OutputFile);
 
-      // Determine if the temp file and the output file are in the same directory,
-      // and if they are, then just rename the temp file rather than copying
-      // it, as this is much faster when working with large (multi-megabyte files)
-      if ((wxStrcmp(outputDirStr.c_str(),_T("")) == 0) ||  // no path specified on output file
-          (wxStrcmp(cwdStr,outputDirStr.c_str()) == 0)) // paths do not match
-      {
-        wxRenameFile(_T("tmp1.rtf"), OutputFile);
-      }
-      else
-      {
-        wxCopyFile(_T("tmp1.rtf"), OutputFile);
-      }
-      delete [] cwdStr;
-      Tex2RTFYield(true);
-      wxRemoveFile(_T("tmp1.rtf"));
-    }
+            // Determine if the temp file and the output file are in the same directory,
+            // and if they are, then just rename the temp file rather than copying
+            // it, as this is much faster when working with large (multi-megabyte files)
+            if ((outputDirStr.empty()) ||  // no path specified on output file
+                (cwdStr != outputDirStr)) // paths do not match
+            {
+                wxRenameFile(_T("tmp1.rtf"), OutputFile);
+            }
+            else
+            {
+                wxCopyFile(_T("tmp1.rtf"), OutputFile);
+            }
+            Tex2RTFYield(true);
+            wxRemoveFile(_T("tmp1.rtf"));
+        }
 
-    if (wxFileExists(ContentsName)) wxRemoveFile(ContentsName);
+        if (wxFileExists(ContentsName)) wxRemoveFile(ContentsName);
 
-    if (!wxRenameFile(TmpContentsName, ContentsName))
-    {
-      wxCopyFile(TmpContentsName, ContentsName);
-      wxRemoveFile(TmpContentsName);
-    }
+        if (!wxRenameFile(TmpContentsName, ContentsName))
+        {
+            wxCopyFile(TmpContentsName, ContentsName);
+            wxRemoveFile(TmpContentsName);
+        }
 
-    wxRemoveFile(_T("chapters.rtf"));
-    wxRemoveFile(_T("header.rtf"));
+        wxRemoveFile(_T("chapters.rtf"));
+        wxRemoveFile(_T("header.rtf"));
 
-    if (winHelp)
-    {
-      wxRemoveFile(_T("sections.rtf"));
-      wxRemoveFile(_T("subsections.rtf"));
-      wxRemoveFile(_T("subsubsections.rtf"));
-      wxRemoveFile(_T("popups.rtf"));
+        if (winHelp)
+        {
+            wxRemoveFile(_T("sections.rtf"));
+            wxRemoveFile(_T("subsections.rtf"));
+            wxRemoveFile(_T("subsubsections.rtf"));
+            wxRemoveFile(_T("popups.rtf"));
+        }
+        if (winHelp && generateHPJ)
+            WriteHPJ(OutputFile);
+        return true;
     }
-    if (winHelp && generateHPJ)
-      WriteHPJ(OutputFile);
-    return true;
-  }
-  return false;
+    return false;
 }