home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Carousel
/
CAROUSEL.cdr
/
mactosh
/
code
/
p_diskop.sit
/
Disks.pas
< prev
Wrap
Pascal/Delphi Source File
|
1987-06-12
|
4KB
|
127 lines
PROGRAM Disk;
CONST
diskSize = 797696;
MaxFiles = 750;
diskMax = 150;
tab = ' ';
VAR
fileList : Text;
i, j, size, filecount, usedCount, filesUsed : Integer;
inDisk, Wasted : LongInt;
aRect : Rect;
foundOne : Boolean;
fname : STRING;
Names : ARRAY[1..MaxFiles] OF STRING[32];
Sizes : ARRAY[1..MaxFiles] OF LongInt;
Used : ARRAY[1..MaxFiles] OF Boolean;
thisDisk : ARRAY[1..diskMax] OF 0..MaxFiles;
BEGIN
SetRect(aRect, 10, 50, 500, 200);
SetTextRect(aRect);
fname := OldFileName('Pick the text file to read');
IF fname = '' THEN
ExitToShell;
reset(fileList, fname);
fileCount := 0;
WHILE NOT EOF(fileList) AND (fileCount < maxFiles) DO
BEGIN
fileCount := fileCount + 1;
readln(fileList, fname);
Names[maxFiles] := copy(fname, 1, pos(tab, fname) - 1);
delete(fname, 1, pos(tab, fname));
readString(fname, Sizes[maxFiles]);
Used[fileCount] := False;
IF Sizes[maxFiles] > diskSize THEN
BEGIN
ShowText;
writeln(Names[maxFiles], ' is too big! It will be ignored.');
fileCount := fileCount - 1;
END
ELSE { Insert in decending size order }
BEGIN
j := 0;
REPEAT
j := j + 1;
UNTIL (Sizes[j] < Sizes[maxFiles]) OR (j >= fileCount);
{ move j to fileCount-1 down }
FOR i := fileCount - 1 DOWNTO j DO
BEGIN
Sizes[i + 1] := Sizes[i];
Names[i + 1] := Names[i];
END;
Names[j] := Names[maxFiles];
Sizes[j] := Sizes[maxFiles];
END;
IF (fileCount = maxFiles) AND NOT EOF(fileList) THEN
BEGIN
ShowText;
Writeln('Too many files! Current limit is ', maxFiles : 1, '.');
Writeln('I will only process that many.');
END;
END;
Close(fileList);
fname := NewFileName('Save as╔', 'Output');
IF fname = '' THEN
ExitToShell;
rewrite(fileList, fname);
usedCount := 0;
filesUsed := 0;
Wasted := 0;
WHILE filesUsed < fileCount DO
BEGIN
{ Fill another disk }
usedCount := usedCount + 1;
inDisk := 0;
j := 0;
FOR i := 1 TO diskMax DO
thisDisk[i] := 0;
FoundOne := True;
WHILE (inDisk < diskSize) AND foundOne DO
BEGIN
{ Find largest file that fits from the sorted file }
FoundOne := false;
i := 0;
WHILE (i < fileCount) DO
BEGIN
i := i + 1;
IF NOT Used[i] AND (Sizes[i] + inDisk < diskSize) THEN
BEGIN
{ Add a found file to the list }
j := j + 1;
filesUsed := filesUsed + 1;
Used[i] := True;
thisDisk[j] := i;
foundOne := True;
inDisk := inDisk + Sizes[i];
END;
END;
END;
Writeln(fileList);
Writeln(fileList, ' Disk #', usedCount : 1);
FOR i := 1 TO j DO
Writeln(fileList, Names[thisDisk[i]], tab, Sizes[thisDisk[i]]);
Wasted := Wasted + diskSize - inDisk;
Writeln(fileList, 'Free space', tab, diskSize - inDisk);
END;
Wasted := Wasted - diskSize + inDisk;
Writeln(fileList);
Writeln(fileList, usedCount : 1, ' disks used for ', fileCount : 1, ' files.');
Writeln(fileList, Wasted : 1, ' bytes (', Wasted / 1024 : 1 : 1, 'K) of wasted space on all filled disks.');
Writeln(fileList, diskSize - inDisk : 1, ' bytes (', (diskSize - inDisk) / 1024 : 1 : 1, 'K) of empty space on last disk.');
Close(fileList);
ShowText;
Writeln;
Writeln(usedCount : 1, ' disks used for ', fileCount : 1, ' files.');
Writeln(Wasted : 1, ' bytes (', Wasted / 1024 : 1 : 1, 'K) of wasted space on all filled disks.');
Writeln(diskSize - inDisk : 1, ' bytes (', (diskSize - inDisk) / 1024 : 1 : 1, 'K) of empty space on last disk.');
Writeln;
Writeln('Press mouse button to continue');
WHILE NOT Button DO
;
WHILE Button DO
;
END.