X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fcore%2Fapplication.cpp;h=51f013e72c3cb5876429035d2604c95eb1a1f578;hb=7f847aeb48e0e8f58ead52ba255cff27527628eb;hp=badc52304ab4df6dc8c563aa9708109e5c387959;hpb=5ad225535ae12d53fd79c8f9fa764bd586211b8f;p=libs%2Fcore.git diff --git a/source/core/application.cpp b/source/core/application.cpp index badc523..51f013e 100644 --- a/source/core/application.cpp +++ b/source/core/application.cpp @@ -1,68 +1,66 @@ -/* $Id$ - -This file is part of libmspcore -Copyright © 2006-2008, 2011 Mikko Rasa, Mikkosoft Productions -Distributed under the LGPL -*/ - +#ifdef WIN32 +#include +#endif #include -#include #include -#include "../debug/backtrace.h" -#include "../debug/demangle.h" -#include "../time/units.h" -#include "../time/utils.h" +#include +#include #include "application.h" -#include "except.h" +#include "getopt.h" using namespace std; namespace Msp { +Application *Application::app_ = 0; +Application::Starter *Application::starter_ = 0; +void *Application::data_ = 0; + Application::Application(): exit_code(0) { } /** Constructs an instance of the registered application class and runs it. If the -application throws a UsageError, the static usage() function is called. +application throws a usage_error, a help message is printed. The GetOpt class +will throw such exceptions automatically in error conditions. This function can only be called once. The global main() function provided by the library normally does it automatically at program startup. */ int Application::run(int argc, char **argv, void *data) { - static bool called=false; + static bool called = false; if(called) { - cerr<<"Trying to call Application::run_app twice!\n"; + IO::cerr.write("Trying to call Application::run_app twice!\n"); return 125; } - called=true; + called = true; if(!starter_) { - cerr<<"Trying to run with no RegisteredApplication class!\n"; + IO::cerr.write("Trying to run with no RegisteredApplication class!\n"); return 126; } - data_=data; + data_ = data; try { try { - app_=starter_->create_app(argc, argv); + app_ = starter_->create_app(argc, argv); } - catch(const UsageError &e) + catch(const usage_error &e) { - starter_->usage(e.what(), argv[0], e.get_brief()); + IO::print(IO::cerr, "%s\n%s\n", e.what(), e.help()); return 1; } - int result=app_->main(); - Application *a=app_; - app_=0; + int result = app_->main(); + Application *a = app_; + app_ = 0; delete a; return result; } @@ -71,42 +69,18 @@ int Application::run(int argc, char **argv, void *data) delete app_; #ifdef WIN32 - string msg=Debug::demangle(typeid(e).name())+":\n"+e.what(); + string msg = Debug::demangle(typeid(e).name())+":\n"+e.what(); MessageBoxA(0, msg.c_str(), "Uncaught exception", MB_OK|MB_ICONERROR); #else - cerr<<"An uncaught exception occurred.\n"; - cerr<<" type: "<(&e); - if(exc && !exc->get_backtrace().get_frames().empty()) - { - cerr<<" backtrace:\n"; - const list &frames=exc->get_backtrace().get_frames(); - for(list::const_iterator i=frames.begin(); i!=frames.end(); ++i) - cerr<<" "<<*i<<'\n'; - } + IO::print(IO::cerr, "An uncaught exception occurred.\n"); + IO::print(IO::cerr, " type: %s\n", Debug::demangle(typeid(e).name())); + IO::print(IO::cerr, " what(): %s\n", e.what()); #endif return 124; } } -/** -Prints a message describing the usage of the application. The default version -will blame the programmer for being lazy. - -@param reason Why the function was called -@param argv0 The value of argv[0], to be used in the message -@param brief Whether to print a brief or long usage message -*/ -void Application::usage(const char *reason, const char *, bool) -{ - if(reason) - cerr<<"UsageError: "<