home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / drdobbs / 1991 / 06 / dflat3 / fileopen.c < prev    next >
Text File  |  1991-04-26  |  5KB  |  231 lines

  1. /* ----------- fileopen.c ------------- */
  2.  
  3.  
  4. #include <string.h>
  5. #ifdef MSC
  6. #include <direct.h>
  7. #else
  8. #include <dir.h>
  9. #endif
  10. #include <dos.h>
  11. #include <ctype.h>
  12. #include "dflat.h"
  13.  
  14. #ifdef INCLUDE_DIALOG_BOXES
  15.  
  16. static int DlgFileOpen(char *, char *, DBOX *);
  17. static int DlgFnOpen(WINDOW, MESSAGE, PARAM, PARAM);
  18. static void InitDlgBox(WINDOW);
  19. static void StripPath(char *);
  20. static int IncompleteFilename(char *);
  21.  
  22. static char OrigSpec[80];
  23. static char FileSpec[80];
  24. char FileName[80];
  25.  
  26. static int Saving;
  27. extern DBOX FileOpen;
  28. extern DBOX SaveAs;
  29.  
  30. /*
  31.  * Dialog Box to select a file to open
  32.  */
  33. int DlgOpenFile(char *Fpath, char *Fname)
  34. {
  35.     return DlgFileOpen(Fpath, Fname, &FileOpen);
  36. }
  37.  
  38. /*
  39.  * Dialog Box to select a file to save as
  40.  */
  41. int DlgSaveAs(char *Fname)
  42. {
  43.     return DlgFileOpen(NULL, Fname, &SaveAs);
  44. }
  45.  
  46. /* --------- generic file open ---------- */
  47. static int DlgFileOpen(char *Fpath, char *Fname, DBOX *db)
  48. {
  49.     int  rtn;
  50.     char savedir[80];
  51.  
  52.     getcwd(savedir, sizeof savedir);
  53.     if (Fpath != NULL)    {
  54.         strncpy(FileSpec, Fpath, sizeof(FileSpec)-1);
  55.         Saving = FALSE;
  56.     }
  57.     else    {
  58.         *FileSpec = '\0';
  59.         Saving = TRUE;
  60.     }
  61.     strcpy(FileName, FileSpec);
  62.     strcpy(OrigSpec, FileSpec);
  63.  
  64.     if ((rtn = DialogBox(NULLWND, db, DlgFnOpen)) != FALSE)
  65.         strcpy(Fname, FileName);
  66.     else
  67.         *Fname = '\0';
  68.  
  69.     setdisk(toupper(*savedir) - 'A');
  70.     chdir(savedir);
  71.  
  72.     return rtn;
  73. }
  74.  
  75. /*
  76.  *  Process dialog box messages
  77.  */
  78. static int DlgFnOpen(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
  79. {
  80.     switch (msg)    {
  81.         case INITIATE_DIALOG:
  82.             InitDlgBox(wnd);
  83.             break;
  84.         case COMMAND:
  85.             switch ((int) p1)    {
  86.                 case ID_FILENAME:
  87.                     if (p2 != ENTERFOCUS)    {
  88.                         /* allow user to modify the file spec */
  89.                         GetItemText(wnd, ID_FILENAME,
  90.                                 FileName, 65);
  91.                         if (IncompleteFilename(FileName) || Saving)    {
  92.                             strcpy(OrigSpec, FileName);
  93.                             StripPath(OrigSpec);
  94.                         }
  95.                         if (p2 != LEAVEFOCUS)
  96.                             SendMessage(wnd, COMMAND, ID_OK, 0);
  97.                     }
  98.                     return TRUE;
  99.                 case ID_OK:
  100.                     GetItemText(wnd, ID_FILENAME,
  101.                             FileName, 65);
  102.                     strcpy(FileSpec, FileName);
  103.                     if (IncompleteFilename(FileName))    {
  104.                         /* no file name yet */
  105.                         InitDlgBox(wnd);
  106.                         PutItemText(wnd, ID_FILENAME, FileSpec);
  107.                         strcpy(OrigSpec, FileSpec);
  108.                         return TRUE;
  109.                     }
  110.                     else    {
  111.                         GetItemText(wnd, ID_PATH, FileName, 65);
  112.                         strcat(FileName, FileSpec);
  113.                     }
  114.                     break;
  115.                 case ID_FILES:
  116.                     switch ((int) p2)    {
  117.                         case LB_SELECTION:
  118.                             /* selected a different filename */
  119.                             GetDlgListText(wnd, FileName,
  120.                                         ID_FILES);
  121.                             PutItemText(wnd, ID_FILENAME,
  122.                                             FileName);
  123.                             break;
  124.                         case LB_CHOOSE:
  125.                             /* chose a file name */
  126.                             GetDlgListText(wnd, FileName,
  127.                                     ID_FILES);
  128.                             SendMessage(wnd, COMMAND, ID_OK, 0);
  129.                             break;
  130.                         default:
  131.                             break;
  132.                     }
  133.                     return TRUE;
  134.                 case ID_DRIVE:
  135.                     switch ((int) p2)    {
  136.                         case ENTERFOCUS:
  137.                             if (Saving)
  138.                                 *FileSpec = '\0';
  139.                             break;
  140.                         case LEAVEFOCUS:
  141.                             if (Saving)
  142.                                 strcpy(FileSpec, FileName);
  143.                             break;
  144.                         case LB_SELECTION:    {
  145.                             char dd[25];
  146.                             /* selected different drive/dir */
  147.                             GetDlgListText(wnd, dd,
  148.                                                 ID_DRIVE);
  149.                             if (*(dd+2) == ':')
  150.                                 *(dd+3) = '\0';
  151.                             else
  152.                                 *(dd+strlen(dd)-1) = '\0';
  153.                             strcpy(FileName, dd+1);
  154.                             if (*(dd+2) != ':' && *OrigSpec != '\\')
  155.                                 strcat(FileName, "\\");
  156.                             strcat(FileName, OrigSpec);
  157.                             if (*(FileName+1) != ':' && *FileName != '.')    {
  158.                                 GetItemText(wnd, ID_PATH, FileSpec, 65);
  159.                                 strcat(FileSpec, FileName);
  160.                             }
  161.                             else 
  162.                                 strcpy(FileSpec, FileName);
  163.                             break;
  164.                         }
  165.                         case LB_CHOOSE:
  166.                             /* chose drive/dir */
  167.                             InitDlgBox(wnd);
  168.                             break;
  169.                         default:
  170.                             break;
  171.                     }
  172.                     PutItemText(wnd, ID_FILENAME, FileSpec);
  173.                     return TRUE;
  174.  
  175.  
  176.                 default:
  177.                     break;
  178.             }
  179.         default:
  180.             break;
  181.     }
  182.     return DefaultWndProc(wnd, msg, p1, p2);
  183. }
  184.  
  185. /*
  186.  *  Initialize the dialog box
  187.  */
  188. static void InitDlgBox(WINDOW wnd)
  189. {
  190.     if (*FileSpec)
  191.         PutItemText(wnd, ID_FILENAME, FileSpec);
  192.     if (DlgDirList(wnd, FileSpec, ID_FILES, ID_PATH, 0))    {
  193.         StripPath(FileSpec);
  194.         DlgDirList(wnd, "*.*", ID_DRIVE, 0, 0xc010);
  195.     }
  196. }
  197.  
  198. /*
  199.  * Strip the drive and path information from a file spec
  200.  */
  201. static void StripPath(char *filespec)
  202. {
  203.     char *cp, *cp1;
  204.  
  205.     cp = strchr(filespec, ':');
  206.     if (cp != NULL)
  207.         cp++;
  208.     else
  209.         cp = filespec;
  210.     while (TRUE)    {
  211.         cp1 = strchr(cp, '\\');
  212.         if (cp1 == NULL)
  213.             break;
  214.         cp = cp1+1;
  215.     }
  216.     strcpy(filespec, cp);
  217. }
  218.  
  219.  
  220. static int IncompleteFilename(char *s)
  221. {
  222.     int lc = strlen(s)-1;
  223.     if (strchr(s, '?') || strchr(s, '*') || !*s)
  224.         return TRUE;
  225.     if (*(s+lc) == ':' || *(s+lc) == '\\')
  226.         return TRUE;
  227.     return FALSE;
  228. }
  229.  
  230. #endif
  231.