Annotation of pmsdk/samples/petzold/chap11/head.c, revision 1.1.1.1

1.1       root        1: /*------------------------------
                      2:    HEAD.C -- Displays File Head
                      3:   ------------------------------*/
                      4: 
                      5: #define INCL_WIN
                      6: #define INCL_GPI
                      7: 
                      8: #include <os2.h>
                      9: #include <malloc.h>
                     10: #include <stdio.h>
                     11: #include <string.h>
                     12: #include "head.h"
                     13: 
                     14: SHORT ParseFileName (CHAR *, CHAR *) ;
                     15: MRESULT EXPENTRY ClientWndProc (HWND, USHORT, MPARAM, MPARAM) ;
                     16: 
                     17: CHAR szFileName [80] ;
                     18: 
                     19: int main (int argc, char *argv[])
                     20:      {
                     21:      static CHAR szClientClass [] = "Head" ;
                     22:      HAB         hab ;
                     23:      HMQ         hmq ;
                     24:      HWND        hwndClient, hwndFrame ;
                     25:      QMSG        qmsg ;
                     26:      ULONG       flFrameFlags = FCF_STANDARD ;
                     27:      ULONG       flFrameStyle = WS_VISIBLE ;
                     28: 
                     29:                /*-----------------------------------------------------
                     30:                   Check for filename parameter and copy to szFileName
                     31:                  -----------------------------------------------------*/
                     32: 
                     33:      if (argc > 1)
                     34:           ParseFileName (szFileName, argv [1]) ;
                     35: 
                     36:                /*-------------------
                     37:                   Continue normally
                     38:                  -------------------*/
                     39:      
                     40:      hab = WinInitialize (0) ;
                     41:      hmq = WinCreateMsgQueue (hab, 0) ;
                     42: 
                     43:      WinRegisterClass (hab, szClientClass, ClientWndProc, CS_SIZEREDRAW, 0) ;
                     44: 
                     45:      hwndFrame = WinCreateStdWindow (HWND_DESKTOP, flFrameStyle,
                     46:                                      &flFrameFlags, szClientClass,
                     47:                                      szClientClass,
                     48:                                     0L, NULL, ID_RESOURCE, &hwndClient) ;
                     49: 
                     50:      while (WinGetMsg (hab, &qmsg, NULL, 0, 0))
                     51:           WinDispatchMsg (hab, &qmsg) ;
                     52: 
                     53:      WinDestroyWindow (hwndFrame) ;
                     54:      WinDestroyMsgQueue (hmq) ;
                     55:      WinTerminate (hab) ;
                     56:      return 0 ;
                     57:      }
                     58: 
                     59: SHORT ParseFileName (CHAR *pcOut, CHAR *pcIn)
                     60:      {
                     61:           /*----------------------------------------------------------------
                     62:              Input:    pcOut -- Pointer to parsed file specification.
                     63:                        pcIn  -- Pointer to raw file specification.
                     64:                        
                     65:              Returns:  0 -- pcIn had invalid drive or directory.
                     66:                        1 -- pcIn was empty or had no filename.
                     67:                        2 -- pcOut points to drive, full dir, and file name.
                     68: 
                     69:              Changes current drive and directory per pcIn string.
                     70:             ----------------------------------------------------------------*/
                     71: 
                     72:      CHAR   *pcLastSlash, *pcFileOnly ;
                     73:      ULONG  ulDriveMap ;
                     74:      USHORT usDriveNum, usDirLen = 64 ;
                     75: 
                     76:      strupr (pcIn) ;
                     77: 
                     78:                /*------------------------------------
                     79:                   If input string is empty, return 1
                     80:                  ------------------------------------*/
                     81: 
                     82:      if (pcIn [0] == '\0')
                     83:           return 1 ;
                     84: 
                     85:                /*----------------------------------------------
                     86:                   Get drive from input string or current drive
                     87:                  ----------------------------------------------*/
                     88: 
                     89:      if (pcIn [1] == ':')
                     90:           {
                     91:           if (DosSelectDisk (pcIn [0] - '@'))
                     92:                return 0 ;
                     93: 
                     94:           pcIn += 2 ;
                     95:           }
                     96: 
                     97:      DosQCurDisk (&usDriveNum, &ulDriveMap) ;
                     98: 
                     99:      *pcOut++ = (CHAR) usDriveNum + '@' ;
                    100:      *pcOut++ = ':' ;
                    101:      *pcOut++ = '\\' ;
                    102: 
                    103:                /*--------------------------------------
                    104:                   If rest of string is empty, return 1
                    105:                  -------------------------------------*/
                    106: 
                    107:      if (pcIn [0] == '\0')
                    108:           return 1 ;
                    109: 
                    110:                /*----------------------------------------------------------
                    111:                   Search for last backslash.  If none, could be directory.
                    112:                  ----------------------------------------------------------*/
                    113: 
                    114:      if (NULL == (pcLastSlash = strrchr (pcIn, '\\')))
                    115:           {
                    116:          if (!DosChDir (pcIn, 0L))
                    117:                return 1 ;
                    118: 
                    119:                     /*----------------------------------------------------
                    120:                        Otherwise, get current dir & attach input filename
                    121:                       ----------------------------------------------------*/
                    122: 
                    123:           DosQCurDir (0, pcOut, &usDirLen) ;
                    124: 
                    125:           if (strlen (pcIn) > 12)
                    126:                return 0 ;
                    127: 
                    128:           if (*(pcOut + strlen (pcOut) - 1) != '\\')
                    129:                strcat (pcOut++, "\\") ;
                    130: 
                    131:           strcat (pcOut, pcIn) ;
                    132:           return 2 ;
                    133:           }
                    134: 
                    135:                /*-------------------------------------------------------
                    136:                   If the only backslash is at beginning, change to root
                    137:                  -------------------------------------------------------*/
                    138: 
                    139:      if (pcIn == pcLastSlash)
                    140:           {
                    141:          DosChDir ("\\", 0L) ;
                    142: 
                    143:           if (pcIn [1] == '\0')
                    144:                return 1 ;
                    145: 
                    146:           strcpy (pcOut, pcIn + 1) ;
                    147:           return 2 ;
                    148:           }
                    149: 
                    150:                /*------------------------------------------------------
                    151:                   Attempt to change directory -- Get current dir if OK
                    152:                  ------------------------------------------------------*/
                    153: 
                    154:      *pcLastSlash = '\0' ;
                    155: 
                    156:      if (DosChDir (pcIn, 0L))
                    157:           return 0 ;
                    158: 
                    159:      DosQCurDir (0, pcOut, &usDirLen) ;
                    160: 
                    161:                /*-------------------------------
                    162:                   Append input filename, if any
                    163:                  -------------------------------*/
                    164: 
                    165:      pcFileOnly = pcLastSlash + 1 ;
                    166: 
                    167:      if (*pcFileOnly == '\0')
                    168:           return 1 ;
                    169: 
                    170:      if (strlen (pcFileOnly) > 12)
                    171:           return 0 ;
                    172: 
                    173:      if (*(pcOut + strlen (pcOut) - 1) != '\\')
                    174:           strcat (pcOut++, "\\") ;
                    175: 
                    176:      strcat (pcOut, pcFileOnly) ;
                    177:      return 2 ;
                    178:      }
                    179: 
                    180: VOID FillDirListBox (HWND hwnd, CHAR *pcCurrentPath)
                    181:      {
                    182:      static CHAR szDrive [] = "  :" ;
                    183:      FILEFINDBUF findbuf ;
                    184:      HDIR        hDir = 1 ;
                    185:      SHORT       sDrive ;
                    186:      USHORT      usDriveNum, usCurPathLen, usSearchCount = 1 ;
                    187:      ULONG       ulDriveMap ;
                    188: 
                    189:      DosQCurDisk (&usDriveNum, &ulDriveMap) ;
                    190:      pcCurrentPath [0] = (CHAR) usDriveNum + '@' ;
                    191:      pcCurrentPath [1] = ':' ;
                    192:      pcCurrentPath [2] = '\\' ;
                    193:      usCurPathLen = 64 ;
                    194:      DosQCurDir (0, pcCurrentPath + 3, &usCurPathLen) ;
                    195: 
                    196:      WinSetWindowText (WinWindowFromID (hwnd, IDD_PATH), pcCurrentPath) ;
                    197: 
                    198:      WinSendDlgItemMsg (hwnd, IDD_DIRLIST, LM_DELETEALL, NULL, NULL) ;
                    199: 
                    200:      for (sDrive = 0 ; sDrive < 26 ; sDrive++)
                    201:           if (ulDriveMap & 1L << sDrive)
                    202:                {
                    203:                szDrive [1] = (CHAR) sDrive + 'A' ;
                    204: 
                    205:                WinSendDlgItemMsg (hwnd, IDD_DIRLIST, LM_INSERTITEM,
                    206:                                   MPFROM2SHORT (LIT_END, 0),
                    207:                                   MPFROMP (szDrive)) ;
                    208:                }
                    209: 
                    210:      DosFindFirst ("*.*", &hDir, 0x0017, &findbuf, sizeof findbuf,
                    211:                               &usSearchCount, 0L) ;
                    212:      while (usSearchCount)
                    213:           {
                    214:           if (findbuf.attrFile & 0x0010 &&
                    215:                     (findbuf.achName [0] != '.' || findbuf.achName [1]))
                    216:                
                    217:                WinSendDlgItemMsg (hwnd, IDD_DIRLIST, LM_INSERTITEM,
                    218:                                   MPFROM2SHORT (LIT_SORTASCENDING, 0),
                    219:                                   MPFROMP (findbuf.achName)) ;
                    220: 
                    221:           DosFindNext (hDir, &findbuf, sizeof findbuf, &usSearchCount) ;
                    222:           }
                    223:      }
                    224: 
                    225: VOID FillFileListBox (HWND hwnd)
                    226:      {
                    227:      FILEFINDBUF findbuf ;
                    228:      HDIR        hDir = 1 ;
                    229:      USHORT      usSearchCount = 1 ;
                    230: 
                    231:      WinSendDlgItemMsg (hwnd, IDD_FILELIST, LM_DELETEALL, NULL, NULL) ;
                    232: 
                    233:      DosFindFirst ("*.*", &hDir, 0x0007, &findbuf, sizeof findbuf,
                    234:                               &usSearchCount, 0L) ;
                    235:      while (usSearchCount)
                    236:           {
                    237:           WinSendDlgItemMsg (hwnd, IDD_FILELIST, LM_INSERTITEM,
                    238:                              MPFROM2SHORT (LIT_SORTASCENDING, 0),
                    239:                              MPFROMP (findbuf.achName)) ;
                    240: 
                    241:           DosFindNext (hDir, &findbuf, sizeof findbuf, &usSearchCount) ;
                    242:           }
                    243:      }
                    244: 
                    245: MRESULT EXPENTRY OpenDlgProc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
                    246:      {
                    247:      static CHAR szCurrentPath [80], szBuffer [80] ;
                    248:      SHORT       sSelect ;
                    249: 
                    250:      switch (msg)
                    251:           {
                    252:           case WM_INITDLG:
                    253:                FillDirListBox (hwnd, szCurrentPath) ;
                    254:                FillFileListBox (hwnd) ;
                    255: 
                    256:                WinSendDlgItemMsg (hwnd, IDD_FILEEDIT, EM_SETTEXTLIMIT,
                    257:                                         MPFROM2SHORT (80, 0), NULL) ;
                    258:                return 0 ;
                    259: 
                    260:           case WM_CONTROL:
                    261:                if (SHORT1FROMMP (mp1) == IDD_DIRLIST ||
                    262:                    SHORT1FROMMP (mp1) == IDD_FILELIST)
                    263:                     {
                    264:                     sSelect = (USHORT) WinSendDlgItemMsg (hwnd,
                    265:                                                   SHORT1FROMMP (mp1),
                    266:                                                   LM_QUERYSELECTION, 0L, 0L) ;
                    267: 
                    268:                     WinSendDlgItemMsg (hwnd, SHORT1FROMMP (mp1),
                    269:                                        LM_QUERYITEMTEXT,
                    270:                                        MPFROM2SHORT (sSelect, sizeof szBuffer),
                    271:                                        MPFROMP (szBuffer)) ;
                    272:                     }
                    273: 
                    274:                switch (SHORT1FROMMP (mp1))        /* Control ID */
                    275:                     {
                    276:                     case IDD_DIRLIST:
                    277:                          switch (SHORT2FROMMP (mp1))   /* notification code */
                    278:                               {
                    279:                               case LN_ENTER:
                    280:                                    if (szBuffer [0] == ' ')
                    281:                                         DosSelectDisk (szBuffer [1] - '@') ;
                    282:                                    else
                    283:                                        DosChDir (szBuffer, 0L) ;
                    284: 
                    285:                                    FillDirListBox (hwnd, szCurrentPath) ;
                    286:                                    FillFileListBox (hwnd) ;
                    287: 
                    288:                                    WinSetDlgItemText (hwnd, IDD_FILEEDIT, "") ;
                    289:                                    return 0 ;
                    290:                               }
                    291:                          break ;
                    292: 
                    293:                     case IDD_FILELIST:
                    294:                          switch (SHORT2FROMMP (mp1))   /* notification code */
                    295:                               {
                    296:                               case LN_SELECT:
                    297:                                    WinSetDlgItemText (hwnd, IDD_FILEEDIT,
                    298:                                                       szBuffer) ;
                    299:                                    return 0 ;
                    300: 
                    301:                               case LN_ENTER:
                    302:                                    ParseFileName (szFileName, szBuffer) ;
                    303:                                    WinDismissDlg (hwnd, TRUE) ;
                    304:                                    return 0 ;
                    305:                               }
                    306:                          break ;
                    307:                     }
                    308:                break ;
                    309: 
                    310:           case WM_COMMAND:
                    311:                switch (COMMANDMSG(&msg)->cmd)
                    312:                     {
                    313:                     case DID_OK:
                    314:                          WinQueryDlgItemText (hwnd, IDD_FILEEDIT,
                    315:                                               sizeof szBuffer, szBuffer) ;
                    316: 
                    317:                          switch (ParseFileName (szCurrentPath, szBuffer))
                    318:                               {
                    319:                               case 0:
                    320:                                    WinAlarm (HWND_DESKTOP, WA_ERROR) ;
                    321:                                    FillDirListBox (hwnd, szCurrentPath) ;
                    322:                                    FillFileListBox (hwnd) ;
                    323:                                    return 0 ;
                    324: 
                    325:                               case 1:
                    326:                                    FillDirListBox (hwnd, szCurrentPath) ;
                    327:                                    FillFileListBox (hwnd) ;
                    328:                                    WinSetDlgItemText (hwnd, IDD_FILEEDIT, "") ;
                    329:                                    return 0 ;
                    330: 
                    331:                               case 2:
                    332:                                    strcpy (szFileName, szCurrentPath) ;
                    333:                                    WinDismissDlg (hwnd, TRUE) ;
                    334:                                    return 0 ;
                    335:                               }
                    336:                          break ;
                    337: 
                    338:                     case DID_CANCEL:
                    339:                          WinDismissDlg (hwnd, FALSE) ;
                    340:                          return 0 ;
                    341:                     }
                    342:                break ;
                    343:           }
                    344:      return WinDefDlgProc (hwnd, msg, mp1, mp2) ;
                    345:      }
                    346: 
                    347: MRESULT EXPENTRY AboutDlgProc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
                    348:      {
                    349:      switch (msg)
                    350:           {
                    351:           case WM_COMMAND:
                    352:                switch (COMMANDMSG(&msg)->cmd)
                    353:                     {
                    354:                     case DID_OK:
                    355:                     case DID_CANCEL:
                    356:                          WinDismissDlg (hwnd, TRUE) ;
                    357:                          return 0 ;
                    358:                     }
                    359:                break ;
                    360:           }
                    361:      return WinDefDlgProc (hwnd, msg, mp1, mp2) ;
                    362:      }
                    363: 
                    364: MRESULT EXPENTRY ClientWndProc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
                    365:      {
                    366:      static CHAR  szErrorMsg [] = "File not found or could not be opened" ;
                    367:      static SHORT cxClient, cyClient, cxChar, cyChar, cyDesc ;
                    368:      CHAR         *pcReadBuffer ;
                    369:      FILE         *fileInput ;
                    370:      FONTMETRICS  fm ;
                    371:      HPS          hps ;
                    372:      POINTL       ptl ;
                    373:      SHORT        sLength ;
                    374: 
                    375:      switch (msg)
                    376:           {
                    377:           case WM_CREATE:
                    378:                hps = WinGetPS (hwnd) ;
                    379:                GpiQueryFontMetrics (hps, (LONG) sizeof fm, &fm) ;
                    380:                cxChar = (SHORT) fm.lAveCharWidth ;
                    381:                cyChar = (SHORT) fm.lMaxBaselineExt ;
                    382:                cyDesc = (SHORT) fm.lMaxDescender ;
                    383:                WinReleasePS (hps) ;
                    384:                return 0 ;
                    385:           
                    386:           case WM_SIZE:
                    387:                cxClient = SHORT1FROMMP (mp2) ;
                    388:                cyClient = SHORT2FROMMP (mp2) ;
                    389:                return 0 ;
                    390: 
                    391:           case WM_COMMAND:
                    392:                switch (COMMANDMSG(&msg)->cmd)
                    393:                     {
                    394:                     case IDM_OPEN:
                    395:                          if (WinDlgBox (HWND_DESKTOP, hwnd, OpenDlgProc,
                    396:                                         NULL, IDD_OPEN, NULL))
                    397:                               WinInvalidateRect (hwnd, NULL, FALSE) ;
                    398:                          return 0 ;
                    399: 
                    400:                     case IDM_ABOUT:
                    401:                          WinDlgBox (HWND_DESKTOP, hwnd, AboutDlgProc,
                    402:                                     NULL, IDD_ABOUT, NULL) ;
                    403:                          return 0 ;
                    404:                     }
                    405:                break ;
                    406: 
                    407:           case WM_PAINT:
                    408:                hps = WinBeginPaint (hwnd, NULL, NULL) ;
                    409: 
                    410:                GpiErase (hps) ;
                    411: 
                    412:                if (szFileName [0] != '\0')
                    413:                     {
                    414:                     ptl.x = cxChar ;
                    415:                     ptl.y = cyClient - cyChar + cyDesc ;
                    416: 
                    417:                     GpiCharStringAt (hps, &ptl, (LONG) strlen (szFileName),
                    418:                                                 szFileName) ;
                    419:                     ptl.y -= cyChar ;
                    420:                                 
                    421:                     if (fileInput = fopen (szFileName, "r"))
                    422:                          {
                    423:                          pcReadBuffer = malloc (cxClient / cxChar) ;
                    424: 
                    425:                          while ((ptl.y -= cyChar) > 0)
                    426:                               {
                    427:                               if (NULL == fgets (pcReadBuffer,
                    428:                                                  cxClient / cxChar - 2,
                    429:                                                  fileInput))
                    430:                                    return 0 ;
                    431: 
                    432:                               sLength = strlen (pcReadBuffer) ;
                    433: 
                    434:                               if (pcReadBuffer [sLength - 1] == '\n')
                    435:                                    sLength-- ;
                    436: 
                    437:                               if (sLength > 0)
                    438:                                    GpiCharStringAt (hps, &ptl, (LONG) sLength,
                    439:                                                     pcReadBuffer) ;
                    440:                               }
                    441:                          free (pcReadBuffer) ;
                    442:                          fclose (fileInput) ;
                    443:                          }
                    444:                     else
                    445:                          {
                    446:                          ptl.y -= cyChar ;
                    447: 
                    448:                          GpiCharStringAt (hps, &ptl,
                    449:                                           (LONG) strlen (szErrorMsg),
                    450:                                           szErrorMsg) ;
                    451:                          }
                    452:                     }
                    453:                WinEndPaint (hps) ;
                    454:                return 0 ;
                    455:           }
                    456:      return WinDefWindowProc (hwnd, msg, mp1, mp2) ;
                    457:      }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.