home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / mnth0106.zip / Timur / files.c < prev    next >
Text File  |  1992-08-13  |  12KB  |  310 lines

  1. /* FILES.C - File I/O routines & dialog boxes window procedures
  2.  
  3. Copyright (c) 1992 Timur Tabi
  4. Copyright (c) 1992 Fasa Corporation
  5.  
  6. The following trademarks are the property of Fasa Corporation:
  7. BattleTech, CityTech, AeroTech, MechWarrior, BattleMech, and 'Mech.
  8. The use of these trademarks should not be construed as a challenge to these marks.
  9.  
  10. NOTE: This code is adapted from examples in Petzold's "Programming the OS/2 Presentation Manager".
  11.       However, this version is quite buggy.  It will be replaced by the template code provided with
  12.       the OS/2 2.0 Toolkit.  Sorry for the inconvenience.  Next time I'll know better :-)
  13. */
  14.  
  15. #define INCL_WIN
  16. #include <os2.h>
  17. #include <string.h>
  18. #include "dialog.h"
  19. #include "hexes.h"
  20.  
  21. #define PATH_LEN 128
  22.  
  23. FILEFINDBUF3 ffb;
  24. HDIR hdir=HDIR_SYSTEM;
  25. ULONG ulSearchCount=1;
  26. char szFileName[PATH_LEN];
  27. char szCurrentPath[PATH_LEN];
  28. ULONG ulCurPathLen=PATH_LEN;
  29. extern HWND hwndClient;                       // For redrawing the screen after a Load Map
  30.  
  31. #define READ_ATTRS OPEN_FLAGS_NO_CACHE|              /* No need to take up precious cache space */ \
  32.                    OPEN_FLAGS_SEQUENTIAL|            /* One-time read, remember?                */ \
  33.                    OPEN_SHARE_DENYWRITE|             /* We don't want anyone changing it        */ \
  34.                    OPEN_ACCESS_READONLY              // To prevent accidentally writing to it
  35.  
  36. #define WRITE_ATTRS OPEN_FLAGS_NO_CACHE|              /* No need to take up precious cache space */ \
  37.                     OPEN_FLAGS_SEQUENTIAL|            /* One-time write, remember?               */ \
  38.                     OPEN_SHARE_DENYREADWRITE|         /* We don't want anyone touching it        */ \
  39.                     OPEN_ACCESS_WRITEONLY             // That's how we're gonna do it
  40.  
  41. void LoadMap(void) {
  42.   HFILE hfile;
  43.   ULONG ulAction;
  44.   ULONG ulBytesRead;
  45.  
  46.   if (DosOpen(szFileName,&hfile,&ulAction,0,0,OPEN_ACTION_OPEN_IF_EXISTS|OPEN_ACTION_FAIL_IF_NEW,READ_ATTRS,0)) return;
  47.   if (DosRead(hfile,amap,sizeof(amap),&ulBytesRead)) return;
  48.   if (ulBytesRead != sizeof(amap)) return;
  49.   DosClose(hfile);
  50. }
  51.  
  52. void SaveMap(void) {
  53.   HFILE hfile;
  54.   ULONG ulAction;
  55.   ULONG ulBytesRead;
  56.  
  57.   if (DosOpen(szFileName,&hfile,&ulAction,sizeof(amap),0,OPEN_ACTION_CREATE_IF_NEW|OPEN_ACTION_REPLACE_IF_EXISTS,WRITE_ATTRS,0)) return;
  58.   if (DosWrite(hfile,amap,sizeof(amap),&ulBytesRead)) return;
  59.   if (ulBytesRead != sizeof(amap)) return;
  60.   DosClose(hfile);
  61. }
  62.  
  63.  
  64. #define DIR_BOX_ATTRS (MUST_HAVE_DIRECTORY|FILE_ARCHIVED|FILE_HIDDEN|FILE_READONLY)
  65. #define FILE_BOX_ATTRS (FILE_ARCHIVED|FILE_HIDDEN|FILE_READONLY)
  66.  
  67. void FillDirListBox(HWND hwnd) {
  68.   static char *szDrive=" :";                            // for adding drives to the list box
  69.   static ULONG ulDriveNum, ulDriveMap;
  70.   BYTE bDrive;
  71.  
  72. //
  73.   DosQueryCurrentDisk(&ulDriveNum,&ulDriveMap);
  74.   szCurrentPath[0]=(char) (ulDriveNum + '@');
  75.   szCurrentPath[1]=':';
  76.   szCurrentPath[2]='\\';
  77.   ulCurPathLen=PATH_LEN;
  78.   DosQueryCurrentDir(0,szCurrentPath+3,&ulCurPathLen);
  79.   WinSetDlgItemText(hwnd,IDD_PATH,szCurrentPath);               // Show current path on dialog box
  80.  
  81.   WinSendDlgItemMsg(hwnd,IDD_DIRLIST,LM_DELETEALL,NULL,NULL);   // Erase all entries
  82.  
  83. // Add the list of directories in the current dir to the list box
  84.   ulSearchCount=1;
  85.   if (!DosFindFirst("*",&hdir,DIR_BOX_ATTRS,&ffb,sizeof(ffb),&ulSearchCount,FIL_STANDARD))
  86.     do
  87.       WinSendDlgItemMsg(hwnd,IDD_DIRLIST,LM_INSERTITEM,MPFROM2SHORT(LIT_SORTASCENDING,0),MPFROMP(ffb.achName));
  88.     while (!DosFindNext(hdir,&ffb,sizeof(ffb),&ulSearchCount));
  89.  
  90. // Add the list of available drives to the list box
  91.   for (bDrive=0; bDrive<26; bDrive++)
  92.     if (ulDriveMap & 1L << bDrive) {
  93.       szDrive[0]=(char) (bDrive + 'A');
  94.       WinSendDlgItemMsg(hwnd,IDD_DIRLIST,LM_INSERTITEM,MPFROM2SHORT(LIT_END,0),MPFROMP(szDrive));
  95.     }
  96. }
  97.  
  98. void FillFileListBox(HWND hwnd) {
  99.   WinSendDlgItemMsg(hwnd,IDD_FILELIST,LM_DELETEALL,NULL,NULL);
  100.  
  101.   ulSearchCount=1;
  102.   if (!DosFindFirst("*",&hdir,FILE_BOX_ATTRS,&ffb,sizeof(ffb),&ulSearchCount,FIL_STANDARD))
  103.     do
  104.       WinSendDlgItemMsg(hwnd,IDD_FILELIST,LM_INSERTITEM,MPFROM2SHORT(LIT_SORTASCENDING,0),MPFROMP(ffb.achName));
  105.     while (!DosFindNext(hdir,&ffb,sizeof(ffb),&ulSearchCount));
  106. }
  107.  
  108. SHORT ParseFileName(char *szOut, char *szIn) {
  109. /*
  110. This routine parses szIn and returns:
  111.         0 - invalid string
  112.         1 - new drive/directory only
  113.         2 - valid filename
  114. szOut contains the new directory and/or filename.
  115. */
  116.   char *pcLastSlash,*pcFileOnly;
  117.   ULONG ulDriveMap,ulDriveNum,ulDirLen = PATH_LEN;
  118.  
  119.   if (*szIn == 0) return 1;
  120.  
  121. // If szIn contains a drive specifier, change the current disk first
  122.   if (szIn[1] == ':') {
  123.     if (DosSelectDisk((ULONG) (*szIn - '@'))) return 0;
  124.     szIn += 2;                                // skip over drive specifier
  125.   }
  126.  
  127. // Get the current disk and drive map data.  The drive map data is ignored
  128.   DosQueryCurrentDisk(&ulDriveNum,&ulDriveMap);
  129.   *szOut++ = (char) (ulDriveNum + '@');
  130.   *szOut++ = ':';
  131.   *szOut++ = '\\';
  132.  
  133.   if (*szIn == 0) return 1;
  134.  
  135.   if (NULL == (pcLastSlash = strrchr(szIn,'\\'))) {
  136.     if (!DosSetCurrentDir(szIn)) return 1;
  137.     DosQueryCurrentDir(0,szOut,&ulDirLen);
  138.     if (*(szOut + strlen(szOut) - 1) != '\\') strcat(szOut++,"\\");
  139.     strcat(szOut,szIn);
  140.     return 2;
  141.   }
  142.  
  143.   if (szIn == pcLastSlash) {
  144.     DosSetCurrentDir("\\");
  145.     if (szIn[1] == 0) return 1;                   // if szIn is only a backslash, change to root
  146.     strcpy(szOut,szIn+1);
  147.     return 2;
  148.   }
  149.  
  150.   *pcLastSlash=0;
  151.   if (DosSetCurrentDir(szIn)) return 0;
  152.   DosQueryCurrentDir(0,szOut,&ulDirLen);
  153.   pcFileOnly=pcLastSlash+1;
  154.   if (*pcFileOnly == 0) return 1;
  155.   if (*(szOut + strlen(szOut) - 1) != '\\') strcat(szOut++,"\\");
  156.   strcat(szOut,pcFileOnly);
  157.   return 2;
  158. }
  159.  
  160. MRESULT EXPENTRY LoadDlgProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2) {
  161.   static char szBuffer[80];
  162.   USHORT usSelect;
  163.  
  164.   switch (msg) {
  165.     case WM_INITDLG:                            // Fill the list boxes
  166.       FillDirListBox(hwnd);
  167.       FillFileListBox(hwnd);
  168. //    WinSendDlgItemMsg(hwnd,IDD_FILEEDIT,EM_SETTEXTLIMIT,MPFROM2SHORT(80,0),NULL);
  169.       return 0;
  170.  
  171.     case WM_CONTROL:                            // User has selected a file/dir
  172.       if (SHORT1FROMMP(mp1) == IDD_DIRLIST || SHORT1FROMMP(mp1) == IDD_FILELIST) {
  173. // The following line returns -1, indicating an error code.  The result is that the user cannot
  174. // select the file from the list box.
  175.         usSelect = (USHORT) WinSendDlgItemMsg(hwnd,SHORT1FROMMP(mp1),LM_QUERYSELECTION,0L,0L);
  176.         WinSendDlgItemMsg(hwnd,SHORT1FROMMP(mp1),LM_QUERYITEMTEXT,MPFROM2SHORT(usSelect,sizeof(szBuffer)),MPFROMP(szBuffer));
  177.       }
  178.       switch (SHORT1FROMMP(mp1)) {              // User has selected a directory
  179.         case IDD_DIRLIST:
  180.           if (SHORT2FROMMP(mp1) == LN_ENTER) {
  181.             if (*szBuffer == ' ')
  182.               DosSetDefaultDisk((ULONG) (szBuffer[1]-'@'));
  183.             else
  184.               DosSetCurrentDir(szBuffer);
  185.             FillDirListBox(hwnd);
  186.             FillFileListBox(hwnd);
  187.             WinSetDlgItemText(hwnd,IDD_FILEEDIT,"");    // Erase the filename entry-field
  188.             return 0;
  189.           }
  190.           break; // case IDD_DIRLIST
  191.         case IDD_FILELIST:                      // User has selected a file
  192.           switch (SHORT2FROMMP(mp1)) {
  193.             case LN_SELECT:
  194.               WinSetDlgItemText(hwnd,IDD_FILELIST,szBuffer);
  195.               return 0;
  196.             case LN_ENTER:
  197.               ParseFileName(szFileName,szBuffer);
  198.               WinDismissDlg(hwnd,TRUE);
  199.               return 0;
  200.           }
  201.           break;        // case IDD_FILELIST
  202.       }                 // switch (SHORT1FROMMP(mp1))
  203.       break;            // case WM_CONTROL
  204.     case WM_COMMAND:
  205.       switch (SHORT1FROMMP(mp1)) {
  206.         case DID_OK:
  207.           WinQueryDlgItemText(hwnd,IDD_FILEEDIT,sizeof(szBuffer),szBuffer);
  208.           switch (ParseFileName(szCurrentPath,szBuffer)) {
  209.             case 0:
  210.               WinAlarm(HWND_DESKTOP,WA_ERROR);
  211.               FillDirListBox(hwnd);
  212.               FillFileListBox(hwnd);
  213.               return 0;
  214.             case 1:
  215.               FillDirListBox(hwnd);
  216.               FillFileListBox(hwnd);
  217.               WinSetDlgItemText(hwnd,IDD_FILEEDIT,"");
  218.               return 0;
  219.             case 2:
  220.               strcpy(szFileName,szCurrentPath);
  221.               WinDismissDlg(hwnd,TRUE);
  222.               LoadMap();
  223.               WinPostMsg(hwndClient,WM_PAINT,0,0);              // Show the new map
  224.               return 0;
  225.           }
  226.           break;
  227.         case DID_CANCEL:
  228.           WinDismissDlg(hwnd,FALSE);
  229.           return 0;
  230.       }                 // switch (SHORT1FROMMP(mp1))
  231.       break;            // case WM_COMMAND
  232.   }                     // switch (msg)
  233.   return WinDefDlgProc(hwnd,msg,mp1,mp2);
  234. }
  235.  
  236. MRESULT EXPENTRY SaveDlgProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2) {
  237.   static char szBuffer[80];
  238.   USHORT usSelect;
  239.  
  240.   switch (msg) {
  241.     case WM_INITDLG:                            // Fill the list boxes
  242.       FillDirListBox(hwnd);
  243.       FillFileListBox(hwnd);
  244. //    WinSendDlgItemMsg(hwnd,IDD_FILEEDIT,EM_SETTEXTLIMIT,MPFROM2SHORT(80,0),NULL);
  245.       return 0;
  246.  
  247.     case WM_CONTROL:                            // User has selected a file/dir
  248.       if (SHORT1FROMMP(mp1) == IDD_DIRLIST || SHORT1FROMMP(mp1) == IDD_FILELIST) {
  249.         usSelect = (USHORT) WinSendDlgItemMsg(hwnd,SHORT1FROMMP(mp1),LM_QUERYSELECTION,0L,0L);
  250. // The following line returns -1, indicating an error code.  The result is that the user cannot
  251. // select the file from the list box.
  252.         WinSendDlgItemMsg(hwnd,SHORT1FROMMP(mp1),LM_QUERYITEMTEXT,MPFROM2SHORT(usSelect,sizeof(szBuffer)),MPFROMP(szBuffer));
  253.       }
  254.       switch (SHORT1FROMMP(mp1)) {              // User has selected a directory
  255.         case IDD_DIRLIST:
  256.           if (SHORT2FROMMP(mp1) == LN_ENTER) {
  257.             if (*szBuffer == ' ')
  258.               DosSetDefaultDisk((ULONG) (szBuffer[1]-'@'));
  259.             else
  260.               DosSetCurrentDir(szBuffer);
  261.             FillDirListBox(hwnd);
  262.             FillFileListBox(hwnd);
  263.             WinSetDlgItemText(hwnd,IDD_FILEEDIT,"");
  264.             return 0;
  265.           }
  266.           break; // case IDD_DIRLIST
  267.         case IDD_FILELIST:                      // User has selected a file
  268.           switch (SHORT2FROMMP(mp1)) {
  269.             case LN_SELECT:
  270.               WinSetDlgItemText(hwnd,IDD_FILELIST,szBuffer);
  271.               return 0;
  272.             case LN_ENTER:
  273.               ParseFileName(szFileName,szBuffer);
  274.               WinDismissDlg(hwnd,TRUE);
  275.               return 0;
  276.           }
  277.           break;        // case IDD_FILELIST
  278.       }                 // switch (SHORT1FROMMP(mp1))
  279.       break;            // case WM_CONTROL
  280.     case WM_COMMAND:
  281.       switch (SHORT1FROMMP(mp1)) {
  282.         case DID_OK:
  283.           WinQueryDlgItemText(hwnd,IDD_FILEEDIT,sizeof(szBuffer),szBuffer);
  284.           switch (ParseFileName(szCurrentPath,szBuffer)) {
  285.             case 0:
  286.               WinAlarm(HWND_DESKTOP,WA_ERROR);
  287.               FillDirListBox(hwnd);
  288.               FillFileListBox(hwnd);
  289.               return 0;
  290.             case 1:
  291.               FillDirListBox(hwnd);
  292.               FillFileListBox(hwnd);
  293.               WinSetDlgItemText(hwnd,IDD_FILEEDIT,"");
  294.               return 0;
  295.             case 2:
  296.               strcpy(szFileName,szCurrentPath);
  297.               WinDismissDlg(hwnd,TRUE);
  298.               SaveMap();
  299.               return 0;
  300.           }
  301.           break;
  302.         case DID_CANCEL:
  303.           WinDismissDlg(hwnd,FALSE);
  304.           return 0;
  305.       }                 // switch (SHORT1FROMMP(mp1))
  306.       break;            // case WM_COMMAND
  307.   }                     // switch (msg)
  308.   return WinDefDlgProc(hwnd,msg,mp1,mp2);
  309. }
  310.