Skip to content

Commit

Permalink
Always send utf-8 data from OMEdit to OMC
Browse files Browse the repository at this point in the history
Use the wide char APIs for file handling
  • Loading branch information
adeas31 committed May 28, 2019
1 parent c8b2e0e commit 767bbe4
Show file tree
Hide file tree
Showing 21 changed files with 161 additions and 138 deletions.
2 changes: 1 addition & 1 deletion OMCompiler/Compiler/Template/GenerateAPIFunctionsTpl.tpl
Expand Up @@ -246,7 +246,7 @@ template getQtInArg(Text name, DAE.Type ty, Text &varDecl)
case T_CODE(ty=C_TYPENAME(__))
case T_STRING(__) then
let &varDecl += 'QByteArray <%name%>_utf8 = <%name%>.toUtf8();<%\n%>'
'mmc_mk_scon(<%name%>_utf8.data())'
'mmc_mk_scon(<%name%>_utf8.constData())'
case T_INTEGER(__)
case T_BOOL(__)
case T_REAL(__) then name
Expand Down
3 changes: 1 addition & 2 deletions OMCompiler/SimulationRuntime/c/util/read_matlab4.c
Expand Up @@ -221,7 +221,6 @@ static int read_double(int type, size_t n, FILE *file, double *val)
/* Returns 0 on success; the error message on error */
const char* omc_new_matlab4_reader(const char *filename, ModelicaMatReader *reader)
{
typedef const char *_string;
const int nMatrix=6;
static const char *matrixNames[6]={"Aclass","name","description","dataInfo","data_1","data_2"};
static const char *matrixNamesMismatch[6]={"Matrix name mismatch: Aclass","Matrix name mismatch: name","Matrix name mismatch: description","Matrix name mismatch: dataInfo","Matrix name mismatch: data_1","Matrix name mismatch: data_2"};
Expand Down Expand Up @@ -497,7 +496,7 @@ const char* omc_new_matlab4_reader(const char *filename, ModelicaMatReader *read
default:
return "Implementation error: Unknown case";
}
};
}
return 0;
}

