00001
00002
00003
00004
00005
00006 #include <mfc_dlog_app_base.h>
00007 #include <comfile.h>
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 UINT AFX_CDECL StartMFCWorkerThread(LPVOID p_object)
00036 {
00037 CMFCDialogAppBase * p_sr = (CMFCDialogAppBase *)p_object;
00038
00039 p_sr->StartWorkerThread();
00040
00041 return 0;
00042 }
00043
00044
00046
00047
00048 BEGIN_MESSAGE_MAP(CMFCDialogAppBase, CWinApp)
00049
00050
00051
00052
00053 ON_COMMAND(ID_HELP, CWinApp::OnHelp)
00054
00055
00056 END_MESSAGE_MAP()
00057
00058
00059
00060
00061 CMFCDialogAppBase::CMFCDialogAppBase()
00062 {
00063 LoggingLevel = 0;
00064 }
00065
00067
00068
00069
00070
00071
00073
00074
00075 BOOL CMFCDialogAppBase::InitInstance()
00076 {
00077
00078 try
00079 {
00080 #ifdef _DEBUG
00081 TLogger::TruncateOnOpen();
00082 #endif
00083
00084
00085
00086
00087 if ( m_lpCmdLine[0] == '\0' )
00088 {
00089 throw worm_exception("Missing command line parm 1 (configuration file name)");
00090 }
00091
00092
00093 if ( ! PrepApp(m_lpCmdLine) )
00094 {
00095 throw worm_exception("Failed preparing application");
00096 }
00097
00098
00099 if ( ! ParseCommandFile(m_lpCmdLine) )
00100 {
00101
00102 worm_exception _expt("Error parsing command file ");
00103 _expt += m_lpCmdLine;
00104 throw _expt;
00105 }
00106
00107
00108
00109 Running = true;
00110
00111 if ( ! InitApp() )
00112 {
00113 throw worm_exception("Failed call to InitApp()");
00114 }
00115
00116
00117 m_pMainWnd = GetMainWindow();
00118
00119 OpenMainDialog();
00120
00121 }
00122 catch( worm_exception _we )
00123 {
00124 TLogger::Logit( WORM_LOG_TOFILE|WORM_LOG_TIMESTAMP
00125 , "%s: Exiting due to\n%s\n"
00126 , GetApplicationName()
00127 , _we.what()
00128 );
00129 }
00130
00131
00132 TLogger::Close();
00133
00134
00135
00136
00137 return FALSE;
00138 }
00139
00141
00142
00144
00145
00146 bool CMFCDialogAppBase::ParseCommandFile( LPTSTR p_filename )
00147 {
00148 bool r_status = true;
00149
00150 TComFileParser * _parser = NULL;
00151
00152 try
00153 {
00154
00155 if ( (_parser = new TComFileParser()) == NULL )
00156 {
00157 throw worm_exception("failed creating TComFileParser to parse configuration file");
00158 }
00159
00160 if ( ! _parser->Open(p_filename) )
00161 {
00162 char _msg[80];
00163 sprintf( _msg, "failed opening configuration file %s", p_filename );
00164 throw worm_exception(_msg);
00165 }
00166
00167
00168 bool _reading = true;
00169
00170 char * _token;
00171
00172 do
00173 {
00174 switch( _parser->ReadLine() )
00175 {
00176 case COMFILE_EOF:
00177 _reading = false;
00178 break;
00179
00180 case COMFILE_ERROR:
00181 throw worm_exception("error returned by TComFileParser::ReadLine()");
00182
00183 case 0:
00184 break;
00185
00186 default:
00187
00188
00189 _token = _parser->NextToken();
00190
00191
00192
00193 if ( HandleConfigLine(_parser) != HANDLER_UNUSED )
00194 {
00195 continue;
00196 }
00197
00198
00199
00200
00201
00202
00203 TLogger::Logit( WORM_LOG_TOFILE|WORM_LOG_TIMESTAMP
00204 , "ParseCommandFile(): unrecognized command: %s\n"
00205 , _token
00206 );
00207
00208 r_status = false;
00209 }
00210 } while( _reading );
00211
00212 delete( _parser );
00213 _parser = NULL;
00214
00215
00216
00217
00218 if ( ! IsReady() )
00219 {
00220 throw worm_exception("application not configured properly");
00221 }
00222
00223 }
00224 catch( worm_exception _we )
00225 {
00226 TLogger::Logit( WORM_LOG_TOFILE
00227 , "ParseCommandFile(): Error: \n%s\n"
00228 , _we.what()
00229 );
00230 r_status = false;
00231 }
00232
00233 if ( _parser != NULL )
00234 {
00235 delete( _parser ) ;
00236 }
00237
00238
00239
00240 return r_status;
00241 }
00242
00244
00245