handle exiting ZFC without using exceptions

Wed, 27 Jan 2021 19:39:14 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Wed, 27 Jan 2021 19:39:14 +0200
changeset 192
94c67ae846fc
parent 191
2e6cbacafdc7
child 193
fb1709f27108

handle exiting ZFC without using exceptions

sources/basics.h file | annotate | diff | comparison | revisions
sources/interface.cpp file | annotate | diff | comparison | revisions
sources/interface.h file | annotate | diff | comparison | revisions
sources/main.cpp file | annotate | diff | comparison | revisions
--- a/sources/basics.h	Wed Jan 27 19:32:55 2021 +0200
+++ b/sources/basics.h	Wed Jan 27 19:39:14 2021 +0200
@@ -125,8 +125,6 @@
 	return N;
 }
 
-struct Exitception {};
-
 #ifdef __GNUC__
 # define GNUATTRIBUTE(X) __attribute__(X)
 #else
--- a/sources/interface.cpp	Wed Jan 27 19:32:55 2021 +0200
+++ b/sources/interface.cpp	Wed Jan 27 19:39:14 2021 +0200
@@ -199,6 +199,11 @@
 	m_needRefresh = false;
 }
 
+Interface::~Interface()
+{
+	::endwin();
+}
+
 // -------------------------------------------------------------------------------------------------
 //
 void Interface::renderTitlebar()
@@ -628,7 +633,7 @@
 
 // -------------------------------------------------------------------------------------------------
 //
-void Interface::handleInput()
+void Interface::handleInput(bool* shouldquit)
 {
 	int ch = ::getch();
 
@@ -679,8 +684,7 @@
 				}
 				else
 				{
-					endwin();
-					throw Exitception();
+					*shouldquit = true;
 				}
 			});
 			break;
@@ -833,7 +837,7 @@
 		case INPUTSTATE_NORMAL:
 			if (getCurrentInput()[0] == '/')
 			{
-				handleCommand(getCurrentInput());
+				handleCommand(getCurrentInput(), shouldquit);
 				flushInput();
 			}
 			else if (m_session.sendCommand(getCurrentInput()))
@@ -1052,7 +1056,7 @@
 
 // -------------------------------------------------------------------------------------------------
 //
-void Interface::handleCommand(const std::string& input)
+void Interface::handleCommand(const std::string& input, bool* shouldquit)
 {
 	if (input[0] != '/')
 		return;
@@ -1079,8 +1083,7 @@
 	else if (command == "quit")
 	{
 		m_session.disconnect();
-		endwin();
-		throw Exitception();
+		*shouldquit = true;
 	}
 	else
 		printError("Unknown command: %s\n", command.data());
--- a/sources/interface.h	Wed Jan 27 19:32:55 2021 +0200
+++ b/sources/interface.h	Wed Jan 27 19:39:14 2021 +0200
@@ -49,11 +49,12 @@
 	};
 
 	Interface();
+	virtual ~Interface();
 	void connect(std::string address, std::string password);
 	void disconnected();
 	RCONSession* getSession() { return &m_session; }
-	void handleCommand(const std::string& input);
-	void handleInput();
+	void handleCommand(const std::string& input, bool *shouldquit);
+	void handleInput(bool *shouldquit);
 	void needRefresh();
 	void __cdecl print(const char* fmtstr, ...) GNUATTRIBUTE((format(printf, 2, 3)));
 	void __cdecl printWarning(const char* fmtstr, ...) GNUATTRIBUTE((format(printf, 2, 3)));
--- a/sources/main.cpp	Wed Jan 27 19:32:55 2021 +0200
+++ b/sources/main.cpp	Wed Jan 27 19:39:14 2021 +0200
@@ -72,35 +72,33 @@
 	if (argc == 3)
 		iface.connect (argv[1], argv[2]);
 
-	try
+	for (;;)
 	{
-		for (;;)
+		fd_set fdset;
+		timeval timeout;
+		timeout.tv_sec = 0;
+		timeout.tv_usec = 250000; // 0.25 seconds
+		FD_ZERO(&fdset);
+		FD_SET(0, &fdset);
+		const int fd = iface.getSession()->getSocket()->file_descriptor;
+		FD_SET(fd, &fdset);
+		::select(fd + 1, &fdset, nullptr, nullptr, &timeout);
+		bool shouldquit = false;
+		if (FD_ISSET(0, &fdset))
 		{
-			fd_set fdset;
-			int highest = 0;
-			timeval timeout;
-			timeout.tv_sec = 0;
-			timeout.tv_usec = 250000; // 0.25 seconds
-			FD_ZERO (&fdset);
-			FD_SET (0, &fdset);
-
-			int fd = iface.getSession()->getSocket()->file_descriptor;
-			highest = zfc::max (highest, fd);
-			FD_SET (fd, &fdset);
-
-			select (highest + 1, &fdset, nullptr, nullptr, &timeout);
-
-			if (FD_ISSET (0, &fdset))
-			{
-				// stdin is ready, what's incoming?
-				iface.handleInput();
-			}
-
+			// stdin is ready, what's incoming?
+			iface.handleInput(&shouldquit);
+		}
+		if (shouldquit)
+		{
+			break;
+		}
+		else
+		{
 			iface.getSession()->tick();
 			iface.render();
 		}
 	}
-	catch (const zfc::Exitception&) {}
 
 	iface.getSession()->disconnect();
 	return EXIT_SUCCESS;

mercurial