home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_122 / 6.ddi / WEXAMPLE.ZIP / HDUMP.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  34.9 KB  |  1,027 lines

  1. // Borland C++ - (C) Copyright 1991, 1992 by Borland International
  2.  
  3. //*******************************************************************
  4. //
  5. // program - Hdump.c
  6. // purpose - a windows program to dump a file in hex.
  7. //
  8. //*******************************************************************
  9.  
  10. #define  STRICT
  11. #include <windows.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <time.h>
  16. #include <bios.h>
  17. #include <ctype.h>
  18. #include <io.h>
  19. #include <dos.h>
  20. #include <dir.h>
  21.  
  22. #include "hdump.h"
  23.  
  24. typedef struct scrollkeys
  25.   {
  26.     WORD wVirtkey;
  27.     int  iMessage;
  28.     WORD wRequest;
  29.   } SCROLLKEYS;
  30.  
  31. SCROLLKEYS key2scroll [] =
  32.   {
  33.     { VK_HOME,  WM_COMMAND, IDM_HOME },
  34.     { VK_END,   WM_VSCROLL, SB_BOTTOM },
  35.     { VK_PRIOR, WM_VSCROLL, SB_PAGEUP },
  36.     { VK_NEXT,  WM_VSCROLL, SB_PAGEDOWN },
  37.     { VK_UP,    WM_VSCROLL, SB_LINEUP },
  38.     { VK_DOWN,  WM_VSCROLL, SB_LINEDOWN },
  39.     { VK_LEFT,  WM_HSCROLL, SB_PAGEUP },
  40.     { VK_RIGHT, WM_HSCROLL, SB_PAGEDOWN }
  41.   };
  42.  
  43. # define NUMKEYS (sizeof key2scroll / sizeof key2scroll[0])
  44.  
  45. // data initialized by first instance
  46. typedef struct tagSETUPDATA
  47.   {
  48.     char   szAppName[10]; // name of application
  49.   } SETUPDATA;
  50.  
  51. SETUPDATA SetUpData;
  52.  
  53. // Data that can be referenced throughout the
  54. // program but not passed to other instances
  55.  
  56. HINSTANCE hInst;                              // hInstance of application
  57. HWND      hWndMain;                           // hWnd of main window
  58.  
  59. int       xChar, yChar, yCharnl;              // character size
  60. int       xClient, yClient;                   // client window size
  61.  
  62. LOGFONT   cursfont;                           // font structure
  63. HFONT     holdsfont;                          // handle of original font
  64. HFONT     hnewsfont;                          // handle of new fixed font
  65.  
  66. // window scroll/paint stuff
  67. int       nVscrollMax, nHscrollMax;           // scroll ranges
  68. int       nVscrollPos, nHscrollPos;           // current scroll positions
  69. int       numlines;                           // number of lines in file
  70. int       maxwidth;                           // width of display format
  71. int       nVscrollInc, nHscrollInc;           // scroll increments
  72. int       nPageMaxLines;                      // max lines on screen
  73.  
  74. // file open dialog stuff
  75. char      szFileSpec[80];                     // file spec for initial search
  76. char      szDefExt[5];                        // default extension
  77. char      szFileName[80];                     // file name
  78. char      szFilePath[80];                     // file path
  79. WORD      wFileAttr;                          // attribute for search
  80. WORD      wStatus;                            // status of search
  81.  
  82. // dump file stuff
  83. char      szFName[64];                        // file to display
  84. FILE     *fh;                                 // handle of open file
  85. LONG      fsize;                              // size of file
  86.  
  87. // function prototypes
  88.  
  89. int      PASCAL        WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  90.                                LPSTR lpszCmdLine, int cmdShow);
  91.  
  92. void                   InitHdump(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  93.                                  LPSTR lpszCmdLine, int cmdShow);
  94. void                   InitHdumpFirst(HINSTANCE hInstance);
  95. void                   InitHdumpAdded(HINSTANCE hPrevInstance);
  96. void                   InitHdumpEvery(HINSTANCE hInstance, int cmdShow);
  97. void                   CloseHdump(void);
  98. BOOL CALLBACK          About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);
  99.  
  100. LRESULT CALLBACK       HdumpWndProc(HWND hWnd, UINT message,
  101.                             WPARAM wParam, LPARAM lParam);
  102.  
  103. void                   SetupScroll(HWND hWnd);
  104. void                   HdumpPaint(HWND hWnd);
  105. char                  *SnapLine(char *szBuf, LPSTR mem, int len,
  106.                                 int dwid, char *olbl);
  107.  
  108. int                    DoFileOpenDlg(HINSTANCE hInst, HWND hWnd,
  109.                                      char *szFileSpecIn, char *szDefExtIn,
  110.                                      WORD wFileAttrIn,
  111.                                      char *szFilePathOut, char *szFileNameOut);
  112. extern BOOL CALLBACK FileOpenDlgProc(HWND hDlg, UINT iMessage,
  113.                        WPARAM wParam, LPARAM lParam);
  114.  
  115. //*******************************************************************
  116. // WinMain - Hdump main
  117. //
  118. // paramaters:
  119. //             hInstance     - The instance of this instance of this
  120. //                             application.
  121. //             hPrevInstance - The instance of the previous instance
  122. //                             of this application. This will be 0
  123. //                             if this is the first instance.
  124. //             lpszCmdLine   - A long pointer to the command line that
  125. //                             started this application.
  126. //             cmdShow       - Indicates how the window is to be shown
  127. //                             initially. ie. SW_SHOWNORMAL, SW_HIDE,
  128. //                             SW_MIMIMIZE.
  129. //
  130. // returns:
  131. //             wParam from last message.
  132. //
  133. //*******************************************************************
  134. int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  135.                    LPSTR lpszCmdLine, int cmdShow)
  136. {
  137.     MSG   msg;
  138.  
  139.     // Go init this application.
  140.     InitHdump(hInstance, hPrevInstance, lpszCmdLine, cmdShow);
  141.  
  142.     // Get and dispatch messages for this applicaton.
  143.     while (GetMessage(&msg, NULL, 0, 0))
  144.     {
  145.         TranslateMessage(&msg);
  146.     DispatchMessage(&msg);
  147.     }
  148.  
  149.     return(msg.wParam);
  150. }
  151.  
  152. //*******************************************************************
  153.  
  154. //*******************************************************************
  155. // InitHdump - init the Hdump application
  156. //
  157. // paramaters:
  158. //             hInstance     - The instance of this instance of this
  159. //                             application.
  160. //             hPrevInstance - The instance of the previous instance
  161. //                             of this application. This will be 0
  162. //                             if this is the first instance.
  163. //             lpszCmdLine   - A long pointer to the command line that
  164. //                             started this application.
  165. //             cmdShow       - Indicates how the window is to be shown
  166. //                             initially. ie. SW_SHOWNORMAL, SW_HIDE,
  167. //                             SW_MIMIMIZE.
  168. //
  169. //*******************************************************************
  170. #pragma argsused
  171. void InitHdump(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  172.            LPSTR lpszCmdLine, int cmdShow)
  173. {
  174.     if (! hPrevInstance)              // if no previous instance, this is first
  175.         InitHdumpFirst(hInstance);
  176.     else
  177.         InitHdumpAdded(hPrevInstance);   // this is not first instance
  178.  
  179.     InitHdumpEvery(hInstance, cmdShow);  // initialization for all instances
  180. }
  181.  
  182. //*******************************************************************
  183. // InitHdumpFirst - done only for first instance of Hdump
  184. //
  185. // paramaters:
  186. //             hInstance     - The instance of this instance of this
  187. //                             application.
  188. //
  189. //*******************************************************************
  190. void InitHdumpFirst(HINSTANCE hInstance)
  191. {
  192.     WNDCLASS wcHdumpClass;
  193.  
  194.     // Get string from resource with application name.
  195.     LoadString(hInstance, IDS_NAME, (LPSTR) SetUpData.szAppName, 10);
  196.  
  197.     // Define the window class for this application.
  198.     wcHdumpClass.lpszClassName = SetUpData.szAppName;
  199.     wcHdumpClass.hInstance     = hInstance;
  200.     wcHdumpClass.lpfnWndProc   = HdumpWndProc;
  201.     wcHdumpClass.hCursor       = LoadCursor(NULL, IDC_ARROW);
  202.     wcHdumpClass.hIcon         = LoadIcon(hInstance, SetUpData.szAppName);
  203.     wcHdumpClass.lpszMenuName  = (LPSTR) SetUpData.szAppName;
  204.     wcHdumpClass.hbrBackground = GetStockObject(WHITE_BRUSH);
  205.     wcHdumpClass.style         = CS_HREDRAW | CS_VREDRAW;
  206.     wcHdumpClass.cbClsExtra    = 0;
  207.     wcHdumpClass.cbWndExtra    = 0;
  208.  
  209.     // Register the class
  210.     RegisterClass(&wcHdumpClass);
  211. }
  212.  
  213. //*******************************************************************
  214. // InitHdumpAdded - done only for added instances of Hdump
  215. //
  216. // paramaters:
  217. //             hPrevInstance - The instance of the previous instance
  218. //                             of this application.
  219. //
  220. //*******************************************************************
  221. void InitHdumpAdded(HINSTANCE hPrevInstance)
  222. {
  223.     // get the results of the initialization of first instance
  224.     GetInstanceData(hPrevInstance, (BYTE*) &SetUpData, sizeof(SETUPDATA));
  225. }
  226.  
  227. //*******************************************************************
  228. // InitHdumpEvery - done for every instance of Hdump
  229. //
  230. // paramaters:
  231. //             hInstance     - The instance of this instance of this
  232. //                             application.
  233. //             cmdShow       - Indicates how the window is to be shown
  234. //                             initially. ie. SW_SHOWNORMAL, SW_HIDE,
  235. //                             SW_MIMIMIZE.
  236. //
  237. //*******************************************************************
  238. void InitHdumpEvery(HINSTANCE hInstance, int cmdShow)
  239. {
  240.     TEXTMETRIC tm;
  241.     HDC        hDC;
  242.  
  243.     hInst = hInstance;       // save for use by window procs
  244.  
  245.     // Create applications main window.
  246.     hWndMain = CreateWindow(
  247.                   SetUpData.szAppName,     // window class name
  248.                   SetUpData.szAppName,     // window title
  249.                   WS_OVERLAPPEDWINDOW |    // type of window
  250.             WS_HSCROLL |
  251.                     WS_VSCROLL,
  252.                   CW_USEDEFAULT,           // x  window location
  253.                   0,                       // y
  254.                   CW_USEDEFAULT,           // cx and size
  255.                   0,                       // cy
  256.                   NULL,                    // no parent for this window
  257.                   NULL,                    // use the class menu
  258.                   hInstance,               // who created this window
  259.                   NULL                     // no parms to pass on
  260.                   );
  261.  
  262.     // Get the display context.
  263.     hDC = GetDC(hWndMain);
  264.  
  265.     // Build fixed screen font. Needed to display hex formated dump.
  266.     cursfont.lfHeight         =  6;
  267.     cursfont.lfWidth          =  6;
  268.     cursfont.lfEscapement     =  0;
  269.     cursfont.lfOrientation    =  0;
  270.     cursfont.lfWeight         =  FW_NORMAL;
  271.     cursfont.lfItalic         =  FALSE;
  272.     cursfont.lfUnderline      =  FALSE;
  273.     cursfont.lfStrikeOut      =  FALSE;
  274.     cursfont.lfCharSet        =  ANSI_CHARSET;
  275.     cursfont.lfOutPrecision   =  OUT_DEFAULT_PRECIS;
  276.     cursfont.lfClipPrecision  =  CLIP_DEFAULT_PRECIS;
  277.     cursfont.lfQuality        =  DEFAULT_QUALITY;
  278.     cursfont.lfPitchAndFamily =  FIXED_PITCH | FF_DONTCARE;
  279.     strcpy((char *)cursfont.lfFaceName, "System");
  280.  
  281.     hnewsfont = CreateFontIndirect((LPLOGFONT) &cursfont);
  282.  
  283.     // Install the font in the current display context.
  284.     holdsfont = SelectObject(hDC, hnewsfont);
  285.  
  286.     // get text metrics for paint
  287.     GetTextMetrics(hDC, &tm);
  288.     xChar = tm.tmAveCharWidth;
  289.     yChar = tm.tmHeight + tm.tmExternalLeading;
  290.     yCharnl = tm.tmHeight;
  291.  
  292.     // Release the display context.
  293.     ReleaseDC(hWndMain, hDC);
  294.  
  295.     // Update display of main window.
  296.     ShowWindow(hWndMain, cmdShow);
  297.     UpdateWindow(hWndMain);
  298. }
  299.  
  300. //*******************************************************************
  301. // CloseHdump - done at termination of every instance of Hdump
  302. //
  303. //*******************************************************************
  304. void CloseHdump()
  305. {
  306.     DeleteObject(hnewsfont);
  307. }
  308.  
  309. //*******************************************************************
  310. // About - handle about dialog messages
  311. //
  312. // paramaters:
  313. //             hDlg          - The window handle for this message
  314. //             message       - The message number
  315. //             wParam        - The WPARAM parameter for this message
  316. //             lParam        - The LPARAM parameter for this message
  317. //
  318. //*******************************************************************
  319. #pragma argsused
  320. BOOL CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  321. {
  322.     if (message == WM_INITDIALOG)
  323.         return(TRUE);
  324.  
  325.     else if (message == WM_COMMAND)
  326.     {
  327.         switch (wParam)
  328.         {
  329.         case IDOK:
  330.                 EndDialog(hDlg, TRUE);
  331.                 return(TRUE);
  332.  
  333.             default:
  334.                 return(TRUE);
  335.         }
  336.     }
  337.  
  338.     return(FALSE);
  339. }
  340.  
  341. //*******************************************************************
  342.  
  343. //*******************************************************************
  344. // HdumpWndProc - handles messages for this application
  345. //
  346. // paramaters:
  347. //             hWnd          - The window handle for this message
  348. //             message       - The message number
  349. //             wParam        - The WPARAM parameter for this message 
  350. //             lParam        - The LPARAM parameter for this message 
  351. //
  352. // returns:
  353. //             depends on message.
  354. //
  355. //*******************************************************************
  356. LRESULT CALLBACK HdumpWndProc(HWND hWnd, UINT message,
  357.                  WPARAM wParam, LPARAM lParam)
  358. {
  359.     static char     sztFilePath[64];  // we remember last directory searched
  360.     static char     sztFileExt[5];
  361.     static char     sztFileSpec[64];
  362.     static char     sztFileName[64];
  363.  
  364.     DLGPROC  lpproc;                  // pointer to thunk for dialog box
  365.     char     buf[128];                // temp buffer
  366.     int      i;
  367.     FILE    *fh;
  368.  
  369.     switch (message)
  370.     {
  371.         case WM_CREATE:
  372.             // Start file search at current directory
  373.             getcwd(sztFilePath, sizeof(sztFilePath));
  374.             strcat(sztFilePath, "\\");
  375.             strcpy(sztFileExt, ".*");
  376.             strcpy(sztFileName, "");
  377.  
  378.             return(DefWindowProc(hWnd, message, wParam, lParam));
  379.  
  380.         case WM_COMMAND:
  381.         switch (wParam)
  382.             {
  383.                 case IDM_QUIT:
  384.                     // User selected Quit on menu
  385.                     PostMessage(hWnd, WM_CLOSE, 0, 0L);
  386.                     break;
  387.  
  388.                 case IDM_HOME:
  389.                     // Used to implement home to topleft from keyboard.
  390.                     SendMessage(hWnd, WM_HSCROLL, SB_TOP, 0L); /* not set up */
  391.                     SendMessage(hWnd, WM_VSCROLL, SB_TOP, 0L);
  392.                     break;
  393.  
  394.         case IDM_ABOUT:
  395.                     // Display about box.
  396.                     lpproc = (DLGPROC)MakeProcInstance((FARPROC)About, hInst);
  397.                     DialogBox(hInst,
  398.                               MAKEINTRESOURCE(ABOUT),
  399.                               hWnd,
  400.                               lpproc);
  401.                     FreeProcInstance((FARPROC)lpproc);
  402.                     break;
  403.  
  404.                 case IDM_OPEN:
  405.                     // Setup initial search path for open dialog box.
  406.                     strcpy(sztFileSpec, sztFilePath);
  407.             strcat(sztFileSpec, "*");
  408.                     strcat(sztFileSpec, sztFileExt);
  409.  
  410.                     // Go ask user to select file to display.
  411.                     if (DoFileOpenDlg(hInst,
  412.                                       hWnd,
  413.                                       sztFileSpec,
  414.                                       sztFileExt,
  415.                                       0x4010, // drives & subdirectories
  416.                                       sztFilePath,
  417.                                       sztFileName))
  418.                     {
  419.                         // If user said OK construct file path/file name
  420.             // to open.
  421.                         strcpy(szFName, sztFilePath);
  422.                         strcat(szFName, sztFileName);
  423.  
  424.                         // Show file name in window caption.
  425.                         sprintf(buf, "hdump - %s", szFName);
  426.                         SetWindowText(hWnd, buf);
  427.  
  428.                         // Determine file size and some display paramaters.
  429.                         fh = fopen(szFName, "r+b");
  430.                         if (fh)
  431.                         {
  432.                             fsize = filelength(fileno(fh));
  433.                 numlines = (int) ((fsize + 16L - 1L) / 16L);
  434.                             SnapLine(buf, szFName, 16, 16,NULL); // display width
  435.                             maxwidth = strlen(buf);
  436.                             nVscrollPos = 0;
  437.                             nHscrollPos = 0;
  438.  
  439.                             fclose(fh);
  440.                         }
  441.  
  442.                         else  // if file open failed.
  443.                         {
  444.                             SetWindowText(hWnd, "hdump");
  445.                             numlines = 0;
  446.                 maxwidth = 0;
  447.                         }
  448.  
  449.                         // Go setup scroll ranges for this file.
  450.                         SetupScroll(hWnd);
  451.  
  452.                         // Show first part of file.
  453.                         InvalidateRect(hWnd, NULL, TRUE);
  454.                         UpdateWindow(hWnd);
  455.                     }
  456.                     break;
  457.  
  458.                 default:
  459.             break;
  460.           }
  461.           break;
  462.  
  463.         case WM_SIZE:
  464.             // Save size of window client area.
  465.             if (lParam)
  466.               {
  467.                 yClient = HIWORD(lParam);
  468.                 xClient = LOWORD(lParam);
  469.  
  470.                 yClient = (yClient / yCharnl + 1) * yCharnl;
  471.  
  472.         lParam = MAKELONG(xClient, yClient);
  473.  
  474.                 // Go setup scroll ranges and file display area based upon
  475.                 // client area size.
  476.                 SetupScroll(hWnd);
  477.  
  478.                 return(DefWindowProc(hWnd, message, wParam, lParam));
  479.               }
  480.             break;
  481.  
  482.         case WM_VSCROLL:
  483.             // React to the various vertical scroll related actions.
  484.             switch (wParam)
  485.         {
  486.                 case SB_TOP:
  487.                     nVscrollInc = -nVscrollPos;
  488.                     break;
  489.  
  490.                 case SB_BOTTOM:
  491.                     nVscrollInc = nVscrollMax - nVscrollPos;
  492.                     break;
  493.  
  494.                 case SB_LINEUP:
  495.                     nVscrollInc = -1;
  496.                     break;
  497.  
  498.         case SB_LINEDOWN:
  499.                     nVscrollInc = 1;
  500.                     break;
  501.  
  502.                 case SB_PAGEUP:
  503.                     nVscrollInc = -max(1, yClient / yChar);
  504.                     break;
  505.  
  506.                 case SB_PAGEDOWN:
  507.                     nVscrollInc = max(1, yClient / yChar);
  508.                     break;
  509.  
  510.                 case SB_THUMBPOSITION:
  511.             nVscrollInc = LOWORD(lParam) - nVscrollPos;
  512.                     break;
  513.  
  514.                 case SB_THUMBTRACK:
  515.                     nVscrollInc = LOWORD(lParam) - nVscrollPos;
  516.                     break;
  517.  
  518.                 default:
  519.                     nVscrollInc = 0;
  520.             }
  521.  
  522.             nVscrollInc = max(-nVscrollPos,
  523.                               min(nVscrollInc, nVscrollMax - nVscrollPos));
  524.         if (nVscrollInc)
  525.             {
  526.                 nVscrollPos += nVscrollInc;
  527.                 ScrollWindow(hWnd, 0, -yChar * nVscrollInc, NULL, NULL);
  528.                 SetScrollPos(hWnd, SB_VERT, nVscrollPos, TRUE);
  529.                 UpdateWindow(hWnd);
  530.             }
  531.             break;
  532.  
  533.         case WM_HSCROLL:
  534.             // React to the various horizontal scroll related actions.
  535.             switch (wParam)
  536.             {
  537.         case SB_LINEUP:
  538.                     nHscrollInc = -1;
  539.                     break;
  540.  
  541.                 case SB_LINEDOWN:
  542.                     nHscrollInc = 1;
  543.                     break;
  544.  
  545.                 case SB_PAGEUP:
  546.                     nHscrollInc = -8;
  547.                     break;
  548.  
  549.                 case SB_PAGEDOWN:
  550.             nHscrollInc = 8;
  551.                     break;
  552.  
  553.                 case SB_THUMBPOSITION:
  554.                     nHscrollInc = LOWORD(lParam) - nHscrollPos;
  555.                     break;
  556.  
  557.                 case SB_THUMBTRACK:
  558.                     nHscrollInc = LOWORD(lParam) - nHscrollPos;
  559.                     break;
  560.  
  561.                 default:
  562.                     nHscrollInc = 0;
  563.         }
  564.  
  565.             nHscrollInc = max(-nHscrollPos,
  566.                               min(nHscrollInc, nHscrollMax - nHscrollPos));
  567.             if (nHscrollInc)
  568.             {
  569.                 nHscrollPos += nHscrollInc;
  570.                 ScrollWindow(hWnd, -xChar * nHscrollInc, 0, NULL, NULL);
  571.                 SetScrollPos(hWnd, SB_HORZ, nHscrollPos, TRUE);
  572.                 UpdateWindow(hWnd);
  573.             }
  574.             break;
  575.  
  576.     case WM_KEYDOWN:
  577.             // Translate various keydown messages to appropriate horizontal
  578.             // and vertical scroll actions.
  579.             for (i = 0; i < NUMKEYS; i++)
  580.             {
  581.                 if (wParam == key2scroll[i].wVirtkey)
  582.                 {
  583.                     SendMessage(hWnd, key2scroll[i].iMessage,
  584.                                 key2scroll[i].wRequest, 0L);
  585.                     break;
  586.                 }
  587.             }
  588.             break;
  589.  
  590.         case WM_PAINT:
  591.             // Go paint the client area of the window with the appropriate
  592.             // part of the selected file.
  593.             HdumpPaint(hWnd);
  594.             break;
  595.  
  596.         case WM_DESTROY:
  597.             // This is the end if we were closed by a DestroyWindow call.
  598.             CloseHdump();         // take any necessary wrapup action.
  599.             PostQuitMessage(0);   // this is the end...
  600.             break;
  601.  
  602.     case WM_QUERYENDSESSION:
  603.             // If we return TRUE we are saying it's ok with us to end the
  604.             // windows session.
  605.             CloseHdump();         // take any necessary wrapup action.
  606.             return((long) TRUE);  // we agree to end session.
  607.  
  608.         case WM_CLOSE:
  609.             // Tell windows to destroy our window.
  610.             DestroyWindow(hWnd);
  611.             break;
  612.  
  613.         default:
  614.             // Let windows handle all messages we choose to ignore.
  615.         return(DefWindowProc(hWnd, message, wParam, lParam));
  616.     }
  617.  
  618.     return(0L);
  619. }
  620.  
  621. //*******************************************************************
  622. // SetupScroll - setup scroll ranges
  623. //
  624. // Setup the vertical and horizontal scroll ranges and positions
  625. // of the applicatons main window based on:
  626. //
  627. //     numlines - The maximum number of lines to display in a hexdump
  628. //                of the selected file.
  629. //     maxwidth - The width of each line to display as formated for
  630. //                the hexdump.
  631. //
  632. // The resulting variables, nVscrollPos and nPageMaxLines, are used
  633. // by the function HdumpPaint to determine what part of the selected
  634. // file to display in the window.
  635. //
  636. // paramaters:
  637. //             hWnd          - The callers window handle
  638. //
  639. //*******************************************************************
  640. void SetupScroll(HWND hWnd)
  641. {
  642.     // numlines established during open
  643.     nVscrollMax = max(0, numlines - yClient / yChar);
  644.     nVscrollPos = min(nVscrollPos, nVscrollMax);
  645.  
  646.     nHscrollMax = max(0, maxwidth - xClient / xChar);
  647.     nHscrollPos = min(nHscrollPos, nHscrollMax);
  648.  
  649.     SetScrollRange (hWnd, SB_VERT, 0, nVscrollMax, FALSE);
  650.     SetScrollPos   (hWnd, SB_VERT, nVscrollPos, TRUE);
  651.  
  652.     SetScrollRange (hWnd, SB_HORZ, 0, nHscrollMax, FALSE);
  653.     SetScrollPos   (hWnd, SB_HORZ, nHscrollPos, TRUE);
  654.  
  655.     nPageMaxLines = min(numlines, yClient / yChar);
  656. }
  657.  
  658. //*******************************************************************
  659. // HdumpPaint - paint the main window
  660. //
  661. // If a file has been selected, as indicated by numlines being
  662. // greater than 0, this module will read and display a portion
  663. // of the file as determined by the current size and scroll
  664. // position of the window.
  665. //
  666. // paramaters:
  667. //             hWnd          - The callers window handle
  668. //
  669. //*******************************************************************
  670. void HdumpPaint(HWND hWnd)
  671. {
  672.     PAINTSTRUCT  ps;
  673.     HDC          hDC;
  674.     LONG         offset;
  675.     int          tlen;
  676.     int          lpos;
  677.     char         buf[128];
  678.     int          i;
  679.     int          len;
  680.     int          hfile;
  681.     HANDLE       hbuff;
  682.     LPSTR        lpbuff;
  683.     LPSTR        lcp;
  684.  
  685.     BeginPaint(hWnd, (LPPAINTSTRUCT) &ps);
  686.     hDC = ps.hdc;
  687.  
  688.     // Establish fixed font in display context.
  689.     SelectObject(hDC, hnewsfont);
  690.  
  691.     if (numlines)
  692.     {
  693.     // Open the file to display
  694.         // (files should not stay open over multiple windows messages)
  695.         if ((hfile = _lopen(szFName, OF_READ)) != -1)
  696.         {
  697.             // Get a buffer to hold data to fill screen.
  698.             len = nPageMaxLines * 16;
  699.             hbuff = GlobalAlloc(GMEM_MOVEABLE, (DWORD) len);
  700.             if (hbuff)
  701.             {
  702.                 // Lock the buffer to get a pointer to it.
  703.                 lpbuff = GlobalLock(hbuff);
  704.                 if (lpbuff)
  705.                 {
  706.             // Get offset into file to start display.
  707.                     offset = (LONG) nVscrollPos * 16L;
  708.  
  709.                     if (_llseek(hfile, offset, 0) != -1L)
  710.                     {
  711.                         tlen = _lread(hfile, lpbuff, len);
  712.                         if (tlen != -1)
  713.                         {
  714.                             lpos = 0;
  715.                             lcp = lpbuff;
  716.  
  717.                             for (i = nVscrollPos; i < (nVscrollPos + nPageMaxLines); i++)
  718.                             {
  719.                 if (tlen > 16)
  720.                                     len = 16;
  721.                                 else
  722.                                     len = tlen;
  723.                                 tlen -= len;
  724.  
  725.                                 SnapLine(buf, lcp, len, 16, (char *) (i * 16));
  726.  
  727.                                 TextOut(hDC,
  728.                                         xChar * (-nHscrollPos + 0),
  729.                                         yChar * lpos++,
  730.                                         buf,
  731.                                         strlen(buf));
  732.  
  733.                                 lcp += 16;
  734.                             }
  735.                         }
  736.                     }
  737.  
  738.                     GlobalUnlock(hbuff);
  739.                 }
  740.  
  741.                 GlobalFree(hbuff);
  742.             }
  743.  
  744.             _lclose(hfile);
  745.     }
  746.     }
  747.  
  748.     EndPaint(hWnd, (LPPAINTSTRUCT) &ps);
  749. }
  750.  
  751. //*******************************************************************
  752. // SnapLine - dump a line of memory
  753. //
  754. // paramaters:
  755. //             szBuf         - buffer to hold result
  756. //             mem           - pointer to memory to format
  757. //             len           - length to format
  758. //             dwid          - max display width (8 or 16 recomended)
  759. //             olbl          - label to put at start of line
  760. //
  761. // returns:
  762. //             A pointer to szBbuf.
  763. //
  764. //*******************************************************************
  765. char *SnapLine(char *szBuf, LPSTR mem, int len, int dwid, char *olbl)
  766. {
  767.     int            i;
  768.     int            j;
  769.     unsigned char  c;
  770.     unsigned char  buff[80];
  771.     unsigned char  tbuf[80];
  772.  
  773.     if (len > dwid)
  774.         len = dwid;
  775.  
  776.     *szBuf = 0;
  777.     i = 0;
  778.     j = 0;
  779.  
  780.     // Show offset for this line.
  781.     sprintf((char *)tbuf, "%04X  ", olbl);
  782.     strcpy(szBuf, (char *)tbuf);
  783.  
  784.     // Format hex portion of line and save chars for ascii portion
  785.     for (i = 0; i < len; i++)
  786.     {
  787.         c = *mem++;
  788.  
  789.         sprintf((char *)tbuf, "%02X ", c);
  790.         strcat(szBuf, (char *)tbuf);
  791.  
  792.         if (c >= 32 && c < 127)
  793.             buff[i] = c;
  794.         else
  795.             buff[i] = 46;
  796.     }
  797.  
  798.     j = dwid - i;
  799.  
  800.     buff[i] = 0;
  801.  
  802.     // Fill out hex portion of short lines.
  803.     for (i = j; i > 0; i--)
  804.         strcat(szBuf, "   ");
  805.  
  806.     // Add ascii portion to line.
  807.     sprintf((char *)tbuf, " |%s|", (char *)buff);
  808.     strcat(szBuf, (char *)tbuf);
  809.  
  810.     // Fill out end of short lines.
  811.     for (i = j; i > 0; i--)
  812.         strcat(szBuf, " ");
  813.  
  814.     return(szBuf);
  815. }
  816.  
  817. //*******************************************************************
  818. // DoFileOpenDlg - invoke FileOpenDlgProc to get name of file to open
  819. //
  820. // Setup call to FileOpenDlgProc to let user select path/filename
  821. //
  822. // paramaters:
  823. //             hInst         - instance of caller
  824. //             hWnd          - window of caller
  825. //             szFileSpecIn  - file path to search initially
  826. //             szDefExtIn    - file extension search initially
  827. //             wFileAttrIn   - attribute for 'DlgDirList' to use in search
  828. //             szFilePathOut - path of selected file
  829. //             szFileNameOut - name of selected file
  830. //
  831. // returns:
  832. //             1 - if a file selected
  833. //             0 - if file not selected
  834. //
  835. //*******************************************************************
  836. DoFileOpenDlg(HINSTANCE hInst, HWND hWnd,
  837.               char *szFileSpecIn, char *szDefExtIn,
  838.               WORD wFileAttrIn,
  839.               char *szFilePathOut, char *szFileNameOut)
  840. {
  841.     DLGPROC   lpfnFileOpenDlgProc;
  842.     int       iReturn;
  843.  
  844.     // Save starting file spec.
  845.     strcpy(szFileSpec, szFileSpecIn);
  846.     strcpy(szDefExt, szDefExtIn);
  847.     wFileAttr = wFileAttrIn;
  848.  
  849.     lpfnFileOpenDlgProc = (DLGPROC)MakeProcInstance((FARPROC)FileOpenDlgProc, hInst);
  850.  
  851.     iReturn = DialogBox(hInst,
  852.                         MAKEINTRESOURCE(OPENFILE),
  853.                         hWnd,
  854.                         lpfnFileOpenDlgProc);
  855.  
  856.     FreeProcInstance((FARPROC)lpfnFileOpenDlgProc);
  857.  
  858.     // Return selected file spec.
  859.     strcpy(szFilePathOut, szFilePath);
  860.     strcpy(szFileNameOut, szFileName);
  861.  
  862.     return(iReturn);
  863. }
  864.  
  865. //*******************************************************************
  866. // FileOpenDlgProc - get the name of a file to open
  867. //
  868. // paramaters:
  869. //             hDlg          - The window handle of this dialog box
  870. //             message       - The message number
  871. //             wParam        - The WPARAM parameter for this message
  872. //             lParam        - The LPARAM parameter for this message
  873. //
  874. // returns:
  875. //             depends on message.
  876. //
  877. //*******************************************************************
  878. BOOL CALLBACK FileOpenDlgProc(HWND hDlg, UINT iMessage,
  879.                 WPARAM wParam, LPARAM lParam)
  880. {
  881.     static char   curpath[64];
  882.  
  883.     char          cLastChar;
  884.     int           nLen;
  885.     struct ffblk  fileinfo;
  886.  
  887.     switch (iMessage)
  888.     {
  889.         case WM_INITDIALOG:
  890.             // Save current working directory (will be restored on exit)
  891.             getcwd(curpath, sizeof(curpath));
  892.  
  893.             SendDlgItemMessage(hDlg, IDD_FNAME, EM_LIMITTEXT, 80, 0L);
  894.  
  895.             // Fill list box with files from starting file spec.
  896.             DlgDirList(hDlg,
  897.                        szFileSpec,
  898.                        IDD_FLIST,
  899.                        IDD_FPATH,
  900.                        wFileAttr);
  901.  
  902.             // Save starting file spec.
  903.             SetDlgItemText(hDlg, IDD_FNAME, szFileSpec);
  904.             return(TRUE);
  905.  
  906.         case WM_COMMAND:
  907.             switch (wParam)
  908.             {
  909.                 case IDD_FLIST:
  910.                     switch (HIWORD(lParam))
  911.                     {
  912.                         case LBN_SELCHANGE:
  913.                             if (DlgDirSelect(hDlg, szFileName, IDD_FLIST))
  914.                 strcat(szFileName, szFileSpec);
  915.                             SetDlgItemText(hDlg, IDD_FNAME, szFileName);
  916.                             break;
  917.  
  918.                         case LBN_DBLCLK:
  919.                             if (DlgDirSelect(hDlg, szFileName, IDD_FLIST))
  920.                             {
  921.                                 strcat(szFileName, szFileSpec);
  922.                                 DlgDirList(hDlg, szFileName, IDD_FLIST, IDD_FPATH, wFileAttr);
  923.                                 SetDlgItemText(hDlg, IDD_FNAME, szFileSpec);
  924.                             }
  925.                             else
  926.                             {
  927.                 SetDlgItemText(hDlg, IDD_FNAME, szFileName);
  928.                                 SendMessage(hDlg, WM_COMMAND, IDOK, 0L);
  929.                             }
  930.                             break;
  931.                     }
  932.                     break;
  933.  
  934.                 case IDD_FNAME:
  935.                     if (HIWORD(lParam) == EN_CHANGE)
  936.                     {
  937.                         EnableWindow(GetDlgItem(hDlg, IDOK),
  938.                                      (BOOL) SendMessage((HWND)LOWORD(lParam),
  939.                                                         WM_GETTEXTLENGTH, 0, 0L));
  940.             }
  941.                     break;
  942.  
  943.                 case IDOK:
  944.                     GetDlgItemText(hDlg, IDD_FNAME, szFileName, 80);
  945.  
  946.                     nLen  = strlen(szFileName);
  947.                     cLastChar = *AnsiPrev(szFileName, szFileName + nLen);
  948.  
  949.                     if (cLastChar == '\\' || cLastChar == ':')
  950.                         strcat(szFileName, szFileSpec);
  951.  
  952.                     if (strchr(szFileName, '*') || strchr(szFileName, '?'))
  953.             {
  954.                         if (DlgDirList(hDlg, szFileName, IDD_FLIST, IDD_FPATH, wFileAttr))
  955.                         {
  956.                             strcpy(szFileSpec, szFileName);
  957.                             SetDlgItemText(hDlg, IDD_FNAME, szFileSpec);
  958.                         }
  959.                         else
  960.                             MessageBeep(0);
  961.                         break;
  962.                     }
  963.  
  964.                     // Fill list with new spec.
  965.                     if (DlgDirList(hDlg, szFileName, IDD_FLIST, IDD_FPATH, wFileAttr))
  966.             {
  967.                         // Save new spec.
  968.                         strcpy(szFileSpec, szFileName);
  969.                         SetDlgItemText(hDlg, IDD_FNAME, szFileSpec);
  970.                         break;
  971.                     }
  972.  
  973.                     // Since we fell through, szFileName was not a search path.
  974.                     szFileName[nLen] = '\0';
  975.  
  976.                     // Make sure file exists.
  977.                     if (findfirst(szFileName, &fileinfo, 0))
  978.                     {
  979.             strcat(szFileName, szDefExt);
  980.  
  981.                         // Make sure file exists.
  982.                         if (findfirst(szFileName, &fileinfo, 0))
  983.                         {
  984.                             // No, beep and break.
  985.                             MessageBeep(0);
  986.                             break;
  987.                         }
  988.                     }
  989.  
  990.                     // Get current path to return.
  991.                     GetDlgItemText(hDlg, IDD_FPATH, szFilePath, 80);
  992.             strupr(szFilePath);
  993.                     if (szFilePath[strlen(szFilePath) - 1] != '\\')
  994.                         strcat(szFilePath, "\\");
  995.  
  996.                     // Return selected file name.
  997.                     strcpy(szFileName, fileinfo.ff_name);
  998.  
  999.                     // Restore current working directory.
  1000.                     chdir(curpath);
  1001.  
  1002.                     // Terminate this dialog box.
  1003.                     EndDialog(hDlg, TRUE);
  1004.                     break;
  1005.  
  1006.                 case IDCANCEL:
  1007.                     // Restore current working directory
  1008.                     chdir(curpath);
  1009.  
  1010.                     // Terminate this dialog box.
  1011.                     EndDialog(hDlg, FALSE);
  1012.                     break;
  1013.  
  1014.         default:
  1015.                     return(FALSE);
  1016.             }
  1017.             break;
  1018.  
  1019.         default:
  1020.             return(FALSE);
  1021.     }
  1022.  
  1023.     return(TRUE);
  1024. }
  1025.  
  1026. //*******************************************************************
  1027.