From ts@uwasa.fi Tue Nov 12 00:00:00 1996 Subject: FAQPAS5.TXT contents Copyright (c) 1993-1996 by Timo Salmi All rights reserved FAQPAS5.TXT The fifth set of frequently (and not so frequently) asked Turbo Pascal questions with Timo's answers. The items are in no particular order. You are free to quote brief passages from this file provided you clearly indicate the source with a proper acknowledgment. Comments and corrections are solicited. But if you wish to have individual Turbo Pascal consultation, please post your questions to a suitable Usenet newsgroup like news:comp.lang.pascal.borland. It is much more efficient than asking me by email. I'd like to help, but I am very pressed for time. I prefer to pick the questions I answer from the Usenet news. Thus I can answer publicly at one go if I happen to have an answer. Besides, newsgroups have a number of readers who might know a better or an alternative answer. Don't be discouraged, though, if you get a reply like this from me. I am always glad to hear from fellow Turbo Pascal users. .................................................................... Prof. Timo Salmi Co-moderator of news:comp.archives.msdos.announce Moderating at ftp:// & http://garbo.uwasa.fi archives 193.166.120.5 Department of Accounting and Business Finance ; University of Vaasa mailto:ts@uwasa.fi ; FIN-65101, Finland -------------------------------------------------------------------- 101) How do I detect if mouse hardware/driver is installed? 102) How can I read absolute sectors directly from a floppy? 103) How can I move a file to another directory in Turbo Pascal? 104) How can I get/set a disk volume label? 105) Is there a function to chop off the leading zero from 0.322? 106) How can I print a text file (and conclude sending a formfeed)? 107) How can I round 4.1256455 to two decimal places to give 4.13? 108) How can I list with paths all the files on a drive? -------------------------------------------------------------------- From ts@uwasa.fi Tue Nov 12 00:01:41 1996 Subject: Detecting mouse 101. ***** Q: How do I detect if mouse hardware/driver is installed? A: The source code is given below. For more mouse related functions please see ftp://garbo.uwasa.fi/pc/programming/inter52c.zip for interrupt $33 functions. uses Dos; (* Detect if mouse hardware/driver is installed; initializes driver *) function MOUSDRFN : boolean; var regs : registers; begin FillChar (regs, SizeOf(regs), 0); { Just to make sure } regs.ax := $0000; { Interrupt function number } Intr ($33, regs); { Call interrupt $33 } if regs.ax = $FFFF then mousdrfn := true else mousdrfn := false; end; (* mousedrfn *) -------------------------------------------------------------------- From ts@uwasa.fi Tue Nov 12 00:01:42 1996 Subject: Reading absolute sectors 102. ***** Q: How can I read absolute sectors directly from a floppy? A: Here is the source code for reading directly from a floppy disk. For directly reading data from hard disk, please study the information for interrupt $13 function $02 in Ralf Brown's list of interrupts ftp://garbo.uwasa.fi/pc/programming/inter52a.zip. uses Dos; type readBufferType = array [1..1024] of byte; procedure READFLPY (drive : char; side : byte; track : byte; sector : byte; var rb : readBufferType; var ok : boolean); var regs : registers; i : byte; begin ok := false; for i := 1 to 3 do begin FillChar (regs, SizeOf(regs), 0); { Just to make sure } regs.ah := $02; { Function } regs.al := 2; { Number of sectors to read } regs.dl := ord(Upcase(drive))-ord('A'); if (regs.dl < 0) or (regs.dl > 1) then exit; { For floppies only } regs.dh := side; regs.ch := track; regs.cl := sector; regs.es := Seg(rb); regs.bx := Ofs(rb); Intr ($13, regs); { Call interrupt $13 } if regs.flags and FCarry = 0 then begin { Was it ok? } ok := true; exit; end; {if} { reset and try again a maximum of three times } FillChar (regs, SizeOf(regs), 0); { Just to make sure } regs.ah := $00; { Function } regs.dl := ord(Upcase(drive))-ord('A'); end; {for i} end; (* readflpy *) -------------------------------------------------------------------- From ts@uwasa.fi Tue Nov 12 00:01:43 1996 Subject: Moving files 103. ***** Q: How can I move a file to another directory in Turbo Pascal? A: If the file and the target directory are on the same disk you can use Turbo Pascal's rename command for the purpose. If they are on separate disks you'll first have to copy the file as explained in the item "How can I copy a file in a Turbo Pascal program?" and then erase the original as explained in the item "Can you tell a beginner how to delete files with Turbo Pascal?" var f : file; begin Assign (f, 'r:\faq.pas'); {$I-} Rename (f, 'r:\cmand\faq.pas'); {$I+} if IOResult = 0 then writeln ('File moved') else writeln ('File not moved'); end. -------------------------------------------------------------------- From ts@uwasa.fi Tue Nov 12 00:01:44 1996 Subject: Getting/setting volume label 104. ***** Q: How can I get/set a disk volume label? A: Getting the volume label can be done in alternative ways. Below is one of them Uses Dos; (* Get a disk's volume label *) function GETLABFN (device : char) : string; var FileInfo : SearchRec; fsplit_dir : DirStr; fsplit_name : NameStr; fsplit_ext : ExtStr; stash : byte; begin getlabfn := ''; device := UpCase (device); if (device < 'A') or (device > 'Z') then exit; {} stash := fileMode; FileMode := $40; FindFirst (device + ':\*.*', AnyFile, FileInfo); while DosError = 0 do begin if ((FileInfo.Attr and VolumeId) > 0) then begin FSplit (FExpand(FileInfo.Name), fsplit_dir, fsplit_name, fsplit_ext); Delete (fsplit_ext, 1, 1); getlabfn := fsplit_name + fsplit_ext; FileMode := stash; exit; end; FindNext (FileInfo); end; {while} FileMode := stash; end; (* getlabfn *) As for setting a disk volume label with Turbo Pascal that is a much more complicated task. You'll need to manipulate the File Control Block (FCB). This alternative is not taken further in here. If you need the procedure it is available without the source code as "SETLABEL Set a disk's volume label" in TSUNTL.TPU in ftp://garbo.uwasa.fi/pc/ts/tspa3570.zip. An alternative is shelling to Dos to call its own LABEL.EXE program as follows {$M 2048, 0, 0} (* <-- Important. Adjust if out of memory. *) Uses Dos; begin SwapVectors; Exec (GetEnv('comspec'), '/c label A:'); (* Execution *) SwapVectors; end. -------------------------------------------------------------------- From ts@uwasa.fi Tue Nov 12 00:01:45 1996 Subject: Omitting leading zero 105. ***** Q: Is there a function to chop off the leading zero from 0.322? A: If you wish to output a real without the leading zero you can use the following function function CHOPFN (x : real; dd : byte) : string; var s : string; begin Str (x:0:dd, s); if x >= 0 then chopfn := Copy (s,2,255) else chopfn := '-' + Copy (s,3,255); end; (* chopfn *) There are other options. What is below is more cumbersome than CHOPFN, but it demonstrates the usage of the Move command rather nicely. function CHOP2FN (x : real; dd : byte) : string; var s : string; begin Str (x:0:dd, s); if x >= 0 then begin Move (s[2],s[1],Length(s)-1); Dec(s[0]); chop2fn := s; end else begin Move (s[3],s[1],Length(s)-2); Dec(s[0],2); chop2fn := '-' + s; end; end; (* chop2fn *) -------------------------------------------------------------------- From ts@uwasa.fi Tue Nov 12 00:01:46 1996 Subject: Printing a file and a formfeed 106. ***** Q: How can I print a text file (and conclude sending a formfeed)? A: We can turn this beginner's question into some instructive source code. Study carefully the many details included. For printer handling you might also wish to see in my FAQ the separate item number 15 "How can I test that the printer is ready?" Uses Printer; { Associates lst with the LPT1 device } const formfeed = #12; { The formfeed character } var s : string; { A string for a single line } filename : string; { A variable for the file name } f : text; { Text-file variable } fmsave : byte; { For storing the original filemode } begin if ParamCount > 0 then { If there are parameters on the command line } filename := ParamStr(1) { get the first of them } else begin writeln ('Usage: ', ParamStr(0), ' [Filename]'); halt(1); { Sets errorlevel to 1 for batches } end; fmSave := FileMode; { Save the current filemode } FileMode := $40; { To handle also read-only and network files } Assign (f, filename); { Associate file variable with file name } {$I-} { Input/Output-Checking temporarily off } Reset (f); { Open the file } {$I+} if IOResult <> 0 then begin { Check failure of opening the file } writeln ('Error opening ', filename); FileMode := fmSave; { Restore original filemode } halt(2); { Sets errorlevel to 2 for batches } end; {if} while not eof(f) do begin readln (f, s); { Read a line, maximum length 255 characters } writeln (lst, s); { Write the line to the printer } end; {while} Close (f); { Close the file } FileMode := fmSave; { Restore the original filemode } write (lst, formfeed); { Eject the page from the printer } end. -------------------------------------------------------------------- From ts@uwasa.fi Tue Nov 12 00:01:47 1996 Subject: Rounding a value 107. ***** Q: How can I round 4.1256455 to two decimal places to give 4.13? A: Here is the source code. Note the two alternatives. The trivial one of just formulating the output, and the more complicated of actually rounding the value of a variable. var x, y : real; {} (* Sign function, needed to round negative values correctly *) function SignFn (a : real) : real; begin if a > 0.0 then signfn := 1.0 else if a < 0.0 then signfn := -1.0 else signfn := 0.0; end; (* sgnfn *) {} (* Round a real variable to d decimal places *) function RoundRealFn (x : real; d : byte) : real; var a : real; i : byte; begin a := 1.0; for i := 1 to d do a := a*10.0; RoundRealFn := Int (a*x + SignFn(x)*0.5) / a; end; (* RoundRealFn *) {} (* Test *) begin x := 4.1256455; {} { ... The case of actually rounding a variable ...} y := RoundRealFn (x, 2); writeln (x, ' ', y); {} {... The more common case case of rounding the output only ...} writeln (x:0:2); end. -------------------------------------------------------------------- From ts@uwasa.fi Tue Nov 12 00:01:48 1996 Subject: Recursing directories 108. ***** Q: How can I list with paths all the files on a drive? A: Here is the example source code {$M 16384,0,0} Uses Dos; {... the top directory ...} procedure FindFiles (Path, FileSpec : string); var FileInfo : SearchRec; begin FindFirst (Path + FileSpec, AnyFile, FileInfo); while DosError = 0 do begin if ((FileInfo.Attr and Directory) = 0) and ((FileInfo.Attr and VolumeId) = 0) then begin writeln (Path+FileInfo.Name); end; {if} FindNext (FileInfo); end; {while} {} {... subdirectories ...} FindFirst (Path + '*.*', Directory, FileInfo); while DosError = 0 do begin if ((FileInfo.Attr and Directory) > 0) and (FileInfo.Name <> '.') and (FileInfo.Name <> '..') then FindFiles (Path + FileInfo.Name + '\', FileSpec); FindNext (FileInfo); end; {while} end; (* findfiles *) {} begin FindFiles ('C:\', '*.*'); { Note the trailing \ } end. For starting below the root, use e.g. FindFiles ('C:\DOS\', '*.*'); --------------------------------------------------------------------