Annotation of mstools/samples/sdktools/capview/capview.cpp, revision 1.1.1.1

1.1       root        1: /*
                      2:  *    Main program
                      3:  */
                      4: 
                      5: 
                      6: #include        <afxwin.h>
                      7: #include        <afxdlgs.h>
                      8: #include        <afxcoll.h>
                      9: #include        <iostream.h>
                     10: #include        <fstream.h>
                     11: 
                     12: extern "C"
                     13: {
                     14:     #include <commdlg.h>
                     15:     #include <stdio.h>
                     16:     #include <string.h>
                     17:     #include <imagehlp.h>
                     18: }
                     19: 
                     20: #include "types.h"
                     21: #include "resource.h"
                     22: #include "ranges.h"
                     23: #include "dispobj.h"
                     24: #include "timer.h"
                     25: #include "tree.h"
                     26: #include "list.h"
                     27: #include "listwin.h"
                     28: #include "capview.h"
                     29: 
                     30: #define SIZESTRING  256
                     31: 
                     32: /************************  Data  *******************************/
                     33: 
                     34: CTheApp         WinAnalApp;
                     35: HCURSOR         HCursorSizer;
                     36: HCURSOR         HCursorNormal;
                     37: HICON           HIconList;
                     38: HICON           HIconTree;
                     39: LOGFONT         DefaultFont;
                     40: 
                     41: /***************************************************************/
                     42: 
                     43: void GetWindows(char * pchProgram, int iThread, CListWnd ** ppListWnd,
                     44:                 CTREEWND ** pTreeWnd);
                     45: 
                     46: /************************  CTheApp definitions *****************/
                     47: 
                     48: /***    CTheApp::InitInstance
                     49:  *
                     50:  *      This function is an override of a virtual function.  The
                     51:  *      function is used to do any initialization which is needed
                     52:  *      for the CapView as a whole (as oppose to window initialization).
                     53:  *
                     54:  *      Create the main application whine and load some standard objects
                     55:  *      from the library of objects.
                     56:  */
                     57: 
                     58: BOOL  CTheApp::InitInstance()
                     59: {
                     60:     CFont       systemFont;
                     61: 
                     62:     /*
                     63:      *  Create the main window
                     64:      */
                     65:     
                     66:     m_pMainWnd = new CMainWindow();
                     67:     m_pMainWnd->ShowWindow( m_nCmdShow );
                     68:     m_pMainWnd->UpdateWindow();
                     69: 
                     70:     /*
                     71:      *  Load stock objects from the system
                     72:      */
                     73:     
                     74:     systemFont.CreateStockObject(SYSTEM_FONT);
                     75:     systemFont.GetObject(sizeof(LOGFONT), &DefaultFont);
                     76: 
                     77:     HIconList = LoadIcon(LIST_ICO);
                     78:     HIconTree = LoadIcon(TREE_ICO);
                     79:     
                     80:     HCursorSizer = LoadStandardCursor(IDC_SIZEWE);    
                     81:     HCursorNormal = LoadStandardCursor(IDC_ARROW);
                     82: 
                     83:     /*
                     84:      *  Process any command line which shows up
                     85:      */
                     86: 
                     87:     if (m_lpCmdLine && *m_lpCmdLine) {
                     88:         ((CMainWindow *) m_pMainWnd)->LoadFile(m_lpCmdLine);
                     89:     }
                     90:     
                     91:     return TRUE;
                     92: }                              /* CTheApp::InitInstance() */
                     93: 
                     94: 
                     95: 
                     96: 
                     97: /***********************  CMainWindow definitions ***************/
                     98: 
                     99: BEGIN_MESSAGE_MAP( CMainWindow, CMDIFrameWnd )
                    100:        ON_WM_CREATE()
                    101: 
                    102:        ON_COMMAND(IDM_EXIT, OnExit)
                    103:        ON_COMMAND(IDM_OPEN, OnOpen)
                    104: 
                    105:        ON_COMMAND(IDM_TILE, MDITile)
                    106:        ON_COMMAND(IDM_CASCADE, MDICascade)
                    107:        ON_COMMAND(IDM_ARRANGEICON, MDIIconArrange)
                    108: 
                    109:        ON_COMMAND(IDM_ABOUT, OnAbout)
                    110:         ON_COMMAND(IDM_HELP, OnHelp)
                    111: END_MESSAGE_MAP()
                    112: 
                    113: CMainWindow::CMainWindow()
                    114: {
                    115:     LoadAccelTable( "MainAccelTable" );
                    116:     Create(NULL, "Capview", WS_OVERLAPPEDWINDOW, rectDefault, NULL,
                    117:            "MdiMenuInit");
                    118:     return;
                    119: }                               /* CMainWindow::CMainWindow() */
                    120: 
                    121: CMainWindow::~CMainWindow()
                    122: {
                    123:     delete m_pMenuInit;
                    124:     return;
                    125: }                               /* CMainWindow::~CMainWindow() */
                    126: 
                    127: int CMainWindow::OnCreate(LPCREATESTRUCT lpCreateStruct)
                    128: {
                    129:     m_pMenuInit = new CMenu();
                    130:     m_pMenuInit->LoadMenu("MdiMenuInit");
                    131:     CreateClient(lpCreateStruct, m_pMenuInit->GetSubMenu(1));
                    132:     return 0;
                    133: }                               /* CMainWindow::OnCreate() */
                    134: 
                    135: /*
                    136:  *  Defined the set of command which are used in response
                    137:  *      to commands from the menu bar
                    138:  */
                    139: 
                    140: void CMainWindow::OnAbout()
                    141: {
                    142:     ShellAbout(m_hWnd, "CapView", NULL, NULL);
                    143:     return;
                    144: }                               /* CMainWindow::OnAbout() */
                    145: 
                    146: void CMainWindow::OnHelp()
                    147: {
                    148:     WinHelp(m_hWnd, "capview.hlp", HELP_CONTENTS, 0);
                    149:     return;
                    150: }                               /* CMainWindow::OnHelp() */
                    151: 
                    152: void CMainWindow::OnExit()
                    153: {
                    154:     DestroyWindow();
                    155:     return;
                    156: }                               /* CMainWindow::OnExit() */
                    157: 
                    158: void CMainWindow::OnOpen()
                    159: {
                    160:     /*
                    161:      *  Attempt to open a profile file and read it.
                    162:      *  If a file or archive exception occurs, catch it and
                    163:      *  present an error message box.
                    164:      */
                    165:     
                    166:     CString szFileName, szFileTitle;
                    167:     
                    168:     /*
                    169:      *  Use CommDlg to get the file name and then call LoadFile.
                    170:      */
                    171:     
                    172:     if ( FileDlg( TRUE, SIZESTRING, szFileName.GetBuffer( SIZESTRING ),
                    173:                 SIZESTRING, szFileTitle.GetBuffer( SIZESTRING ) ) ) {
                    174:        
                    175:        szFileName.ReleaseBuffer();
                    176:        szFileTitle.ReleaseBuffer();
                    177: 
                    178:         LoadFile(szFileName);
                    179:     }
                    180: 
                    181:     return;
                    182: }                               /* CMainWindow::OnOpen() */
                    183: 
                    184: 
                    185: void CMainWindow::LoadFile(CString szFileName)
                    186: {
                    187:     ifstream    myFile;
                    188:     char        rgch[4000];
                    189:     char        rgch2[256];
                    190:     char *      pchDll;
                    191:     char *      pchFunction;
                    192:     char *      pch;
                    193:     char *      pchProgram;
                    194:     int         iDepth;
                    195:     int         iCalls;
                    196:     int         iThread;
                    197:     TIMETYPE    iTotalTime;
                    198:     TIMETYPE    iRoutineTime;
                    199:     CListWnd *  pListWnd;
                    200:     CTREEWND *  pTreeWnd;
                    201: 
                    202:     /*
                    203:      *  Now start processing the file
                    204:      */
                    205:     
                    206:     myFile.open(szFileName );
                    207:     if (!myFile.is_open()) {
                    208:         _snprintf(rgch, sizeof(rgch), "Cannot open file '%s'", szFileName);
                    209:         MessageBox(rgch);
                    210:         return;
                    211:     }
                    212: 
                    213:     /*
                    214:      *  Get the first line -- it tells us which file is being profiled
                    215:      */
                    216:     
                    217:     myFile.getline( rgch, sizeof(rgch) );
                    218:     if (strncmp(rgch, "Call Profile of ", 16) != 0) {
                    219:         /*
                    220:          *  Message box -- not correct file formatter
                    221:          */
                    222: 
                    223:         _snprintf(rgch, sizeof(rgch),
                    224:                   "The file '%s' is incorrectly formatted", szFileName);
                    225:         MessageBox(rgch);
                    226:         return;
                    227:     }
                    228: 
                    229:     /*
                    230:      * Capture the program name
                    231:      */
                    232:      
                    233:     pchProgram = &rgch[16];
                    234:     strtok(pchProgram, " ");
                    235:     pchProgram = strdup(pchProgram);
                    236: 
                    237:     /*
                    238:      * Process the rest of the file
                    239:      */
                    240:     
                    241:     while (TRUE) {
                    242:         /*
                    243:          *  When we can't get a line -- exit the loop
                    244:          */
                    245:         
                    246:         myFile.getline( rgch, sizeof(rgch) );
                    247:         if (!myFile.good()) {
                    248:             break;
                    249:         }
                    250: 
                    251:         /*
                    252:          *  Look for the first possible valid line
                    253:          */
                    254:         
                    255:         if ((strncmp( rgch, "T h r e a d  #", 14) == 0) ||
                    256:             (strncmp( rgch, "S e c t i o n  #", 16) == 0)) {
                    257:             iThread = atoi(&rgch[14]);
                    258: 
                    259:             /*
                    260:              *  Get the List and Tree window to be used for this threads
                    261:              *  set of data.
                    262:              */
                    263: 
                    264:             GetWindows(pchProgram, iThread, &pListWnd, &pTreeWnd);
                    265: 
                    266:             pListWnd->InitTiming();
                    267:             pTreeWnd->InitTiming();
                    268:             
                    269:             /*
                    270:              *  Skip over the headers
                    271:              */
                    272:             
                    273:             myFile.getline( rgch, sizeof(rgch) );
                    274:             myFile.getline( rgch, sizeof(rgch) );
                    275:             myFile.getline( rgch, sizeof(rgch) );
                    276:             
                    277:             /*
                    278:              * Start processing each data line
                    279:              */
                    280:             
                    281:             while (TRUE) {
                    282:                 myFile.getline( rgch, sizeof(rgch) );
                    283:                 pch = strtok(rgch, " \t\n");
                    284:                 if (pch == NULL) {
                    285:                     break;
                    286:                 }
                    287:                 
                    288:                 iDepth = atoi(pch);
                    289:                 pchDll = strtok(NULL, " \t:");
                    290:                 if (*pchDll == '*') {
                    291:                     pchDll = strtok(NULL, " \t:");
                    292:                 }
                    293:                 pchFunction = strtok(NULL, " \t");
                    294:                 iCalls = atoi(strtok(NULL, " \t"));
                    295:                 iTotalTime = strtok(NULL, " \t");
                    296:                 strtok(NULL, " \t");
                    297:                 iRoutineTime = strtok(NULL, " \t");
                    298: 
                    299:                 /*
                    300:                  *  Remove undeceration from function name
                    301:                  */
                    302: 
                    303:                 if (UnDecorateSymbolName(pchFunction, rgch2, sizeof(rgch2),
                    304:                               UNDNAME_NO_FUNCTION_RETURNS |
                    305:                               UNDNAME_32_BIT_DECODE |
                    306:                               UNDNAME_NO_ACCESS_SPECIFIERS |
                    307:                               UNDNAME_NO_MS_KEYWORDS)) {
                    308:                     pchFunction = rgch2;
                    309:                 }
                    310:                 
                    311:                 pListWnd->AddTiming(iDepth, pchDll, pchFunction, iCalls,
                    312:                                     iTotalTime, iRoutineTime);
                    313:                 pTreeWnd->AddTiming(iDepth, pchDll, pchFunction, iCalls,
                    314:                                     iTotalTime, iRoutineTime);
                    315: 
                    316:             }
                    317:             
                    318:             /*
                    319:              *
                    320:              */
                    321:             
                    322:             pTreeWnd->EndTiming();
                    323:             pTreeWnd->Invalidate();
                    324:             pTreeWnd->SetScrollRange(SB_HORZ, 0, 100);
                    325:             pTreeWnd->SetScrollRange(SB_VERT, 0, 100);
                    326:             pTreeWnd->ShowWindow(SW_SHOW);
                    327:             
                    328:             /*
                    329:              *
                    330:              */
                    331:             
                    332:             pListWnd->EndTiming();
                    333:             pListWnd->Invalidate();
                    334:         }
                    335:     }
                    336: 
                    337:     free(pchProgram);
                    338:     return;
                    339: }                               /* CMainWindow::LoadFile() */
                    340: 
                    341: //////////////////////////////////////////////////
                    342: //  CMainWindow::FileDlg
                    343: //  Call the commdlg routine to display File Open or File Save As
                    344: //  dialogs.  The setup is the same for either.  If bOpen is TRUE
                    345: //  then File Open is display otherwise File Save As is displayed.
                    346: //  The File Name and File Title are stored at the string pointer
                    347: //  passed in.
                    348: //
                    349: BOOL CMainWindow::FileDlg( BOOL bOpen, int nMaxFile, LPSTR szFile,
                    350:                int nMaxFileTitle, LPSTR szFileTitle )
                    351: {
                    352:     OPENFILENAME of;
                    353:     
                    354:     char szDirName[SIZESTRING];
                    355:     char szFilter[] = "Cap input files (*.cap)\0"
                    356:          "*.cap\0"
                    357:          "Cap end files (*.end)\0"
                    358:          "*.end\0"
                    359:          "All files (*.*)\0"
                    360:          "*.*\0"
                    361:          "\0";
                    362: 
                    363:     szDirName[0] = '.';
                    364: 
                    365:     of.lStructSize = sizeof( OPENFILENAME );
                    366:     of.hwndOwner = m_hWnd;
                    367:     of.lpstrFilter = szFilter;
                    368:     of.lpstrCustomFilter = NULL;
                    369:     of.nMaxCustFilter = 0L;
                    370:     of.nFilterIndex = 1L;
                    371:     of.lpstrFile=szFile;
                    372:     of.nMaxFile=nMaxFile;
                    373:     of.lpstrFileTitle = szFileTitle;
                    374:     of.nMaxFileTitle = nMaxFileTitle;
                    375:     of.lpstrInitialDir = szDirName;
                    376:     of.lpstrTitle = NULL;
                    377:     of.nFileOffset = 0;
                    378:     of.nFileExtension = 0;
                    379:     of.lpstrDefExt = "pb";
                    380:     
                    381:     if ( bOpen ) {
                    382:        of.Flags = OFN_HIDEREADONLY | OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
                    383:        return GetOpenFileName( &of );
                    384:     } else {
                    385:        of.Flags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;
                    386:        return GetSaveFileName( &of );
                    387:     }
                    388: }                               /* CMainWindow::FileDlg() */
                    389: 
                    390: void CMainWindow::GetWindows(char * szProgram, int iThread,
                    391:                              CListWnd ** ppListWnd, CTREEWND ** ppTreeWnd)
                    392: {
                    393:     char        rgchKey[100];
                    394:     char        rgchTitle[100];
                    395:     int         count;
                    396:     void *      pv;
                    397:     
                    398:     *ppListWnd = new CListWnd;
                    399:     *ppTreeWnd = new CTREEWND;
                    400: 
                    401:     _snprintf(rgchKey, sizeof(rgchKey), "%s Thread: %d",
                    402:              szProgram, iThread);
                    403: 
                    404:     if (mapCInstance.Lookup(rgchKey,  pv)) {
                    405:         count = 1 + (int) pv;
                    406:         mapCInstance[rgchKey] = (void *) count;
                    407:     } else {
                    408:         count = 1;
                    409:         mapCInstance[rgchKey] = (void *) 1;
                    410:     }
                    411: 
                    412:     _snprintf(rgchTitle, sizeof(rgchTitle), "Tree: %s <%d>", rgchKey, count);
                    413:     if (!(*ppTreeWnd)->Create(rgchTitle, WS_HSCROLL | WS_VSCROLL,
                    414:                           rectDefault, this)) {
                    415:         delete *ppTreeWnd;
                    416:         *ppTreeWnd = NULL;
                    417:         return;
                    418:     }
                    419:             
                    420:     _snprintf(rgchTitle, sizeof(rgchTitle), "List: %s <%d>", rgchKey, count);
                    421: 
                    422:     if (!(*ppListWnd)->Create(rgchTitle, WS_VSCROLL, rectDefault,
                    423:                           this)) {
                    424:         delete *ppListWnd;
                    425:         *ppListWnd = NULL;
                    426:         return;
                    427:         
                    428:     }
                    429:     return;
                    430: }                               /* CMainWindow::GetWindows() */

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.