Skip to content

Commit

Permalink
- updated parseFile, parseString to scripting API.
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@24476 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
adeas31 committed Feb 8, 2015
1 parent a887df1 commit 122cb67
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 51 deletions.
4 changes: 2 additions & 2 deletions OMEdit/OMEditGUI/Editors/ModelicaTextEditor.cpp
Expand Up @@ -172,9 +172,9 @@ QStringList ModelicaTextEditor::getClassNames(QString *errorString)
return QStringList();
} else {
if (pLibraryTreeNode->getParentName().isEmpty()) {
classNames = pOMCProxy->parseString(StringHandler::escapeString(toPlainText()), pLibraryTreeNode->getNameStructure());
classNames = pOMCProxy->parseString(toPlainText(), pLibraryTreeNode->getNameStructure());
} else {
classNames = pOMCProxy->parseString("within " + pLibraryTreeNode->getParentName() + ";" + StringHandler::escapeString(toPlainText()), pLibraryTreeNode->getNameStructure());
classNames = pOMCProxy->parseString("within " + pLibraryTreeNode->getParentName() + ";" + toPlainText(), pLibraryTreeNode->getNameStructure());
}
}
// if user is defining multiple top level classes.
Expand Down
38 changes: 13 additions & 25 deletions OMEdit/OMEditGUI/Modeling/LibraryTreeWidget.cpp
Expand Up @@ -1841,41 +1841,35 @@ void LibraryTreeWidget::openFile(QString fileName, QString encoding, bool showPr
// get the class names now to check if they are already loaded or not
QStringList existingmodelsList;
if (showProgress) mpMainWindow->getStatusBar()->showMessage(QString(Helper::loading).append(": ").append(fileName));
if (mpMainWindow->getOMCProxy()->parseFile(fileName, encoding))
{
QString result = StringHandler::removeFirstLastCurlBrackets(mpMainWindow->getOMCProxy()->getResult());
QStringList modelsList = result.split(",", QString::SkipEmptyParts);
QStringList classesList = mpMainWindow->getOMCProxy()->parseFile(fileName, encoding);
if (!classesList.isEmpty()) {
/*
Only allow loading of files that has just one nonstructured entity.
From Modelica specs section 13.2.2.2,
"A nonstructured entity [e.g. the file A.mo] shall contain only a stored-definition that defines a class [A] with a name
matching the name of the nonstructured entity."
*/
if (modelsList.size() > 1)
{
if (classesList.size() > 1) {
QMessageBox *pMessageBox = new QMessageBox(mpMainWindow);
pMessageBox->setWindowTitle(QString(Helper::applicationName).append(" - ").append(Helper::error));
pMessageBox->setIcon(QMessageBox::Critical);
pMessageBox->setText(QString(GUIMessages::getMessage(GUIMessages::UNABLE_TO_LOAD_FILE).arg(fileName)));
pMessageBox->setInformativeText(QString(GUIMessages::getMessage(GUIMessages::MULTIPLE_TOP_LEVEL_CLASSES)).arg(fileName)
.arg(modelsList.join(",")));
.arg(classesList.join(",")));
pMessageBox->setStandardButtons(QMessageBox::Ok);
pMessageBox->exec();
return;
}
bool existModel = false;
// check if the model already exists
foreach(QString model, modelsList)
{
if (mpMainWindow->getOMCProxy()->existClass(model))
{
foreach(QString model, classesList) {
if (mpMainWindow->getOMCProxy()->existClass(model)) {
existingmodelsList.append(model);
existModel = true;
}
}
// if existModel is true, show user an error message
if (existModel)
{
if (existModel) {
QMessageBox *pMessageBox = new QMessageBox(mpMainWindow);
pMessageBox->setWindowTitle(QString(Helper::applicationName).append(" - ").append(Helper::information));
pMessageBox->setIcon(QMessageBox::Information);
Expand All @@ -1885,22 +1879,16 @@ void LibraryTreeWidget::openFile(QString fileName, QString encoding, bool showPr
.append(GUIMessages::getMessage(GUIMessages::DELETE_AND_LOAD).arg(fileName)));
pMessageBox->setStandardButtons(QMessageBox::Ok);
pMessageBox->exec();
}
// if no conflicting model found then just load the file simply
else
{
} else { // if no conflicting model found then just load the file simply
// load the file in OMC
if (mpMainWindow->getOMCProxy()->loadFile(fileName, encoding))
{
if (mpMainWindow->getOMCProxy()->loadFile(fileName, encoding)) {
// create library tree nodes for loaded models
int progressvalue = 0;
if (showProgress)
{
mpMainWindow->getProgressBar()->setRange(0, modelsList.size());
if (showProgress) {
mpMainWindow->getProgressBar()->setRange(0, classesList.size());
mpMainWindow->showProgressBar();
}
foreach (QString model, modelsList)
{
foreach (QString model, classesList) {
LibraryTreeNode *pLibraryTreeNode = addLibraryTreeNode(model, mpMainWindow->getOMCProxy()->getClassRestriction(model), "");
createLibraryTreeNodes(pLibraryTreeNode);
if (showProgress) mpMainWindow->getProgressBar()->setValue(++progressvalue);
Expand All @@ -1915,7 +1903,7 @@ void LibraryTreeWidget::openFile(QString fileName, QString encoding, bool showPr

void LibraryTreeWidget::parseAndLoadModelicaText(QString modelText)
{
QStringList classNames = mpMainWindow->getOMCProxy()->parseString(StringHandler::escapeString(modelText), "");
QStringList classNames = mpMainWindow->getOMCProxy()->parseString(modelText, "");
if (classNames.size() == 0) {
return;
}
Expand Down
4 changes: 2 additions & 2 deletions OMEdit/OMEditGUI/Modeling/ModelWidgetContainer.cpp
Expand Up @@ -2723,10 +2723,10 @@ bool ModelWidget::modelicaEditorTextChanged()
/* if no errors are found with the Modelica Text then load it in OMC */
QString modelicaText = mpModelicaTextEditor->toPlainText();
if (mpLibraryTreeNode->getParentName().isEmpty()) {
if (!pOMCProxy->loadString(StringHandler::escapeString(modelicaText), classNames.at(0)))
if (!pOMCProxy->loadString(modelicaText, classNames.at(0)))
return false;
} else {
if (!pOMCProxy->loadString("within " + mpLibraryTreeNode->getParentName() + ";" + StringHandler::escapeString(modelicaText), classNames.at(0))) {
if (!pOMCProxy->loadString("within " + mpLibraryTreeNode->getParentName() + ";" + modelicaText, classNames.at(0))) {
return false;
}
}
Expand Down
35 changes: 15 additions & 20 deletions OMEdit/OMEditGUI/OMC/OMCProxy.cpp
Expand Up @@ -972,30 +972,29 @@ void OMCProxy::loadUserLibraries(QSplashScreen *pSplashScreen)
QString encoding = pSettings->value("userlibraries/" + lib).toString();
QString fileName = QUrl::fromPercentEncoding(QByteArray(lib.toStdString().c_str()));
pSplashScreen->showMessage(QString(Helper::loading).append(" ").append(fileName), Qt::AlignRight, Qt::white);
if (parseFile(fileName, encoding)) {
QString result = StringHandler::removeFirstLastCurlBrackets(getResult());
QStringList modelsList = result.split(",", QString::SkipEmptyParts);
QStringList classesList = parseFile(fileName, encoding);
if (!classesList.isEmpty()) {
/*
Only allow loading of files that has just one nonstructured entity.
From Modelica specs section 13.2.2.2,
"A nonstructured entity [e.g. the file A.mo] shall contain only a stored-definition that defines a class [A] with a name
matching the name of the nonstructured entity."
*/
if (modelsList.size() > 1) {
if (classesList.size() > 1) {
QMessageBox *pMessageBox = new QMessageBox(mpMainWindow);
pMessageBox->setWindowTitle(QString(Helper::applicationName).append(" - ").append(Helper::error));
pMessageBox->setIcon(QMessageBox::Critical);
pMessageBox->setText(QString(GUIMessages::getMessage(GUIMessages::UNABLE_TO_LOAD_FILE).arg(fileName)));
pMessageBox->setInformativeText(QString(GUIMessages::getMessage(GUIMessages::MULTIPLE_TOP_LEVEL_CLASSES)).arg(fileName)
.arg(modelsList.join(",")));
.arg(classesList.join(",")));
pMessageBox->setStandardButtons(QMessageBox::Ok);
pMessageBox->exec();
return;
}
QStringList existingmodelsList;
bool existModel = false;
// check if the model already exists
foreach(QString model, modelsList) {
foreach(QString model, classesList) {
if (existClass(model)) {
existingmodelsList.append(model);
existModel = true;
Expand Down Expand Up @@ -1657,7 +1656,7 @@ bool OMCProxy::loadFile(QString fileName, QString encoding, bool uses)
*/
bool OMCProxy::loadString(QString value, QString fileName, QString encoding, bool checkError)
{
bool result = mpOMCInterface->loadString(value.replace("\"", "\\\""), fileName, encoding);
bool result = mpOMCInterface->loadString(StringHandler::escapeStringWithoutNewLine(value), fileName, encoding);
if (checkError) {
printMessagesStringInternal();
}
Expand All @@ -1669,30 +1668,26 @@ bool OMCProxy::loadString(QString value, QString fileName, QString encoding, boo
\param fileName - the file to parse.
\return true on success
*/
bool OMCProxy::parseFile(QString fileName, QString encoding)
QList<QString> OMCProxy::parseFile(QString fileName, QString encoding)
{
fileName = fileName.replace('\\', '/');
sendCommand("parseFile(\"" + fileName + "\",\"" + encoding + "\")");
if (getResult() == "{}") {
QList<QString> resultList = mpOMCInterface->parseFile(fileName, encoding);
if (resultList.isEmpty()) {
printMessagesStringInternal();
return false;
} else {
return true;
}
return resultList;
}

/*!
Parse the string. Doesn't load it into OMC.
\param value - the string to parse.
\return the list of models inside the string.
*/
QStringList OMCProxy::parseString(QString value, QString fileName)
QList<QString> OMCProxy::parseString(QString value, QString fileName)
{
sendCommand("parseString(\"" + value.replace("\"", "\\\"") + "\", \"" + fileName + "\")");
QString result = StringHandler::removeFirstLastCurlBrackets(getResult());
QStringList list = result.split(",", QString::SkipEmptyParts);
QList<QString> resultList = mpOMCInterface->parseString(StringHandler::escapeStringWithoutNewLine(value), fileName);
printMessagesStringInternal();
return list;
return resultList;
}

/*!
Expand All @@ -1709,7 +1704,7 @@ bool OMCProxy::createClass(QString type, QString className, QString extendsClass
} else {
expression = type + " " + className + " extends " + extendsClass + "; end " + className + ";";
}
return loadString(StringHandler::escapeString(expression), className, Helper::utf8, false);
return loadString(expression, className, Helper::utf8, false);
}

/*!
Expand All @@ -1727,7 +1722,7 @@ bool OMCProxy::createSubClass(QString type, QString className, QString parentCla
} else {
expression = "within " + parentClassName + "; " + type + " " + className + " extends " + extendsClass + "; end " + className + ";";
}
return loadString(StringHandler::escapeString(expression), parentClassName + "." + className, Helper::utf8, false);
return loadString(expression, parentClassName + "." + className, Helper::utf8, false);
}

/*!
Expand Down
4 changes: 2 additions & 2 deletions OMEdit/OMEditGUI/OMC/OMCProxy.h
Expand Up @@ -163,8 +163,8 @@ class OMCProxy : public QObject
bool requireExactVersion = false);
bool loadFile(QString fileName, QString encoding = Helper::utf8, bool uses = true);
bool loadString(QString value, QString fileName, QString encoding = Helper::utf8, bool checkError = true);
bool parseFile(QString fileName, QString encoding = Helper::utf8);
QStringList parseString(QString value, QString fileName);
QList<QString> parseFile(QString fileName, QString encoding = Helper::utf8);
QList<QString> parseString(QString value, QString fileName);
bool createClass(QString type, QString className, QString extendsClass);
bool createSubClass(QString type, QString className, QString parentClassName, QString extendsClass);
bool existClass(QString className);
Expand Down
21 changes: 21 additions & 0 deletions OMEdit/OMEditGUI/Util/StringHandler.cpp
Expand Up @@ -891,6 +891,27 @@ QString StringHandler::escapeString(QString value)
return res;
}

QString StringHandler::escapeStringWithoutNewLine(QString value)
{
QString res;
value = value.trimmed();
for (int i = 0; i < value.length(); i++) {
switch (value[i].toAscii()) {
case '"': res.append('\"'); break;
case '\\': res.append("\\\\"); break;
case '\a': res.append("\\a"); break;
case '\b': res.append("\\b"); break;
case '\f': res.append("\\f"); break;
case '\n': res.append(""); break;
case '\r': res.append("\\r"); break;
case '\t': res.append("\\t"); break;
case '\v': res.append("\\v"); break;
default: res.append(value[i]); break;
}
}
return res;
}

#define CONSUME_CHAR(value,res,i) \
if (value.at(i) == '\\') { \
i++; \
Expand Down
1 change: 1 addition & 0 deletions OMEdit/OMEditGUI/Util/StringHandler.h
Expand Up @@ -116,6 +116,7 @@ class StringHandler : public QObject
static QString removeComment(QString value);
static QString getModifierValue(QString value);
static QString escapeString(QString value);
static QString escapeStringWithoutNewLine(QString value);
// Returns "" if the string is not a standard Modelica string. Else it unparses it into normal form.
static QString unparse(QString value);
// Returns empty list if the string is not a standard Modelica string-array. Else it unparses it into normal form.
Expand Down

0 comments on commit 122cb67

Please sign in to comment.