Main Page Class Hierarchy Compound List File List Compound Members File Members

# mfc_dlog_app_base.cpp

Go to the documentation of this file.

    00001 // mfc_dlog_app_base.cpp : Base class for MFC applications that use a dialog-style.
    00002 //
    00003
    00004 //#include <stdafx.h>
    00005
    00006 #include <mfc_dlog_app_base.h>
    00007 #include <comfile.h>
    00008
    00009 //#ifdef _DEBUG
    00010 //#define new DEBUG_NEW
    00011 //#undef THIS_FILE
    00012 //#define THIS_FILE "__FILE__"
    00013 //static char THIS_FILE[] = __FILE__;
    00014 //#endif
    00015
    00016
    00017
    00019
    00020
    00021 /*
    00022 ** This function is used by CMFCDialogAppBase, and all derivatives,
    00023 ** to start worker threads running in a given class method.
    00024 **
    00025 ** First, set class variable(s) to track start directives,
    00026 ** then call this method, with 'this' as the parameter,
    00027 ** afterwards the worker thread can determine from the class
    00028 ** variables what the thread activities should be performed.
    00029 **
    00030 ** Note that the class variables are not thread-safe, so
    00031 ** multiple worker threads, the caller should impose a sleep
    00032 ** after each AfxBeginThread() to allow the worker thread to
    00033 ** read the class variables before they are changed.
    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 // CMFCDialogAppBase
    00047
    00048 BEGIN_MESSAGE_MAP(CMFCDialogAppBase, CWinApp)
    00049         //{{AFX_MSG_MAP(CMFCDialogAppBase)
    00050                 // NOTE - the ClassWizard will add and remove mapping macros here.
    00051                 //    DO NOT EDIT what you see in these blocks of generated code!
    00052         //}}AFX_MSG
    00053         ON_COMMAND(ID_HELP, CWinApp::OnHelp)
    00054    // Using dialog-based application needs special idle processing
    00055    // to updates menus, ets.
    00056 END_MESSAGE_MAP()
    00057
    00058
    00059 // CMFCDialogAppBase construction
    00060
    00061 CMFCDialogAppBase::CMFCDialogAppBase()
    00062 {
    00063    LoggingLevel = 0;
    00064 }
    00065
    00067 // The one and only CMFCDialogAppBase object
    00068
    00069 // CMFCDialogAppBase theApp;
    00070
    00071
    00073 // CMFCDialogAppBase initialization
    00074
    00075 BOOL CMFCDialogAppBase::InitInstance()
    00076 {
    00077
    00078    try
    00079    {
    00080 #ifdef _DEBUG
    00081       TLogger::TruncateOnOpen();
    00082 #endif
    00083
    00084       // m_lpCmdLine should be the configuration (.d) file name
    00085
    00086       // Check the command line
    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          // error parsing command file
    00102          worm_exception _expt("Error parsing command file ");
    00103                         _expt += m_lpCmdLine;
    00104          throw _expt;
    00105       }
    00106
    00107       // Running = true so any threads started in InitApp()
    00108       //           won't quit right away
    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         // Since the dialog has been closed, return FALSE so that we exit the
    00136         //  application, rather than start the application's message pump.
    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:  // eof
    00177                  _reading = false;
    00178                  break;
    00179
    00180             case COMFILE_ERROR: // error
    00181                  throw worm_exception("error returned by TComFileParser::ReadLine()");
    00182
    00183             case 0:  // empty line, TComFileParser should not return this
    00184                  break;
    00185
    00186             default:
    00187
    00188                  // Get the command into _token
    00189                  _token = _parser->NextToken();
    00190
    00191                  // check if module .d commands handled by deriving class
    00192                  //
    00193                  if ( HandleConfigLine(_parser) != HANDLER_UNUSED )
    00194                  {
    00195                     continue;
    00196                  }
    00197
    00198                  // Not handled
    00199                  //
    00200                  // Don't throw an error here because it is preferable to report
    00201                  // as many errors as possible to the file.
    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       // Is derivative class ready?
    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