Skip to content

Commit 357b26d

Browse files
authored
Use noexcept versions of filesystem (#829)
* Use noexcept versions of filesystem exists, is_directory, is_regular_file, remove, remove_all * Fix errors * Fixing error on mac * Fix static analysis shadowing * Update clang-format to 19.1.5, CI is using 18.1.3 * Match version of clang-format on CI * Display full path in title bar, fixes #806
1 parent a3155f8 commit 357b26d

File tree

11 files changed

+62
-46
lines changed

11 files changed

+62
-46
lines changed

src/model_editor/PathWatcher.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,17 @@
1515

1616
/// constructor
1717
PathWatcher::PathWatcher(const openstudio::path& p, int msec)
18-
: m_enabled(true), m_exists(openstudio::filesystem::exists(p)), m_dirty(false), m_checksum(openstudio::checksum(p)), m_path(p), m_msec(msec) {
18+
: m_enabled(true), m_exists(false), m_dirty(false), m_checksum(openstudio::checksum(p)), m_path(p), m_msec(msec) {
19+
20+
boost::system::error_code ec;
21+
m_exists = openstudio::filesystem::exists(p, ec);
22+
1923
// make sure a QApplication exists
2024
openstudio::Application::instance().application(false);
2125
openstudio::Application::instance().processEvents();
2226

2327
const bool isDirectory =
24-
(openstudio::filesystem::is_directory(p) || openstudio::toString(p.filename()) == "." || openstudio::toString(p.filename()) == "/");
28+
(openstudio::filesystem::is_directory(p, ec) || openstudio::toString(p.filename()) == "." || openstudio::toString(p.filename()) == "/");
2529
if (isDirectory) {
2630

2731
LOG_FREE_AND_THROW("openstudio.PathWatcher", "Watching Directory '" << openstudio::toString(p) << "' is not supported");
@@ -71,7 +75,8 @@ bool PathWatcher::dirty() const {
7175
}
7276

7377
void PathWatcher::clearState() {
74-
m_exists = openstudio::filesystem::exists(m_path);
78+
boost::system::error_code ec;
79+
m_exists = openstudio::filesystem::exists(m_path, ec);
7580
m_dirty = false;
7681
m_checksum = openstudio::checksum(m_path);
7782
}
@@ -87,7 +92,8 @@ void PathWatcher::fileChanged(const QString& path) {
8792
}
8893

8994
void PathWatcher::checkFile() {
90-
bool exists = openstudio::filesystem::exists(m_path);
95+
boost::system::error_code ec;
96+
bool exists = openstudio::filesystem::exists(m_path, ec);
9197
std::string checksum = openstudio::checksum(m_path);
9298

9399
if (checksum == "00000000") {

src/openstudio_app/OpenStudioApp.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,8 @@ OpenStudioApp::OpenStudioApp(int& argc, char** argv)
171171
std::stringstream webenginePath;
172172
webenginePath << QCoreApplication::applicationDirPath().toStdString();
173173
webenginePath << "/../Frameworks/QtWebEngineCore.framework/Versions/A/Helpers/QtWebEngineProcess.app/Contents/MacOS/QtWebEngineProcess";
174-
if (filesystem::exists(filesystem::path(webenginePath.str()))) {
174+
boost::system::error_code ec;
175+
if (filesystem::exists(filesystem::path(webenginePath.str()), ec)) {
175176
setenv("QTWEBENGINEPROCESS_PATH", webenginePath.str().c_str(), true);
176177
}
177178

@@ -1102,9 +1103,10 @@ void OpenStudioApp::versionUpdateMessageBox(const osversion::VersionTranslator&
11021103
};
11031104

11041105
for (const auto& scriptfolder : scriptfolders) {
1105-
if (openstudio::filesystem::exists(scriptfolder)) {
1106+
boost::system::error_code ec;
1107+
if (openstudio::filesystem::exists(scriptfolder, ec)) {
11061108
removedScriptDirs = true;
1107-
openstudio::filesystem::remove_all(scriptfolder);
1109+
openstudio::filesystem::remove_all(scriptfolder, ec);
11081110
}
11091111
}
11101112
}
@@ -1149,9 +1151,7 @@ void OpenStudioApp::readSettings() {
11491151
setLastPath(settings.value("lastPath", QDir::homePath()).toString());
11501152
setDviewPath(openstudio::toPath(settings.value("dviewPath", "").toString()));
11511153
m_currLang = settings.value("language", "en").toString();
1152-
LOG_FREE(Debug, "OpenStudioApp",
1153-
"\n\n\nm_currLang=[" << m_currLang.toStdString() << "]"
1154-
<< "\n\n\n");
1154+
LOG_FREE(Debug, "OpenStudioApp", "\n\n\nm_currLang=[" << m_currLang.toStdString() << "]" << "\n\n\n");
11551155
if (m_currLang.isEmpty()) {
11561156
m_currLang = "en";
11571157
}
@@ -1554,7 +1554,8 @@ void OpenStudioApp::loadShoeboxModel() {
15541554

15551555
auto filePath = resourcesPath() / toPath("ShoeboxModel/ShoeboxExample.osm");
15561556
boost::optional<openstudio::model::Model> model_;
1557-
if (openstudio::filesystem::is_regular_file(filePath)) {
1557+
boost::system::error_code ec;
1558+
if (openstudio::filesystem::is_regular_file(filePath, ec)) {
15581559
model_ = versionTranslator.loadModel(filePath);
15591560
} else if (isOpenStudioApplicationRunningFromBuildDirectory()) {
15601561
filePath = getOpenStudioCoalitionMeasuresSourceDirectory() / toPath("models/ShoeboxExample.osm");
@@ -1714,7 +1715,8 @@ void OpenStudioApp::setDviewPath(const openstudio::path& t_dviewPath) {
17141715
LOG_FREE(Debug, "OpenStudioApp", "setDViewPath t_dviewPath is not empty.");
17151716

17161717
// check if exists?
1717-
if (openstudio::filesystem::exists(t_dviewPath) && !openstudio::filesystem::is_directory(t_dviewPath)) {
1718+
boost::system::error_code ec;
1719+
if (openstudio::filesystem::exists(t_dviewPath, ec) && !openstudio::filesystem::is_directory(t_dviewPath, ec)) {
17181720
m_dviewPath = t_dviewPath;
17191721
} else {
17201722
LOG_FREE(Error, "OpenStudioApp", "setDViewPath: t_dviewPath doesn't not appear to be valid: '" << t_dviewPath << "'.");

src/openstudio_lib/ApplyMeasureNowDialog.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -380,9 +380,7 @@ void ApplyMeasureNowDialog::runMeasure() {
380380
if (OSAppBase::instance()->currentDocument()->mainWindow()->useClassicCLI()) {
381381
arguments << "classic";
382382
}
383-
arguments << "run"
384-
<< "-m"
385-
<< "-w" << toQString(*tempWorkflowJSONPath);
383+
arguments << "run" << "-m" << "-w" << toQString(*tempWorkflowJSONPath);
386384
LOG(Debug, "openstudioExePath='" << toString(openstudioExePath) << "'");
387385
LOG(Debug, "run arguments" << arguments.join(";").toStdString());
388386

@@ -408,7 +406,8 @@ void ApplyMeasureNowDialog::displayResults() {
408406

409407
this->okButton()->setText(ACCEPT_CHANGES);
410408
this->okButton()->show();
411-
if (boost::filesystem::exists(*m_reloadPath)) {
409+
boost::system::error_code ec;
410+
if (boost::filesystem::exists(*m_reloadPath, ec)) {
412411
this->okButton()->setEnabled(true);
413412
} else {
414413
this->okButton()->setEnabled(false);

src/openstudio_lib/LocationTabView.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,7 @@ void LocationView::onWeatherFileBtnClicked() {
722722

723723
if (!previousEPWPath.empty()) {
724724
if (previousEPWPath.filename() != newPath.filename()) {
725+
// inside try/catch, allow exception
725726
if (openstudio::filesystem::exists(previousEPWPath)) {
726727
openstudio::filesystem::remove_all(previousEPWPath);
727728
}
@@ -762,7 +763,8 @@ void LocationView::onWeatherFileBtnClicked() {
762763

763764
} catch (...) {
764765

765-
openstudio::filesystem::remove_all(newPath);
766+
boost::system::error_code ec;
767+
openstudio::filesystem::remove_all(newPath, ec);
766768

767769
QMessageBox box(QMessageBox::Warning, tr("Failed To Set Weather File"), tr("Failed To Set Weather File To ") + fileName, QMessageBox::Ok);
768770
box.setDetailedText(toQString(ss.string()));

src/openstudio_lib/OSDocument.cpp

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@ OSDocument::OSDocument(const openstudio::model::Model& library, const openstudio
150150
if (!m_savePath.isEmpty()) {
151151
auto p = toPath(m_savePath);
152152
modelTempDirPath = model::initializeModel(*model, p);
153-
m_mainWindow->setWindowTitle(toQString(p.filename()) + "[*]");
153+
m_mainWindow->setWindowFilePath(m_savePath);
154+
m_mainWindow->setWindowTitle(m_savePath + "[*]");
154155
} else {
155156
modelTempDirPath = model::initializeModel(*model);
156157
m_mainWindow->setWindowTitle("Untitled[*]");
@@ -930,12 +931,13 @@ bool OSDocument::fixWeatherFileInTemp(bool opening) {
930931
epwPathAbsolute = true;
931932

932933
epwInUserPath = *weatherFilePath;
933-
if (boost::filesystem::exists(epwInUserPath)) {
934+
boost::system::error_code ec;
935+
if (boost::filesystem::exists(epwInUserPath, ec)) {
934936
epwInUserPathChecksum = checksum(epwInUserPath);
935937
}
936938

937939
epwInTempPath = tempFilesDir / epwInUserPath.filename();
938-
if (boost::filesystem::exists(epwInTempPath)) {
940+
if (boost::filesystem::exists(epwInTempPath, ec)) {
939941
epwInTempPathChecksum = checksum(epwInTempPath);
940942
}
941943

@@ -946,11 +948,12 @@ bool OSDocument::fixWeatherFileInTemp(bool opening) {
946948

947949
// Look in temp model "resources" and "resources/files"
948950
epwInTempPath = tempResourcesDir / *weatherFilePath;
949-
if (boost::filesystem::exists(epwInTempPath)) {
951+
boost::system::error_code ec;
952+
if (boost::filesystem::exists(epwInTempPath, ec)) {
950953
epwInTempPathChecksum = checksum(epwInTempPath);
951954
} else {
952955
epwInTempPath = tempFilesDir / *weatherFilePath;
953-
if (boost::filesystem::exists(epwInTempPath)) {
956+
if (boost::filesystem::exists(epwInTempPath, ec)) {
954957
epwInTempPathChecksum = checksum(epwInTempPath);
955958
}
956959
}
@@ -966,12 +969,12 @@ bool OSDocument::fixWeatherFileInTemp(bool opening) {
966969

967970
// Expected location is companion_folder/files
968971
epwInUserPath = searchFilesDir / *weatherFilePath;
969-
if (boost::filesystem::exists(epwInUserPath)) {
972+
if (boost::filesystem::exists(epwInUserPath, ec)) {
970973
epwInUserPathChecksum = checksum(epwInUserPath);
971974
} else {
972975
// Just in case, we look in the companion_folder
973976
epwInUserPath = searchCompanionDir / *weatherFilePath;
974-
if (boost::filesystem::exists(epwInUserPath)) {
977+
if (boost::filesystem::exists(epwInUserPath, ec)) {
975978
epwInUserPathChecksum = checksum(epwInUserPath);
976979
}
977980
}
@@ -1098,7 +1101,8 @@ bool OSDocument::fixWeatherFileInTemp(bool opening) {
10981101

10991102
if (doCopy) {
11001103
LOG(Debug, "Removing weather file at " << copyDest);
1101-
boost::filesystem::remove_all(copyDest);
1104+
boost::system::error_code ec;
1105+
boost::filesystem::remove_all(copyDest, ec);
11021106
LOG(Debug, "Removing weather file complete");
11031107
}
11041108

@@ -1510,9 +1514,10 @@ bool OSDocument::saveAs() {
15101514
// remove old model
15111515
if (!m_savePath.isEmpty()) {
15121516
openstudio::path oldModelPath = toPath(m_modelTempDir) / toPath(m_savePath).filename();
1513-
if (boost::filesystem::exists(oldModelPath)) {
1517+
boost::system::error_code ec;
1518+
if (boost::filesystem::exists(oldModelPath, ec)) {
15141519
LOG(Debug, "Removing " << oldModelPath << " starting");
1515-
boost::filesystem::remove(oldModelPath);
1520+
boost::filesystem::remove(oldModelPath, ec);
15161521
LOG(Debug, "Removing " << oldModelPath << " complete");
15171522
}
15181523
}
@@ -1631,8 +1636,8 @@ boost::optional<model::Component> OSDocument::getComponent(const OSItemId& itemI
16311636
}
16321637

16331638
#endif
1634-
1635-
//OS_ASSERT(openstudio::filesystem::exists(oscPath));
1639+
//std::error_code ec;
1640+
//OS_ASSERT(openstudio::filesystem::exists(oscPath, ec));
16361641

16371642
osversion::VersionTranslator translator;
16381643
//translator.setAllowNewerVersions(false); // DLM: allow to open newer versions?
@@ -1734,9 +1739,7 @@ void OSDocument::updateWindowFilePath() {
17341739
} else {
17351740
// m_mainWindow->setWindowTitle();
17361741
m_mainWindow->setWindowFilePath(m_savePath);
1737-
QFileInfo fi(m_savePath);
1738-
QString fileName = fi.fileName();
1739-
m_mainWindow->setWindowTitle(fileName + "[*]");
1742+
m_mainWindow->setWindowTitle(m_savePath + "[*]");
17401743
}
17411744
}
17421745

src/openstudio_lib/OSItem.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,8 @@ OSItem* OSItem::makeItem(const OSItemId& itemId, OSItemType osItemType) {
152152
result = new ModelObjectItem(*modelObject, itemId.isDefaulted(), osItemType);
153153
} else {
154154
openstudio::path p = openstudio::toPath(itemId.itemId());
155-
if (openstudio::filesystem::exists(p)) {
155+
boost::system::error_code ec;
156+
if (openstudio::filesystem::exists(p, ec)) {
156157
result = new ScriptItem(p, osItemType);
157158
}
158159
}

src/openstudio_lib/ResultsTabView.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,8 @@ void ResultsView::searchForExistingResults(const openstudio::path& t_runDir, con
194194
std::vector<openstudio::path> reports;
195195

196196
// Check that the directory does exists first
197-
if (openstudio::filesystem::is_directory(t_runDir) && openstudio::filesystem::exists(t_runDir)) {
197+
boost::system::error_code ec;
198+
if (openstudio::filesystem::is_directory(t_runDir, ec) && openstudio::filesystem::exists(t_runDir, ec)) {
198199
for (openstudio::filesystem::recursive_directory_iterator end, dir(t_runDir); dir != end; ++dir) {
199200
openstudio::path p = *dir;
200201
if (openstudio::toString(p.filename()) == "eplusout.sql") {
@@ -210,7 +211,7 @@ void ResultsView::searchForExistingResults(const openstudio::path& t_runDir, con
210211
LOG(Debug, "Looking for existing results in: " << openstudio::toString(t_reportsDir));
211212

212213
// Check that the directory does exists first
213-
if (openstudio::filesystem::is_directory(t_reportsDir) && openstudio::filesystem::exists(t_reportsDir)) {
214+
if (openstudio::filesystem::is_directory(t_reportsDir, ec) && openstudio::filesystem::exists(t_reportsDir, ec)) {
214215
for (openstudio::filesystem::directory_iterator end, dir(t_reportsDir); dir != end; ++dir) {
215216
openstudio::path p = *dir;
216217
if (openstudio::toString(p.extension()) == ".html" || openstudio::toString(p.extension()) == ".htm") {

src/openstudio_lib/ScheduleFileInspectorView.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -323,9 +323,7 @@ void ScheduleFileInspectorView::attach(openstudio::model::ScheduleFile& sch) {
323323
m_columnSeparator->bind<std::string>(
324324
*m_sch, static_cast<std::string (*)(const std::string&)>(&openstudio::toString),
325325
// ScheduleFile::columnSeparatorValues does not exist: https://github.com/NREL/OpenStudio/issues/5246
326-
[]() {
327-
return std::vector<std::string>{"Comma", "Tab", "Space", "Semicolon"};
328-
},
326+
[]() { return std::vector<std::string>{"Comma", "Tab", "Space", "Semicolon"}; },
329327
std::bind(&model::ScheduleFile::columnSeparator, m_sch.get_ptr()),
330328
[this](const std::string& value) -> bool {
331329
bool result = m_sch->setColumnSeparator(value);
@@ -424,7 +422,8 @@ void ScheduleFileInspectorView::refreshContent() {
424422
openstudio::path fpath = m_sch->externalFile().filePath();
425423
m_contentLines->clear();
426424

427-
if (openstudio::filesystem::is_regular_file(fpath)) {
425+
boost::system::error_code ec;
426+
if (openstudio::filesystem::is_regular_file(fpath, ec)) {
428427
const int rowstoSkipatTop = m_sch->rowstoSkipatTop();
429428

430429
const int colNum = m_sch->columnNumber() - 1; // Turn 1-indexed to 0-indexed

src/openstudio_lib/ScriptItem.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,10 @@ void ScriptItem::saveArgumentsToDb() {
128128

129129
void ScriptItem::deleteDb() {
130130
m_removed = true;
131-
if (openstudio::filesystem::exists(argsDbPath())) {
132-
openstudio::filesystem::remove(argsDbPath());
133-
openstudio::filesystem::remove(toPath(toString((argsDbPath())) + "-journal"));
131+
boost::system::error_code ec;
132+
if (openstudio::filesystem::exists(argsDbPath(), ec)) {
133+
openstudio::filesystem::remove(argsDbPath(), ec);
134+
openstudio::filesystem::remove(toPath(toString((argsDbPath())) + "-journal"), ec);
134135
}
135136
}
136137

src/shared_gui_components/BCLMeasureDialog.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,10 @@ boost::optional<openstudio::BCLMeasure> BCLMeasureDialog::createMeasure() {
176176
openstudio::path measureDir = umd / toPath(folderName);
177177

178178
// prompt user ???
179-
if (openstudio::filesystem::exists(measureDir)) {
179+
boost::system::error_code ec;
180+
if (openstudio::filesystem::exists(measureDir, ec)) {
180181
int i = 1;
181-
while (openstudio::filesystem::exists(measureDir)) {
182+
while (openstudio::filesystem::exists(measureDir, ec)) {
182183
folderName = toQString(lowerClassName).append(" ").append(QString::number(i)).append("/");
183184
measureDir = umd / toPath(folderName);
184185
++i;

0 commit comments

Comments
 (0)