home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / TURBOPAS / TREEDIR.ZIP / TREEDIR.PAS
Encoding:
Pascal/Delphi Source File  |  1985-12-28  |  4.9 KB  |  187 lines

  1. { routine for tree-structure directory }
  2.  
  3. { requires MS-DOS 2.0 or higher. }
  4.  
  5. { All the routines require these type definitions.          }
  6. { However, except as noted, they do not require each other. }
  7.  
  8. type pathtype  = string[63];
  9.      drivetype = string[2];
  10.      rtype     = record
  11.                    ax,bx,cx,dx,bp,si,di,ds,es,flags:integer
  12.                  end;
  13.  
  14. procedure xxdiskerr(x:drivetype);
  15. begin
  16.   writeln('Error -- Invalid disk drive, ''',x,'''');
  17.   halt;
  18. end;
  19.  
  20. procedure xxpatherr(x:pathtype);
  21. begin
  22.   writeln('Error -- Invalid path, ''',x,'''');
  23.   halt;
  24. end;
  25.  
  26. function currentdrive: drivetype;
  27.  
  28. { Returns designator for current default drive, e.g., 'A:'. }
  29. { Works under DOS version 1.x }
  30.  
  31. var w:   drivetype;
  32.     reg: rtype;
  33. begin
  34.   reg.ax:=$1900;
  35.   intr($21,reg);
  36.   w:='A:';
  37.   w[1]:=chr(ord(w[1])+lo(reg.ax));
  38.   currentdrive:=w;
  39. end;
  40.  
  41. procedure chdrive(x:drivetype);
  42.   { Chooses a new default drive. }
  43.   { Parameter can have the form 'A:', 'A', 'a:', or 'a'.     }
  44.   { Works under DOS version 1.x.  Requires XXDISKERR, above. }
  45. var reg: rtype;
  46.  
  47. begin
  48.   reg.ax:=$0E00;
  49.   reg.dx:=ord(upcase(x[1])) - ord('A');
  50.   intr($21,reg);
  51.   if (reg.dx < 0) or (lo(reg.ax) < lo(reg.dx)) then xxdiskerr(x);
  52. end;
  53.  
  54. function diskspace(x:drivetype): real;
  55.   { Returns number of bytes available on specified disk.  }
  56.   { Parameter as for CHDRIVE.  Requires XXDISKERR, above. }
  57. var reg: rtype;
  58.  
  59. begin
  60.   reg.ax:=$3600;
  61.   reg.dx:=1 + ord(upcase(x[1])) - ord('A');
  62.   intr($21,reg);
  63.   if reg.ax = $FFFF then xxdiskerr(x)
  64.   else diskspace := (256.0*hi(reg.bx) + lo(reg.bx)) * reg.cx * reg.ax;
  65. end;
  66.  
  67. function currentdir(x:drivetype): pathtype;
  68.   { Returns full path to active directory on specified drive.        }
  69.   { including backslah at beginning, not including drive designator. }
  70.   { Parameter as for CHDIVE.   }
  71.   { Requires XXDISKERR, above. }
  72. var w:   pathtype;
  73.     reg: rtype;
  74.     i:   integer;
  75.  
  76. begin
  77.   { Get current path }
  78.   reg.ax:=$4700;
  79.   reg.dx:=1 + ord(upcase(x[1])) - ord('A');
  80.   reg.ds:=seg(w[1]);
  81.   reg.si:=ofs(w[1]);
  82.   intr($21,reg);
  83.   if (reg.flags and 1) > 0 then xxdiskerr(x);
  84.  
  85.   { Turn it into a Turbo string }
  86.   i:=1;
  87.   while w[i]<>chr(0) do i:=i+1;
  88.   w[0]:=chr(i-1);
  89.   for i:=1 to length(w) do w[i]:=upcase(w[i]);
  90.  
  91.   currentdir:='\' + w;
  92. end;
  93.  
  94. procedure xxdir(x:pathtype; k:integer);
  95.   { Executes CHDIR, MKDIR, RMDIR requests.      }
  96.   { Requires XXPATHERR and CURRENTDRIVE, above. }
  97. var w:   pathtype;
  98.     reg: rtype;
  99.  
  100. begin
  101.   w:=x + chr(0);
  102.   if w[2] <> ':' then w:=currentdrive + w;  { add drive designator }
  103.   reg.ax:=k;
  104.   reg.ds:=seg(w[1]);
  105.   reg.dx:=ofs(w[1]);
  106.   intr($21,reg);
  107.   if (reg.flags and 1) > 0 then xxpatherr(x);
  108. end;
  109.  
  110. procedure chdir(x:pathtype);
  111.   { Equivalent to CHDIR command in DOS.                 }
  112.   { Requires XXDIR, XXPATHERR, and CURRENTDRIVE, above. }
  113.   { Caution!  Do NOT leave a directory if you have files in it open. }
  114.  
  115. begin
  116.   xxdir(x,$3B00);
  117. end;
  118.  
  119. procedure rmdir(x:pathtype);
  120.   { Equivalent to RMDIR command in DOS.                 }
  121.   { Requires XXDIR, XXPATHERR, and CURRENTDRIVE, above. }
  122.  
  123. begin
  124.   xxdir(x,$3A00);
  125. end;
  126.  
  127. procedure mkdir(x:pathtype);
  128.   { Equivalent to MKDIR command in DOS.                 }
  129.   { Requires XXDIR, XXPATHERR, and CURRENTDRIVE, above. }
  130.  
  131. begin
  132.   xxdir(x,$3900);
  133. end;
  134.  
  135. procedure rename(x,y:pathtype);
  136.   { Renames a file; unlike the DOS RENAME command,       }
  137.   { both parameters of this command are full paths.      }
  138.   { The paths need not be the same, allowing a file      }
  139.   { to be moved from one directory to another.           }
  140.   { First parameter can specify a drive;                 }
  141.   { any drive letter on the second parameter is ignored. }
  142. var wx, wy: pathtype;
  143.     reg:    rtype;
  144. begin
  145.   wx:=x + chr(0);
  146.   wy:=y + chr(0);
  147.   if wx[2] <> ':' then wx:=currentdrive + wx;
  148.   reg.ax:=$5600;
  149.   reg.ds:=seg(wx[1]);
  150.   reg.dx:=ofs(wx[1]);
  151.   reg.es:=seg(wy[1]);
  152.   reg.di:=ofs(wy[1]);
  153.   if (reg.flags and 1) <> 0 then
  154.     begin
  155.       writeln('Error -- Invalid rename request');
  156.       writeln('      -- From: ''',x,'''');
  157.       writeln('      -- To:   ''',y,'''');
  158.       halt;
  159.     end;
  160. end;
  161.  
  162. { sample program that uses the above subroutines
  163. begin
  164.   writeln('Current drive is ',currentdrive);
  165.   chdrive('A:');
  166.   writeln('Drive changed to ',currentdrive);
  167.   writeln;
  168.   writeln(diskspace(currentdrive):10:0,' bytes free on drive ',currentdrive);
  169.   chdrive('B:');
  170.   writeln;
  171.   writeln('Drive changed to ',currentdrive);
  172.   writeln;
  173.   writeln(diskspace(currentdrive):10:0,' bytes free on drive ',currentdrive);
  174.   writeln('Current Path is ',currentdrive,currentdir(currentdrive));
  175.   writeln;
  176.   mkdir('\test');
  177.   chdir('\test');
  178.   writeln('Path changed to ',currentdrive,currentdir(currentdrive));
  179.   writeln;
  180.   chdir('\');
  181.   writeln('Path changed to ',currentdrive,currentdir(currentdrive));
  182.   writeln;
  183.   rmdir('\test');
  184.  
  185. end.
  186.   }
  187.