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