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

  1. /* ----------- fileopen.c ------------- */
  2.  
  3. #include <string.h>
  4. #ifdef MSC
  5. #include <direct.h>
  6. #else
  7. #include <dir.h>
  8. #endif
  9. #include <dos.h>
  10. #include <ctype.h>
  11. #include "dflat.h"
  12.  
  13. #ifdef INCLUDE_DIALOG_BOXES
  14.  
  15. static int DlgFileOpen(char *, char *, DBOX *);
  16. static int DlgFnOpen(WINDOW, MESSAGE, PARAM, PARAM);
  17. static void InitDlgBox(WINDOW);
  18. static void StripPath(char *);
  19. static int IncompleteFilename(char *);
  20.  
  21. static char OrigSpec[80];
  22. static char FileSpec[80];
  23. char FileName[80];
  24.  
  25. static int Saving;
  26. extern DBOX FileOpen;
  27. extern DBOX SaveAs;
  28.  
  29. /*
  30.  * Dialog Box to select a file to open
  31.  */
  32. int DlgOpenFile(char *Fpath, char *Fname)
  33. {
  34.     return DlgFileOpen(Fpath, Fname, &FileOpen);
  35. }
  36.  
  37. /*
  38.  * Dialog Box to select a file to save as
  39.  */
  40. int DlgSaveAs(char *Fname)
  41. {
  42.     return DlgFileOpen(NULL, Fname, &SaveAs);
  43. }
  44.  
  45. /* --------- generic file open ---------- */
  46. static int DlgFileOpen(char *Fpath, char *Fname, DBOX *db)
  47. {
  48.     int  rtn;
  49.     char savedir[80];
  50.  
  51.     getcwd(savedir, sizeof savedir);
  52.     if (Fpath != NULL)    {
  53.         strncpy(FileSpec, Fpath, sizeof(FileSpec)-1);
  54.         Saving = FALSE;
  55.     }
  56.     else    {
  57.         *FileSpec = '\0';
  58.         Saving = TRUE;
  59.     }
  60.     strcpy(FileName, FileSpec);
  61.     strcpy(OrigSpec, FileSpec);
  62.  
  63.     if ((rtn = DialogBox(NULLWND, db, TRUE, DlgFnOpen)) != FALSE)
  64.         strcpy(Fname, FileName);
  65.     else
  66.         *Fname = '\0';
  67.  
  68.     setdisk(toupper(*savedir) - 'A');
  69.     chdir(savedir);
  70.  
  71.     return rtn;
  72. }
  73.  
  74. /*
  75.  *  Process dialog box messages
  76.  */
  77. static int DlgFnOpen(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
  78. {
  79.     switch (msg)    {
  80.         case INITIATE_DIALOG:
  81.             InitDlgBox(wnd);
  82.             break;
  83.         case COMMAND:
  84.             switch ((int) p1)    {
  85.                 case ID_FILENAME:
  86.                     if (p2 != ENTERFOCUS)    {
  87.                         /* allow user to modify the file spec */
  88.                         GetItemText(wnd, ID_FILENAME,
  89.                                 FileName, 65);
  90.                         if (IncompleteFilename(FileName) || Saving)    {
  91.                             strcpy(OrigSpec, FileName);
  92.                             StripPath(OrigSpec);
  93.                         }
  94.                         if (p2 != LEAVEFOCUS)
  95.                             SendMessage(wnd, COMMAND, ID_OK, 0);
  96.                     }
  97.                     return TRUE;
  98.                 case ID_OK:
  99.                     GetItemText(wnd, ID_FILENAME,
  100.                             FileName, 65);
  101.                     strcpy(FileSpec, FileName);
  102.                     if (IncompleteFilename(FileName))    {
  103.                         /* no file name yet */
  104.                         InitDlgBox(wnd);
  105.                         strcpy(OrigSpec, FileSpec);
  106.                         return TRUE;
  107.                     }
  108.                     else    {
  109.                         GetItemText(wnd, ID_PATH, FileName, 65);
  110.                         strcat(FileName, FileSpec);
  111.                     }
  112.                     break;
  113.                 case ID_FILES:
  114.                     switch ((int) p2)    {
  115.                         case LB_SELECTION:
  116.                             /* selected a different filename */
  117.                             GetDlgListText(wnd, FileName,
  118.                                         ID_FILES);
  119.                             PutItemText(wnd, ID_FILENAME,
  120.                                             FileName);
  121.                             break;
  122.                         case LB_CHOOSE:
  123.                             /* chose a file name */
  124.                             GetDlgListText(wnd, FileName,
  125.                                     ID_FILES);
  126.                             SendMessage(wnd, COMMAND, ID_OK, 0);
  127.                             break;
  128.                         default:
  129.                             break;
  130.                     }
  131.                     return TRUE;
  132.                 case ID_DRIVE:
  133.                     switch ((int) p2)    {
  134.                         case ENTERFOCUS:
  135.                             if (Saving)
  136.                                 *FileSpec = '\0';
  137.                             break;
  138.                         case LEAVEFOCUS:
  139.                             if (Saving)
  140.                                 strcpy(FileSpec, FileName);
  141.                             break;
  142.                         case LB_SELECTION:    {
  143.                             char dd[25];
  144.                             /* selected different drive/dir */
  145.                             GetDlgListText(wnd, dd,
  146.                                                 ID_DRIVE);
  147.                             if (*(dd+2) == ':')
  148.                                 *(dd+3) = '\0';
  149.                             else
  150.                                 *(dd+strlen(dd)-1) = '\0';
  151.                             strcpy(FileName, dd+1);
  152.                             if (*(dd+2) != ':' && *OrigSpec != '\\')
  153.                                 strcat(FileName, "\\");
  154.                             strcat(FileName, OrigSpec);
  155.                             if (*(FileName+1) != ':' && *FileName != '.')    {
  156.                                 GetItemText(wnd, ID_PATH, FileSpec, 65);
  157.                                 strcat(FileSpec, FileName);
  158.                             }
  159.                             else 
  160.                                 strcpy(FileSpec, FileName);
  161.                             break;
  162.                         }
  163.                         case LB_CHOOSE:
  164.                             /* chose drive/dir */
  165.                             if (Saving)
  166.                                 PutItemText(wnd, ID_FILENAME, "");
  167.                             InitDlgBox(wnd);
  168.                             return TRUE;
  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 && !Saving)
  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.