home *** CD-ROM | disk | FTP | other *** search
/ POINT Software Programming / PPROG1.ISO / pascal / swag / dirs.swg / 0007_DIRDEMO.PAS.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1993-05-28  |  6.8 KB  |  237 lines

  1. { DIRDEMO.PAS
  2.   Author: Trevor Carlsen. Released into the public domain 1989
  3.                           Last modification 1992.
  4.   Demonstrates in a very simple way how to display a directory in a screen
  5.   Window and scroll backwards or Forwards.  }
  6.  
  7. Uses
  8.   Dos,
  9.   Crt,
  10.   keyinput;
  11.  
  12. Type
  13.   str3    = String[3];
  14.   str6    = String[6];
  15.   str16   = String[16];
  16.   sType   = (_name,_ext,_date,_size);
  17.   DirRec  = Record
  18.               name  : NameStr;
  19.               ext   : ExtStr;
  20.               size  : str6;
  21.               date  : str16;
  22.               Lsize,
  23.               Ldate : LongInt;
  24.               dir   : Boolean;
  25.             end;
  26.  
  27. Const
  28.   maxdir       = 1000;     { maximum number of directory entries }
  29.   months : Array[1..12] of str3 =
  30.            ('Jan','Feb','Mar','Apr','May','Jun',
  31.             'Jul','Aug','Sep','Oct','Nov','Dec');
  32.   WinX1 = 14; WinX2 = 1;
  33.   WinY1 = 65; WinY2 = 23;
  34.   LtGrayOnBlue      = $17;
  35.   BlueOnLtGray      = $71;
  36.   page              = 22;
  37.   maxlines : Word   = page;
  38.  
  39. Type
  40.   DataArr           = Array[1..maxdir] of DirRec;
  41.  
  42. Var
  43.   DirEntry          : DataArr;
  44.   x, numb           : Integer;
  45.   path              : DirStr;
  46.   key               : Byte;
  47.   finished          : Boolean;
  48.   OldAttr           : Byte;
  49.  
  50. Procedure quicksort(Var s; left,right : Word; SortType: sType);
  51.   Var
  52.     data      : DataArr Absolute s;
  53.     pivotStr,
  54.     tempStr   : String;
  55.     pivotLong,
  56.     tempLong  : LongInt;
  57.     lower,
  58.     upper,
  59.     middle    : Word;
  60.  
  61.   Procedure swap(Var a,b);
  62.     Var x : DirRec Absolute a;
  63.         y : DirRec Absolute b;
  64.         t : DirRec;
  65.     begin
  66.       t := x;
  67.       x := y;
  68.       y := t;
  69.     end;
  70.  
  71.   begin
  72.     lower := left;
  73.     upper := right;
  74.     middle:= (left + right) div 2;
  75.     Case SortType of
  76.       _name: pivotStr   := data[middle].name;
  77.       _ext : pivotStr   := data[middle].ext;
  78.       _size: pivotLong  := data[middle].Lsize;
  79.       _date: pivotLong  := data[middle].Ldate;
  80.     end; { Case SortType }
  81.     Repeat
  82.       Case SortType of
  83.         _name: begin
  84.                  While data[lower].name < pivotStr do inc(lower);
  85.                  While pivotStr < data[upper].name do dec(upper);
  86.                end;
  87.         _ext : begin
  88.                  While data[lower].ext < pivotStr do inc(lower);
  89.                  While pivotStr < data[upper].ext do dec(upper);
  90.                end;
  91.         _size: begin
  92.                  While data[lower].Lsize < pivotLong do inc(lower);
  93.                  While pivotLong < data[upper].Lsize do dec(upper);
  94.                end;
  95.         _date: begin
  96.                  While data[lower].Ldate < pivotLong do inc(lower);
  97.                  While pivotLong < data[upper].Ldate do dec(upper);
  98.                end;
  99.       end; { Case SortType }
  100.       if lower <= upper then begin
  101.         swap(data[lower],data[upper]);
  102.         inc(lower);
  103.         dec(upper);
  104.        end;
  105.     Until lower > upper;
  106.     if left < upper then quicksort(data,left,upper,SortType);
  107.     if lower < right then quicksort(data,lower,right,SortType);
  108.   end; { quicksort }
  109.  
  110. Function Form(st : String; len : Byte): String;
  111.   { Replaces spaces in a numeric String With zeroes  }
  112.   Var
  113.     x : Byte ;
  114.   begin
  115.     Form := st;
  116.     For x := 1 to len do
  117.       if st[x] = ' ' then
  118.         Form[x] := '0'
  119.   end;
  120.  
  121. Procedure ReadDir(Var count : Integer);
  122.   { Reads the current directory and places in the main Array }
  123.   Var
  124.     DirInfo    : SearchRec;
  125.  
  126.   Procedure CreateRecord;
  127.     Var
  128.       Dt : DateTime;
  129.       st : str6;
  130.     begin
  131.       With DirEntry[count] do begin
  132.         FSplit(DirInfo.name,path,name,ext);             { Split File name up }
  133.         if ext[1] = '.' then                                { get rid of dot }
  134.           ext := copy(ext,2,3);
  135.         name[0] := #8;  ext[0] := #3; { Force to a set length For Formatting }
  136.         Lsize := DirInfo.size;
  137.         Ldate := DirInfo.time;
  138.         str(DirInfo.size:6,size);
  139.         UnPackTime(DirInfo.time,Dt);
  140.         date := '';
  141.         str(Dt.day:2,st);
  142.         date := st + '-' + months[Dt.month] + '-';
  143.         str((Dt.year-1900):2,st);
  144.         date := date + st + #255#255;
  145.         str(Dt.hour:2,st);
  146.         date := date + st + ':';
  147.         str(Dt.Min:2,st);
  148.         date := date + st;
  149.         date := Form(date,length(date));
  150.         dir := DirInfo.attr and Directory = Directory;
  151.       end; { With }
  152.     end; { CreateRecord }
  153.  
  154.   begin { ReadDir }
  155.     count := 0;         { For keeping a Record of the number of entries read }
  156.     FillChar(DirEntry,sizeof(DirEntry),32);           { initialize the Array }
  157.     FindFirst('*.*',AnyFile,DirInfo);
  158.     While (DosError = 0) and (count < maxdir) do begin
  159.       inc(count);
  160.       CreateRecord;
  161.       FindNext(DirInfo);
  162.     end; { While }
  163.     if count < page then
  164.       maxlines := count;
  165.     quicksort(DirEntry,1,count,_name);
  166.   end; { ReadDir }
  167.  
  168. Procedure DisplayDirectory(n : Integer);
  169.   Var
  170.     x,y : Integer;
  171.   begin
  172.     y := 1;
  173.     For x := n to n + maxlines do
  174.       With DirEntry[x] do begin
  175.         GotoXY(4,y);inc(y);
  176.         Write(name,'  ');
  177.         Write(ext,' ');
  178.         if dir then Write('<DIR>')
  179.         else Write('     ');
  180.         Write(size:8,date:18);
  181.       end; { With }
  182.   end; { DisplayDirectory }
  183.  
  184. begin { main }
  185.   ClrScr;
  186.   GotoXY(5,24);
  187.   OldAttr  := TextAttr;
  188.   TextAttr := BlueOnLtGray;
  189.   Write(' F1=Sort by name F2=Sort by extension F3=Sort by size F4=Sort by date ');
  190.   GotoXY(5,25);
  191.   Write('   Use arrow keys to scroll through directory display - <ESC> quits   ');
  192.   TextAttr := LtGrayOnBlue;
  193.   Window(WinX1,WinX2,WinY1,WinY2);  { make the Window }
  194.   ClrScr;
  195.   HiddenCursor;
  196.   ReadDir(numb);
  197.   x := 1; finished := False;
  198.   Repeat
  199.     DisplayDirectory(x); { display maxlines Files }
  200.       Case KeyWord of
  201.       F1 {name} : begin
  202.                     x := 1;
  203.                     quicksort(DirEntry,1,numb,_name);
  204.                   end;
  205.       F2 {ext}  : begin
  206.                     x := 1;
  207.                     quicksort(DirEntry,1,numb,_ext);
  208.                   end;
  209.       F3 {size} : begin
  210.                     x := 1;
  211.                     quicksort(DirEntry,1,numb,_size);
  212.                   end;
  213.       F4 {date} : begin
  214.                     x := 1;
  215.                     quicksort(DirEntry,1,numb,_date);
  216.                   end;
  217.       home      : x := 1;
  218.       endKey    : x := numb - maxlines;
  219.       UpArrow   : if x > 1 then
  220.                     dec(x);
  221.       DownArrow : if x < (numb - maxlines) then
  222.                     inc(x);
  223.       PageDn    : if (x + page) > (numb - maxlines) then
  224.                     x := numb - maxlines
  225.                   else inc(x,page);
  226.       PageUp    : if (x - page) > 0 then
  227.                     dec(x,page)
  228.                   else x := 1;
  229.       escape    : finished := True
  230.       end; { Case }
  231.   Until finished;
  232.   NormalCursor;
  233.   TextAttr := OldAttr;
  234.   ClrScr;
  235. end.
  236.  
  237.