Expand Down
4 changes: 2 additions & 2 deletions OMEdit/OMEdit/OMEditGUI/Animation/AnimationUtil.h
Expand Up @@ -109,8 +109,8 @@ inline std::string assembleXMLFileName(const std::string& modelFile, const std::
/*! \brief Checks if the file is accessible. */
inline bool fileExists(const std::string& file)
{
struct stat buffer;
return (stat(file.c_str(), &buffer) == 0);
QFileInfo fileInfo(file.c_str());
return fileInfo.exists();
}

/*!
Expand Down
2 changes: 1 addition & 1 deletion OMEdit/OMEdit/OMEditGUI/CrashReport/CrashReportDialog.cpp
Expand Up @@ -213,7 +213,7 @@ void CrashReportDialog::createGDBBacktrace()
stackTraceFile.setFileName(OMStackTraceFilePath);
if (stackTraceFile.open(QIODevice::WriteOnly)) {
QTextStream out(&stackTraceFile);
out.setCodec(Helper::utf8.toStdString().data());
out.setCodec(Helper::utf8.toUtf8().constData());
out.setGenerateByteOrderMark(false);
out << mStackTrace;
out.flush();
Expand Down
2 changes: 1 addition & 1 deletion OMEdit/OMEdit/OMEditGUI/CrashReport/GDBBacktrace.cpp
Expand Up @@ -101,7 +101,7 @@ QString GDBBacktrace::createCommandsFile(QString *errorString)
gdbBacktraceCommandsFile.setFileName(gdbBacktraceCommandsFilePath);
if (gdbBacktraceCommandsFile.open(QIODevice::WriteOnly)) {
QTextStream out(&gdbBacktraceCommandsFile);
out.setCodec(Helper::utf8.toStdString().data());
out.setCodec(Helper::utf8.toUtf8().constData());
out.setGenerateByteOrderMark(false);
out << gdbBatchCommands;
out.flush();
Expand Down
20 changes: 10 additions & 10 deletions OMEdit/OMEdit/OMEditGUI/Debugger/GDB/CommandFactory.cpp
Expand Up @@ -96,7 +96,7 @@ QByteArray CommandFactory::breakInsert(QString fileName, int line, bool isDisabl
command.append(QString::number(ignoreCount));
}
command.append("\"\\\"" + fileName + "\\\":" + QString::number(line) + "\"");
return QByteArray(command.join(" ").toStdString().c_str());
return command.join(" ").toUtf8();
}

/*!
Expand Down Expand Up @@ -229,7 +229,7 @@ QByteArray CommandFactory::threadInfo()
QByteArray CommandFactory::stackListFrames(int thread)
{
QString command = QString("-stack-list-frames --thread %1").arg(thread);
return QByteArray(command.toStdString().c_str());
return command.toUtf8();
}

/*!
Expand All @@ -246,7 +246,7 @@ QByteArray CommandFactory::stackListFrames(int thread)
QByteArray CommandFactory::stackListVariables(int thread, int frame, QString printValues)
{
QString command = QString("-stack-list-variables --thread %1 --frame %2 %3").arg(thread).arg(frame).arg(printValues);
return QByteArray(command.toStdString().c_str());
return command.toUtf8();
}

/*!
Expand All @@ -272,7 +272,7 @@ QByteArray CommandFactory::createFullBacktrace()
QByteArray CommandFactory::dataEvaluateExpression(int thread, int frame, QString expression)
{
QString command = QString("-data-evaluate-expression --thread %1 --frame %2 \"%3\"").arg(thread).arg(frame).arg(expression);
return QByteArray(command.toStdString().c_str());
return command.toUtf8();
}

/*!
Expand All @@ -289,7 +289,7 @@ QByteArray CommandFactory::getTypeOfAny(int thread, int frame, QString expressio
{
QString command = QString("-data-evaluate-expression --thread %1 --frame %2 \"(char*)getTypeOfAny(%3, %4)\"").arg(thread).arg(frame)
.arg(expression).arg(inRecord ? "1" : "0");
return QByteArray(command.toStdString().c_str());
return command.toUtf8();
}

/*!
Expand All @@ -309,7 +309,7 @@ QByteArray CommandFactory::anyString(int thread, int frame, QString expression)
{
QString command = QString("-data-evaluate-expression --thread %1 --frame %2 \"(char*)anyString(%3)\"").arg(thread).arg(frame)
.arg(expression);
return QByteArray(command.toStdString().c_str());
return command.toUtf8();
}

/*!
Expand All @@ -330,7 +330,7 @@ QByteArray CommandFactory::getMetaTypeElement(int thread, int frame, QString exp
{
QString command = QString("-data-evaluate-expression --thread %1 --frame %2 \"(char*)getMetaTypeElement(%3, %4, %5)\"").arg(thread)
.arg(frame).arg(expression).arg(index).arg(mt);
return QByteArray(command.toStdString().c_str());
return command.toUtf8();
}

/*!
Expand All @@ -350,7 +350,7 @@ QByteArray CommandFactory::arrayLength(int thread, int frame, QString expression
{
QString command = QString("-data-evaluate-expression --thread %1 --frame %2 \"(int)mmc_gdb_arrayLength(%3)\"").arg(thread).arg(frame)
.arg(expression);
return QByteArray(command.toStdString().c_str());
return command.toUtf8();
}

/*!
Expand All @@ -364,7 +364,7 @@ QByteArray CommandFactory::arrayLength(int thread, int frame, QString expression
QByteArray CommandFactory::listLength(int thread, int frame, QString expression)
{
QString command = QString("-data-evaluate-expression --thread %1 --frame %2 \"(int)listLength(%3)\"").arg(thread).arg(frame).arg(expression);
return QByteArray(command.toStdString().c_str());
return command.toUtf8();
}

/*!
Expand All @@ -380,7 +380,7 @@ QByteArray CommandFactory::isOptionNone(int thread, int frame, QString expressio
{
QString command = QString("-data-evaluate-expression --thread %1 --frame %2 \"(int)isOptionNone(%3)\"").arg(thread).arg(frame)
.arg(expression);
return QByteArray(command.toStdString().c_str());
return command.toUtf8();
}

/*!
Expand Down
10 changes: 5 additions & 5 deletions OMEdit/OMEdit/OMEditGUI/Debugger/GDB/GDBAdapter.cpp
Expand Up @@ -147,7 +147,7 @@ void GDBLoggerWidget::postCommand()
if (mpCommandTextBox->text().isEmpty()) {
return;
}
GDBAdapter::instance()->postCommand(QByteArray(mpCommandTextBox->text().toStdString().c_str()));
GDBAdapter::instance()->postCommand(QByteArray(mpCommandTextBox->text().toUtf8().constData()));
}

/*!
Expand Down Expand Up @@ -701,7 +701,7 @@ void GDBAdapter::getMetaTypeElementCB(GDBMIResultRecord *pGDBMIResultRecord)
QString trimmedValue = value.mid(beginIndex, endIndex - beginIndex);
trimmedValue = trimmedValue.remove("\\");
QString name, displayName, type;
GDBMIResponse *pGDBMIResponse = parseGDBOutput(trimmedValue.toStdString().c_str());
GDBMIResponse *pGDBMIResponse = parseGDBOutput(trimmedValue.toUtf8().constData());
if (pGDBMIResponse) {
if (pGDBMIResponse->type == GDBMIResponse::ResultRecordResponse) {
GDBMIResult* pGDBMIResult = getGDBMIResult("omc_element", pGDBMIResponse->miResultRecord->miResultsList);
Expand Down Expand Up @@ -882,7 +882,7 @@ void GDBAdapter::handleGDBProcessStartedHelper()
mDebuggerLogFile.setFileName(QString("%1omeditdebugger.log").arg(tmpPath));
if (mDebuggerLogFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
mDebuggerLogFileTextStream.setDevice(&mDebuggerLogFile);
mDebuggerLogFileTextStream.setCodec(Helper::utf8.toStdString().data());
mDebuggerLogFileTextStream.setCodec(Helper::utf8.toUtf8().constData());
mDebuggerLogFileTextStream.setGenerateByteOrderMark(false);
}
emit GDBProcessStarted();
Expand Down Expand Up @@ -988,8 +988,8 @@ void GDBAdapter::processGDBMIResponse(QString response)
}

mCurrentResponse = response;
GDBMIResponse *pGDBMIResponse = parseGDBOutput(response.toStdString().c_str());
// fprintf(stdout, "Read Line :: %s\n\n", response.toStdString().c_str());fflush(NULL);
GDBMIResponse *pGDBMIResponse = parseGDBOutput(response.toUtf8().constData());
// fprintf(stdout, "Read Line :: %s\n\n", response.toUtf8().constData());fflush(NULL);
// fprintf(stdout, "Parsed Line :: ");fflush(NULL);
// printGDBMIResponse(pGDBMIResponse);
// fprintf(stdout, "\n\n");fflush(NULL);
Expand Down
Expand Up @@ -119,7 +119,7 @@ QString StackFrameItem::cleanupFunction(const QString &function)
if (function.startsWith("omc_")) {
cleanFunction = function.mid(4);
} else if (function.startsWith("_omcQuot_")) { /* if the names are converted to hex values */
char* hexString = omcHexToString(function.toStdString().c_str());
char* hexString = omcHexToString(function.toUtf8().constData());
cleanFunction = QString(hexString);
if (hexString) free(hexString);
}
Expand Down
14 changes: 11 additions & 3 deletions OMEdit/OMEdit/OMEditGUI/MainWindow.cpp
Expand Up @@ -130,11 +130,19 @@ void MainWindow::setUpMainWindow(threadData_t *threadData)
{
// Reopen the standard output stream.
QString outputFileName = Utilities::tempDirectory() + "/omeditoutput.txt";
freopen(outputFileName.toStdString().c_str(), "w", stdout);
#ifdef Q_OS_WIN
_wfreopen((wchar_t*)outputFileName.utf16(), L"w", stdout);
#else
freopen(outputFileName.toUtf8().constData(), "w", stdout);
#endif
setbuf(stdout, NULL); // used non-buffered stdout
// Reopen the standard error stream.
QString errorFileName = Utilities::tempDirectory() + "/omediterror.txt";
freopen(errorFileName.toStdString().c_str(), "w", stderr);
#ifdef Q_OS_WIN
_wfreopen((wchar_t*)errorFileName.utf16(), L"w", stderr);
#else
freopen(outputFileName.toUtf8().constData(), "w", stderr);
#endif
setbuf(stderr, NULL); // used non-buffered stderr
SplashScreen::instance()->showMessage(tr("Initializing"), Qt::AlignRight, Qt::white);
// Create an object of MessagesWidget.
Expand Down Expand Up @@ -1164,7 +1172,7 @@ void MainWindow::exportModelToOMNotebook(LibraryTreeItem *pLibraryTreeItem)
QFile omnotebookFile(omnotebookFileName);
omnotebookFile.open(QIODevice::WriteOnly);
QTextStream textStream(&omnotebookFile);
textStream.setCodec(Helper::utf8.toStdString().data());
textStream.setCodec(Helper::utf8.toUtf8().constData());
textStream.setGenerateByteOrderMark(false);
textStream << xmlDocument.toString();
omnotebookFile.close();
Expand Down
4 changes: 2 additions & 2 deletions OMEdit/OMEdit/OMEditGUI/Modeling/DocumentationWidget.cpp
Expand Up @@ -558,7 +558,7 @@ void DocumentationWidget::writeDocumentationFile(QString documentation)
/* Create a local file with the html we want to view as otherwise JavaScript does not run properly. */
mDocumentationFile.open(QIODevice::WriteOnly);
QTextStream out(&mDocumentationFile);
out.setCodec(Helper::utf8.toStdString().data());
out.setCodec(Helper::utf8.toUtf8().constData());
out << documentation;
mDocumentationFile.close();
}
Expand Down Expand Up @@ -1133,7 +1133,7 @@ DocumentationViewer::DocumentationViewer(DocumentationWidget *pDocumentationWidg
settings()->setFontFamily(QWebSettings::StandardFont, Helper::systemFontInfo.family());
settings()->setFontSize(QWebSettings::DefaultFontSize, Helper::systemFontInfo.pointSize());
settings()->setAttribute(QWebSettings::LocalStorageEnabled, true);
settings()->setDefaultTextEncoding(Helper::utf8.toStdString().data());
settings()->setDefaultTextEncoding(Helper::utf8.toUtf8().constData());
// set DocumentationViewer web page policy
page()->setContentEditable(isContentEditable);
page()->setLinkDelegationPolicy(QWebPage::DelegateAllLinks);
Expand Down
2 changes: 1 addition & 1 deletion OMEdit/OMEdit/OMEditGUI/Modeling/LibraryTreeWidget.cpp
Expand Up @@ -4545,7 +4545,7 @@ bool LibraryWidget::saveFile(QString fileName, QString contents)
if (file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
QTextStream textStream(&file);
// set to UTF-8
textStream.setCodec(Helper::utf8.toStdString().data());
textStream.setCodec(Helper::utf8.toUtf8().constData());
textStream.setGenerateByteOrderMark(bom);
textStream << newContents;
file.close();
Expand Down
4 changes: 2 additions & 2 deletions OMEdit/OMEdit/OMEditGUI/Modeling/ModelWidgetContainer.cpp
Expand Up @@ -4793,7 +4793,7 @@ bool ModelWidget::writeCoSimulationResultFile(QString fileName)
if (file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
QTextStream resultFile(&file);
// set to UTF-8
resultFile.setCodec(Helper::utf8.toStdString().data());
resultFile.setCodec(Helper::utf8.toUtf8().constData());
resultFile.setGenerateByteOrderMark(false);
// write result file header
resultFile << "\"" << "time\",";
Expand Down Expand Up @@ -4936,7 +4936,7 @@ bool ModelWidget::writeVisualXMLFile(QString fileName, bool canWriteVisualXMLFil
if (file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
QTextStream visualFile(&file);
// set to UTF-8
visualFile.setCodec(Helper::utf8.toStdString().data());
visualFile.setCodec(Helper::utf8.toUtf8().constData());
visualFile.setGenerateByteOrderMark(false);

visualFile << "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n";
Expand Down
4 changes: 2 additions & 2 deletions OMEdit/OMEdit/OMEditGUI/Modeling/ModelicaClassDialog.cpp
Expand Up @@ -469,7 +469,7 @@ void OpenModelicaFile::convertModelicaFile(QString fileName, QTextCodec *pCodec)
file.close();
file.open(QIODevice::WriteOnly | QIODevice::Truncate);
QTextStream out(&file);
out.setCodec(Helper::utf8.toStdString().data());
out.setCodec(Helper::utf8.toUtf8().constData());
out.setGenerateByteOrderMark(false);
out << fileData;
file.close();
Expand Down Expand Up @@ -536,7 +536,7 @@ void OpenModelicaFile::openModelicaFiles(bool convertedToUTF8)
*/
void OpenModelicaFile::convertModelicaFiles()
{
QTextCodec *pCodec = QTextCodec::codecForName(mpEncodingComboBox->itemData(mpEncodingComboBox->currentIndex()).toString().toStdString().data());
QTextCodec *pCodec = QTextCodec::codecForName(mpEncodingComboBox->itemData(mpEncodingComboBox->currentIndex()).toString().toUtf8().constData());
if (pCodec != NULL)
{
MainWindow::instance()->getStatusBar()->showMessage(tr("Converting files to UTF-8"));
Expand Down
30 changes: 19 additions & 11 deletions OMEdit/OMEdit/OMEditGUI/OMC/OMCProxy.cpp
Expand Up @@ -188,17 +188,25 @@ bool OMCProxy::initializeOMC(threadData_t *threadData)
QString& tmpPath = Utilities::tempDirectory();
/* create a file to write OMEdit communication log */
QString communicationLogFilePath = QString("%1omeditcommunication.log").arg(tmpPath);
mpCommunicationLogFile = fopen(communicationLogFilePath.toStdString().c_str(), "w");
#ifdef Q_OS_WIN
mpCommunicationLogFile = _wfopen((wchar_t*)communicationLogFilePath.utf16(), L"w");
#else
mpCommunicationLogFile = fopen(communicationLogFilePath.toUtf8().constData(), "w");
#endif
/* create a file to write OMEdit commands */
QString commandsLogFilePath = QString("%1omeditcommands.mos").arg(tmpPath);
mpCommandsLogFile = fopen(commandsLogFilePath.toStdString().c_str(), "w");
#ifdef Q_OS_WIN
mpCommandsLogFile = _wfopen((wchar_t*)commandsLogFilePath.utf16(), L"w");
#else
mpCommandsLogFile = fopen(commandsLogFilePath.toUtf8().constData(), "w");
#endif
// read the locale
QSettings *pSettings = Utilities::getApplicationSettings();
QLocale settingsLocale = QLocale(pSettings->value("language").toString());
settingsLocale = settingsLocale.name() == "C" ? pSettings->value("language").toLocale() : settingsLocale;
void *args = mmc_mk_nil();
QString locale = "+locale=" + settingsLocale.name();
args = mmc_mk_cons(mmc_mk_scon(locale.toStdString().c_str()), args);
args = mmc_mk_cons(mmc_mk_scon(locale.toUtf8().constData()), args);
// initialize garbage collector
omc_System_initGarbageCollector(NULL);
MMC_TRY_TOP_INTERNAL()
Expand All @@ -217,7 +225,7 @@ bool OMCProxy::initializeOMC(threadData_t *threadData)
Helper::OpenModelicaHome = mpOMCInterface->getInstallationDirectoryPath();
#ifdef WIN32
MMC_TRY_TOP_INTERNAL()
omc_Main_setWindowsPaths(threadData, mmc_mk_scon(Helper::OpenModelicaHome.toStdString().c_str()));
omc_Main_setWindowsPaths(threadData, mmc_mk_scon(Helper::OpenModelicaHome.toUtf8().constData()));
MMC_CATCH_TOP()
#endif
/* set the tmp directory as the working directory */
Expand Down Expand Up @@ -263,7 +271,7 @@ void OMCProxy::sendCommand(const QString expression, bool saveToHistory)

MMC_TRY_STACK()

if (!omc_Main_handleCommand(threadData, mmc_mk_scon(expression.toStdString().c_str()), &reply_str)) {
if (!omc_Main_handleCommand(threadData, mmc_mk_scon(expression.toUtf8().constData()), &reply_str)) {
if (expression == "quit()") {
return;
}
Expand Down Expand Up @@ -325,14 +333,14 @@ void OMCProxy::logCommand(QString command, QTime *commandTime, bool saveToHistor
}
// write the log to communication log file
if (mpCommunicationLogFile) {
fputs(QString("%1 %2\n").arg(command, commandTime->currentTime().toString("hh:mm:ss:zzz")).toStdString().c_str(), mpCommunicationLogFile);
fputs(QString("%1 %2\n").arg(command, commandTime->currentTime().toString("hh:mm:ss:zzz")).toUtf8().constData(), mpCommunicationLogFile);
}
// write commands mos file
if (mpCommandsLogFile) {
if (command.compare("quit()") == 0) {
fputs(QString("%1;\n").arg(command).toStdString().c_str(), mpCommandsLogFile);
fputs(QString("%1;\n").arg(command).toUtf8().constData(), mpCommandsLogFile);
} else {
fputs(QString("%1; getErrorString();\n").arg(command).toStdString().c_str(), mpCommandsLogFile);
fputs(QString("%1; getErrorString();\n").arg(command).toUtf8().constData(), mpCommandsLogFile);
}
}
}
Expand Down Expand Up @@ -364,9 +372,9 @@ void OMCProxy::logResponse(QString command, QString response, QTime *responseTim
Utilities::insertText(mpOMCLoggerTextBox, response + "\n\n", format);
// write the log to communication log file
if (mpCommunicationLogFile) {
fputs(QString("%1 %2\n").arg(response).arg(responseTime->currentTime().toString("hh:mm:ss:zzz")).toStdString().c_str(), mpCommunicationLogFile);
fputs(QString("%1 %2\n").arg(response).arg(responseTime->currentTime().toString("hh:mm:ss:zzz")).toUtf8().constData(), mpCommunicationLogFile);
mTotalOMCCallsTime += (double)responseTime->elapsed() / 1000;
fputs(QString("#s#; %1; %2; \'%3\'\n\n").arg(QString::number((double)responseTime->elapsed() / 1000)).arg(QString::number(mTotalOMCCallsTime)).arg(firstLine).toStdString().c_str(), mpCommunicationLogFile);
fputs(QString("#s#; %1; %2; \'%3\'\n\n").arg(QString::number((double)responseTime->elapsed() / 1000)).arg(QString::number(mTotalOMCCallsTime)).arg(firstLine).toUtf8().constData(), mpCommunicationLogFile);
}
}
}
Expand Down Expand Up @@ -652,7 +660,7 @@ void OMCProxy::loadUserLibraries()
pSettings->endGroup();
foreach (QString lib, libraries) {
QString encoding = pSettings->value("userlibraries/" + lib).toString();
QString fileName = QUrl::fromPercentEncoding(QByteArray(lib.toStdString().c_str()));
QString fileName = QUrl::fromPercentEncoding(QByteArray(lib.toUtf8().constData()));
QStringList classesList = parseFile(fileName, encoding);
if (!classesList.isEmpty()) {
/*
Expand Down

0 comments on commit 767bbe4

Please sign in to comment.