Pol  Revision:cb584c9
ProgramMain.cpp
Go to the documentation of this file.
1 #include "ProgramMain.h"
2 
3 #include <stdlib.h>
4 
5 #include "../Debugging/ExceptionParser.h"
6 #include "../logfacility.h"
7 #include "ProgramConfig.h"
8 #include "pol_global_config.h"
9 
10 #ifdef ENABLE_BENCHMARK
11 #include <benchmark/benchmark.h>
12 #endif
13 
14 #ifdef _WIN32
15 #define WIN32_LEAN_AND_MEAN
16 #include "../Header_Windows.h"
17 #include <crtdbg.h>
18 #include <psapi.h>
19 #include <windows.h> // for GetModuleFileName
20 
21 #pragma comment( lib, "psapi.lib" ) // 32bit is a bit dumb..
22 #else
23 #endif
24 #include <clocale>
25 #include <exception>
26 #include <iostream>
27 #include <stdexcept>
28 #include <string>
29 
30 namespace Pol
31 {
32 namespace Clib
33 {
34 using namespace std;
35 
37 
41 
42 void ProgramMain::start( int argc, char* argv[] )
43 {
44  using namespace Pol;
46  Clib::Logging::initLogging( &logger );
48 
49  std::setlocale( LC_TIME, "" );
50 
51  int exitcode = 0;
52 
53  try
54  {
55 // FIXME: 2008 Upgrades needed here? Make sure this still valid on 2008
56 #if defined( _WIN32 ) && defined( _DEBUG ) && _MSC_VER >= 1300
57  // on VS.NET, disable obnoxious heap checking
58  int flags = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG );
59  flags &= 0x0000FFFF; // set heap check frequency to 0
60  _CrtSetDbgFlag( flags );
61 #endif
62 
63  /**********************************************
64  * store program arguments
65  **********************************************/
66  m_programArguments.clear();
67  for ( int i = 0; i < argc; i++ )
68  {
69  m_programArguments.push_back( std::string( argv[i] ) );
70  }
71 
72  /**********************************************
73  * determine and store the executable name
74  **********************************************/
75  std::string binaryPath = argv[0];
76 #ifdef WINDOWS
77  char modulePath[_MAX_PATH];
78  if ( GetModuleFileName( NULL, modulePath, sizeof modulePath ) )
79  binaryPath = modulePath;
80 #endif
81  PROG_CONFIG::configureProgramEnvironment( binaryPath );
82 
83 #ifdef ENABLE_BENCHMARK
84  benchmark::Initialize( &argc, argv );
85 #endif
86 
87  /**********************************************
88  * MAIN
89  **********************************************/
90  exitcode = main();
91  }
92  catch ( const char* msg )
93  {
94  ERROR_PRINT << "Execution aborted due to: " << msg << "\n";
95  exitcode = 1;
96  }
97  catch ( std::string& str )
98  {
99  ERROR_PRINT << "Execution aborted due to: " << str << "\n";
100  exitcode = 1;
101  } // egcs has some trouble realizing 'exception' should catch
102  catch ( std::runtime_error& re ) // runtime_errors, so...
103  {
104  ERROR_PRINT << "Execution aborted due to: " << re.what() << "\n";
105  exitcode = 1;
106  }
107  catch ( std::exception& ex )
108  {
109  ERROR_PRINT << "Execution aborted due to: " << ex.what() << "\n";
110  exitcode = 1;
111  }
112  catch ( int xn )
113  {
114  // Something that throws an integer is responsible for printing
115  // its own error message.
116  // "throw 3" is meant as an alternative to exit(3).
117  exitcode = xn;
118  }
119 #ifndef _WIN32
120  catch ( ... )
121  {
122  ERROR_PRINT << "Execution aborted due to generic exception."
123  << "\n";
124  exitcode = 2;
125  }
126 #endif
128 
129  exit( exitcode );
130 }
131 
132 const std::vector<std::string>& ProgramMain::programArgs() const
133 {
134  return m_programArguments;
135 }
136 
137 bool ProgramMain::programArgsFind( const std::string& filter, std::string* rest ) const
138 {
139  const std::vector<std::string>& binArgs = programArgs();
140  for ( size_t i = 1; i < binArgs.size(); i++ )
141  {
142  const std::string& param = binArgs[i];
143  switch ( param[0] )
144  {
145  case '/':
146  case '-':
147  if ( param.substr( 1, filter.size() ) == filter )
148  {
149  if ( rest != nullptr )
150  *rest = param.substr( 1 + filter.size(), param.size() - ( 1 + filter.size() ) );
151  return true;
152  }
153  break;
154  default:
155  break;
156  }
157  }
158  return false;
159 }
160 
161 std::string ProgramMain::programArgsFindEquals( const std::string& filter,
162  std::string defaultVal ) const
163 {
164  const std::vector<std::string>& binArgs = programArgs();
165  for ( size_t i = 1; i < binArgs.size(); i++ )
166  {
167  const std::string& param = binArgs[i];
168  if ( param.substr( 0, filter.size() ) == filter )
169  return param.substr( filter.size(), param.size() - ( filter.size() ) );
170  }
171  return defaultVal;
172 }
173 
174 int ProgramMain::programArgsFindEquals( const std::string& filter, int defaultVal,
175  bool hexVal ) const
176 {
177  std::string val = programArgsFindEquals( filter, "" );
178  if ( val.empty() )
179  return defaultVal;
180  return strtoul( val.c_str(), NULL, hexVal ? 16 : 10 );
181 }
182 }
183 } // namespaces
bool programArgsFind(const std::string &filter, std::string *rest=nullptr) const
void initLogging(LogFacility *logger)
Definition: logfacility.cpp:63
const std::vector< std::string > & programArgs() const
int main(int argc, char *argv[])
STL namespace.
void start(int argc, char *argv[])
Definition: ProgramMain.cpp:42
static void initGlobalExceptionCatching()
Initiates globally the exception catching (signal handlers for Linux)
std::string programArgsFindEquals(const std::string &filter, std::string defaultVal) const
#define ERROR_PRINT
Definition: logfacility.h:230
LogFacility * global_logger
Definition: logfacility.cpp:62
Definition: berror.cpp:12