home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / AVBROWSE.ZIP / BROWSE.C < prev    next >
C/C++ Source or Header  |  1989-02-08  |  6KB  |  271 lines

  1. /*
  2.     browse.c -- AVIO File Browsing Utility
  3.  
  4.     Created by Microsoft Corporation, 1989
  5. */
  6. #define    INCL_WINTRACKRECT
  7. #define    INCL_WINWINDOWMGR
  8. #define    INCL_WINPOINTERS
  9. #define INCL_WINFRAMEMGR
  10. #include <os2.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include "avio.h"
  15. #include "browse.h"
  16. #include <opendlg.h>
  17. /*
  18.     Constants
  19. */
  20. #define MAXLINELEN    120
  21. #define AVIO_PS_ROWS    25
  22. #define    AVIO_PS_COLS    80
  23. /*
  24.     Global Variables
  25. */
  26. FILE    *pfInput;
  27. PFNWP    pfnOldClient;
  28. char    *aszLines[NUM_DATA_LINES];
  29. SHORT    sTopLine = 0;
  30. DLF    dlfInput;
  31. HFILE    hfInput;
  32. USHORT    usAction;
  33. LBINFO    lbiData;
  34. HPOINTER hptrWait;
  35. HPOINTER hptrArrow;
  36. HWND    hWndClient;
  37. HWND    hWndFrame;
  38. BOOL    fLargeFont = FALSE;
  39. SHORT    sMaxLine;
  40. /*
  41.     Open the input file
  42. */
  43. int cdecl main(int argc, char *argv[]) {
  44.      static CHAR szClientClass[] = "Browse";
  45.      static CHAR szCaption[]     = "";
  46.      HAB    hAB;
  47.      HMQ    hmq;
  48.      QMSG    qmsg;
  49.      ULONG    flFrameFlags = FCF_STANDARD | FCF_HORZSCROLL | FCF_VERTSCROLL;
  50.      ULONG     flFrameStyle = WS_VISIBLE | FS_SCREENALIGN;
  51.      char    *szInFile;
  52.  
  53.      hAB = WinInitialize(0);
  54.      hmq = WinCreateMsgQueue(hAB, 0);
  55.  
  56.      WinRegisterClass(hAB, szClientClass, BrowseWndProc, CS_SYNCPAINT, 0);
  57.  
  58.      hWndFrame = WinCreateStdWindow(HWND_DESKTOP, flFrameStyle,
  59.                                     &flFrameFlags, szClientClass, szCaption,
  60.                                      0L, NULL, ID_RESOURCE, &hWndClient);
  61.      /*
  62.     Get the hourglass and arrow pointers
  63.      */
  64.      hptrWait  = WinQuerySysPointer(HWND_DESKTOP, SPTR_WAIT,  FALSE);
  65.      hptrArrow = WinQuerySysPointer(HWND_DESKTOP, SPTR_ARROW, FALSE);
  66.  
  67.      if (argc == 1) pfInput = stdin;
  68.      else {
  69.     if (!(pfInput = fopen(argv[1], "r"))) {
  70.         fprintf(stderr, "***Error:  Could not open %s", szInFile);
  71.         return(-1);
  72.     }
  73.      }
  74.      ReadFile();
  75.      /*
  76.     Setup AVIO PS and force a paint
  77.     Note:  This subclasses the client and frame windows
  78.      */
  79.      lbiData.sPSrows    = AVIO_PS_ROWS;
  80.      lbiData.sPScols    = AVIO_PS_COLS;
  81.      lbiData.sRows    = sTopLine;
  82.      lbiData.sCols    = sMaxLine;
  83.      lbiData.pfnQL    = (PFNQL) RetrieveLine;
  84.      lbiData.fLargeFont    = FALSE;
  85.      AvioInit(&lbiData);
  86.      /*
  87.     Process messages
  88.      */
  89.      while (WinGetMsg(hAB, &qmsg, NULL, 0, 0)) WinDispatchMsg(hAB, &qmsg);
  90.  
  91.      /* Blast the AVIO PS */
  92.      AvioClose();
  93.  
  94.      WinDestroyWindow(hWndFrame);
  95.      WinDestroyMsgQueue(hmq);
  96.      WinTerminate(hAB);
  97.      return 0;
  98. }
  99.  
  100. void ReadFile(void) {
  101. /*
  102.     Reads in a file using <stdio.h> fgets() calls.
  103.     It might be wise to put better word wrap facilities here
  104. */
  105.     char    szLine[MAXLINELEN];
  106.  
  107.     /* Put up the hourglass */
  108.     WinSetPointer(HWND_DESKTOP, hptrWait);
  109.  
  110.     /* Reinitialize buffer, MaxLineLength */
  111.     for (; sTopLine > 0; ) free(aszLines[--sTopLine]);
  112.     sMaxLine = 0;
  113.  
  114.     /* Read in the file */
  115.     while (fgets(szLine, MAXLINELEN, pfInput)) {
  116.  
  117.     /* Convert LF (\n) into NULL (\0) */
  118.     if (szLine[strlen(szLine) - 1] == '\n') {
  119.         szLine[strlen(szLine) - 1] = 0;
  120.     } else szLine[MAXLINELEN - 1] = 0;
  121.  
  122.     if (StoreLine(szLine)) {
  123.         fprintf(stderr,"***Error:  Line buffer full\n");
  124.         return;
  125.     }
  126.     }
  127.     fclose(pfInput);
  128.  
  129.     /* Reset the mouse pointer */
  130.     WinSetPointer(HWND_DESKTOP, hptrArrow);
  131.  
  132.     return;
  133. }
  134.  
  135. SHORT StoreLine(char *szLine) {
  136. /*
  137.     Put a line into the line buffer; line numbers are free
  138.     For > 64K data, add code here and in RetrieveLine
  139. */
  140.     int        i, cLinePos;
  141.     BOOL    fDone;
  142.     /*
  143.     Check if top line exceeded, or malloc() fails
  144.     */
  145.     if (sTopLine == NUM_DATA_LINES)  return -1;
  146.     /*
  147.     Compute line length with tabs expanded
  148.     */
  149.     cLinePos = 0;
  150.     for (i = 0; i < MAXLINELEN; i++) {
  151.     switch(szLine[i]) {
  152.         case '\0':
  153.         cLinePos++; i = MAXLINELEN;
  154.         break;
  155.         case '\t':
  156.         do {
  157.             cLinePos++;
  158.         } while (cLinePos % 8);
  159.         break;
  160.  
  161.         default:
  162.         cLinePos++;
  163.     }
  164.  
  165.     }
  166.     if (cLinePos > sMaxLine) sMaxLine = cLinePos;
  167.     if (!(aszLines[sTopLine] = malloc(cLinePos))) return -1;
  168.     /*
  169.     Copy szLine into the line buffer.  Expand tabs here.
  170.     */
  171.     i = cLinePos = 0; fDone = FALSE;
  172.     while ((i <= MAXLINELEN) && (!fDone)) {
  173.     switch(szLine[i]) {
  174.         case '\t':
  175.         do {
  176.             aszLines[sTopLine][cLinePos++] = ' ';
  177.         } while (cLinePos % 8);
  178.         break;
  179.  
  180.         default:
  181.         aszLines[sTopLine][cLinePos++] = szLine[i];
  182.         fDone = !szLine[i];
  183.         break;
  184.     }
  185.     i++;
  186.     }
  187.     sTopLine++;
  188.     return 0;
  189. }
  190.  
  191. char * _loadds RetrieveLine(USHORT usLineNum) {
  192. /*
  193.     Return line numbered usLineNum
  194. */
  195.     if (usLineNum >= sTopLine) {        /* Out of range */
  196.     return NULL;
  197.     }
  198.     return aszLines[usLineNum];
  199. }
  200.  
  201. MRESULT CALLBACK BrowseWndProc(hWnd, msg, mp1, mp2)
  202. HWND hWnd;
  203. USHORT msg;
  204. MPARAM mp1;
  205. MPARAM mp2;
  206. {
  207. /*
  208.     Handle the About... and Open... messages
  209. */
  210.     switch(msg) {
  211.     case WM_COMMAND:
  212.         switch (COMMANDMSG(&msg)->cmd) {
  213.         case IDM_ABOUT:
  214.             WinDlgBox(HWND_DESKTOP, hWnd, AboutDlgProc,
  215.                   NULL, IDD_ABOUT, NULL);
  216.                     return 0;
  217.  
  218.         case IDM_OPEN:
  219.             /*
  220.             Open the file, using the file dialog
  221.             then reopen it with stdio calls
  222.             */
  223.             SetupDLF(&dlfInput, DLG_OPENDLG, hfInput, "\\*.*", NULL);
  224.             DlgFile(hWnd, &dlfInput);
  225.             pfInput = fopen(dlfInput.szOpenFile, "r");
  226.             ReadFile();
  227.             /*
  228.             Close the opened handle
  229.             */
  230.             DosClose(hfInput);
  231.  
  232.             /* Fix up the screen display */
  233.             lbiData.sRows = sTopLine;
  234.             lbiData.sCols = sMaxLine;
  235.             lbiData.fLargeFont = fLargeFont;
  236.             AvioInit(&lbiData);
  237.  
  238.             return 0;
  239.  
  240.         case IDM_FONT:
  241.             AvioLargeFont(fLargeFont = !fLargeFont);
  242.                return 0;
  243.  
  244.         default: return 0;
  245.         }
  246.         break;
  247.     default: return WinDefWindowProc(hWnd, msg, mp1, mp2);
  248.     }
  249.     return 0L;
  250. }
  251.  
  252. MRESULT CALLBACK AboutDlgProc(hDlg, msg, mp1, mp2)
  253. /*
  254.     About... dialog procedure
  255. */
  256. HWND    hDlg;
  257. USHORT    msg;
  258. MPARAM    mp1;
  259. MPARAM    mp2;
  260. {
  261.     switch(msg) {
  262.     case WM_COMMAND:
  263.         switch(COMMANDMSG(&msg)->cmd) {
  264.         case DID_OK: WinDismissDlg(hDlg, TRUE); break;
  265.         default: break;
  266.         }
  267.     default: return WinDefDlgProc(hDlg, msg, mp1, mp2);
  268.     }
  269.     return FALSE;
  270. }
  271.