Skip to content

Commit 3cc42f6

Browse files
committed
Correct exits in main
1 parent 728505d commit 3cc42f6

File tree

1 file changed

+40
-42
lines changed

1 file changed

+40
-42
lines changed

src/hyperiond/main.cpp

Lines changed: 40 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,9 @@ int main(int argc, char** argv)
165165
if (parser.isSet(versionOption))
166166
{
167167
std::cout
168-
<< "Hyperion Ambilight Deamon" << std::endl
169-
<< "\tVersion : " << HYPERION_VERSION << " (" << HYPERION_BUILD_ID << ")" << std::endl
170-
<< "\tBuild Time: " << __DATE__ << " " << __TIME__ << std::endl;
168+
<< "Hyperion Ambilight Deamon" << "\n"
169+
<< "\tVersion : " << HYPERION_VERSION << " (" << HYPERION_BUILD_ID << ")" << "\n"
170+
<< "\tBuild Time: " << __DATE__ << " " << __TIME__ << "\n";
171171

172172
return 0;
173173
}
@@ -178,21 +178,24 @@ int main(int argc, char** argv)
178178
{
179179
Error(log, "The Hyperion Daemon is already running, abort start");
180180

181-
// use the first non-localhost IPv4 address, IPv6 are not supported by Yeelight currently
182-
for (const auto& address : QNetworkInterface::allAddresses())
181+
// use the first non-localhost IPv4 address
182+
QList<QHostAddress> const allNetworkAddresses {QNetworkInterface::allAddresses()};
183+
auto it = std::find_if(allNetworkAddresses.begin(), allNetworkAddresses.end(),
184+
[](const QHostAddress& address)
185+
{
186+
return !address.isLoopback() && (address.protocol() == QAbstractSocket::IPv4Protocol);
187+
});
188+
189+
if (it != allNetworkAddresses.end())
183190
{
184-
if (!address.isLoopback() && (address.protocol() == QAbstractSocket::IPv4Protocol))
191+
std::cout << "Access the Hyperion User-Interface for configuration and control via:" << "\n";
192+
std::cout << "http://" << it->toString().toStdString() << ":8090" << "\n";
193+
194+
QHostInfo const hostInfo = QHostInfo::fromName(it->toString());
195+
if (hostInfo.error() == QHostInfo::NoError)
185196
{
186-
std::cout << "Access the Hyperion User-Interface for configuration and control via:" << std::endl;
187-
std::cout << "http://" << address.toString().toStdString() << ":8090" << std::endl;
188-
189-
QHostInfo hostInfo = QHostInfo::fromName(address.toString());
190-
if (hostInfo.error() == QHostInfo::NoError)
191-
{
192-
QString hostname = hostInfo.hostName();
193-
std::cout << "http://" << hostname.toStdString() << ":8090" << std::endl;
194-
}
195-
break;
197+
QString const hostname = hostInfo.hostName();
198+
std::cout << "http://" << hostname.toStdString() << ":8090" << "\n";
196199
}
197200
}
198201
return 0;
@@ -292,11 +295,11 @@ int main(int argc, char** argv)
292295
Info(log,"Hyperion %s, %s, built: %s:%s", HYPERION_VERSION, HYPERION_BUILD_ID, __DATE__, __TIME__);
293296
Debug(log,"QtVersion [%s]", QT_VERSION_STR);
294297

295-
int rc = 1;
298+
int exitCode = 1;
296299
bool readonlyMode = false;
297300

298-
QString userDataPath(userDataOption.value(parser));
299-
QDir userDataDirectory(userDataPath);
301+
QString const userDataPath(userDataOption.value(parser));
302+
QDir const userDataDirectory(userDataPath);
300303

301304

302305
if (parser.isSet(readOnlyModeOption))
@@ -308,7 +311,7 @@ int main(int argc, char** argv)
308311

309312
Info(log, "Hyperion configuration and user data location: '%s'", QSTRING_CSTR(userDataDirectory.absolutePath()));
310313

311-
QFileInfo dbFile(DBManager::getFileInfo());
314+
QFileInfo const dbFile(DBManager::getFileInfo());
312315

313316
try
314317
{
@@ -338,7 +341,7 @@ int main(int argc, char** argv)
338341
throw std::runtime_error("Configuration export failed'");
339342
}
340343

341-
exit(0);
344+
return 0;
342345
}
343346
}
344347
else
@@ -366,16 +369,14 @@ int main(int argc, char** argv)
366369
throw std::runtime_error("Password reset failed");
367370
}
368371

369-
AuthTable* table = new AuthTable();
370-
if(table->resetHyperionUser()){
371-
Info(log,"Password reset successful");
372-
delete table;
373-
exit(0);
374-
} else {
375-
Error(log,"Failed to reset password!");
376-
delete table;
377-
exit(1);
372+
QScopedPointer<AuthTable> const table(new AuthTable());
373+
if(!table->resetHyperionUser())
374+
{
375+
throw std::runtime_error("Failed to reset password!");
378376
}
377+
378+
Info(log,"Password reset successful");
379+
return 0;
379380
}
380381

381382
// delete database before start
@@ -391,21 +392,18 @@ int main(int argc, char** argv)
391392
{
392393
if (!QFile::remove(dbFile.absoluteFilePath()))
393394
{
394-
Error(log,"Failed to delete Database!");
395-
exit(1);
396-
}
397-
else
398-
{
399-
Info(log,"Configuration database deleted successfully.");
395+
throw std::runtime_error("Failed to delete Database!");
400396
}
397+
398+
Info(log,"Configuration database deleted successfully.");
401399
}
402400
else
403401
{
404402
Warning(log,"Configuration database '%s' does not exist!", QSTRING_CSTR(dbFile.absoluteFilePath()));
405403
}
406404
}
407405

408-
QString configFile(importConfig.value(parser));
406+
QString const configFile(importConfig.value(parser));
409407
if (!configFile.isEmpty())
410408
{
411409
if ( readonlyMode )
@@ -441,7 +439,7 @@ int main(int argc, char** argv)
441439
if (!configFile.isEmpty())
442440
{
443441
Info(log,"Configuration imported sucessfully. You can start Hyperion now.");
444-
exit(0);
442+
return 0;
445443
}
446444

447445
Info(log,"Starting Hyperion in %sGUI mode, DB is %s", isGuiApp ? "": "non-", readonlyMode ? "read-only" : "read/write");
@@ -469,11 +467,11 @@ int main(int argc, char** argv)
469467
QApplication::setQuitOnLastWindowClosed(false);
470468
SysTray tray(hyperiond.get());
471469
tray.hide();
472-
rc = (qobject_cast<QApplication *>(app.data()))->exec();
470+
exitCode = (qobject_cast<QApplication *>(app.data()))->exec();
473471
}
474472
else
475473
{
476-
rc = app->exec();
474+
exitCode = app->exec();
477475
}
478476
}
479477
catch (std::exception& e)
@@ -488,7 +486,7 @@ int main(int argc, char** argv)
488486
}
489487
#endif
490488

491-
Info(log, "Application ended with code %d", rc);
489+
Info(log, "Application ended with code %d", exitCode);
492490

493-
return rc;
491+
return exitCode;
494492
}

0 commit comments

Comments
 (0)