📄 gapp.cpp
字号:
/* Copyright (C) 2006, Mike Gashler This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. see http://www.gnu.org/copyleft/lesser.html*/#include "GApp.h"#include <stdio.h>#include <stdlib.h>#ifdef WIN32# include <windows.h># include <direct.h>#else // WIN32# include <unistd.h># include <signal.h>#endif // !WIN32#include "GMacros.h"#include "GFile.h"/*static*/ int GApp::LaunchDaemon(DaemonMainFunc pDaemonMain, void* pArg){#ifdef WIN32 // Windows isn't POSIX compliant and it has its own process system that // isn't really friendly to launching daemons. You're supposed to create // a "service", but I don't know how to do that (and I'm too lazy to learn // something that can't generalize off a proprietary platform) so let's // just launch it like a normal app and be happy with that. pDaemonMain(pArg); return 0;#else // WIN32 // Fork the process int pid = fork(); if(pid < 0) throw "Error forking the daemon process"; if(pid == 0) { // Drop my process group leader and become my own process group leader // (so the process isn't terminated when the group leader is killed) setsid(); // Get off any mounted drives so that they can be unmounted without // killing the daemon chdir("/"); // Launch the daemon pDaemonMain(pArg); exit(0); } return pid;#endif // !WIN32}/*static*/ char* GApp::GetApplicationPath(const char* szArg0){ // Make sure the app name includes path info char szAppPath[512];#ifdef WIN32 GetModuleFileName(NULL, szAppPath, 512);#else // WIN32 if(szArg0[0] == '/') strcpy(szAppPath, szArg0); else { getcwd(szAppPath, 512); if(szAppPath[strlen(szAppPath) - 1] != '/') strcat(szAppPath, "/"); strcat(szAppPath, szArg0); }#endif // !WIN32 // Cut off the filename int n = strlen(szAppPath); for(n--; n >= 0; n--) { if(szAppPath[n] == '/' || szAppPath[n] == '\\') { szAppPath[n + 1] = '\0'; break; } } if(n < 0) { getcwd(szAppPath, 512); n = strlen(szAppPath) - 1; if(szAppPath[n] != '/' && szAppPath[n] != '\\') strcat(szAppPath, "/"); } // Copy to an allocated buffer GFile::CondensePath(szAppPath); char* szApplicationPath = new char[strlen(szAppPath) + 1]; strcpy(szApplicationPath, szAppPath); return szApplicationPath;}void GApp_onSigSegV(int n){ throw "A memory access violation occurred. The most common cause is an attempt to dereference null";}void GApp_onSigInt(int n){ throw "The program was interrupted when the user pressed Ctrl-C";}void GApp_onSigQuit(int n){ throw "The program was interrupted with SIGQUIT";}void GApp_onSigTstp(int n){ throw "The program was interrupted with SIGTSTP";}void GApp_onSigAbrt(int n){ throw "An unhandled exception was thrown";}void GApp_onSigFpe(int n){ throw "A floating point error occurred";}/*static*/ void GApp::TurnSegFaultsIntoExceptions(){#ifndef WIN32 signal(SIGSEGV, GApp_onSigSegV); signal(SIGINT, GApp_onSigInt); signal(SIGFPE, GApp_onSigFpe); //signal(SIGQUIT, GApp_onSigQuit); //signal(SIGTSTP, GApp_onSigTstp); //signal(SIGABRT, GApp_onSigAbrt);#endif // !WIN32}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -