home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 8 / CDASC08.ISO / VRAC / DIRCNT16.ZIP / DIRCOUNT.PAS < prev    next >
Pascal/Delphi Source File  |  1993-08-27  |  8KB  |  349 lines

  1.  
  2. (*
  3.  * DirCount - Count file directory entries and insert headers with
  4.  *            file information.
  5.  *
  6.  * Written by Samuel H. Smith, 12-30-88
  7.  *
  8.  *)
  9.  
  10. uses qread;
  11.  
  12. const
  13.    version   = 'DirCount 1.5, 08-27-93';
  14.  
  15. var
  16.    console:    text;
  17.    ctlfd:      text;
  18.  
  19.    bbsname:    string;
  20.    dirfile:    string;
  21.    title:      string;
  22.    subtitle:   string;
  23.    dirnum:     integer;
  24.  
  25.    ibuf:       array[1..20480] of byte;
  26.    obuf:       array[1..20480] of byte;
  27.    line:       string;
  28.  
  29.    sizes:      longint;
  30.    tsizes:     longint;
  31.    files:      longint;
  32.    tfiles:     longint;
  33.    tdirs:      longint;
  34.  
  35.  
  36. (* --------------------------------------------------------- *)
  37. function insert_commas(s: string): string;
  38. var
  39.    i: integer;
  40. begin
  41.    i := length(s);
  42.    while i > 3 do
  43.    begin
  44.       dec(i,3);
  45.       insert(',',s,i+1);
  46.    end;
  47.  
  48.    insert_commas := s;
  49. end;
  50.  
  51.  
  52. (* --------------------------------------------------------- *)
  53. function itoa (n: longint): string;
  54. var
  55.    tstr:          string;
  56.  
  57. begin
  58.    str(n, tstr);
  59.    itoa := insert_commas(tstr);
  60. end;
  61.  
  62.  
  63. (* --------------------------------------------------------- *)
  64. function itoan(n: longint; width: integer): string;
  65. var
  66.    s: string;
  67. begin
  68.    s := itoa(n);
  69.    while length(s) < width do
  70.       s := ' ' + s;
  71.    itoan := s;
  72. end;
  73.  
  74.  
  75. (* --------------------------------------------------------- *)
  76. function ljust(s: string; width: integer): string;
  77. begin
  78.    s := copy(s,1,width);
  79.    while length(s) < width do
  80.       s := s + ' ';
  81.    ljust := s;
  82. end;
  83.  
  84.  
  85. (* --------------------------------------------------------- *)
  86. function center(s: string; width: integer): string;
  87. var
  88.    i: integer;
  89. begin
  90.    s := copy(s,1,width);
  91.    i := (width - length(s)) div 2;
  92.    while i > 0 do
  93.    begin
  94.       s := ' ' + s;
  95.       dec(i);
  96.    end;
  97.    center := s;
  98. end;
  99.  
  100.  
  101. (* --------------------------------------------------------- *)
  102. function cjust(s: string; width: integer): string;
  103. var
  104.    i: integer;
  105. begin
  106.    s := copy(s,1,width);
  107.    i := (width - length(s)) div 2;
  108.    while i > 0 do
  109.    begin
  110.       s := ' ' + s;
  111.       dec(i);
  112.    end;
  113.    while length(s) < width do
  114.       s := s + ' ';
  115.    cjust := s;
  116. end;
  117.  
  118.  
  119. (* --------------------------------------------------------- *)
  120. function isfile: boolean;
  121. begin
  122.    if length(line) < 35 then
  123.       isfile := false
  124.    else
  125.    if (line[26] = '-')  and (line[29] = '-') and
  126.       (line[21] >= '0') and (line[21] <= '9') and
  127.       (line[24] >= '0') and (line[24] <= '9') then
  128.       isfile := true
  129.    else
  130.    if (line[24] = 'D') and (line[25] = 'E') and
  131.       (line[26] = 'L') and (line[27] = 'E') and
  132.       (line[28] = 'T') and (line[29] = 'E') then
  133.       isfile := true
  134.    else
  135.       isfile := false;
  136. end;
  137.  
  138.  
  139. (* --------------------------------------------------------- *)
  140. procedure count_files;
  141. var
  142.    size: longint;
  143.    err:  integer;
  144.    tmp:  string;
  145.    ifd:  text;
  146.  
  147. begin
  148.    files := 0;
  149.    sizes := 0;
  150.  
  151.    assign(ifd,dirfile);
  152.    {$i-} reset(ifd); {$i+}
  153.    if ioresult <> 0 then
  154.    begin
  155.       writeln(console,'Can''t open DIR file ',dirfile);
  156.       exit;;
  157.    end;
  158.  
  159.    setTextBuf(ifd,ibuf);
  160.    write(console,'  Counting: ',dirfile,'':10,^M);
  161.  
  162.    while not eof(ifd) do
  163.    begin
  164.       qreadln(ifd,line,sizeof(line));
  165.       if isfile then
  166.       begin
  167.          inc(files);
  168.          tmp := copy(line,13,9);
  169.          while tmp[1] = ' ' do
  170.             delete(tmp,1,1);
  171.          val(tmp,size,err);
  172.          sizes := sizes + size;
  173.       end;
  174.    end;
  175.  
  176.    close(ifd);
  177. end;
  178.  
  179.  
  180. (* --------------------------------------------------------- *)
  181. procedure update_dirfile;
  182. var
  183.    ifd:     text;
  184.    ofd:     text;
  185.    tmp:     string;
  186.  
  187. begin
  188.    assign(ifd,dirfile);
  189.    {$i-} reset(ifd); {$i+}
  190.    if ioresult <> 0 then
  191.    begin
  192.       exit;
  193.    end;
  194.  
  195.    assign(ofd,dirfile+'$');
  196.    {$i-} rewrite(ofd); {$i+}
  197.    if ioresult <> 0 then
  198.    begin
  199.       writeln(console,'Can''t create tempfile ',dirfile,'$');
  200.       halt(99);
  201.    end;
  202.  
  203.    setTextBuf(ifd,ibuf);
  204.    setTextBuf(ofd,obuf);
  205.    write(console,'Formatting: ',dirfile,'':10,^M);
  206.  
  207.    repeat
  208.       qreadln(ifd,line,sizeof(line));
  209.    until isfile or eof(ifd);
  210.  
  211.    writeln(ofd);
  212.    writeln(ofd,center(bbsname,79));
  213.    writeln(ofd);
  214.    writeln(ofd,center(title,79));
  215.  
  216.    tmp := itoa(files) + ' files using ' + itoa(sizes) + ' bytes';
  217.    writeln(ofd,center(tmp,79));
  218.  
  219.    writeln(ofd);
  220.    writeln(ofd,' File Name      Size     Date                  File Description');
  221.    writeln(ofd,'------------  -------  --------  ---------------------------------------------');
  222.    writeln(ofd);
  223.  
  224.    writeln(ofd,line);
  225.    while not eof(ifd) do
  226.    begin
  227.       qreadln(ifd,line,sizeof(line));
  228.       writeln(ofd,line);
  229.    end;
  230.  
  231.    close(ofd);
  232.    close(ifd);
  233.  
  234.    {$i-} erase(ifd); {$i+}
  235.    if ioresult <> 0 then
  236.    begin
  237.       writeln(console,'Can''t erase old dirfile ',dirfile);
  238.       halt(99);
  239.    end;
  240.  
  241.    {$i-} rename(ofd,dirfile); {$i+}
  242.    if ioresult <> 0 then
  243.    begin
  244.       writeln(console,'Can''t rename new dirfile ',dirfile,'$ to ',dirfile);
  245.       halt(99);
  246.    end;
  247.  
  248. end;
  249.  
  250.  
  251. (* --------------------------------------------------------- *)
  252. var
  253.    temp: string;
  254.    i:    integer;
  255. begin
  256.    assign(console,'CON');
  257.    rewrite(console);
  258.    writeln(console);
  259.    writeln(console,version);
  260.    writeln(console,'Public Domain Material by Samuel H. Smith');
  261.    writeln(console);
  262.  
  263.    if paramcount <> 1 then
  264.    begin
  265.       writeln(console,'Usage:    DirCount configfile [>summary]');
  266.       writeln(console,'Example:  DirCount COUNT.CNF >\PCB\GEN\BLT16');
  267.       halt(99);
  268.    end;
  269.  
  270.    assign(ctlfd,paramstr(1));
  271.    {$i-} reset(ctlfd); {$i+}
  272.    if ioresult <> 0 then
  273.    begin
  274.       writeln(console,'Can''t open configuration file ',paramstr(1));
  275.       halt(99);
  276.    end;
  277.  
  278.    readln(ctlfd,bbsname);
  279.    readln(ctlfd,subtitle);
  280.    dirnum := 0;
  281.    tfiles := 0;
  282.    tsizes := 0;
  283.    tdirs := 0;
  284.  
  285.    writeln;
  286.    writeln(center(bbsname,79));
  287.    writeln(center(subtitle,79));
  288.    writeln;
  289.  
  290.    writeln('  ',
  291.            cjust('Area',6),'   ',
  292.            cjust('Description',36),'  ',
  293.            cjust('Files',9),' ',
  294.            center('File Sizes',16));
  295.    writeln('  ',
  296.            cjust('------',6),'  ',
  297.            cjust('-------------------------------------',36),'   ',
  298.            cjust('--------',9),
  299.            center('---------------',18));
  300.  
  301.    while not eof(ctlfd) do
  302.    begin
  303.       readln(ctlfd,dirfile);
  304.       readln(ctlfd,title);
  305.       inc(dirnum);
  306.  
  307.       count_files;
  308.  
  309.       if files > 0 then
  310.       begin
  311.          temp := '  ' +
  312.                  itoan(dirnum,4)+'     '+
  313.                  ljust(title,36)+
  314.                  itoan(files,9)+
  315.                  itoan(sizes,17);
  316.  
  317.          if odd(tdirs) then
  318.          begin
  319.             for i := 8 to length(temp) do
  320.                if (not odd(i)) and (temp[i] = ' ') then
  321.                   temp[i] := '·';
  322.          end;
  323.  
  324.          writeln(temp);
  325.          inc(tdirs);
  326.       end;
  327.  
  328.       tfiles := tfiles + files;
  329.       tsizes := tsizes + sizes;
  330.  
  331.       update_dirfile;
  332.    end;
  333.  
  334.    write(console,'':60,^M);
  335.    close(ctlfd);
  336.  
  337.    writeln('  ',
  338.            cjust('',6),'  ',
  339.            cjust('                                     ',36),'   ',
  340.            cjust('--------',9),
  341.            center('---------------',18));
  342.    writeln('  ',
  343.            '':6,
  344.            'Overall Totals':38,' ',
  345.            itoan(tfiles,9),
  346.            itoan(tsizes,17));
  347. end.
  348.  
  349.