Skip to content

Commit

Permalink
Moved the bom detection code to Utilities.
Browse files Browse the repository at this point in the history
This should speed up the OMEdit starting since now we don't read all the MSL files to detect bom.
  • Loading branch information
adeas31 committed Apr 28, 2016
1 parent d8a8af9 commit bef411c
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 61 deletions.
79 changes: 24 additions & 55 deletions OMEdit/OMEditGUI/Modeling/LibraryTreeWidget.cpp
Expand Up @@ -309,7 +309,6 @@ LibraryTreeItem::LibraryTreeItem()
setClassTextAfter("");
setExpanded(false);
setNonExisting(true);
setHasBOM(false);
}

/*!
Expand Down Expand Up @@ -339,7 +338,6 @@ LibraryTreeItem::LibraryTreeItem(LibraryType type, QString text, QString nameStr
setReadOnly(!StringHandler::isFileWritAble(fileName));
}
setIsSaved(isSaved);
detectBOM();
if (isFilePathValid()) {
QFileInfo fileInfo(getFileName());
// if item has file name as package.mo and is top level then its save folder structure
Expand Down Expand Up @@ -715,31 +713,6 @@ void LibraryTreeItem::emitComponentAdded(Component *pComponent)
emit componentAddedForComponent();
}

/*!
* \brief LibraryTreeItem::detectBOM
* Detects if the file has byte order mark (BOM) or not.
*/
void LibraryTreeItem::detectBOM()
{
if (isFilePathValid()) {
QFile file(getFileName());
if (file.open(QIODevice::ReadOnly)) {
QByteArray data = file.readAll();
const int bytesRead = data.size();
const unsigned char *buf = reinterpret_cast<const unsigned char *>(data.constData());
// code taken from qtextstream
if (bytesRead >= 3 && ((buf[0] == 0xef && buf[1] == 0xbb) && buf[2] == 0xbf)) {
setHasBOM(true);
} else {
setHasBOM(false);
}
file.close();
}
} else {
setHasBOM(false);
}
}

