home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c081_7 / 2.ddi / WEXAMPLE.ZIP / HDUMP.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-02-13  |  34.8 KB  |  1,026 lines

  1. // Borland C++ - (C) Copyright 1991 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. #include <windows.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <time.h>
  15. #include <bios.h>
  16. #include <ctype.h>
  17. #include <io.h>
  18. #include <dos.h>
  19. #include <dir.h>
  20.  
  21. #include "hdump.h"
  22.  
  23. typedef struct scrollkeys
  24.   {
  25.     WORD wVirtkey;
  26.     int  iMessage;
  27.     WORD wRequest;
  28.   } SCROLLKEYS;
  29.  
  30. SCROLLKEYS key2scroll [] =
  31.   {
  32.     VK_HOME,  WM_COMMAND, IDM_HOME,
  33.     VK_END,   WM_VSCROLL, SB_BOTTOM,
  34.     VK_PRIOR, WM_VSCROLL, SB_PAGEUP,
  35.     VK_NEXT,  WM_VSCROLL, SB_PAGEDOWN,
  36.     VK_UP,    WM_VSCROLL, SB_LINEUP,
  37.     VK_DOWN,  WM_VSCROLL, SB_LINEDOWN,
  38.     VK_LEFT,  WM_HSCROLL, SB_PAGEUP,
  39.     VK_RIGHT, WM_HSCROLL, SB_PAGEDOWN
  40.   };
  41.  
  42. # define NUMKEYS (sizeof key2scroll / sizeof key2scroll[0])
  43.  
  44. // data initialized by first instance
  45. typedef struct tagSETUPDATA
  46.   {
  47.     char   szAppName[10]; // name of application
  48.   } SETUPDATA;
  49.  
  50. SETUPDATA SetUpData;
  51.  
  52. // Data that can be referenced throughout the
  53. // program but not passed to other instances
  54.  
  55. HANDLE    hInst;                              // hInstance of application
  56. HWND      hWndMain;                           // hWnd of main window
  57. //char      szAppName[10];                      // name of application
  58.  
  59. int       xChar, yChar, yCharnl;              // character size
  60. int       xClient, yClient;                   // client window size
  61.  
  62. LOGFONT   cursfont;                           // font structure
  63. HANDLE    holdsfont;                          // handle of original font
  64. HANDLE    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(HANDLE hInstance, HANDLE hPrevInstance,
  90.                                LPSTR lpszCmdLine, int cmdShow);
  91.  
  92. void                   InitHdump(HANDLE hInstance, HANDLE hPrevInstance,
  93.                                  LPSTR lpszCmdLine, int cmdShow);
  94. void                   InitHdumpFirst(HANDLE hInstance);
  95. void                   InitHdumpAdded(HANDLE hPrevInstance);
  96. void                   InitHdumpEvery(HANDLE hInstance, int cmdShow);
  97. void                   CloseHdump(void);
  98. BOOL FAR PASCAL        About(HWND hDlg, WORD message, WORD wParam, LONG lParam);
  99.  
  100. long FAR PASCAL        HdumpWndProc(HWND hWnd, WORD message,
  101.                     WORD wParam, LONG 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(HANDLE hInst, HWND hWnd,
  109.                                      char *szFileSpecIn, char *szDefExtIn,
  110.                                      WORD wFileAttrIn,
  111.                                      char *szFilePathOut, char *szFileNameOut);
  112. extern BOOL FAR PASCAL FileOpenDlgProc(HWND hDlg, unsigned iMessage,
  113.                                        WORD wParam, LONG 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(HANDLE hInstance, HANDLE 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(HANDLE hInstance, HANDLE 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(HANDLE 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(HANDLE hPrevInstance)
  222. {
  223.     // get the results of the initialization of first instance
  224.     GetInstanceData(hPrevInstance, (PSTR) &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(HANDLE 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(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. }
  307.  
  308. //*******************************************************************
  309. // About - handle about dialog messages
  310. //
  311. // paramaters:
  312. //             hDlg          - The window handle for this message
  313. //             message       - The message number
  314. //             wParam        - The WORD parmater for this message
  315. //             lParam        - The LONG parmater for this message
  316. //
  317. //*******************************************************************
  318. #pragma argsused
  319. BOOL FAR PASCAL About(HWND hDlg, WORD message, WORD wParam, LONG lParam)
  320. {
  321.     if (message == WM_INITDIALOG)
  322.         return(TRUE);
  323.  
  324.     else if (message == WM_COMMAND)
  325.     {
  326.         switch (wParam)
  327.         {
  328.         case IDOK:
  329.                 EndDialog(hDlg, TRUE);
  330.                 return(TRUE);
  331.  
  332.             default:
  333.                 return(TRUE);
  334.         }
  335.     }
  336.  
  337.     return(FALSE);
  338. }
  339.  
  340. //*******************************************************************
  341.  
  342. //*******************************************************************
  343. // HdumpWndProc - handles messages for this application
  344. //
  345. // paramaters:
  346. //             hWnd          - The window handle for this message
  347. //             message       - The message number
  348. //             wParam        - The WORD parmater for this message
  349. //             lParam        - The LONG parmater for this message
  350. //
  351. // returns:
  352. //             depends on message.
  353. //
  354. //*******************************************************************
  355. long FAR PASCAL HdumpWndProc(HWND hWnd, WORD message,
  356.                              WORD wParam, LONG lParam)
  357. {
  358.     static char     sztFilePath[64];  // we remember last directory searched
  359.     static char     sztFileExt[5];
  360.     static char     sztFileSpec[64];
  361.     static char     sztFileName[64];
  362.  
  363.     FARPROC  lpproc;                  // pointer to thunk for dialog box
  364.     char     buf[128];                // temp buffer
  365.     int      i;
  366.     FILE    *fh;
  367.  
  368.     switch (message)
  369.     {
  370.         case WM_CREATE:
  371.             // Start file search at current directory
  372.             getcwd(sztFilePath, sizeof(sztFilePath));
  373.             strcat(sztFilePath, "\\");
  374.             strcpy(sztFileExt, ".*");
  375.             strcpy(sztFileName, "");
  376.  
  377.             return(DefWindowProc(hWnd, message, wParam, lParam));
  378.  
  379.         case WM_COMMAND:
  380.         switch (wParam)
  381.             {
  382.                 case IDM_QUIT:
  383.                     // User selected Quit on menu
  384.                     PostMessage(hWnd, WM_CLOSE, 0, 0L);
  385.                     break;
  386.  
  387.                 case IDM_HOME:
  388.                     // Used to implement home to topleft from keyboard.
  389.                     SendMessage(hWnd, WM_HSCROLL, SB_TOP, 0L); /* not set up */
  390.                     SendMessage(hWnd, WM_VSCROLL, SB_TOP, 0L);
  391.                     break;
  392.  
  393.         case IDM_ABOUT:
  394.                     // Display about box.
  395.                     lpproc = MakeProcInstance(About, hInst);
  396.                     DialogBox(hInst,
  397.                               MAKEINTRESOURCE(ABOUT),
  398.                               hWnd,
  399.                               lpproc);
  400.                     FreeProcInstance(lpproc);
  401.                     break;
  402.  
  403.                 case IDM_OPEN:
  404.                     // Setup initial search path for open dialog box.
  405.                     strcpy(sztFileSpec, sztFilePath);
  406.             strcat(sztFileSpec, "*");
  407.                     strcat(sztFileSpec, sztFileExt);
  408.  
  409.                     // Go ask user to select file to display.
  410.                     if (DoFileOpenDlg(hInst,
  411.                                       hWnd,
  412.                                       sztFileSpec,
  413.                                       sztFileExt,
  414.                                       0x4010, // drives & subdirectories
  415.                                       sztFilePath,
  416.                                       sztFileName))
  417.                     {
  418.                         // If user said OK construct file path/file name
  419.             // to open.
  420.                         strcpy(szFName, sztFilePath);
  421.                         strcat(szFName, sztFileName);
  422.  
  423.                         // Show file name in window caption.
  424.                         sprintf(buf, "hdump - %s", szFName);
  425.                         SetWindowText(hWnd, buf);
  426.  
  427.                         // Determine file size and some display paramaters.
  428.                         fh = fopen(szFName, "r+b");
  429.                         if (fh)
  430.                         {
  431.                             fsize = filelength(fileno(fh));
  432.                 numlines = (int) ((fsize + 16L - 1L) / 16L);
  433.                             SnapLine(buf, szFName, 16, 16,NULL); // display width
  434.                             maxwidth = strlen(buf);
  435.                             nVscrollPos = 0;
  436.                             nHscrollPos = 0;
  437.  
  438.                             fclose(fh);
  439.                         }
  440.  
  441.                         else  // if file open failed.
  442.                         {
  443.                             SetWindowText(hWnd, "hdump");
  444.                             numlines = 0;
  445.                 maxwidth = 0;
  446.                         }
  447.  
  448.                         // Go setup scroll ranges for this file.
  449.                         SetupScroll(hWnd);
  450.  
  451.                         // Show first part of file.
  452.                         InvalidateRect(hWnd, NULL, TRUE);
  453.                         UpdateWindow(hWnd);
  454.                     }
  455.                     break;
  456.  
  457.                 default:
  458.             break;
  459.           }
  460.           break;
  461.  
  462.         case WM_SIZE:
  463.             // Save size of window client area.
  464.             if (lParam)
  465.               {
  466.                 yClient = HIWORD(lParam);
  467.                 xClient = LOWORD(lParam);
  468.  
  469.                 yClient = (yClient / yCharnl + 1) * yCharnl;
  470.  
  471.         lParam = MAKELONG(xClient, yClient);
  472.  
  473.                 // Go setup scroll ranges and file display area based upon
  474.                 // client area size.
  475.                 SetupScroll(hWnd);
  476.  
  477.                 return(DefWindowProc(hWnd, message, wParam, lParam));
  478.               }
  479.             break;
  480.  
  481.         case WM_VSCROLL:
  482.             // React to the various vertical scroll related actions.
  483.             switch (wParam)
  484.         {
  485.                 case SB_TOP:
  486.                     nVscrollInc = -nVscrollPos;
  487.                     break;
  488.  
  489.                 case SB_BOTTOM:
  490.                     nVscrollInc = nVscrollMax - nVscrollPos;
  491.                     break;
  492.  
  493.                 case SB_LINEUP:
  494.                     nVscrollInc = -1;
  495.                     break;
  496.  
  497.         case SB_LINEDOWN:
  498.                     nVscrollInc = 1;
  499.                     break;
  500.  
  501.                 case SB_PAGEUP:
  502.                     nVscrollInc = -max(1, yClient / yChar);
  503.                     break;
  504.  
  505.                 case SB_PAGEDOWN:
  506.                     nVscrollInc = max(1, yClient / yChar);
  507.                     break;
  508.  
  509.                 case SB_THUMBPOSITION:
  510.             nVscrollInc = LOWORD(lParam) - nVscrollPos;
  511.                     break;
  512.  
  513.                 case SB_THUMBTRACK:
  514.                     nVscrollInc = LOWORD(lParam) - nVscrollPos;
  515.                     break;
  516.  
  517.                 default:
  518.                     nVscrollInc = 0;
  519.             }
  520.  
  521.             nVscrollInc = max(-nVscrollPos,
  522.                               min(nVscrollInc, nVscrollMax - nVscrollPos));
  523.         if (nVscrollInc)
  524.             {
  525.                 nVscrollPos += nVscrollInc;
  526.                 ScrollWindow(hWnd, 0, -yChar * nVscrollInc, NULL, NULL);
  527.                 SetScrollPos(hWnd, SB_VERT, nVscrollPos, TRUE);
  528.                 UpdateWindow(hWnd);
  529.             }
  530.             break;
  531.  
  532.         case WM_HSCROLL:
  533.             // React to the various horizontal scroll related actions.
  534.             switch (wParam)
  535.             {
  536.         case SB_LINEUP:
  537.                     nHscrollInc = -1;
  538.                     break;
  539.  
  540.                 case SB_LINEDOWN:
  541.                     nHscrollInc = 1;
  542.                     break;
  543.  
  544.                 case SB_PAGEUP:
  545.                     nHscrollInc = -8;
  546.                     break;
  547.  
  548.                 case SB_PAGEDOWN:
  549.             nHscrollInc = 8;
  550.                     break;
  551.  
  552.                 case SB_THUMBPOSITION:
  553.                     nHscrollInc = LOWORD(lParam) - nHscrollPos;
  554.                     break;
  555.  
  556.                 case SB_THUMBTRACK:
  557.                     nHscrollInc = LOWORD(lParam) - nHscrollPos;
  558.                     break;
  559.  
  560.                 default:
  561.                     nHscrollInc = 0;
  562.         }
  563.  
  564.             nHscrollInc = max(-nHscrollPos,
  565.                               min(nHscrollInc, nHscrollMax - nHscrollPos));
  566.             if (nHscrollInc)
  567.             {
  568.                 nHscrollPos += nHscrollInc;
  569.                 ScrollWindow(hWnd, -xChar * nHscrollInc, 0, NULL, NULL);
  570.                 SetScrollPos(hWnd, SB_HORZ, nHscrollPos, TRUE);
  571.                 UpdateWindow(hWnd);
  572.             }
  573.             break;
  574.  
  575.     case WM_KEYDOWN:
  576.             // Translate various keydown messages to appropriate horizontal
  577.             // and vertical scroll actions.
  578.             for (i = 0; i < NUMKEYS; i++)
  579.             {
  580.                 if (wParam == key2scroll[i].wVirtkey)
  581.                 {
  582.                     SendMessage(hWnd, key2scroll[i].iMessage,
  583.                                 key2scroll[i].wRequest, 0L);
  584.                     break;
  585.                 }
  586.             }
  587.             break;
  588.  
  589.         case WM_PAINT:
  590.             // Go paint the client area of the window with the appropriate
  591.             // part of the selected file.
  592.             HdumpPaint(hWnd);
  593.             break;
  594.  
  595.         case WM_DESTROY:
  596.             // This is the end if we were closed by a DestroyWindow call.
  597.             CloseHdump();         // take any necessary wrapup action.
  598.             PostQuitMessage(0);   // this is the end...
  599.             break;
  600.  
  601.     case WM_QUERYENDSESSION:
  602.             // If we return TRUE we are saying it's ok with us to end the
  603.             // windows session.
  604.             CloseHdump();         // take any necessary wrapup action.
  605.             return((long) TRUE);  // we agree to end session.
  606.  
  607.         case WM_CLOSE:
  608.             // Tell windows to destroy our window.
  609.             DestroyWindow(hWnd);
  610.             break;
  611.  
  612.         default:
  613.             // Let windows handle all messages we choose to ignore.
  614.         return(DefWindowProc(hWnd, message, wParam, lParam));
  615.     }
  616.  
  617.     return(0L);
  618. }
  619.  
  620. //*******************************************************************
  621. // SetupScroll - setup scroll ranges
  622. //
  623. // Setup the vertical and horizontal scroll ranges and positions
  624. // of the applicatons main window based on:
  625. //
  626. //     numlines - The maximum number of lines to display in a hexdump
  627. //                of the selected file.
  628. //     maxwidth - The width of each line to display as formated for
  629. //                the hexdump.
  630. //
  631. // The resulting variables, nVscrollPos and nPageMaxLines, are used
  632. // by the function HdumpPaint to determine what part of the selected
  633. // file to display in the window.
  634. //
  635. // paramaters:
  636. //             hWnd          - The callers window handle
  637. //
  638. //*******************************************************************
  639. void SetupScroll(HWND hWnd)
  640. {
  641.     // numlines established during open
  642.     nVscrollMax = max(0, numlines - yClient / yChar);
  643.     nVscrollPos = min(nVscrollPos, nVscrollMax);
  644.  
  645.     nHscrollMax = max(0, maxwidth - xClient / xChar);
  646.     nHscrollPos = min(nHscrollPos, nHscrollMax);
  647.  
  648.     SetScrollRange (hWnd, SB_VERT, 0, nVscrollMax, FALSE);
  649.     SetScrollPos   (hWnd, SB_VERT, nVscrollPos, TRUE);
  650.  
  651.     SetScrollRange (hWnd, SB_HORZ, 0, nHscrollMax, FALSE);
  652.     SetScrollPos   (hWnd, SB_HORZ, nHscrollPos, TRUE);
  653.  
  654.     nPageMaxLines = min(numlines, yClient / yChar);
  655. }
  656.  
  657. //*******************************************************************
  658. // HdumpPaint - paint the main window
  659. //
  660. // If a file has been selected, as indicated by numlines being
  661. // greater than 0, this module will read and display a portion
  662. // of the file as determined by the current size and scroll
  663. // position of the window.
  664. //
  665. // paramaters:
  666. //             hWnd          - The callers window handle
  667. //
  668. //*******************************************************************
  669. void HdumpPaint(HWND hWnd)
  670. {
  671.     PAINTSTRUCT  ps;
  672.     HDC          hDC;
  673.     LONG         offset;
  674.     int          tlen;
  675.     int          lpos;
  676.     char         buf[128];
  677.     int          i;
  678.     int          len;
  679.     int          hfile;
  680.     HANDLE       hbuff;
  681.     LPSTR        lpbuff;
  682.     LPSTR        lcp;
  683.  
  684.     BeginPaint(hWnd, (LPPAINTSTRUCT) &ps);
  685.     hDC = ps.hdc;
  686.  
  687.     // Establish fixed font in display context.
  688.     SelectObject(hDC, hnewsfont);
  689.  
  690.     if (numlines)
  691.     {
  692.     // Open the file to display
  693.         // (files should not stay open over multiple windows messages)
  694.         if ((hfile = _lopen(szFName, OF_READ)) != -1)
  695.         {
  696.             // Get a buffer to hold data to fill screen.
  697.             len = nPageMaxLines * 16;
  698.             hbuff = GlobalAlloc(GMEM_MOVEABLE, (DWORD) len);
  699.             if (hbuff)
  700.             {
  701.                 // Lock the buffer to get a pointer to it.
  702.                 lpbuff = GlobalLock(hbuff);
  703.                 if (lpbuff)
  704.                 {
  705.             // Get offset into file to start display.
  706.                     offset = (LONG) nVscrollPos * 16L;
  707.  
  708.                     if (_llseek(hfile, offset, 0) != -1L)
  709.                     {
  710.                         tlen = _lread(hfile, lpbuff, len);
  711.                         if (tlen != -1)
  712.                         {
  713.                             lpos = 0;
  714.                             lcp = lpbuff;
  715.  
  716.                             for (i = nVscrollPos; i < (nVscrollPos + nPageMaxLines); i++)
  717.                             {
  718.                 if (tlen > 16)
  719.                                     len = 16;
  720.                                 else
  721.                                     len = tlen;
  722.                                 tlen -= len;
  723.  
  724.                                 SnapLine(buf, lcp, len, 16, (char *) (i * 16));
  725.  
  726.                                 TextOut(hDC,
  727.                                         xChar * (-nHscrollPos + 0),
  728.                                         yChar * lpos++,
  729.                                         buf,
  730.                                         strlen(buf));
  731.  
  732.                                 lcp += 16;
  733.                             }
  734.                         }
  735.                     }
  736.  
  737.                     GlobalUnlock(hbuff);
  738.                 }
  739.  
  740.                 GlobalFree(hbuff);
  741.             }
  742.  
  743.             _lclose(hfile);
  744.     }
  745.     }
  746.  
  747.     EndPaint(hWnd, (LPPAINTSTRUCT) &ps);
  748. }
  749.  
  750. //*******************************************************************
  751. // SnapLine - dump a line of memory
  752. //
  753. // paramaters:
  754. //             szBuf         - buffer to hold result
  755. //             mem           - pointer to memory to format
  756. //             len           - length to format
  757. //             dwid          - max display width (8 or 16 recomended)
  758. //             olbl          - label to put at start of line
  759. //
  760. // returns:
  761. //             A pointer to szBbuf.
  762. //
  763. //*******************************************************************
  764. char *SnapLine(char *szBuf, LPSTR mem, int len, int dwid, char *olbl)
  765. {
  766.     int            i;
  767.     int            j;
  768.     unsigned char  c;
  769.     unsigned char  buff[80];
  770.     unsigned char  tbuf[80];
  771.  
  772.     if (len > dwid)
  773.         len = dwid;
  774.  
  775.     *szBuf = 0;
  776.     i = 0;
  777.     j = 0;
  778.  
  779.     // Show offset for this line.
  780.     sprintf(tbuf, "%04X  ", olbl);
  781.     strcpy(szBuf, tbuf);
  782.  
  783.     // Format hex portion of line and save chars for ascii portion
  784.     for (i = 0; i < len; i++)
  785.     {
  786.         c = *mem++;
  787.  
  788.         sprintf(tbuf, "%02X ", c);
  789.         strcat(szBuf, tbuf);
  790.  
  791.         if (c >= 32 && c < 127)
  792.             buff[i] = c;
  793.         else
  794.             buff[i] = 46;
  795.     }
  796.  
  797.     j = dwid - i;
  798.  
  799.     buff[i] = 0;
  800.  
  801.     // Fill out hex portion of short lines.
  802.     for (i = j; i > 0; i--)
  803.         strcat(szBuf, "   ");
  804.  
  805.     // Add ascii portion to line.
  806.     sprintf(tbuf, " |%s|", buff);
  807.     strcat(szBuf, tbuf);
  808.  
  809.     // Fill out end of short lines.
  810.     for (i = j; i > 0; i--)
  811.         strcat(szBuf, " ");
  812.  
  813.     return(szBuf);
  814. }
  815.  
  816. //*******************************************************************
  817. // DoFileOpenDlg - invoke FileOpenDlgProc to get name of file to open
  818. //
  819. // Setup call to FileOpenDlgProc to let user select path/filename
  820. //
  821. // paramaters:
  822. //             hInst         - instance of caller
  823. //             hWnd          - window of caller
  824. //             szFileSpecIn  - file path to search initially
  825. //             szDefExtIn    - file extension search initially
  826. //             wFileAttrIn   - attribute for 'DlgDirList' to use in search
  827. //             szFilePathOut - path of selected file
  828. //             szFileNameOut - name of selected file
  829. //
  830. // returns:
  831. //             1 - if a file selected
  832. //             0 - if file not selected
  833. //
  834. //*******************************************************************
  835. DoFileOpenDlg(HANDLE hInst, HWND hWnd,
  836.               char *szFileSpecIn, char *szDefExtIn,
  837.               WORD wFileAttrIn,
  838.               char *szFilePathOut, char *szFileNameOut)
  839. {
  840.     FARPROC   lpfnFileOpenDlgProc;
  841.     int       iReturn;
  842.  
  843.     // Save starting file spec.
  844.     strcpy(szFileSpec, szFileSpecIn);
  845.     strcpy(szDefExt, szDefExtIn);
  846.     wFileAttr = wFileAttrIn;
  847.  
  848.     lpfnFileOpenDlgProc = MakeProcInstance(FileOpenDlgProc, hInst);
  849.  
  850.     iReturn = DialogBox(hInst,
  851.                         MAKEINTRESOURCE(OPENFILE),
  852.                         hWnd,
  853.                         lpfnFileOpenDlgProc);
  854.  
  855.     FreeProcInstance(lpfnFileOpenDlgProc);
  856.  
  857.     // Return selected file spec.
  858.     strcpy(szFilePathOut, szFilePath);
  859.     strcpy(szFileNameOut, szFileName);
  860.  
  861.     return(iReturn);
  862. }
  863.  
  864. //*******************************************************************
  865. // FileOpenDlgProc - get the name of a file to open
  866. //
  867. // paramaters:
  868. //             hDlg          - The window handle of this dialog box
  869. //             message       - The message number
  870. //             wParam        - The WORD parmater for this message
  871. //             lParam        - The LONG parmater for this message
  872. //
  873. // returns:
  874. //             depends on message.
  875. //
  876. //*******************************************************************
  877. BOOL FAR PASCAL FileOpenDlgProc(HWND hDlg, WORD iMessage,
  878.                                 WORD wParam, LONG lParam)
  879. {
  880.     static char   curpath[64];
  881.  
  882.     char          cLastChar;
  883.     int           nLen;
  884.     struct ffblk  fileinfo;
  885.  
  886.     switch (iMessage)
  887.     {
  888.         case WM_INITDIALOG:
  889.             // Save current working directory (will be restored on exit)
  890.             getcwd(curpath, sizeof(curpath));
  891.  
  892.             SendDlgItemMessage(hDlg, IDD_FNAME, EM_LIMITTEXT, 80, 0L);
  893.  
  894.             // Fill list box with files from starting file spec.
  895.             DlgDirList(hDlg,
  896.                        szFileSpec,
  897.                        IDD_FLIST,
  898.                        IDD_FPATH,
  899.                        wFileAttr);
  900.  
  901.             // Save starting file spec.
  902.             SetDlgItemText(hDlg, IDD_FNAME, szFileSpec);
  903.             return(TRUE);
  904.  
  905.         case WM_COMMAND:
  906.             switch (wParam)
  907.             {
  908.                 case IDD_FLIST:
  909.                     switch (HIWORD(lParam))
  910.                     {
  911.                         case LBN_SELCHANGE:
  912.                             if (DlgDirSelect(hDlg, szFileName, IDD_FLIST))
  913.                 strcat(szFileName, szFileSpec);
  914.                             SetDlgItemText(hDlg, IDD_FNAME, szFileName);
  915.                             break;
  916.  
  917.                         case LBN_DBLCLK:
  918.                             if (DlgDirSelect(hDlg, szFileName, IDD_FLIST))
  919.                             {
  920.                                 strcat(szFileName, szFileSpec);
  921.                                 DlgDirList(hDlg, szFileName, IDD_FLIST, IDD_FPATH, wFileAttr);
  922.                                 SetDlgItemText(hDlg, IDD_FNAME, szFileSpec);
  923.                             }
  924.                             else
  925.                             {
  926.                 SetDlgItemText(hDlg, IDD_FNAME, szFileName);
  927.                                 SendMessage(hDlg, WM_COMMAND, IDOK, 0L);
  928.                             }
  929.                             break;
  930.                     }
  931.                     break;
  932.  
  933.                 case IDD_FNAME:
  934.                     if (HIWORD(lParam) == EN_CHANGE)
  935.                     {
  936.                         EnableWindow(GetDlgItem(hDlg, IDOK),
  937.                                      (BOOL) SendMessage(LOWORD(lParam),
  938.                                                         WM_GETTEXTLENGTH, 0, 0L));
  939.             }
  940.                     break;
  941.  
  942.                 case IDOK:
  943.                     GetDlgItemText(hDlg, IDD_FNAME, szFileName, 80);
  944.  
  945.                     nLen  = strlen(szFileName);
  946.                     cLastChar = *AnsiPrev(szFileName, szFileName + nLen);
  947.  
  948.                     if (cLastChar == '\\' || cLastChar == ':')
  949.                         strcat(szFileName, szFileSpec);
  950.  
  951.                     if (strchr(szFileName, '*') || strchr(szFileName, '?'))
  952.             {
  953.                         if (DlgDirList(hDlg, szFileName, IDD_FLIST, IDD_FPATH, wFileAttr))
  954.                         {
  955.                             strcpy(szFileSpec, szFileName);
  956.                             SetDlgItemText(hDlg, IDD_FNAME, szFileSpec);
  957.                         }
  958.                         else
  959.                             MessageBeep(0);
  960.                         break;
  961.                     }
  962.  
  963.                     // Fill list with new spec.
  964.                     if (DlgDirList(hDlg, szFileName, IDD_FLIST, IDD_FPATH, wFileAttr))
  965.             {
  966.                         // Save new spec.
  967.                         strcpy(szFileSpec, szFileName);
  968.                         SetDlgItemText(hDlg, IDD_FNAME, szFileSpec);
  969.                         break;
  970.                     }
  971.  
  972.                     // Since we fell through, szFileName was not a search path.
  973.                     szFileName[nLen] = '\0';
  974.  
  975.                     // Make sure file exists.
  976.                     if (findfirst(szFileName, &fileinfo, 0))
  977.                     {
  978.             strcat(szFileName, szDefExt);
  979.  
  980.                         // Make sure file exists.
  981.                         if (findfirst(szFileName, &fileinfo, 0))
  982.                         {
  983.                             // No, beep and break.
  984.                             MessageBeep(0);
  985.                             break;
  986.                         }
  987.                     }
  988.  
  989.                     // Get current path to return.
  990.                     GetDlgItemText(hDlg, IDD_FPATH, szFilePath, 80);
  991.             strupr(szFilePath);
  992.                     if (szFilePath[strlen(szFilePath) - 1] != '\\')
  993.                         strcat(szFilePath, "\\");
  994.  
  995.                     // Return selected file name.
  996.                     strcpy(szFileName, fileinfo.ff_name);
  997.  
  998.                     // Restore current working directory.
  999.                     chdir(curpath);
  1000.  
  1001.                     // Terminate this dialog box.
  1002.                     EndDialog(hDlg, TRUE);
  1003.                     break;
  1004.  
  1005.                 case IDCANCEL:
  1006.                     // Restore current working directory
  1007.                     chdir(curpath);
  1008.  
  1009.                     // Terminate this dialog box.
  1010.                     EndDialog(hDlg, FALSE);
  1011.                     break;
  1012.  
  1013.         default:
  1014.                     return(FALSE);
  1015.             }
  1016.             break;
  1017.  
  1018.         default:
  1019.             return(FALSE);
  1020.     }
  1021.  
  1022.     return(TRUE);
  1023. }
  1024.  
  1025. //*******************************************************************
  1026.