Skip to content

Commit

Permalink
QSlangDriver: Enhance File Parsing and Path Handling
Browse files Browse the repository at this point in the history
- Implemented contentCleanComment to remove single and multiline comments,
  normalizing line endings for cross-platform consistency.
- Added contentRelativeToAbsolute to convert relative paths in content to
  absolute paths, leveraging QDir for path resolution.
- Modified parseFileList to integrate new content cleaning and path
  conversion functions, improving handling of file list content.
- Adjusted includes in qslangdriver.h to support new functions, ensuring
  proper compilation and functionality.
- Streamlined file parsing logic in parseFileList, enhancing readability
  and maintainability of the code.

These updates significantly improve file parsing capabilities and path
handling in QSlangDriver, aligning with cross-platform standards.

Signed-off-by: Huang Rui <vowstar@gmail.com>
  • Loading branch information
vowstar committed Nov 19, 2023
1 parent ce9fb4c commit 773bcd3
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 8 deletions.
50 changes: 42 additions & 8 deletions src/common/qslangdriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,23 +139,24 @@ bool QSlangDriver::parseFileList(const QString &fileListPath, const QStringList
/* Append file path list to the end of content */
content.append("\n" + filePathList.join("\n"));
}
/* Remove single line comment */
content.remove(QRegularExpression(R"(\s*//[^\n]*\s*)"));
/* Remove multiline comments */
content.remove(QRegularExpression(R"(\s*/\*.*?\*/\s*)"));
/* Remove empty lines */
content.remove(QRegularExpression(R"(\n\s*\n)"));

/* Removes comments from the content */
content = contentCleanComment(content);
/* Substitute environment variables */
if (projectManager) {
const QMap<QString, QString> env = projectManager->getEnv();
QMapIterator<QString, QString> iterator(env);
while (iterator.hasNext()) {
iterator.next();
/* Create pattern */
const QString pattern = QString("${%1}").arg(iterator.key());
content = content.replace(pattern, iterator.value());
/* Replace environment variable */
content = content.replace(pattern, iterator.value());
}
}
/* Convert relative path to absolute path */
if (QFileInfo::exists(fileListPath)) {
content = contentRelativeToAbsolute(content, QFileInfo(fileListPath).absoluteDir());
}
/* Create a temporary file */
QTemporaryFile tempFile("socstudio.fl");
/* Do not remove file after close */
Expand Down Expand Up @@ -216,3 +217,36 @@ const QStringList &QSlangDriver::getModuleList()
}
return moduleList;
}

QString QSlangDriver::contentCleanComment(const QString &content)
{
QString result = content;
/* Normalize line endings to Unix-style */
result.replace(QRegularExpression(R"(\r\n|\r)"), "\n");
/* Remove single line comment */
result.remove(QRegularExpression(R"(\s*//[^\n]*\s*)"));
/* Remove multiline comments */
result.remove(QRegularExpression(R"(\s*/\*.*?\*/\s*)"));
/* Remove empty lines */
result.remove(QRegularExpression(R"(\n\s*\n)"));
return result;
}

QString QSlangDriver::contentRelativeToAbsolute(const QString &content, const QDir &baseDir)
{
QStringList result;
/* Splitting content into lines, considering different newline characters */
const QStringList lines = content.split(QRegularExpression(R"(\r\n|\n|\r)"), Qt::KeepEmptyParts);

for (const QString &line : lines) {
/* Check for relative path and convert it to absolute */
if (QDir::isRelativePath(line)) {
/* Convert relative path to absolute path */
result.append(baseDir.filePath(line));
} else {
/* Preserve absolute paths and non-path content as is */
result.append(line);
}
}
return result.join("\n");
}
23 changes: 23 additions & 0 deletions src/common/qslangdriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@

#include "common/qsocprojectmanager.h"

#include <QDir>
#include <QMap>
#include <QObject>
#include <QString>
#include <QStringList>

#include <nlohmann/json.hpp>

Expand Down Expand Up @@ -72,6 +75,26 @@ public slots:
* @return QStringList & The module list
*/
const QStringList &getModuleList();
/**
* @brief Removes comments from the content.
* @details This function strips both single line and multiline comments
* from the input string. It normalizes line endings to Unix-style
* for cross-platform compatibility and removes empty lines.
* @param content The string containing the content with comments.
* @return QString The cleaned content string without comments.
*/
QString contentCleanComment(const QString &content);
/**
* @brief Converts relative paths in content to absolute paths.
* @details This function processes the given string and converts
* any relative file paths to absolute paths, using the
* specified base directory. Assumes path formats are
* appropriate for the operating system in use.
* @param content String containing content with relative paths.
* @param baseDir Base directory for resolving relative paths.
* @return QString with modified content, featuring absolute paths.
*/
QString contentRelativeToAbsolute(const QString &content, const QDir &baseDir);

private:
/* Pointer of project manager */
Expand Down

0 comments on commit 773bcd3

Please sign in to comment.