/*!
* \brief LibraryTreeItem::handleLoaded
* Handles the case when an undefined inherited class is loaded.
Expand Down Expand Up @@ -3131,37 +3104,33 @@ void LibraryWidget::parseAndLoadModelicaText(QString modelText)
* Saves the file with contents.
* \param fileName
* \param contents
* \param pLibraryTreeItem
* \return
*/
bool LibraryWidget::saveFile(QString fileName, QString contents, LibraryTreeItem *pLibraryTreeItem)
bool LibraryWidget::saveFile(QString fileName, QString contents)
{
// set the BOM settings
QComboBox *pBOMComboBox = mpMainWindow->getOptionsDialog()->getTextEditorPage()->getBOMComboBox();
Utilities::BomMode bomMode = (Utilities::BomMode)pBOMComboBox->itemData(pBOMComboBox->currentIndex()).toInt();
bool bom = false;
switch (bomMode) {
case Utilities::AlwaysAddBom:
bom = true;
break;
case Utilities::KeepBom:
bom = Utilities::detectBOM(fileName);
break;
case Utilities::AlwaysDeleteBom:
default:
bom = false;
break;
}
// open the file for writing
QFile file(fileName);
if (file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
QTextStream textStream(&file);
// set to UTF-8
textStream.setCodec(Helper::utf8.toStdString().data());
// set the BOM settings
QComboBox *pBOMComboBox = mpMainWindow->getOptionsDialog()->getTextEditorPage()->getBOMComboBox();
Utilities::BomMode bomMode = (Utilities::BomMode)pBOMComboBox->itemData(pBOMComboBox->currentIndex()).toInt();
switch (bomMode) {
case Utilities::AlwaysAddBom:
textStream.setGenerateByteOrderMark(true);
if (pLibraryTreeItem) {
pLibraryTreeItem->setHasBOM(true);
}
break;
case Utilities::KeepBom:
textStream.setGenerateByteOrderMark(pLibraryTreeItem ? pLibraryTreeItem->hasBOM() : false);
break;
case Utilities::AlwaysDeleteBom:
default:
textStream.setGenerateByteOrderMark(false);
if (pLibraryTreeItem) {
pLibraryTreeItem->setHasBOM(false);
}
break;
}
textStream.setGenerateByteOrderMark(bom);
// set the line ending format
QComboBox *pLineEndingComboBox = mpMainWindow->getOptionsDialog()->getTextEditorPage()->getLineEndingComboBox();
Utilities::LineEndingMode lineEndingMode = (Utilities::LineEndingMode)pLineEndingComboBox->itemData(pLineEndingComboBox->currentIndex()).toInt();
Expand Down Expand Up @@ -3365,7 +3334,7 @@ bool LibraryWidget::saveModelicaLibraryTreeItemOneFile(LibraryTreeItem *pLibrary
} else {
contents = pLibraryTreeItem->getClassText(mpLibraryTreeModel);
}
if (saveFile(fileName, contents, pLibraryTreeItem)) {
if (saveFile(fileName, contents)) {
/* mark the file as saved and update the labels. */
pLibraryTreeItem->setIsSaved(true);
pLibraryTreeItem->setFileName(fileName);
Expand Down Expand Up @@ -3460,7 +3429,7 @@ bool LibraryWidget::saveModelicaLibraryTreeItemFolder(LibraryTreeItem *pLibraryT
} else {
contents = pLibraryTreeItem->getClassText(mpLibraryTreeModel);
}
if (saveFile(fileName, contents, pLibraryTreeItem)) {
if (saveFile(fileName, contents)) {
/* mark the file as saved and update the labels. */
pLibraryTreeItem->setIsSaved(true);
pLibraryTreeItem->setFileName(fileName);
Expand Down Expand Up @@ -3507,7 +3476,7 @@ bool LibraryWidget::saveModelicaLibraryTreeItemFolder(LibraryTreeItem *pLibraryT
contents.append(pLibraryTreeItem->child(i)->getName()).append("\n");
}
// create a new package.order file
saveFile(QString("%1/package.order").arg(fileInfo.absoluteDir().absolutePath()), contents, pLibraryTreeItem);
saveFile(QString("%1/package.order").arg(fileInfo.absoluteDir().absolutePath()), contents);
return true;
}

Expand All @@ -3531,7 +3500,7 @@ bool LibraryWidget::saveTextLibraryTreeItem(LibraryTreeItem *pLibraryTreeItem)
fileName = pLibraryTreeItem->getFileName();
}

if (saveFile(fileName, pLibraryTreeItem->getModelWidget()->getEditor()->getPlainTextEdit()->toPlainText(), pLibraryTreeItem)) {
if (saveFile(fileName, pLibraryTreeItem->getModelWidget()->getEditor()->getPlainTextEdit()->toPlainText())) {
/* mark the file as saved and update the labels. */
pLibraryTreeItem->setIsSaved(true);
pLibraryTreeItem->setFileName(fileName);
Expand Down Expand Up @@ -3586,7 +3555,7 @@ bool LibraryWidget::saveAsMetaModelLibraryTreeItem(LibraryTreeItem *pLibraryTree
*/
bool LibraryWidget::saveMetaModelLibraryTreeItem(LibraryTreeItem *pLibraryTreeItem, QString fileName)
{
if (saveFile(fileName, pLibraryTreeItem->getModelWidget()->getEditor()->getPlainTextEdit()->toPlainText(), pLibraryTreeItem)) {
if (saveFile(fileName, pLibraryTreeItem->getModelWidget()->getEditor()->getPlainTextEdit()->toPlainText())) {
/* mark the file as saved and update the labels. */
pLibraryTreeItem->setIsSaved(true);
QString oldMetaModelFile = pLibraryTreeItem->getFileName();
Expand Down
7 changes: 1 addition & 6 deletions OMEdit/OMEditGUI/Modeling/LibraryTreeWidget.h
Expand Up @@ -127,8 +127,6 @@ class LibraryTreeItem : public QObject
bool isExpanded() const {return mExpanded;}
void setNonExisting(bool nonExisting) {mNonExisting = nonExisting;}
bool isNonExisting() const {return mNonExisting;}
void setHasBOM(bool hasBOM) {mHasBOM = hasBOM;}
bool hasBOM() const {return mHasBOM;}
void updateAttributes();
QIcon getLibraryTreeItemIcon();
bool inRange(int lineNumber) {return (lineNumber >= mClassInformation.lineNumberStart) && (lineNumber <= mClassInformation.lineNumberEnd);}
Expand Down Expand Up @@ -177,9 +175,6 @@ class LibraryTreeItem : public QObject
QString mClassTextAfter;
bool mExpanded;
bool mNonExisting;
bool mHasBOM;

void detectBOM();
signals:
void loaded(LibraryTreeItem *pLibraryTreeItem);
void loadedForComponent();
Expand Down Expand Up @@ -366,7 +361,7 @@ class LibraryWidget : public QWidget
void openMetaModelOrTextFile(QFileInfo fileInfo, bool showProgress = true);
bool parseMetaModelFile(QFileInfo fileInfo);
void parseAndLoadModelicaText(QString modelText);
bool saveFile(QString fileName, QString contents, LibraryTreeItem *pLibraryTreeItem = 0);
bool saveFile(QString fileName, QString contents);
bool saveLibraryTreeItem(LibraryTreeItem *pLibraryTreeItem);
void saveAsLibraryTreeItem(LibraryTreeItem *pLibraryTreeItem);
bool saveTotalLibraryTreeItem(LibraryTreeItem *pLibraryTreeItem);
Expand Down
26 changes: 26 additions & 0 deletions OMEdit/OMEditGUI/Util/Utilities.cpp
Expand Up @@ -338,6 +338,32 @@ QFrame* Utilities::getHeadingLine()
return pHeadingLine;
}

/*!
* \brief Utilities::detectBOM
* Detects if the file has byte order mark (BOM) or not.
* \param fileName
* \return
*/
bool Utilities::detectBOM(QString fileName)
{
QFile file(fileName);
if (file.open(QIODevice::ReadOnly)) {
QByteArray data = file.readAll();
const int bytesRead = data.size();
const unsigned char *buf = reinterpret_cast<const unsigned char *>(data.constData());
// code taken from qtextstream
if (bytesRead >= 3 && ((buf[0] == 0xef && buf[1] == 0xbb) && buf[2] == 0xbf)) {
return true;
} else {
return false;
}
file.close();
} else {
qDebug() << QString("Failed to detect byte order mark. Unable to open file %1.").arg(fileName);
}
return false;
}

QTextCharFormat Utilities::getParenthesesMatchFormat()
{
QTextCharFormat parenthesesMatchFormat;
Expand Down
1 change: 1 addition & 0 deletions OMEdit/OMEditGUI/Util/Utilities.h
Expand Up @@ -341,6 +341,7 @@ namespace Utilities {
qreal convertUnit(qreal value, qreal offset, qreal scaleFactor);
Label* getHeadingLabel(QString heading);
QFrame* getHeadingLine();
bool detectBOM(QString fileName);
QTextCharFormat getParenthesesMatchFormat();
QTextCharFormat getParenthesesMisMatchFormat();
void highlightCurrentLine(QPlainTextEdit *pPlainTextEdit);
Expand Down

0 comments on commit bef411c

Please sign in to comment.