home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 24 / CD_ASCQ_24_0995.iso / vrac / dflt20.zip / DIRECT.C < prev    next >
C/C++ Source or Header  |  1994-07-20  |  5KB  |  198 lines

  1. /* ---------- direct.c --------- */
  2.  
  3. #include "dflat.h"
  4.  
  5. #ifndef BCPP
  6. #define FA_DIREC 0x10
  7. #endif
  8.  
  9. static char path[MAXPATH];
  10. static char drive[MAXDRIVE] = " :";
  11. static char dir[MAXDIR];
  12. static char name[MAXFILE];
  13. static char ext[MAXEXT];
  14.  
  15. /* ----- Create unambiguous path from file spec, filling in the
  16.      drive and directory if incomplete. Optionally change to
  17.      the new drive and subdirectory ------ */
  18. void CreatePath(char *spath,char *fspec,int InclName,int Change)
  19. {
  20.     int cm = 0;
  21.     unsigned currdrive;
  22.     char currdir[MAXPATH+1];
  23.     char *cp;
  24.  
  25.     if (!Change)    {
  26.         /* ---- save the current drive and subdirectory ---- */
  27.         currdrive = getdisk();
  28.         getcwd(currdir, sizeof currdir);
  29.         memmove(currdir, currdir+2, strlen(currdir+1));
  30.         cp = currdir+strlen(currdir)-1;
  31.         if (*cp == '\\')
  32.             *cp = '\0';
  33.     }
  34.     *drive = *dir = *name = *ext = '\0';
  35.     fnsplit(fspec, drive, dir, name, ext);
  36.     if (!InclName)
  37.         *name = *ext = '\0';
  38.     *drive = toupper(*drive);
  39.     if (*ext)
  40.         cm |= EXTENSION;
  41.     if (InclName && *name)
  42.         cm |= FILENAME;
  43.     if (*dir)
  44.         cm |= DIRECTORY;
  45.     if (*drive)
  46.         cm |= DRIVE;
  47.     if (cm & DRIVE)
  48.         setdisk(*drive - 'A');
  49.     else     {
  50.         *drive = getdisk();
  51.         *drive += 'A';
  52.     }
  53.     if (cm & DIRECTORY)    {
  54.         cp = dir+strlen(dir)-1;
  55.         if (*cp == '\\')
  56.             *cp = '\0';
  57.         chdir(dir);
  58.     }
  59.     getcwd(dir, sizeof dir);
  60.     memmove(dir, dir+2, strlen(dir+1));
  61.     if (InclName)    {
  62.         if (!(cm & FILENAME))
  63.             strcpy(name, "*");
  64.         if (!(cm & EXTENSION) && strchr(fspec, '.') != NULL)
  65.             strcpy(ext, ".*");
  66.     }
  67.     else
  68.         *name = *ext = '\0';
  69.     if (dir[strlen(dir)-1] != '\\')
  70.         strcat(dir, "\\");
  71.     if (spath != NULL)
  72.         fnmerge(spath, drive, dir, name, ext);
  73.     if (!Change)    {
  74.         setdisk(currdrive);
  75.         chdir(currdir);
  76.     }
  77. }
  78.  
  79. static int dircmp(const void *c1, const void *c2)
  80. {
  81.     return stricmp(*(char **)c1, *(char **)c2);
  82. }
  83.  
  84. static BOOL BuildList(WINDOW wnd, char *fspec, BOOL dirs)
  85. {
  86.     int ax, i = 0, criterr = 1;
  87.     struct ffblk ff;
  88.     CTLWINDOW *ct = FindCommand(wnd->extension,
  89.                             dirs ? ID_DIRECTORY : ID_FILES,LISTBOX);
  90.     WINDOW lwnd;
  91.     char **dirlist = NULL;
  92.  
  93.     if (ct != NULL)    {
  94.         lwnd = ct->wnd;
  95.         SendMessage(lwnd, CLEARTEXT, 0, 0);
  96.  
  97.            while (criterr == 1)    {
  98.                ax = findfirst(fspec, &ff, dirs ? FA_DIREC: 0);
  99.                criterr = TestCriticalError();
  100.            }
  101.            if (criterr)
  102.                return FALSE;
  103.         while (ax == 0)    {
  104.             if (!dirs || (ff.ff_attrib & FA_DIREC) && strcmp(ff.ff_name, "."))    {
  105.                 dirlist = DFrealloc(dirlist, sizeof(char *)*(i+1));
  106.                 dirlist[i] = DFmalloc(strlen(ff.ff_name)+1);
  107.                 strcpy(dirlist[i++], ff.ff_name);
  108.             }
  109.             ax = findnext(&ff);
  110.         }
  111.         if (dirlist != NULL)    {
  112.             int j;
  113.             /* -- sort file or directory list box data -- */
  114.             qsort(dirlist, i, sizeof(void *), dircmp);
  115.             /* ---- send sorted list to list box ---- */
  116.             for (j = 0; j < i; j++)    {
  117.                 SendMessage(lwnd,ADDTEXT,(PARAM)dirlist[j],0);
  118.                 free(dirlist[j]);
  119.             }
  120.             free(dirlist);
  121.         }
  122.         SendMessage(lwnd, SHOW_WINDOW, 0, 0);
  123.     }
  124.     return TRUE;
  125. }
  126.  
  127. BOOL BuildFileList(WINDOW wnd, char *fspec)
  128. {
  129.     return BuildList(wnd, fspec, FALSE);
  130. }
  131.  
  132. void BuildDirectoryList(WINDOW wnd)
  133. {
  134.     BuildList(wnd, "*.*", TRUE);
  135. }
  136.  
  137. void BuildDriveList(WINDOW wnd)
  138. {
  139.     CTLWINDOW *ct = FindCommand(wnd->extension, ID_DRIVE,LISTBOX);
  140.     if (ct != NULL)    {
  141.         union REGS regs;
  142.         char drname[15];
  143.         unsigned int cd, dr;
  144.         WINDOW lwnd = ct->wnd;
  145.         SendMessage(lwnd, CLEARTEXT, 0, 0);
  146.  
  147.         cd = getdisk();
  148.         for (dr = 0; dr < 26; dr++)    {
  149.             unsigned ndr;
  150.             setdisk(dr);
  151.             ndr = getdisk();
  152.             if (ndr == dr)    {
  153.                 /* ----- test for remapped B drive ----- */
  154.                 if (dr == 1)    {
  155.                     regs.x.ax = 0x440e; /* IOCTL func 14 */
  156.                     regs.h.bl = dr+1;
  157.                     int86(DOS, ®s, ®s);
  158.                     if (regs.h.al != 0)
  159.                         continue;
  160.                 }
  161.  
  162.                 sprintf(drname, "%c:", dr+'A');
  163.  
  164.                 /* ---- test for network or RAM disk ---- */
  165.                 regs.x.ax = 0x4409;     /* IOCTL func 9 */
  166.                 regs.h.bl = dr+1;
  167.                 int86(DOS, ®s, ®s);
  168.                 if (!regs.x.cflag)    {
  169.                     if (regs.x.dx & 0x1000)
  170.                         strcat(drname, " (Net)");
  171.                     else if (regs.x.dx == 0x0800)
  172.                         strcat(drname, " (RAM)");
  173.                 }
  174.                 SendMessage(lwnd,ADDTEXT,(PARAM)drname,0);
  175.             }
  176.         }
  177.         SendMessage(lwnd, SHOW_WINDOW, 0, 0);
  178.         setdisk(cd);
  179.     }
  180. }
  181.  
  182. void BuildPathDisplay(WINDOW wnd)
  183. {
  184.     CTLWINDOW *ct = FindCommand(wnd->extension, ID_PATH,TEXT);
  185.     if (ct != NULL)    {
  186.         int len;
  187.         WINDOW lwnd = ct->wnd;
  188.         CreatePath(path, "*.*", FALSE, FALSE);
  189.         len = strlen(path);
  190.         if (len > 3)
  191.             path[len-1] = '\0';
  192.            SendMessage(lwnd,SETTEXT,(PARAM)path,0);
  193.         SendMessage(lwnd, PAINT, 0, 0);
  194.     }
  195. }
  196.  
  197.  
  198.