X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/9c9691ba241768af0cc050e021c8323b7d16fe5a..86d58c90cbc82c8f71155497150139985576b9c3:/utils/tex2rtf/src/texutils.cpp diff --git a/utils/tex2rtf/src/texutils.cpp b/utils/tex2rtf/src/texutils.cpp index f595df4def..5bf4838829 100644 --- a/utils/tex2rtf/src/texutils.cpp +++ b/utils/tex2rtf/src/texutils.cpp @@ -23,11 +23,13 @@ #include "wx/app.h" #include "wx/hash.h" +#include "wx/textfile.h" #ifdef new #undef new #endif +#include "wx/beforestd.h" #if wxUSE_IOSTREAMH #include #include @@ -36,14 +38,13 @@ #include using namespace std; #endif +#include "wx/afterstd.h" #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 +209,7 @@ wxChar *FindTopicName(TexChunk *chunk) * */ -void StartSimulateArgument(wxChar *data) +void StartSimulateArgument(const wxChar *data) { wxStrcpy(currentArgData, data); haveArgData = true; @@ -224,8 +225,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 +390,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); } - } } @@ -487,15 +475,15 @@ void BibEatWhiteSpace(wxString& line) { while(!line.empty() && (line[0] == _T(' ') || line[0] == _T('\t') || line[0] == (wxChar)EOF)) { - if (line[0] == 10) + if (line[0] == '\r') BibLine ++; line = line.substr(1); } // Ignore end-of-line comments - if (line[0] == _T('%') || line[0] == _T(';') || line[0] == _T('#')) + if ( !line.empty() && (line[0] == _T('%') || line[0] == _T(';') || line[0] == _T('#'))) { - line = wxEmptyString; + line.clear(); } } @@ -577,13 +565,13 @@ wxString BibReadToEOL(wxString& line) // 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] != 9) && + (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] == '"') + if (!line.empty() && line[0] == '"') line = line.substr(1); return val; @@ -721,7 +709,7 @@ void BibReadValue(wxSTD istream& istr, wxChar *buffer, bool ignoreBraces = true, wxUnusedVar(stopping); } -bool ReadBib(wxChar *filename) +bool ReadBib(const wxChar *filename) { if (!wxFileExists(filename)) return false; @@ -1173,7 +1161,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); @@ -1184,7 +1172,7 @@ void AddCitation(wxChar *citeKey) } } -TexRef *FindReference(wxChar *key) +TexRef *FindReference(const wxChar *key) { return (TexRef *)TexReferences.Get(key); } @@ -1200,6 +1188,7 @@ bool StringTobool(const wxString& val) up.MakeUpper(); if (up.IsSameAs(_T("YES")) || + up.IsSameAs(_T("TRUE")) || up.IsSameAs(_T("ON")) || up.IsSameAs(_T("OK")) | up.IsSameAs(_T("1"))) @@ -1539,7 +1528,7 @@ bool ReadCustomMacros(const wxString& filename) macro->macroBody = copystring(macroBody.c_str()); BibEatWhiteSpace(line); - CustomMacroList.Append(macroName.c_str(), macro); + CustomMacroList.Append(macroName, macro); AddMacroDef(ltCUSTOM_MACRO, macroName.c_str(), noArgs); }