X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/1b392b3a6846e7cc3272b95de5a7cc2c05bfb8d5..c0cc7004a6ec1c53c0fd974898ef9ffd3739bef4:/utils/tex2rtf/src/tex2any.cpp diff --git a/utils/tex2rtf/src/tex2any.cpp b/utils/tex2rtf/src/tex2any.cpp index 8b1ef42b5d..4984789f8b 100644 --- a/utils/tex2rtf/src/tex2any.cpp +++ b/utils/tex2rtf/src/tex2any.cpp @@ -2,7 +2,7 @@ // Name: tex2any.cpp // Purpose: Utilities for Latex conversion. // Author: Julian Smart -// Modified by: Wlodzimiez ABX Skiba 2003/2004 Unicode support +// Modified by: Wlodzimierz ABX Skiba 2003/2004 Unicode support // Ron Lee // Created: 01/01/99 // RCS-ID: $Id$ @@ -10,10 +10,6 @@ // Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// -#ifdef __GNUG__ -#pragma implementation -#endif - // For compilers that support precompilation, includes "wx.h". #include "wx/wxprec.h" @@ -29,10 +25,8 @@ #include #include -#if !WXWIN_COMPATIBILITY_2_4 static inline wxChar* copystring(const wxChar* s) { return wxStrcpy(new wxChar[wxStrlen(s) + 1], s); } -#endif /* * Variables accessible from clients @@ -252,17 +246,15 @@ CustomMacro::~CustomMacro() delete [] macroBody; } -void TexOutput(const wxChar *s, bool ordinaryText) +void TexOutput(const wxString& s, bool ordinaryText) { - int len = wxStrlen(s); - // Update current column, but only if we're guaranteed to // be ordinary text (not mark-up stuff) int i; if (ordinaryText) - for (i = 0; i < len; i++) + for (wxString::const_iterator i = s.begin(); i != s.end(); ++i) { - if (s[i] == 13 || s[i] == 10) + if (*i == 13 || *i == 10) currentColumn = 0; else currentColumn ++; @@ -289,13 +281,13 @@ void ForbidWarning(TexMacroDef *def) case FORBID_WARN: { informBuf.Printf(_T("Warning: it is recommended that command %s is not used."), def->name); - OnInform((const wxChar *)informBuf.c_str()); + OnInform(informBuf); break; } case FORBID_ABSOLUTELY: { informBuf.Printf(_T("Error: command %s cannot be used and will lead to errors."), def->name); - OnInform((const wxChar *)informBuf.c_str()); + OnInform(informBuf); break; } default: @@ -305,81 +297,84 @@ void ForbidWarning(TexMacroDef *def) TexMacroDef *MatchMacro(wxChar *buffer, int *pos, wxChar **env, bool *parseToBrace) { - *parseToBrace = true; - int i = (*pos); - TexMacroDef *def = NULL; - wxChar macroBuf[40]; - - // First, try to find begin{thing} - if (wxStrncmp(buffer+i, _T("begin{"), 6) == 0) - { - i += 6; + *parseToBrace = true; + int i = (*pos); + TexMacroDef *def = NULL; + wxChar macroBuf[40]; - int j = i; - while ((isalpha(buffer[j]) || buffer[j] == '*') && ((j - i) < 39)) + // First, try to find begin{thing} + if (wxStrncmp(buffer+i, _T("begin{"), 6) == 0) { - macroBuf[j-i] = buffer[j]; - j ++; - } - macroBuf[j-i] = 0; - def = (TexMacroDef *)MacroDefs.Get(macroBuf); + i += 6; - if (def) - { - *pos = j + 1; // BUGBUG Should this be + 1??? - *env = def->name; - ForbidWarning(def); - return def; + int j = i; + while ((isalpha(buffer[j]) || buffer[j] == '*') && ((j - i) < 39)) + { + macroBuf[j-i] = buffer[j]; + j ++; + } + macroBuf[j-i] = 0; + def = (TexMacroDef *)MacroDefs.Get(macroBuf); + + if (def) + { + *pos = j + 1; // BUGBUG Should this be + 1??? + *env = def->name; + ForbidWarning(def); + return def; + } + else + { + return NULL; + } } - else return NULL; - } - // Failed, so try to find macro from definition list - int j = i; + // Failed, so try to find macro from definition list + int j = i; - // First try getting a one-character macro, but ONLY - // if these TWO characters are not both alphabetical (could - // be a longer macro) - if (!(isalpha(buffer[i]) && isalpha(buffer[i+1]))) - { - macroBuf[0] = buffer[i]; - macroBuf[1] = 0; + // First try getting a one-character macro, but ONLY + // if these TWO characters are not both alphabetical (could + // be a longer macro) + if (!(isalpha(buffer[i]) && isalpha(buffer[i+1]))) + { + macroBuf[0] = buffer[i]; + macroBuf[1] = 0; - def = (TexMacroDef *)MacroDefs.Get(macroBuf); - if (def) j ++; - } + def = (TexMacroDef *)MacroDefs.Get(macroBuf); + if (def) j ++; + } - if (!def) - { - while ((isalpha(buffer[j]) || buffer[j] == '*') && ((j - i) < 39)) + if (!def) { - macroBuf[j-i] = buffer[j]; - j ++; + while ((isalpha(buffer[j]) || buffer[j] == '*') && ((j - i) < 39)) + { + macroBuf[j-i] = buffer[j]; + j ++; + } + macroBuf[j-i] = 0; + def = (TexMacroDef *)MacroDefs.Get(macroBuf); } - macroBuf[j-i] = 0; - def = (TexMacroDef *)MacroDefs.Get(macroBuf); - } - - if (def) - { - i = j; - // We want to check whether this is a space-consuming macro - // (e.g. {\bf word}) - // No brace, e.g. \input thing.tex instead of \input{thing}; - // or a numeric argument, such as \parindent0pt - if ((def->no_args > 0) && ((buffer[i] == 32) || (buffer[i] == '=') || (isdigit(buffer[i])))) + if (def) { - if ((buffer[i] == 32) || (buffer[i] == '=')) - i ++; + i = j; + + // We want to check whether this is a space-consuming macro + // (e.g. {\bf word}) + // No brace, e.g. \input thing.tex instead of \input{thing}; + // or a numeric argument, such as \parindent0pt + if ((def->no_args > 0) && ((buffer[i] == 32) || (buffer[i] == '=') || (isdigit(buffer[i])))) + { + if ((buffer[i] == 32) || (buffer[i] == '=')) + i ++; - *parseToBrace = false; + *parseToBrace = false; + } + *pos = i; + ForbidWarning(def); + return def; } - *pos = i; - ForbidWarning(def); - return def; - } - return NULL; + return NULL; } void EatWhiteSpace(wxChar *buffer, int *pos) @@ -430,7 +425,7 @@ bool readInVerbatim = false; // Within a verbatim, but not nec. verbatiminput unsigned long leftCurly = 0; unsigned long rightCurly = 0; -static wxString currentFileName = _T(""); +static wxString currentFileName = wxEmptyString; bool read_a_line(wxChar *buf) { @@ -452,7 +447,7 @@ bool read_a_line(wxChar *buf) wxString errBuf; errBuf.Printf(_T("Line %lu of file %s is too long. Lines can be no longer than %lu characters. Truncated."), LineNumbers[CurrentInputIndex], (const wxChar*) currentFileName.c_str(), MAX_LINE_BUFFER_SIZE); - OnError((wxChar *)errBuf.c_str()); + OnError(errBuf); return false; } @@ -474,7 +469,7 @@ bool read_a_line(wxChar *buf) { wxString errBuf; errBuf.Printf(_T("An extra right Curly brace ('}') was detected at line %lu inside file %s"), LineNumbers[CurrentInputIndex], (const wxChar*) currentFileName.c_str()); - OnError((wxChar *)errBuf.c_str()); + OnError(errBuf); // Reduce the count of right Curly braces, so the mismatched count // isn't reported on every line that has a '}' after the first mismatch @@ -503,7 +498,7 @@ bool read_a_line(wxChar *buf) wxString errBuf; errBuf.Printf(_T("Line %lu of file %s is too long. Lines can be no longer than %lu characters. Truncated."), LineNumbers[CurrentInputIndex], (const wxChar*) currentFileName.c_str(),MAX_LINE_BUFFER_SIZE); - OnError((wxChar *)errBuf.c_str()); + OnError(errBuf); return false; } wxStrcat(buf, _T("\\par")); @@ -518,7 +513,7 @@ bool read_a_line(wxChar *buf) wxString errBuf; errBuf.Printf(_T("Line %lu of file %s is too long. Lines can be no longer than %lu characters. Truncated."), LineNumbers[CurrentInputIndex], (const wxChar*) currentFileName.c_str(),MAX_LINE_BUFFER_SIZE); - OnError((wxChar *)errBuf.c_str()); + OnError(errBuf); return false; } @@ -543,7 +538,7 @@ bool read_a_line(wxChar *buf) wxString errBuf; errBuf.Printf(_T("Line %lu of file %s is too long. Lines can be no longer than %lu characters. Truncated."), LineNumbers[CurrentInputIndex], (const wxChar*) currentFileName.c_str(),MAX_LINE_BUFFER_SIZE); - OnError((wxChar *)errBuf.c_str()); + OnError(errBuf); return false; } buf[bufIndex++]='\\'; @@ -566,7 +561,7 @@ bool read_a_line(wxChar *buf) wxString errBuf; errBuf.Printf(_T("Line %lu of file %s is too long. Lines can be no longer than %lu characters. Truncated."), LineNumbers[CurrentInputIndex], (const wxChar*) currentFileName.c_str(),MAX_LINE_BUFFER_SIZE); - OnError((wxChar *)errBuf.c_str()); + OnError(errBuf); return false; } buf[bufIndex++]='\\'; @@ -581,7 +576,7 @@ bool read_a_line(wxChar *buf) wxString errBuf; errBuf.Printf(_T("Line %lu of file %s is too long. Lines can be no longer than %lu characters. Truncated."), LineNumbers[CurrentInputIndex], (const wxChar*) currentFileName.c_str(),MAX_LINE_BUFFER_SIZE); - OnError((wxChar *)errBuf.c_str()); + OnError(errBuf); return false; } // If the current character read in is a '_', we need to check @@ -602,7 +597,7 @@ bool read_a_line(wxChar *buf) // wxString errBuf; // errBuf.Printf(_T("An underscore ('_') was detected at line %lu inside file %s that should NOT have a '\\' before it."), // LineNumbers[CurrentInputIndex], (const wxChar*) currentFileName.c_str()); -// OnError((wxChar *)errBuf.c_str()); +// OnError(errBuf); } } else @@ -613,7 +608,7 @@ bool read_a_line(wxChar *buf) wxString errBuf; errBuf.Printf(_T("An underscore ('_') was detected at line %lu inside file %s that may need a '\\' before it."), LineNumbers[CurrentInputIndex], (const wxChar*) currentFileName.c_str()); - OnError((wxChar *)errBuf.c_str()); + OnError(errBuf); } else if ((buf[bufIndex-1] != '\\') && (buf[0] != '%') && // If it is a comment line, then no warnings (wxStrncmp(buf, _T("\\input"), 6))) // do not report filenames that have underscores in them @@ -621,7 +616,7 @@ bool read_a_line(wxChar *buf) wxString errBuf; errBuf.Printf(_T("An underscore ('_') was detected at line %lu inside file %s that may need a '\\' before it."), LineNumbers[CurrentInputIndex], (const wxChar*) currentFileName.c_str()); - OnError((wxChar *)errBuf.c_str()); + OnError(errBuf); } } } @@ -646,7 +641,7 @@ bool read_a_line(wxChar *buf) wxString errBuf; errBuf.Printf(_T("Curly braces do not match inside file %s\n%lu opens, %lu closes"), (const wxChar*) currentFileName.c_str(),leftCurly,rightCurly); - OnError((wxChar *)errBuf.c_str()); + OnError(errBuf); } leftCurly = 0; rightCurly = 0; @@ -693,17 +688,17 @@ bool read_a_line(wxChar *buf) wxString actualFile = TexPathList.FindValidPath(fileName); currentFileName = actualFile; - if (actualFile == _T("")) + if (actualFile.empty()) { wxString errBuf; errBuf.Printf(_T("Could not find file: %s"),fileName); - OnError((wxChar *)errBuf.c_str()); + OnError(errBuf); } else { wxString informStr; informStr.Printf(_T("Processing: %s"),actualFile.c_str()); - OnInform((wxChar *)informStr.c_str()); + OnInform(informStr); CurrentInputIndex ++; Inputs[CurrentInputIndex] = wxFopen(actualFile, _T("r")); @@ -761,13 +756,13 @@ bool read_a_line(wxChar *buf) fileNameStr.Replace(_T("\\"), _T("")); // Ignore some types of input files (e.g. macro definition files) - wxChar *fileOnly = wxFileNameFromPath((wxChar*) (const wxChar*) fileNameStr); + wxString fileOnly = wxFileNameFromPath(fileNameStr); currentFileName = fileOnly; if (IgnorableInputFiles.Member(fileOnly)) return read_a_line(buf); wxString actualFile = TexPathList.FindValidPath(fileNameStr); - if (actualFile == _T("")) + if (actualFile.empty()) { wxChar buf2[400]; wxSnprintf(buf2, sizeof(buf2), _T("%s.tex"), fileNameStr.c_str()); @@ -775,11 +770,11 @@ bool read_a_line(wxChar *buf) } currentFileName = actualFile; - if (actualFile == _T("")) + if (actualFile.empty()) { wxString errBuf; errBuf.Printf(_T("Could not find file: %s"),fileName); - OnError((wxChar *)errBuf.c_str()); + OnError(errBuf); } else { @@ -789,7 +784,7 @@ bool read_a_line(wxChar *buf) wxString informStr; informStr.Printf(_T("Processing: %s"),actualFile.c_str()); - OnInform((wxChar *)informStr.c_str()); + OnInform(informStr); CurrentInputIndex ++; Inputs[CurrentInputIndex] = wxFopen(actualFile, _T("r")); @@ -803,7 +798,7 @@ bool read_a_line(wxChar *buf) wxString errBuf; errBuf.Printf(_T("Could not open include file %s"), (const wxChar*) actualFile); CurrentInputIndex --; - OnError((wxChar *)errBuf.c_str()); + OnError(errBuf); } } bool succ = read_a_line(buf); @@ -813,7 +808,7 @@ bool read_a_line(wxChar *buf) if (checkSyntax) { wxString bufStr = buf; - for (int index=0; syntaxTokens[index] != wxEmptyString; index++) + for (int index=0; !syntaxTokens[index].empty(); index++) { size_t pos = bufStr.find(syntaxTokens[index]); if (pos != wxString::npos && pos != 0) @@ -836,7 +831,7 @@ bool read_a_line(wxChar *buf) LineNumbers[CurrentInputIndex], currentFileName.c_str()); } - OnError((wxChar *)errBuf.c_str()); + OnError(errBuf); } } } @@ -856,7 +851,7 @@ bool read_a_line(wxChar *buf) wxString errBuf; errBuf.Printf(_T("Curly braces do not match inside file %s\n%lu opens, %lu closes"), (const wxChar*) currentFileName.c_str(),leftCurly,rightCurly); - OnError((wxChar *)errBuf.c_str()); + OnError(errBuf); } } @@ -954,7 +949,7 @@ void MacroError(wxChar *buffer) errBuf.Printf(_T("Could not find macro: %s at line %d, file %s"), macroBuf, (int)(LineNumbers[CurrentInputIndex]-1), FileNames[CurrentInputIndex]); - OnError((wxChar *)errBuf.c_str()); + OnError(errBuf); if (wxStrcmp(macroBuf,_T("\\end{document}")) == 0) { @@ -1222,7 +1217,7 @@ int ParseArg(TexChunk *thisArg, wxList& children, wxChar *buffer, int pos, wxCha pos ++; // Find the delimiter character - int ch = buffer[pos]; + wxChar ch = buffer[pos]; pos ++; // Now at start of verbatim text int j = pos; @@ -1275,16 +1270,20 @@ int ParseArg(TexChunk *thisArg, wxList& children, wxChar *buffer, int pos, wxCha children.Append((wxObject *)chunk); // Eliminate newline after a \begin{} or a \\ if possible - if (env || wxStrcmp(def->name, _T("\\")) == 0) - if (buffer[pos] == 13) - { + if ((env || wxStrcmp(def->name, _T("\\")) == 0) && (buffer[pos] == 13)) + { pos ++; if (buffer[pos] == 10) pos ++; - } + } - pos = ParseMacroBody(def->name, chunk, chunk->no_args, - buffer, pos, env, tmpParseToBrace, customMacroArgs); + pos = ParseMacroBody(def->name, + chunk, chunk->no_args, + buffer, + pos, + env, + tmpParseToBrace, + customMacroArgs); // If custom macro, parse the body substituting the above found args. if (customMacro) @@ -1633,7 +1632,7 @@ int ParseMacroBody(const wxChar *WXUNUSED(macro_name), TexChunk *parent, tmpBuffer = tmpBuffer.Mid(0,tmpBuffer.length()-4); } errBuf.Printf(_T("Missing macro argument in the line:\n\t%s\n"),tmpBuffer.c_str()); - OnError((wxChar *)errBuf.c_str()); + OnError(errBuf); } } @@ -1663,34 +1662,34 @@ int ParseMacroBody(const wxChar *WXUNUSED(macro_name), TexChunk *parent, return pos; } -bool TexLoadFile(wxChar *filename) +bool TexLoadFile(const wxString& filename) { - static wxChar *line_buffer; - stopRunning = false; - wxStrcpy(TexFileRoot, filename); - StripExtension(TexFileRoot); - wxSnprintf(TexBibName, 300, _T("%s.bb"), TexFileRoot); - wxSnprintf(TexTmpBibName, 300, _T("%s.bb1"), TexFileRoot); + static wxChar *line_buffer; + stopRunning = false; + wxStrcpy(TexFileRoot, filename); + StripExtension(TexFileRoot); + wxSnprintf(TexBibName, 300, _T("%s.bb"), TexFileRoot); + wxSnprintf(TexTmpBibName, 300, _T("%s.bb1"), TexFileRoot); - TexPathList.EnsureFileAccessible(filename); + TexPathList.EnsureFileAccessible(filename); - if (line_buffer) - delete line_buffer; + if (line_buffer) + delete line_buffer; - line_buffer = new wxChar[MAX_LINE_BUFFER_SIZE]; + line_buffer = new wxChar[MAX_LINE_BUFFER_SIZE]; - Inputs[0] = wxFopen(filename, _T("r")); - LineNumbers[0] = 1; - FileNames[0] = copystring(filename); - if (Inputs[0]) - { - read_a_line(line_buffer); - ParseMacroBody(_T("toplevel"), TopLevel, 1, line_buffer, 0, NULL, true); - if (Inputs[0]) fclose(Inputs[0]); - return true; - } + Inputs[0] = wxFopen(filename, _T("r")); + LineNumbers[0] = 1; + FileNames[0] = copystring(filename); + if (Inputs[0]) + { + read_a_line(line_buffer); + ParseMacroBody(_T("toplevel"), TopLevel, 1, line_buffer, 0, NULL, true); + if (Inputs[0]) fclose(Inputs[0]); + return true; + } - return false; + return false; } TexMacroDef::TexMacroDef(int the_id, const wxChar *the_name, int n, bool ig, bool forbidLevel) @@ -2620,11 +2619,21 @@ void DefaultOnMacro(int macroId, int no_args, bool start) case ltCINSERT: if (start) - TexOutput(_T("<<"), true); + { + if (convertMode == TEX_HTML) + TexOutput(_T("<<")); + else + TexOutput(_T("<<"), true); + } break; case ltCEXTRACT: if (start) - TexOutput(_T(">>"), true); + { + if (convertMode == TEX_HTML) + TexOutput(_T(">>")); + else + TexOutput(_T(">>"), true); + } break; case ltDESTRUCT: if (start) @@ -2859,10 +2868,22 @@ void DefaultOnMacro(int macroId, int no_args, bool start) // Binary operation symbols case ltLE: case ltLEQ: - if (start) TexOutput(_T("<=")); + if (start) + { + if (convertMode == TEX_HTML) + TexOutput(_T("<=")); + else + TexOutput(_T("<=")); + } break; case ltLL: - if (start) TexOutput(_T("<<")); + if (start) + { + if (convertMode == TEX_HTML) + TexOutput(_T("<<")); + else + TexOutput(_T("<<")); + } break; case ltSUBSET: if (start) TexOutput(_T("SUBSET")); @@ -2881,10 +2902,24 @@ void DefaultOnMacro(int macroId, int no_args, bool start) break; case ltGE: case ltGEQ: - if (start) TexOutput(_T(">=")); + { + if (start) + { + if (convertMode == TEX_HTML) + TexOutput(_T(">=")); + else + TexOutput(_T(">=")); + } break; + } case ltGG: - if (start) TexOutput(_T(">>")); + if (start) + { + if (convertMode == TEX_HTML) + TexOutput(_T(">>")); + else + TexOutput(_T(">>")); + } break; case ltSUPSET: if (start) TexOutput(_T("SUPSET")); @@ -2966,22 +3001,58 @@ void DefaultOnMacro(int macroId, int no_args, bool start) // Arrows case ltLEFTARROW: - if (start) TexOutput(_T("<--")); + if (start) + { + if (convertMode == TEX_HTML) + TexOutput(_T("<--")); + else + TexOutput(_T("<--")); + } break; case ltLEFTARROW2: - if (start) TexOutput(_T("<==")); + if (start) + { + if (convertMode == TEX_HTML) + TexOutput(_T("<==")); + else + TexOutput(_T("<==")); + } break; case ltRIGHTARROW: - if (start) TexOutput(_T("-->")); + if (start) + { + if (convertMode == TEX_HTML) + TexOutput(_T("-->")); + else + TexOutput(_T("-->")); + } break; case ltRIGHTARROW2: - if (start) TexOutput(_T("==>")); + if (start) + { + if (convertMode == TEX_HTML) + TexOutput(_T("==>")); + else + TexOutput(_T("==>")); + } break; case ltLEFTRIGHTARROW: - if (start) TexOutput(_T("<-->")); + if (start) + { + if (convertMode == TEX_HTML) + TexOutput(_T("<-->")); + else + TexOutput(_T("<-->")); + } break; case ltLEFTRIGHTARROW2: - if (start) TexOutput(_T("<==>")); + if (start) + { + if (convertMode == TEX_HTML) + TexOutput(_T("<==>")); + else + TexOutput(_T("<==>")); + } break; case ltUPARROW: if (start) TexOutput(_T("UPARROW")); @@ -3176,7 +3247,7 @@ bool DefaultOnArgument(int macroId, int arg_no, bool start) { wxString informBuf; informBuf.Printf(_T("Warning: unresolved reference '%s'"), refName); - OnInform((wxChar *)informBuf.c_str()); + OnInform(informBuf); } } else TexOutput(_T("??"), true); @@ -3330,7 +3401,7 @@ bool DefaultOnArgument(int macroId, int arg_no, bool start) { wxString informBuf; informBuf.Printf(_T("Warning: unresolved citation %s."), citeKey); - OnInform((wxChar *)informBuf.c_str()); + OnInform(informBuf); } } citeKey = ParseMultifieldString(citeKeys, &pos); @@ -3460,25 +3531,25 @@ bool DefaultOnArgument(int macroId, int arg_no, bool start) wxChar fileBuf[300]; wxStrcpy(fileBuf, bibFile); wxString actualFile = TexPathList.FindValidPath(fileBuf); - if (actualFile == _T("")) + if (actualFile.empty()) { wxStrcat(fileBuf, _T(".bib")); actualFile = TexPathList.FindValidPath(fileBuf); } - if (actualFile != _T("")) + if (!actualFile.empty()) { if (!ReadBib((wxChar*) (const wxChar*) actualFile)) { wxString errBuf; errBuf.Printf(_T(".bib file %s not found or malformed"), (const wxChar*) actualFile); - OnError((wxChar *)errBuf.c_str()); + OnError(errBuf); } } else { wxString errBuf; errBuf.Printf(_T(".bib file %s not found"), fileBuf); - OnError((wxChar *)errBuf.c_str()); + OnError(errBuf); } bibFile = ParseMultifieldString(allFiles, &pos); } @@ -3714,4 +3785,3 @@ bool DefaultOnArgument(int macroId, int arg_no, bool start) } return true; } -