X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/8907154c1a8a6882c6797d1f16393ddfb23e7f3a..4871d34290873072ca2783074d4779ba5ecb2052:/utils/tex2rtf/src/texutils.cpp diff --git a/utils/tex2rtf/src/texutils.cpp b/utils/tex2rtf/src/texutils.cpp index 4bbf372ad3..eead3b6dfc 100644 --- a/utils/tex2rtf/src/texutils.cpp +++ b/utils/tex2rtf/src/texutils.cpp @@ -23,6 +23,7 @@ #include "wx/app.h" #include "wx/hash.h" +#include "wx/textfile.h" #ifdef new #undef new @@ -40,10 +41,8 @@ using namespace std; #include #include "tex2any.h" -#if !WXWIN_COMPATIBILITY_2_4 static inline wxChar* copystring(const wxChar* s) { return wxStrcpy(new wxChar[wxStrlen(s) + 1], s); } -#endif wxHashTable TexReferences(wxKEY_STRING); wxList BibList(wxKEY_STRING); @@ -208,7 +207,7 @@ wxChar *FindTopicName(TexChunk *chunk) * */ -void StartSimulateArgument(wxChar *data) +void StartSimulateArgument(const wxChar *data) { wxStrcpy(currentArgData, data); haveArgData = true; @@ -224,8 +223,11 @@ void EndSimulateArgument(void) * */ -int ParseUnitArgument(wxChar *unitArg) +int ParseUnitArgument(const wxChar *unitArg_) { + wxWxCharBuffer unitBuf(unitArg_); + wxChar *unitArg = unitBuf.data(); + float conversionFactor = 1.0; float unitValue = 0.0; int len = wxStrlen(unitArg); @@ -386,95 +388,79 @@ void AddTexRef(wxChar *name, wxChar *file, wxChar *sectionName, void WriteTexReferences(wxChar *filename) { - wxString converter; - wxString name = filename; - wxSTD ofstream ostr((char const *)name.fn_str()); - if (ostr.bad()) return; + wxString name = filename; + wxTextFile file; - TexReferences.BeginFind(); - wxHashTable::Node *node = TexReferences.Next(); - while (node) - { - Tex2RTFYield(); - TexRef *ref = (TexRef *)node->GetData(); - converter = ref->refLabel; - ostr << converter.mb_str(); - ostr << " "; - converter = (ref->refFile ? ref->refFile : _T("??")); - ostr << converter.mb_str(); - ostr << " "; - converter = (ref->sectionName ? ref->sectionName : _T("??")) ; - ostr << converter.mb_str(); - ostr << " "; - converter = (ref->sectionNumber ? ref->sectionNumber : _T("??")) ; - ostr << converter.mb_str(); - ostr << "\n"; - if (!ref->sectionNumber || (wxStrcmp(ref->sectionNumber, _T("??")) == 0 && wxStrcmp(ref->sectionName, _T("??")) == 0)) - { - wxChar buf[200]; - wxSnprintf(buf, sizeof(buf), _T("Warning: reference %s not resolved."), ref->refLabel); - OnInform(buf); + if (!(wxFileExists(name)?file.Open(name):file.Create(name))) + return; + + file.Clear(); + + TexReferences.BeginFind(); + wxHashTable::Node *node = TexReferences.Next(); + while (node) + { + Tex2RTFYield(); + TexRef *ref = (TexRef *)node->GetData(); + wxString converter = ref->refLabel; + converter << wxT(" "); + converter << (ref->refFile ? ref->refFile : _T("??")); + converter << wxT(" "); + converter << (ref->sectionName ? ref->sectionName : _T("??")) ; + converter << wxT(" "); + converter << (ref->sectionNumber ? ref->sectionNumber : _T("??")) ; + file.AddLine(converter); + + if (!ref->sectionNumber || (wxStrcmp(ref->sectionNumber, _T("??")) == 0 && wxStrcmp(ref->sectionName, _T("??")) == 0)) + { + wxChar buf[200]; + wxSnprintf(buf, sizeof(buf), _T("Warning: reference %s not resolved."), ref->refLabel); + OnInform(buf); + } + node = TexReferences.Next(); } - node = TexReferences.Next(); - } + + file.Write(); + file.Close(); } void ReadTexReferences(wxChar *filename) { - if (!wxFileExists(filename)) - return; + wxString name = filename; - wxString name = filename; - wxSTD ifstream istr((char const *)name.fn_str(), wxSTD ios::in); - - if (istr.bad()) return; + if (!wxFileExists(name)) + return; - char label[100]; - char file[400]; - char section[100]; - char sectionName[100]; + wxTextFile file; + if (!file.Open(name)) + return; - while (!istr.eof()) - { - istr >> label; - if (!istr.eof()) - { - istr >> file; - istr >> sectionName; - char ch; - istr.get(ch); // Read past space - istr.get(ch); - int i = 0; - while (ch != '\n' && !istr.eof()) - { - section[i] = ch; - i ++; - istr.get(ch); - } - section[i] = 0; - - wxString label_string = wxString::FromAscii(label); - wxString file_string = wxString::FromAscii(file); - wxString sectionName_string = wxString::FromAscii(sectionName); - wxString section_string = wxString::FromAscii(section); - - // gt - needed to trick the hash table "TexReferences" into deleting the key - // strings it creates in the Put() function, but not the item that is - // created here, as that is destroyed elsewhere. Without doing this, there - // were massive memory leaks - TexReferences.DeleteContents(true); - TexReferences.Put( - label_string.c_str(), - new TexRef( - label_string.c_str(), - file_string.c_str(), - section_string.c_str(), - sectionName_string.c_str() - ) - ); - TexReferences.DeleteContents(false); + wxString line; + for ( line = file.GetFirstLine(); !file.Eof(); line = file.GetNextLine() ) + { + wxString labelStr = line.BeforeFirst(wxT(' ')); + line = line.AfterFirst(wxT(' ')); + wxString fileStr = line.BeforeFirst(wxT(' ')); + line = line.AfterFirst(wxT(' ')); + wxString sectionNameStr = line.BeforeFirst(wxT(' ')); + wxString sectionStr = line.AfterFirst(wxT(' ')); + + // gt - needed to trick the hash table "TexReferences" into deleting the key + // strings it creates in the Put() function, but not the item that is + // created here, as that is destroyed elsewhere. Without doing this, there + // were massive memory leaks + TexReferences.DeleteContents(true); + TexReferences.Put( + labelStr, + new TexRef( + labelStr.c_str(), + fileStr.c_str(), + sectionStr.c_str(), + sectionNameStr.c_str() + ) + ); + TexReferences.DeleteContents(false); } - } } @@ -483,6 +469,22 @@ void ReadTexReferences(wxChar *filename) * */ +void BibEatWhiteSpace(wxString& line) +{ + while(!line.empty() && (line[0] == _T(' ') || line[0] == _T('\t') || line[0] == (wxChar)EOF)) + { + if (line[0] == '\r') + BibLine ++; + line = line.substr(1); + } + + // Ignore end-of-line comments + if (line[0] == _T('%') || line[0] == _T(';') || line[0] == _T('#')) + { + line = wxEmptyString; + } +} + void BibEatWhiteSpace(wxSTD istream& str) { char ch = (char)str.peek(); @@ -511,6 +513,24 @@ void BibEatWhiteSpace(wxSTD istream& str) } // Read word up to { or , or space +wxString BibReadWord(wxString& line) +{ + wxString val; + + while (!line.empty() && + line[0] != _T('\t') && + line[0] != _T(' ') && + line[0] != _T('{') && + line[0] != _T('(') && + line[0] != _T(',') && + line[0] != _T('=')) + { + val << line[0]; + line = line.substr(1); + } + return val; +} + void BibReadWord(wxSTD istream& istr, wxChar *buffer) { int i = 0; @@ -528,88 +548,166 @@ void BibReadWord(wxSTD istream& istr, wxChar *buffer) } // Read string (double-quoted or not) to end quote or EOL -void BibReadToEOL(wxSTD istream& istr, wxChar *buffer) +wxString BibReadToEOL(wxString& line) { - int i = 0; - buffer[i] = 0; - char ch = (char)istr.peek(); - bool inQuotes = false; - if (ch == '"') - { - istr.get(ch); - ch = (char)istr.peek(); - inQuotes = true; - } - // If in quotes, read white space too. If not, - // stop at white space or comment. - while (!istr.eof() && ch != 13 && ch != 10 && ch != _T('"') && - (inQuotes || ((ch != _T(' ')) && (ch != 9) && - (ch != _T(';')) && (ch != _T('%')) && (ch != _T('#'))))) - { - istr.get(ch); - buffer[i] = ch; - i ++; - ch = (char)istr.peek(); - } - if (ch == '"') - istr.get(ch); - buffer[i] = 0; + if(line.empty()) + return wxEmptyString; + + wxString val; + bool inQuotes = false; + if (line[0] == _T('"')) + { + line = line.substr(1); + inQuotes = true; + } + // If in quotes, read white space too. If not, + // stop at white space or comment. + while (!line.empty() && line[0] != _T('"') && + (inQuotes || ((line[0] != _T(' ')) && (line[0] != '\t') && + (line[0] != _T(';')) && (line[0] != _T('%')) && (line[0] != _T('#'))))) + { + val << line[0]; + line = line.substr(1); + } + if (line[0] == '"') + line = line.substr(1); + + return val; } -// Read }-terminated value, taking nested braces into account. -void BibReadValue(wxSTD istream& istr, wxChar *buffer, bool ignoreBraces = true, - bool quotesMayTerminate = true) +void BibReadToEOL(wxSTD istream& istr, wxChar *buffer) { - int braceCount = 1; - int i = 0; - buffer[i] = 0; - char ch = (char)istr.peek(); - bool stopping = false; - while (!istr.eof() && !stopping) - { -// i ++; - if (i >= 4000) + int i = 0; + buffer[i] = 0; + char ch = (char)istr.peek(); + bool inQuotes = false; + if (ch == '"') { - wxChar buf[100]; - wxSnprintf(buf, sizeof(buf), _T("Sorry, value > 4000 chars in bib file at line %ld."), BibLine); - wxLogError(buf, "Tex2RTF Fatal Error"); - return; + istr.get(ch); + ch = (char)istr.peek(); + inQuotes = true; } - istr.get(ch); + // If in quotes, read white space too. If not, + // stop at white space or comment. + while (!istr.eof() && ch != 13 && ch != 10 && ch != _T('"') && + (inQuotes || ((ch != _T(' ')) && (ch != 9) && + (ch != _T(';')) && (ch != _T('%')) && (ch != _T('#'))))) + { + istr.get(ch); + buffer[i] = ch; + i ++; + ch = (char)istr.peek(); + } + if (ch == '"') + istr.get(ch); + buffer[i] = 0; +} - if (ch == '{') - braceCount ++; +// Read }-terminated value, taking nested braces into account. +wxString BibReadValue(wxString& line, + bool ignoreBraces = true, + bool quotesMayTerminate = true) +{ + wxString val; + int braceCount = 1; + bool stopping = false; - if (ch == '}') + if (line.length() >= 4000) { - braceCount --; - if (braceCount == 0) - { - stopping = true; - break; - } + wxChar buf[100]; + wxSnprintf(buf, sizeof(buf), _T("Sorry, value > 4000 chars in bib file at line %ld."), BibLine); + wxLogError(buf, "Tex2RTF Fatal Error"); + return wxEmptyString; } - else if (quotesMayTerminate && ch == '"') + + while (!line.empty() && !stopping) { - stopping = true; - break; + wxChar ch = line[0]; + line = line.substr(1); + + if (ch == _T('{')) + braceCount ++; + + if (ch == _T('}')) + { + braceCount --; + if (braceCount == 0) + { + stopping = true; + break; + } + } + else if (quotesMayTerminate && ch == _T('"')) + { + stopping = true; + break; + } + + if (!stopping) + { + if (!ignoreBraces || (ch != _T('{') && ch != _T('}'))) + { + val << ch; + } + } } - if (!stopping) - { - if (!ignoreBraces || (ch != '{' && ch != '}')) - { - buffer[i] = ch; - i ++; - } + + return val; +} + +void BibReadValue(wxSTD istream& istr, wxChar *buffer, bool ignoreBraces = true, + bool quotesMayTerminate = true) +{ + int braceCount = 1; + int i = 0; + buffer[i] = 0; + char ch = (char)istr.peek(); + bool stopping = false; + while (!istr.eof() && !stopping) + { +// i ++; + if (i >= 4000) + { + wxChar buf[100]; + wxSnprintf(buf, sizeof(buf), _T("Sorry, value > 4000 chars in bib file at line %ld."), BibLine); + wxLogError(buf, "Tex2RTF Fatal Error"); + return; + } + istr.get(ch); + + if (ch == '{') + braceCount ++; + + if (ch == '}') + { + braceCount --; + if (braceCount == 0) + { + stopping = true; + break; + } + } + else if (quotesMayTerminate && ch == '"') + { + stopping = true; + break; + } + if (!stopping) + { + if (!ignoreBraces || (ch != '{' && ch != '}')) + { + buffer[i] = ch; + i ++; + } + } + if (ch == 10) + BibLine ++; } - if (ch == 10) - BibLine ++; - } - buffer[i] = 0; - wxUnusedVar(stopping); + buffer[i] = 0; + wxUnusedVar(stopping); } -bool ReadBib(wxChar *filename) +bool ReadBib(const wxChar *filename) { if (!wxFileExists(filename)) return false; @@ -1061,7 +1159,7 @@ void ResolveBibReferences(void) } // Remember we need to resolve this citation -void AddCitation(wxChar *citeKey) +void AddCitation(const wxChar *citeKey) { if (!CitationList.Member(citeKey)) CitationList.Add(citeKey); @@ -1072,7 +1170,7 @@ void AddCitation(wxChar *citeKey) } } -TexRef *FindReference(wxChar *key) +TexRef *FindReference(const wxChar *key) { return (TexRef *)TexReferences.Get(key); } @@ -1082,15 +1180,18 @@ TexRef *FindReference(wxChar *key) * */ -bool StringTobool(wxChar *val) +bool StringTobool(const wxString& val) { - if (wxStrncmp(val, _T("yes"), 3) == 0 || wxStrncmp(val, _T("YES"), 3) == 0 || - wxStrncmp(val, _T("on"), 2) == 0 || wxStrncmp(val, _T("ON"), 2) == 0 || - wxStrncmp(val, _T("true"), 4) == 0 || wxStrncmp(val, _T("true"), 4) == 0 || - wxStrncmp(val, _T("ok"), 2) == 0 || wxStrncmp(val, _T("OK"), 2) == 0 || - wxStrncmp(val, _T("1"), 1) == 0) - return true; - else + wxString up(val); + up.MakeUpper(); + + if (up.IsSameAs(_T("YES")) || + up.IsSameAs(_T("TRUE")) || + up.IsSameAs(_T("ON")) || + up.IsSameAs(_T("OK")) | + up.IsSameAs(_T("1"))) + return true; + return false; } @@ -1105,7 +1206,7 @@ void RegisterIntSetting (const wxString& s, int *number) } // Define a variable value from the .ini file -wxChar *RegisterSetting(wxChar *settingName, wxChar *settingValue, bool interactive) +wxChar *RegisterSetting(const wxString& settingName, const wxString& settingValue, bool interactive) { wxString settingValueStr( settingValue ); @@ -1351,7 +1452,7 @@ wxChar *RegisterSetting(wxChar *settingName, wxChar *settingValue, bool interact else { wxChar buf[200]; - wxSnprintf(buf, sizeof(buf), _T("Initialisation file error: unrecognised setting %s."), settingName); + wxSnprintf(buf, sizeof(buf), _T("Initialisation file error: unrecognised setting %s."), settingName.c_str()); if (interactive) OnInform(buf); wxStrcpy(errorCode, buf); @@ -1359,87 +1460,81 @@ wxChar *RegisterSetting(wxChar *settingName, wxChar *settingValue, bool interact return errorCode; } -bool ReadCustomMacros(wxChar *filename) +bool ReadCustomMacros(const wxString& filename) { - if (!wxFileExists(filename)) - return false; + if (!wxFileExists(filename)) + return false; - wxString name = filename; - wxSTD ifstream istr((char const *)name.fn_str(), wxSTD ios::in); + wxFileInputStream input( filename ); + if(!input.Ok()) return false; + wxTextInputStream ini( input ); - if (istr.bad()) return false; + CustomMacroList.Clear(); - CustomMacroList.Clear(); - char ch; - wxChar macroName[100]; - wxChar macroBody[1000]; - int noArgs; + while (!input.Eof()) + { + wxString line = ini.ReadLine(); + BibEatWhiteSpace(line); + if (line.empty()) continue; - while (!istr.eof()) - { - BibEatWhiteSpace(istr); - istr.get(ch); - if (istr.eof()) - break; + if (line[0] != _T('\\')) // Not a macro definition, so must be NAME=VALUE + { + wxString settingName = BibReadWord(line); + BibEatWhiteSpace(line); + if (line.empty() || line[0] != _T('=')) + { + OnError(_T("Expected = following name: malformed tex2rtf.ini file.")); + return false; + } + else + { + line = line.substr(1); + BibEatWhiteSpace(line); + wxString settingValue = BibReadToEOL(line); + RegisterSetting(settingName, settingValue); + } + } + else + { + line = line.substr(1); + wxString macroName = BibReadWord(line); + BibEatWhiteSpace(line); + if (line[0] != _T('[')) + { + OnError(_T("Expected [ followed by number of arguments: malformed tex2rtf.ini file.")); + return false; + } + line = line.substr(1); + wxString noAargStr = line.BeforeFirst(_T(']')); + line = line.AfterFirst(_T(']')); + long noArgs; + if (!noAargStr.ToLong(&noArgs) || line.empty()) + { + OnError(_T("Expected ] following number of arguments: malformed tex2rtf.ini file.")); + return false; + } + BibEatWhiteSpace(line); + if (line[0] != _T('{')) + { + OnError(_T("Expected { followed by macro body: malformed tex2rtf.ini file.")); + return false; + } - if (ch != '\\') // Not a macro definition, so must be NAME=VALUE - { - wxChar settingName[100]; - settingName[0] = ch; - BibReadWord(istr, (settingName+1)); - BibEatWhiteSpace(istr); - istr.get(ch); - if (ch != '=') - { - OnError(_T("Expected = following name: malformed tex2rtf.ini file.")); - return false; - } - else - { - wxChar settingValue[200]; - BibEatWhiteSpace(istr); - BibReadToEOL(istr, settingValue); - RegisterSetting(settingName, settingValue); - } - } - else - { - BibReadWord(istr, macroName); - BibEatWhiteSpace(istr); - istr.get(ch); - if (ch != '[') - { - OnError(_T("Expected [ followed by number of arguments: malformed tex2rtf.ini file.")); - return false; - } - istr >> noArgs; - istr.get(ch); - if (ch != ']') - { - OnError(_T("Expected ] following number of arguments: malformed tex2rtf.ini file.")); - return false; - } - BibEatWhiteSpace(istr); - istr.get(ch); - if (ch != '{') - { - OnError(_T("Expected { followed by macro body: malformed tex2rtf.ini file.")); - return false; - } - CustomMacro *macro = new CustomMacro(macroName, noArgs, NULL); - BibReadValue(istr, macroBody, false, false); // Don't ignore extra braces - if (wxStrlen(macroBody) > 0) - macro->macroBody = copystring(macroBody); + CustomMacro *macro = new CustomMacro(macroName.c_str(), noArgs, NULL); + wxString macroBody = BibReadValue(line, false, false); // Don't ignore extra braces + if (!macroBody.empty()) + macro->macroBody = copystring(macroBody.c_str()); + + BibEatWhiteSpace(line); + CustomMacroList.Append(macroName, macro); + AddMacroDef(ltCUSTOM_MACRO, macroName.c_str(), noArgs); + } - BibEatWhiteSpace(istr); - CustomMacroList.Append(macroName, macro); - AddMacroDef(ltCUSTOM_MACRO, macroName, noArgs); } - } - wxChar mbuf[200]; - wxSnprintf(mbuf, sizeof(mbuf), _T("Read initialization file %s."), filename); - OnInform(mbuf); - return true; + wxChar mbuf[200]; + wxSnprintf(mbuf, sizeof(mbuf), _T("Read initialization file %s."), filename.c_str()); + OnInform(mbuf); + return true; } CustomMacro *FindCustomMacro(wxChar *name)