home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 May / Pcwk5b98.iso / Borland / Cplus45 / BC45 / HDUMP.PAK / HDUMP.C next >
C/C++ Source or Header  |  1995-08-29  |  35KB  |  1,069 lines

  1. // Borland C++ - (C) Copyright 1991, 1994 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, 0, 0))
  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. #pragma warn -eff
  364. LRESULT CALLBACK _export HdumpWndProc(HWND hWnd, UINT message,
  365.            WPARAM wParam, LPARAM lParam)
  366. {
  367.     static char     sztFilePath[64];  // we remember last directory searched
  368.     static char     sztFileExt[5];
  369.     static char     sztFileSpec[64];
  370.     static char     sztFileName[64];
  371.  
  372.     DLGPROC  lpproc;                  // pointer to thunk for dialog box
  373.     char     buf[128];                // temp buffer
  374.     int      i;
  375.     FILE    *fh;
  376.  
  377.     switch (message)
  378.     {
  379.         case WM_CREATE:
  380.             // Start file search at current directory
  381.             getcwd(sztFilePath, sizeof(sztFilePath));
  382.             strcat(sztFilePath, "\\");
  383.             strcpy(sztFileExt, ".*");
  384.             strcpy(sztFileName, "");
  385.  
  386.       return(DefWindowProc(hWnd, message, wParam, lParam));
  387.  
  388.   case WM_COMMAND:
  389.       switch (GET_WM_COMMAND_ID(wParam, lParam))
  390.             {
  391.                 case IDM_QUIT:
  392.                     // User selected Quit on menu
  393.                     PostMessage(hWnd, WM_CLOSE, 0, 0L);
  394.                     break;
  395.  
  396.                 case IDM_HOME:
  397.                     // Used to implement home to topleft from keyboard.
  398.         SendMessage(hWnd, WM_HSCROLL, GET_WM_HSCROLL_MPS (SB_TOP, 0, 0));
  399.         SendMessage(hWnd, WM_VSCROLL, GET_WM_VSCROLL_MPS (SB_TOP, 0, 0));
  400.                     break;
  401.  
  402.     case IDM_ABOUT:
  403.                     // Display about box.
  404.                     lpproc = (DLGPROC)MakeProcInstance((FARPROC)About, hInst);
  405.                     DialogBox(hInst,
  406.                               MAKEINTRESOURCE(ABOUT),
  407.                               hWnd,
  408.                               lpproc);
  409.                     FreeProcInstance((FARPROC)lpproc);
  410.                     break;
  411.  
  412.                 case IDM_OPEN:
  413.                     // Setup initial search path for open dialog box.
  414.                     strcpy(sztFileSpec, sztFilePath);
  415.         strcat(sztFileSpec, "*");
  416.                     strcat(sztFileSpec, sztFileExt);
  417.  
  418.                     // Go ask user to select file to display.
  419.                     if (DoFileOpenDlg(hInst,
  420.                                       hWnd,
  421.                                       sztFileSpec,
  422.                                       sztFileExt,
  423.                                       0x4010, // drives & subdirectories
  424.                                       sztFilePath,
  425.                                       sztFileName))
  426.                     {
  427.                         // If user said OK construct file path/file name
  428.       // to open.
  429.                         strcpy(szFName, sztFilePath);
  430.                         strcat(szFName, sztFileName);
  431.  
  432.                         // Show file name in window caption.
  433.                         sprintf(buf, "hdump - %s", szFName);
  434.                         SetWindowText(hWnd, buf);
  435.  
  436.                         // Determine file size and some display paramaters.
  437.                         fh = fopen(szFName, "r+b");
  438.                         if (fh)
  439.                         {
  440.                             fsize = filelength(fileno(fh));
  441.           numlines = (int) ((fsize + 16L - 1L) / 16L);
  442.                             SnapLine(buf, szFName, 16, 16,NULL); // display width
  443.                             maxwidth = strlen(buf);
  444.                             nVscrollPos = 0;
  445.                             nHscrollPos = 0;
  446.  
  447.                             fclose(fh);
  448.                         }
  449.  
  450.                         else  // if file open failed.
  451.                         {
  452.                             SetWindowText(hWnd, "hdump");
  453.                             numlines = 0;
  454.           maxwidth = 0;
  455.                         }
  456.  
  457.                         // Go setup scroll ranges for this file.
  458.                         SetupScroll(hWnd);
  459.  
  460.                         // Show first part of file.
  461.                         InvalidateRect(hWnd, NULL, TRUE);
  462.                         UpdateWindow(hWnd);
  463.                     }
  464.                     break;
  465.  
  466.                 default:
  467.         break;
  468.           }
  469.           break;
  470.  
  471.         case WM_SIZE:
  472.             // Save size of window client area.
  473.             if (lParam)
  474.               {
  475.                 yClient = HIWORD(lParam);
  476.     xClient = LOWORD(lParam);
  477.  
  478.     yClient = (yClient / yCharnl + 1) * yCharnl;
  479.  
  480.     lParam = MAKELONG(xClient, yClient);
  481.  
  482.     // Go setup scroll ranges and file display area based upon
  483.     // client area size.
  484.     SetupScroll(hWnd);
  485.  
  486.     return(DefWindowProc(hWnd, message, wParam, lParam));
  487.         }
  488.       break;
  489.  
  490.   case WM_VSCROLL:
  491.       // React to the various vertical scroll related actions.
  492.       switch (GET_WM_VSCROLL_CODE(wParam, lParam))
  493.       {
  494.     case SB_TOP:
  495.         nVscrollInc = -nVscrollPos;
  496.         break;
  497.  
  498.     case SB_BOTTOM:
  499.         nVscrollInc = nVscrollMax - nVscrollPos;
  500.         break;
  501.  
  502.     case SB_LINEUP:
  503.         nVscrollInc = -1;
  504.         break;
  505.  
  506.     case SB_LINEDOWN:
  507.         nVscrollInc = 1;
  508.         break;
  509.  
  510.     case SB_PAGEUP:
  511.         nVscrollInc = -max(1, yClient / yChar);
  512.         break;
  513.  
  514.     case SB_PAGEDOWN:
  515.         nVscrollInc = max(1, yClient / yChar);
  516.         break;
  517.  
  518.     case SB_THUMBPOSITION:
  519.         nVscrollInc = GET_WM_VSCROLL_POS(wParam, lParam) - nVscrollPos;
  520.         break;
  521.  
  522.     case SB_THUMBTRACK:
  523.         nVscrollInc = GET_WM_VSCROLL_POS(wParam, lParam) - nVscrollPos;
  524.         break;
  525.  
  526.     default:
  527.         nVscrollInc = 0;
  528.       }
  529.  
  530.       nVscrollInc = max(-nVscrollPos,
  531.             min(nVscrollInc, nVscrollMax - nVscrollPos));
  532.       if (nVscrollInc)
  533.       {
  534.     nVscrollPos += nVscrollInc;
  535.     ScrollWindow(hWnd, 0, -yChar * nVscrollInc, NULL, NULL);
  536.     SetScrollPos(hWnd, SB_VERT, nVscrollPos, TRUE);
  537.     UpdateWindow(hWnd);
  538.       }
  539.       break;
  540.  
  541.   case WM_HSCROLL:
  542.       // React to the various horizontal scroll related actions.
  543.       switch (GET_WM_HSCROLL_CODE(wParam, lParam))
  544.       {
  545.     case SB_LINEUP:
  546.         nHscrollInc = -1;
  547.         break;
  548.  
  549.     case SB_LINEDOWN:
  550.         nHscrollInc = 1;
  551.         break;
  552.  
  553.     case SB_PAGEUP:
  554.         nHscrollInc = -8;
  555.         break;
  556.  
  557.     case SB_PAGEDOWN:
  558.         nHscrollInc = 8;
  559.         break;
  560.  
  561.     case SB_THUMBPOSITION:
  562.         nHscrollInc = GET_WM_HSCROLL_POS(wParam, lParam) - nHscrollPos;
  563.         break;
  564.  
  565.     case SB_THUMBTRACK:
  566.         nHscrollInc = GET_WM_HSCROLL_POS(wParam, lParam) - nHscrollPos;
  567.         break;
  568.  
  569.     default:
  570.         nHscrollInc = 0;
  571.       }
  572.  
  573.       nHscrollInc = max(-nHscrollPos,
  574.             min(nHscrollInc, nHscrollMax - nHscrollPos));
  575.       if (nHscrollInc)
  576.       {
  577.     nHscrollPos += nHscrollInc;
  578.     ScrollWindow(hWnd, -xChar * nHscrollInc, 0, NULL, NULL);
  579.     SetScrollPos(hWnd, SB_HORZ, nHscrollPos, TRUE);
  580.     UpdateWindow(hWnd);
  581.       }
  582.       break;
  583.  
  584.   case WM_KEYDOWN:
  585.       // Translate various keydown messages to appropriate horizontal
  586.       // and vertical scroll actions.
  587.       for (i = 0; i < NUMKEYS; i++)
  588.       {
  589.     if (wParam == key2scroll[i].wVirtkey)
  590.     {
  591.         SendMessage(hWnd, key2scroll[i].iMessage,
  592.         key2scroll[i].wRequest, 0L);
  593.         break;
  594.     }
  595.       }
  596.       break;
  597.  
  598.   case WM_PAINT:
  599.       // Go paint the client area of the window with the appropriate
  600.       // part of the selected file.
  601.       HdumpPaint(hWnd);
  602.       break;
  603.  
  604.   case WM_DESTROY:
  605.       // This is the end if we were closed by a DestroyWindow call.
  606.       CloseHdump();         // take any necessary wrapup action.
  607.       PostQuitMessage(0);   // this is the end...
  608.       break;
  609.  
  610.   case WM_QUERYENDSESSION:
  611.       // If we return TRUE we are saying it's ok with us to end the
  612.       // windows session.
  613.       CloseHdump();         // take any necessary wrapup action.
  614.       return((long) TRUE);  // we agree to end session.
  615.  
  616.   case WM_CLOSE:
  617.       // Tell windows to destroy our window.
  618.       DestroyWindow(hWnd);
  619.       break;
  620.  
  621.   default:
  622.       // Let windows handle all messages we choose to ignore.
  623.       return(DefWindowProc(hWnd, message, wParam, lParam));
  624.     }
  625.  
  626.     return(0L);
  627. }
  628. #pragma warn .eff
  629.  
  630. //*******************************************************************
  631. // SetupScroll - setup scroll ranges
  632. //
  633. // Setup the vertical and horizontal scroll ranges and positions
  634. // of the applicatons main window based on:
  635. //
  636. //     numlines - The maximum number of lines to display in a hexdump
  637. //                of the selected file.
  638. //     maxwidth - The width of each line to display as formated for
  639. //                the hexdump.
  640. //
  641. // The resulting variables, nVscrollPos and nPageMaxLines, are used
  642. // by the function HdumpPaint to determine what part of the selected
  643. // file to display in the window.
  644. //
  645. // paramaters:
  646. //             hWnd          - The callers window handle
  647. //
  648. //*******************************************************************
  649. void SetupScroll(HWND hWnd)
  650. {
  651.     // numlines established during open
  652.     nVscrollMax = max(0, numlines - yClient / yChar);
  653.     nVscrollPos = min(nVscrollPos, nVscrollMax);
  654.  
  655.     nHscrollMax = max(0, maxwidth - xClient / xChar);
  656.     nHscrollPos = min(nHscrollPos, nHscrollMax);
  657.  
  658.     SetScrollRange (hWnd, SB_VERT, 0, nVscrollMax, FALSE);
  659.     SetScrollPos   (hWnd, SB_VERT, nVscrollPos, TRUE);
  660.  
  661.     SetScrollRange (hWnd, SB_HORZ, 0, nHscrollMax, FALSE);
  662.     SetScrollPos   (hWnd, SB_HORZ, nHscrollPos, TRUE);
  663.  
  664.     nPageMaxLines = min(numlines, yClient / yChar);
  665. }
  666.  
  667. //*******************************************************************
  668. // HdumpPaint - paint the main window
  669. //
  670. // If a file has been selected, as indicated by numlines being
  671. // greater than 0, this module will read and display a portion
  672. // of the file as determined by the current size and scroll
  673. // position of the window.
  674. //
  675. // paramaters:
  676. //             hWnd          - The callers window handle
  677. //
  678. //*******************************************************************
  679. void HdumpPaint(HWND hWnd)
  680. {
  681.     PAINTSTRUCT  ps;
  682.     HDC          hDC;
  683.     LONG         offset;
  684.     int          tlen;
  685.     int          lpos;
  686.     char         buf[128];
  687.     int          i;
  688.     int          len;
  689.     int          hfile;
  690.     HANDLE       hbuff;
  691.     LPSTR        lpbuff;
  692.     LPSTR        lcp;
  693.  
  694.     BeginPaint(hWnd, (LPPAINTSTRUCT) &ps);
  695.     hDC = ps.hdc;
  696.  
  697.     // Establish fixed font in display context.
  698.     SelectObject(hDC, hnewsfont);
  699.  
  700.     if (numlines)
  701.     {
  702.   // Open the file to display
  703.         // (files should not stay open over multiple windows messages)
  704.         if ((hfile = _lopen(szFName, OF_READ)) != -1)
  705.         {
  706.             // Get a buffer to hold data to fill screen.
  707.             len = nPageMaxLines * 16;
  708.             hbuff = GlobalAlloc(GMEM_MOVEABLE, (DWORD) len);
  709.             if (hbuff)
  710.             {
  711.                 // Lock the buffer to get a pointer to it.
  712.                 lpbuff = GlobalLock(hbuff);
  713.                 if (lpbuff)
  714.                 {
  715.         // Get offset into file to start display.
  716.                     offset = (LONG) nVscrollPos * 16L;
  717.  
  718.                     if (_llseek(hfile, offset, 0) != -1L)
  719.                     {
  720.                         tlen = _lread(hfile, lpbuff, len);
  721.                         if (tlen != -1)
  722.                         {
  723.                             lpos = 0;
  724.                             lcp = lpbuff;
  725.  
  726.                             for (i = nVscrollPos; i < (nVscrollPos + nPageMaxLines); i++)
  727.                             {
  728.         if (tlen > 16)
  729.                                     len = 16;
  730.                                 else
  731.                                     len = tlen;
  732.                                 tlen -= len;
  733.  
  734.                                 SnapLine(buf, lcp, len, 16, (char *) (i * 16));
  735.  
  736.                                 TextOut(hDC,
  737.                                         xChar * (-nHscrollPos + 0),
  738.                                         yChar * lpos++,
  739.                                         buf,
  740.                                         strlen(buf));
  741.  
  742.                                 lcp += 16;
  743.                             }
  744.                         }
  745.                     }
  746.  
  747.                     GlobalUnlock(hbuff);
  748.                 }
  749.  
  750.                 GlobalFree(hbuff);
  751.             }
  752.  
  753.             _lclose(hfile);
  754.   }
  755.     }
  756.  
  757.     EndPaint(hWnd, (LPPAINTSTRUCT) &ps);
  758. }
  759.  
  760. //*******************************************************************
  761. // SnapLine - dump a line of memory
  762. //
  763. // paramaters:
  764. //             szBuf         - buffer to hold result
  765. //             mem           - pointer to memory to format
  766. //             len           - length to format
  767. //             dwid          - max display width (8 or 16 recomended)
  768. //             olbl          - label to put at start of line
  769. //
  770. // returns:
  771. //             A pointer to szBbuf.
  772. //
  773. //*******************************************************************
  774. char *SnapLine(char *szBuf, LPSTR mem, int len, int dwid, char *olbl)
  775. {
  776.     int            i;
  777.     int            j;
  778.     unsigned char  c;
  779.     unsigned char  buff[80];
  780.     unsigned char  tbuf[80];
  781.  
  782.     if (len > dwid)
  783.         len = dwid;
  784.  
  785.     *szBuf = 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. #pragma warn -eff
  844. DoFileOpenDlg(HINSTANCE hInst, HWND hWnd,
  845.               char *szFileSpecIn, char *szDefExtIn,
  846.               WORD wFileAttrIn,
  847.               char *szFilePathOut, char *szFileNameOut)
  848. {
  849.     DLGPROC   lpfnFileOpenDlgProc;
  850.     int       iReturn;
  851.  
  852.     // Save starting file spec.
  853.     strcpy(szFileSpec, szFileSpecIn);
  854.     strcpy(szDefExt, szDefExtIn);
  855.     strcpy(szFilePath, szFilePathOut);
  856.     wFileAttr = wFileAttrIn;
  857.  
  858.     lpfnFileOpenDlgProc = (DLGPROC)MakeProcInstance((FARPROC)FileOpenDlgProc, hInst);
  859.  
  860.     iReturn = DialogBox(hInst,
  861.                         MAKEINTRESOURCE(OPENFILE),
  862.                         hWnd,
  863.                         lpfnFileOpenDlgProc);
  864.  
  865.     FreeProcInstance((FARPROC)lpfnFileOpenDlgProc);
  866.  
  867.     // Return selected file spec.
  868.     strcpy(szFilePathOut, szFilePath);
  869.     strcpy(szFileNameOut, szFileName);
  870.  
  871.     return(iReturn);
  872. }
  873. #pragma warn .eff
  874.  
  875. //*******************************************************************
  876. // FileOpenDlgProc - get the name of a file to open
  877. //
  878. // paramaters:
  879. //             hDlg          - The window handle of this dialog box
  880. //             message       - The message number
  881. //             wParam        - The WPARAM parameter for this message
  882. //             lParam        - The LPARAM parameter for this message
  883. //
  884. // returns:
  885. //             depends on message.
  886. //
  887. //*******************************************************************
  888. BOOL CALLBACK _export FileOpenDlgProc(HWND hDlg, UINT iMessage,
  889.         WPARAM wParam, LPARAM lParam)
  890. {
  891.     static char   curpath[64];
  892.     char          tempDir[80], *tempStr = NULL;
  893.     unsigned      attrib;
  894.     char          cLastChar;
  895.     int           nLen;
  896.     struct ffblk  fileinfo;
  897.  
  898.     switch (iMessage)
  899.     {
  900.         case WM_INITDIALOG:
  901.             // Save current working directory (will be restored on exit)
  902.             getcwd(curpath, sizeof(curpath));
  903.  
  904.             SendDlgItemMessage(hDlg, IDD_FNAME, EM_LIMITTEXT, 80, 0L);
  905.  
  906.             // Fill list box with files from starting file spec.
  907.             DlgDirList(hDlg,
  908.                        szFileSpec,
  909.                        IDD_FLIST,
  910.                        IDD_FPATH,
  911.                        wFileAttr);
  912.  
  913.       // Save starting file spec.
  914.       SetDlgItemText(hDlg, IDD_FNAME, szFileSpec);
  915.       return(TRUE);
  916.  
  917.   case WM_COMMAND:
  918.       switch (GET_WM_COMMAND_ID(wParam, lParam))
  919.       {
  920.     case IDD_FLIST:
  921.         switch (GET_WM_COMMAND_CMD(wParam, lParam))
  922.         {
  923.       case LBN_SELCHANGE:
  924.           if (DlgDirSelectEx(hDlg, szFileName, sizeof (szFileName), IDD_FLIST))
  925.         strcat(szFileName, szFileSpec);
  926.           SetDlgItemText(hDlg, IDD_FNAME, szFileName);
  927.           break;
  928.  
  929.       case LBN_DBLCLK:
  930.           if (DlgDirSelectEx(hDlg, szFileName, sizeof (szFileName), IDD_FLIST))
  931.           {
  932.         SendMessage(hDlg, WM_COMMAND,
  933.               GET_WM_COMMAND_MPS (IDOK, 0, 0));
  934.           }
  935.           else
  936.           {
  937.         SetDlgItemText(hDlg, IDD_FNAME, szFileName);
  938.         SendMessage(hDlg, WM_COMMAND,
  939.               GET_WM_COMMAND_MPS (IDOK, 0, 0));
  940.           }
  941.           break;
  942.         }
  943.         break;
  944.  
  945.     case IDD_FNAME:
  946.         if (GET_WM_COMMAND_CMD(wParam, lParam) == EN_CHANGE)
  947.         {
  948.       EnableWindow(GetDlgItem(hDlg, IDOK),
  949.              (BOOL) SendMessage((HWND)lParam,
  950.               WM_GETTEXTLENGTH, 0, 0L));
  951.         }
  952.         break;
  953.  
  954.                 case IDOK:
  955.         GetDlgItemText(hDlg, IDD_FNAME, szFileName, 80);
  956.  
  957.         nLen  = strlen(szFileName);
  958.  
  959.         // Save new spec. & directory
  960.         strcpy(tempDir, szFileName);
  961. #ifdef __FLAT__
  962.         attrib = GetFileAttributes( (LPCTSTR) tempDir );
  963. #else
  964.         _dos_getfileattr(tempDir, &attrib);
  965. #endif
  966.         if(!(attrib == _A_SUBDIR) )
  967.         {
  968.          tempStr = strrchr(tempDir, '\\');
  969.          if(tempStr)
  970.             tempStr[1] = 0;
  971.         }
  972.         if(tempStr || (attrib == _A_SUBDIR))
  973.         {
  974.            strcat(szFilePath, tempDir);
  975.         }
  976.  
  977.         cLastChar = *AnsiPrev(szFileName, szFileName + nLen);
  978.         if (cLastChar == '\\' || cLastChar == ':')
  979.         {
  980.          strcpy(szFilePath, szFileName);
  981.          chdir(szFilePath);
  982.          SetDlgItemText(hDlg, IDD_FPATH, szFilePath);
  983.          strcat(szFileName, szFileSpec);
  984.         }
  985.         if( szFileName[ 1 ] == ':' && 
  986.             strcmp( "*.*", &szFileName[ 2 ] ) == 0 )
  987.         {
  988.          strcpy( szFilePath, szFileName );
  989.          szFilePath[ 2 ] = 0x0;
  990.          chdir(szFilePath);
  991.          SetDlgItemText(hDlg, IDD_FPATH, szFilePath);
  992.         }
  993.  
  994.         if (strchr(szFileName, '*') || strchr(szFileName, '?'))
  995.         {
  996.          if (DlgDirList(hDlg, szFileName, IDD_FLIST, IDD_FPATH, wFileAttr))
  997.          {
  998.             strcpy(szFileSpec, szFileName);
  999.             SetDlgItemText(hDlg, IDD_FNAME, szFileSpec);
  1000.          }
  1001.          else
  1002.             MessageBeep(0);
  1003.          break;
  1004.         }
  1005.  
  1006.         // Fill list with new spec.
  1007.         if (DlgDirList(hDlg, szFileName, IDD_FLIST, IDD_FPATH, wFileAttr))
  1008.         {
  1009.          strcpy(szFileSpec, szFileName);
  1010.          SetDlgItemText(hDlg, IDD_FNAME, szFileSpec);
  1011.          break;
  1012.         }
  1013.  
  1014.         // Since we fell through, szFileName was not a search path.
  1015.         szFileName[nLen] = '\0';
  1016.  
  1017.         // Make sure file exists.
  1018.         if (findfirst(szFileName, &fileinfo, 0))
  1019.         {
  1020.          strcat(szFileName, szDefExt);
  1021.  
  1022.          // Make sure file exists.
  1023.          if (findfirst(szFileName, &fileinfo, 0))
  1024.          {
  1025.             // No, beep and break.
  1026.             MessageBeep(0);
  1027.             break;
  1028.          }
  1029.         }
  1030.  
  1031.         strupr(szFilePath);
  1032.         nLen = strlen( szFilePath ) - 1;
  1033.         if (szFilePath[ nLen ] != '\\' &&
  1034.             szFilePath[ nLen ] != ':' )
  1035.                strcat(szFilePath, "\\");
  1036.  
  1037.         // Return selected file name.
  1038.         strcpy(szFileName, fileinfo.ff_name);
  1039.  
  1040.         // Restore current working directory.
  1041.         chdir(curpath);
  1042.  
  1043.         // Terminate this dialog box.
  1044.         EndDialog(hDlg, TRUE);
  1045.                     break;
  1046.  
  1047.                 case IDCANCEL:
  1048.                     // Restore current working directory
  1049.                     chdir(curpath);
  1050.  
  1051.                     // Terminate this dialog box.
  1052.                     EndDialog(hDlg, FALSE);
  1053.                     break;
  1054.  
  1055.     default:
  1056.                     return(FALSE);
  1057.       }
  1058.             break;
  1059.  
  1060.         default:
  1061.             return(FALSE);
  1062.     }
  1063.  
  1064.     return(TRUE);
  1065. }
  1066.  
  1067. //*******************************************************************
  1068.  
  1069.