diff options
| author | Gustav Sörnäs <gustav@sornas.net> | 2020-12-03 17:11:43 +0100 |
|---|---|---|
| committer | Gustav Sörnäs <gustav@sornas.net> | 2020-12-08 10:21:07 +0100 |
| commit | 0c39051ba80f04b1177833a006f2d442a7170b56 (patch) | |
| tree | 9e657946a061b5b305f9cf75634db7b37e979eb3 /labb8/lib/StanfordCPPLib/console.cpp | |
| parent | 7b7f6808a7b2db2ed21103767434c1445f7815c2 (diff) | |
| download | tddd86-0c39051ba80f04b1177833a006f2d442a7170b56.tar.gz | |
add initial files l8
Diffstat (limited to 'labb8/lib/StanfordCPPLib/console.cpp')
| -rwxr-xr-x | labb8/lib/StanfordCPPLib/console.cpp | 164 |
1 files changed, 164 insertions, 0 deletions
diff --git a/labb8/lib/StanfordCPPLib/console.cpp b/labb8/lib/StanfordCPPLib/console.cpp new file mode 100755 index 0000000..c55564c --- /dev/null +++ b/labb8/lib/StanfordCPPLib/console.cpp @@ -0,0 +1,164 @@ +/* + * File: console.cpp + * ----------------- + * This file implements the console.h interface. + */ + +#include <string> +#include "console.h" +#include "error.h" +#include "platform.h" +using namespace std; + +static void sclTerminateHandler(); + +static Platform *pp = getPlatform(); +static bool consoleEcho = false; +static bool consolePrintExceptions = false; +static string consoleLogFile = ""; +static void (*old_terminate)() = NULL; + +void clearConsole() { + pp->clearConsole(); +} + +bool getConsoleEcho() { + return consoleEcho; +} + +string getConsoleLogFile() { + return consoleLogFile; +} + +bool getConsolePrintExceptions() { + return consolePrintExceptions; +} + +void setConsoleEcho(bool echo) { + consoleEcho = echo; +} + +void setConsoleFont(const string & font) { + pp->setConsoleFont(font); +} + +void setConsoleLogFile(const string & filename) { + consoleLogFile = filename; +} + +void setConsolePrintExceptions(bool printExceptions) { + if (printExceptions && !consolePrintExceptions) { + old_terminate = set_terminate(sclTerminateHandler); + } else if (!printExceptions && consolePrintExceptions) { + set_terminate(old_terminate); + } + consolePrintExceptions = printExceptions; +} + +void setConsoleSize(double width, double height) { + pp->setConsoleSize(width, height); +} + +static void sclTerminateHandler() { + ostream& out = cerr; + try { + throw; // re-throws the exception that already occurred + } catch (const ErrorException& ex) { + string msg = "\n ***\n"; + msg += " *** STANFORD C++ LIBRARY \n"; + msg += " *** An ErrorException occurred during program execution: \n"; + msg += " *** "; + msg += ex.what(); + msg += "\n ***\n\n"; + cout.flush(); + out << msg; + throw ex; + } catch (const std::exception& ex) { + string msg = "\n ***\n"; + msg += " *** STANFORD C++ LIBRARY \n"; + msg += " *** An exception occurred during program execution: \n"; + msg += " *** "; + msg += ex.what(); + msg += "\n ***\n\n"; + cout.flush(); + out << msg; + throw ex; + } catch (std::string str) { + string msg = "\n ***\n"; + msg += " *** STANFORD C++ LIBRARY \n"; + msg += " *** A string exception occurred during program execution: \n"; + msg += " *** \""; + msg += str; + msg += "\"\n ***\n"; + cout.flush(); + out << msg; + throw str; + } catch (char const* str) { + string msg = "\n ***\n"; + msg += " *** STANFORD C++ LIBRARY \n"; + msg += " *** A string exception occurred during program execution: \n"; + msg += " *** \""; + msg += str; + msg += "\"\n ***\n"; + cout.flush(); + out << msg; + throw str; + } catch (int n) { + char buf[128]; + snprintf(buf, 128, "%d", n); + string msg = "\n ***\n"; + msg += " *** STANFORD C++ LIBRARY \n"; + msg += " *** An int exception occurred during program execution: \n"; + msg += " *** "; + msg += buf; + msg += "\n ***\n\n"; + cout.flush(); + out << msg; + throw n; + } catch (long l) { + char buf[128]; + snprintf(buf, 128, "%ld", l); + string msg = "\n ***\n"; + msg += " *** STANFORD C++ LIBRARY \n"; + msg += " *** A long exception occurred during program execution: \n"; + msg += " *** "; + msg += buf; + msg += "\n ***\n\n"; + cout.flush(); + out << msg; + throw l; + } catch (char c) { + string msg = "\n ***\n"; + msg += " *** STANFORD C++ LIBRARY \n"; + msg += " *** A char exception occurred during program execution: \n"; + msg += " *** '"; + msg += c; + msg += "'\n ***\n"; + cout.flush(); + out << msg; + throw c; + } catch (bool b) { + string msg = "\n ***\n"; + msg += " *** STANFORD C++ LIBRARY \n"; + msg += " *** A bool exception occurred during program execution: \n"; + msg += " *** "; + msg += (b ? "true" : "false"); + msg += "\n ***\n\n"; + cout.flush(); + out << msg; + throw b; + } catch (double d) { + char buf[128]; + snprintf(buf, 128, "%lf", d); + string msg = "\n ***\n"; + msg += " *** STANFORD C++ LIBRARY \n"; + msg += " *** A double exception occurred during program execution: \n"; + msg += " *** "; + msg += buf; + msg += "\n ***\n\n"; + cout.flush(); + out << msg; + throw d; + } + abort(); +} |
