home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 24 / CD_ASCQ_24_0995.iso / vrac / tsfaqp26.zip / FAQPAS2.TXT < prev    next >
Internet Message Format  |  1995-07-05  |  61KB

  1. From ts@uwasa.fi Wed Jul 5 00:00:00 1995
  2. Subject: FAQPAS2.TXT contents
  3.  
  4.                              Copyright (c) 1993-1995 by Timo Salmi
  5.                                                All rights reserved
  6.  
  7. FAQPAS2.TXT More frequently (and not so frequently) asked Turbo
  8. Pascal questions with Timo's answers. The items are in no particular
  9. order.
  10.  
  11. You are free to quote brief passages from this file provided you
  12. clearly indicate the source with a proper acknowledgment.
  13.  
  14. Comments and corrections are solicited. But if you wish to have
  15. individual Turbo Pascal consultation, please post your questions to
  16. a suitable Usenet newsgroup like news:comp.lang.pascal.borland. It
  17. is much more efficient than asking me by email. I'd like to help,
  18. but I am very pressed for time. I prefer to pick the questions I
  19. answer from the Usenet news. Thus I can answer publicly at one go if
  20. I happen to have an answer. Besides, newsgroups have a number of
  21. readers who might know a better or an alternative answer. Don't be
  22. discouraged, though, if you get a reply like this from me. I am
  23. always glad to hear from fellow Turbo Pascal users.
  24.  
  25. ....................................................................
  26. Prof. Timo Salmi   Co-moderator of news:comp.archives.msdos.announce
  27. Moderating at ftp:// & http://garbo.uwasa.fi archives  193.166.120.5
  28. Department of Accounting and Business Finance  ; University of Vaasa
  29. ts@uwasa.fi http://uwasa.fi/~ts BBS 961-3170972; FIN-65101,  Finland
  30.  
  31. --------------------------------------------------------------------
  32. 31) How does one store, and then restore the original screen?
  33. 32) How can I convert a TPU unit of one TP version to another?
  34. 33) Which error is e.g. Runtime error 205, etc
  35. 34) Why can't I open read-only files? I get "File access denied".
  36. 35) How do I obtain high and low parts of a byte variable?
  37. 36) How can I set a hi-intensity color background in the text mode?
  38. 37) Where can I find a program to convert (Turbo) Pascal to C?
  39. 38) How can I read input without echoing to the screen?
  40. 39) How can I edit the readln input stream?
  41. 40) How can I write (brand) something into my executables?
  42. 41) What is wrong with my program? It hangs without a clear pattern?
  43. 42) How do I convert a decimal word into a hexadecimal string, etc?
  44. 43) How to determine the last drive?
  45. 44) How can I put a running clock into my Turbo Pascal program?
  46. 45) How to establish if a name refers to a directory or not?
  47. 46) How does one disable alt-ctrl-del?
  48. 47) How can I test whether a file exists?
  49. 48) What is the name of the current Turbo Pascal program?
  50. 49) How is the code for rebooting the PC written in Turbo Pascal?
  51. 50) How can I write inline code?
  52. 51) I am running out of memory when compiling my large program.
  53. 52) How do I avoid scrolling in the last column of the last row?
  54. 53) How can one hide (or unhide) a directory using a TP program?
  55. 54) How do I test whether a file is already open in a TP program?
  56. 55) How can I test and convert a numerical string into a real?
  57. 56) How can I reverse a TP .EXE or .TPU back into source code?
  58. 57) How can I calculate the difference between two points of time?
  59. 58) Is a program running stand-alone or from within the IDE?
  60. 59) Please explain Turbo Pascal memory addressing to me.
  61. 60) How do I obtain a bit or bits from a byte, a word or a longint?
  62. --------------------------------------------------------------------
  63.  
  64. From ts@uwasa.fi Wed Jul 5 00:00:31 1995
  65. Subject: Saving the screen
  66.  
  67. 31. *****
  68.  Q: How does one store, and then restore the original screen?
  69.  
  70.  A: Here is a simple outline for storing and restoring a text mode
  71. screen in the standard 80 x 25 mode. Note that the code below is
  72. incomplete in a sense that it works for a color monitor only,
  73. because the monochrome screen address is $B000:$0000.
  74.    For storing and restoring the graphics screen see Ohlsen & Stoker
  75. (1989), Turbo Pascal Advanced Techniques, Que, pp 333-337.
  76.   uses Crt;
  77.   type ScreenType = array [1..4000] of byte;        (* 2 x 80 x 25 *)
  78.   var ColorScreen : ScreenType Absolute $B800:$0000;
  79.       SavedScreen : ScreenType;
  80.       posx, posy : byte;
  81.   begin
  82.     SavedScreen := ColorScreen;      (* Save the screen *)
  83.     posx := WhereX; posy := WhereY;  (* Save the cursor position *)
  84.     writeln ('A simple demo storing and restoring the color text screen');
  85.     writeln ('By Prof. Timo Salmi, ts@uwasa.fi');
  86.     writeln; write ('Press <-'''); readln;
  87.     ColorScreen := SavedScreen;   (* Restore the screen *)
  88.     GotoXY(posx,posy);            (* Go to the stored cursor position *)
  89.   end.
  90. If you would prefer not using the Crt unit, you can apply WHEREXFN,
  91. WHEREYFN, and GOATXY from TSUNTG.TPU from my units collection
  92. ftp://garbo.uwasa.fi/pc/ts/tspa3470.zip.
  93.    If you wish to test for the monitor type, that is choose between
  94. $B800:$0000 and $B000:$0000 bases, you can use the following
  95. function to test for the monochrome adapter.
  96.   function MONOFN : boolean;
  97.   var regs : registers;
  98.   begin
  99.     FillChar (regs, SizeOf(regs), 0);
  100.     regs.ah := $0F;
  101.     Intr ($10, regs);
  102.     monofn := (regs.al = 7);
  103.   end;  (* monofn *)
  104. --------------------------------------------------------------------
  105.  
  106. From ts@uwasa.fi Wed Jul 5 00:00:32 1995
  107. Subject: Converting TPUs
  108.  
  109. 32. *****
  110.  Q: How can I convert a TPU unit of one TP version to another?
  111.  
  112.  A: Forget it. In practical terms such a conversion is not on. The
  113. Turbo Pascal TPU units are strictly version dependent. If there were
  114. a working solution I assume we would have heard of it long since.
  115. The hacks that have been tried won't solve this dilemma. For all
  116. practical purposes you need the source code and the relevant
  117. compiler version.
  118.    You may nevertheless wish to ascertain for which version a TPU
  119. unit has been compiled. This is very simple. Just look at the first
  120. four character of a TPU file. The codes are
  121.  TPU0  for 4.0
  122.  TPU5  for 5.0
  123.  TPU6  for 5.5
  124.  TPU9  for 6.0
  125.  TPUQ  for 7.0 real mode
  126. But don't go editing these. It will not get you anywhere.
  127. --------------------------------------------------------------------
  128.  
  129. From ts@uwasa.fi Wed Jul 5 00:00:33 1995
  130. Subject: Finding about runtime errors
  131.  
  132. 33. *****
  133.  Q: Which error is e.g. Runtime error 205
  134.  
  135.  A: Basically this is a case of RTFM (read the f*ing manual). But it
  136. is very easy to find out even without resorting to the manual. Put
  137. temporarily the statement RunError (205); as the first statement of
  138. your program. Then run your program from the Turbo Pascal IDE, that
  139. is from within the TP editor. The description of the error will
  140. appear.
  141.    If you run a program from within a Turbo Pascal IDE, it is
  142. advisable to turn on the debug options on. You'll get both the error
  143. number and the description. Furthermore by pressing F1 after the
  144. error you get its description in a more verbal format.
  145.    One further trick is to put "uses TSERR"; (Include verbal
  146. run-time error messages) into your program. If you do that, the
  147. run-time errors will be given with a verbal description not just as
  148. a number. TSERR.TPU is part of my TPU collection at Garbo
  149. ftp://garbo.uwasa.fi/pc/ts/tspa3470.zip.
  150.    In TP 7.0 the run time errors can also be found by invoking
  151. "Help" from the main manu (Alt-H) and selecting "Error messages".
  152. --------------------------------------------------------------------
  153.  
  154. From ts@uwasa.fi Wed Jul 5 00:00:34 1995
  155. Subject: Opening read-only files
  156.  
  157. 34. *****
  158.  Q: Why can't I open read-only files? I get "File access denied".
  159.  
  160.  A: The answer is rather simple, but it is not well displayed in the
  161. manuals. In order to read a read-only file you have to set the
  162. FileMode as 0 like below. Else you'll get runtime error 005 "File
  163. access denied".
  164.   var f      : text;          (* Can be any file type *)
  165.       savefm : byte;
  166.   begin
  167.     savefm := FileMode;       (* Save the current FileMode status *)
  168.     FileMode := 0;            (* The default is 2 *)
  169.     assign (f, 'readonly.txt');
  170.     reset (f);
  171.     { have your wicked ways }
  172.     close (f);
  173.     FileMode := savefm;       (* Restore the original FileMode *)
  174.   end.
  175. --------------------------------------------------------------------
  176.  
  177. From ts@uwasa.fi Wed Jul 5 00:00:35 1995
  178. Subject: Getting a nybble from a byte
  179.  
  180. 35. *****
  181.  Q: I have a variable of type BYTE and would like to extract two
  182. numbers from it. (The first 4 bits making up number A, the second 4
  183. bits making up number B).  How can I extract these two numbers?
  184.  
  185.  A: Ah, this questions bring back the good bad old days of the
  186. Commodore C64 programming when bit operations were rather a rule
  187. than an exception. Here is the solution.
  188.   function HIBYTEFN (x : byte) : byte;
  189.   begin
  190.     hibytefn := x Shr 4;           (* Shift right by four bits *)
  191.   end;
  192.   {}
  193.   function LOBYTEFN (x : byte) : byte;
  194.   begin
  195.     lobytefn := x and 15;          (* x and 00001111 *)
  196.   end;
  197. From Patrick Taylor (exuptr@exu.ericsson.se): Ah, leave it to Timo
  198. to come up with a different way! An other is (n div 16)
  199. (n mod 16).
  200.    Patrick is right.  But unless the compiler is optimized, the
  201. former produces more efficient code. Not that it really makes any
  202. practical difference whatsoever.
  203.    Of course the fastest code is produced using assembler as pointed
  204. out by Maarten Pennings (maarten@cs.ruu.nl) who provided the
  205. following inline example:
  206.   function high(b:byte):byte;
  207.     inline($58         { POP AX      | AH=?, AL=b       }
  208.           /$30/$e4     { XOR AH,AH   | AH=0, AL=b       }
  209.           /$b9/$04/$00 { MOV CX,0004 | AH=0, AL=b, CL=4 }
  210.           /$d3/$e8     { SHR AX,CL   | AX=b shr 4       }
  211.           );
  212.  
  213.  A2: Getting a word from a longint can alternatively be achieved
  214. without any calculations by using a kind of typecasting. Below is
  215. the code I have utilized in ftp://garbo.uwasa.fi/pc/ts/tspa3470.zip.
  216.   (* Get the high-order word of the longint argument *)
  217.   function HIWORDFN (x : longint) : word;
  218.   type type1 = record
  219.                  low  : word;
  220.                  high : word;
  221.                end;
  222.   var m1 : type1 absolute x;
  223.   begin
  224.     hiwordfn := m1.high;
  225.   end;  (* hiwordfn *)
  226. --------------------------------------------------------------------
  227.  
  228. From ts@uwasa.fi Wed Jul 5 00:00:36 1995
  229. Subject: Setting hi-intensity background
  230.  
  231. 36. *****
  232.  Q: How can I set a hi-intensity color background in the text mode?
  233.  
  234.  A: As you should know, the you can test for a blinking text for
  235. example as follows.
  236.   uses Crt;
  237.   begin
  238.     TextColor (11 + 128);  (* or LightCyan + Blink *)
  239.     TextBackground (Blue);
  240.     writeln ('What''s the catch?');  (* An aside, note the '' pair *)
  241.   end.
  242. In the above, bit 7 (the 128) controls the blinking. If you have at
  243. least an EGA, you can alter the interpretation of the highest text
  244. color bit to denote a hi-intensity background, but then you lose the
  245. the blinking. The following piece of code disables blinking,
  246. enabling a hi-intensity background.
  247.   uses Dos;
  248.   var regs : registers;
  249.   begin
  250.     FillChar (regs, SizeOf(regs), 0); (* An initialization precaution *)
  251.     regs.ah := $10;                   (* Function $10 *)
  252.     regs.al := $03;                   (* Subfunction $03 *)
  253.     regs.bl := $00;
  254.     Intr ($10, regs);      (* ROM BIOS video driver interrupt *)
  255.   end.
  256. To enable blinking again, set regs.bl := $01; Any high-intensity
  257. background you may have currently on the screen, will instantly
  258. change into a blinking text a a low-intensity background.
  259.  
  260.  A2: The previous answer assumes at least an EGA. Otherwise ports
  261. must be accessed. This is both advanced and dangerous programming,
  262. because errors in handling posts can do real harm. Besides it is
  263. fair to require at least an EGA in writing modern programs, at least
  264. for non-laptops, and on the latter the colors don't really matter
  265. for CGA and below. Let's take a look, nevertheless, how this is done
  266. for a CGA. Note that this won't work an an EGA and beyond, not at
  267. least in my tests. For detecting the video adapter you have, see the
  268. DetectGraph procedure in you Turbo Pascal manual.
  269.    First we need some basics from MEMORY.LST in Ralf Brown's
  270. ftp://garbo.uwasa.fi/pc/programming/inter46b.zip (or whatever
  271. version is current):
  272.  Format of BIOS Data Segment at segment 40h:
  273.   63h WORD Video CRT controller base address: color=03D4h, mono=03B4h
  274.   65h BYTE Video current setting of mode select register 03D8h/03B8h
  275. From David Jurgens's ftp://garbo.uwasa.fi/pc/programming/helppc21.zip
  276. we see
  277.   3D0-3DF Color Graphics Monitor Adapter (ports 3D0-3DB are
  278.           write only, see 6845)
  279.   3D8 6845 Mode control register (CGA, EGA, VGA, except PCjr)
  280. From Darryl Friesen's (friesend@jester.usask.ca) in the late
  281. comp.lang.pascal we have, the following procedure, with my own added
  282. comments (* *).
  283.   procedure SetBlinkState (state : boolean);
  284.   var ModeRegPort : word;
  285.       ModeReg     : byte;
  286.   begin
  287.     Inline($FA); { CLI }           (* Interrupts off *)
  288.     ModeRegPort := MemW[$0040:$0063]+4;  (* Typically $03D4+4 = $03D8 *)
  289.     ModeReg := Mem[$0040:$0065];   (* Typically 1001 *)
  290.     if state then                  (* Bit 5 controls blink enable *)
  291.       ModeReg := ModeReg or $20    (* $20 = 00100000 (base2) *)
  292.     else
  293.       ModeReg := ModeReg and $DF;  (* $DF = 11011111 disable *)
  294.     Port[ModeRegPort] := ModeReg;  (* Typically $9 = 00001001 *)
  295.     Mem[$0040:$0065] := ModeReg;   (*       or $29 = 00101001 *)
  296.     Inline($FB) { STI }            (* Interrupts on *)
  297.   end;
  298. --------------------------------------------------------------------
  299.  
  300. From ts@uwasa.fi Wed Jul 5 00:00:37 1995
  301. Subject: Pascal to C
  302.  
  303. 37. *****
  304.  Q: Where can I find a program to convert (Turbo) Pascal to C?
  305.  
  306.  A: This is a relevant question, but I have placed elsewhere the
  307. tips on the "looking for a program" questions. Here are the
  308. pointers to further pointers :-). (The FAQ versions might have been
  309. updated since I wrote this.)
  310.  ftp://garbo.uwasa.fi/pc/pd2/camfaq.zip
  311.  camfaq.zip comp.archives.msdos.(d/announce) FAQ (general finding)
  312.  :
  313.  ftp://garbo.uwasa.fi/pc/ts/tsfaqn43.zip
  314.  tsfaqn43.zip Questions from UseNet and Timo's answers
  315.  :
  316.  ftp://garbo.uwasa.fi/pc/pd2/faquote.zip
  317.  faquote.zip Old information from tsfaq Frequently Asked Questions
  318. --------------------------------------------------------------------
  319.  
  320. From ts@uwasa.fi Wed Jul 5 00:00:38 1995
  321. Subject: Turning off the input echo
  322.  
  323. 38. *****
  324.  Q: How can I read input without echoing to the screen?
  325.  
  326.  A: It is fairly simple. Study this example source code, with the
  327. manual, if need be.
  328.   uses Crt;
  329.   var password : string;
  330.   {}
  331.   (* Read without echoing *)
  332.   procedure GETPASS (var s : string);
  333.   var key : integer;
  334.       ch : char;
  335.   begin
  336.     s := '';
  337.     repeat
  338.       ch := ReadKey; key := ord (ch);
  339.       case key of
  340.          0 : ch := ReadKey;  (* Discard two-character keys, like F1 *)
  341.         13 : exit;           (* Enter has been pressed *)
  342.         1..12,13..31,255 :;  (* Discard the special characters *)
  343.         else s := s + ch;
  344.       end;
  345.    until false;
  346.   end;  (* getpass *)
  347.   {}
  348.   (* The main program *)
  349.   begin
  350.     write ('Password: ');
  351.     GETPASS (password);
  352.     writeln;
  353.     writeln (password);
  354.   end.
  355.   {}
  356. If you wish to be able to edit the input stream, like having the
  357. BackSpace functional, that is more complicated, and is left as an
  358. exercise after these basics. A hint: 8 : Delete (s, Length(s), 1);
  359.    There is another approach to this problem pointed out by Colin
  360. Lamond colin@sound.demon.co.uk. Quite innovative in its simplicity
  361. once one comes to think of it. "Set the textcolor, and the
  362. textbackground to the same color, and so the typed text can not be
  363. seen on the screen."
  364. --------------------------------------------------------------------
  365.  
  366. From ts@uwasa.fi Wed Jul 5 00:00:39 1995
  367. Subject: Input line-editing
  368.  
  369. 39. *****
  370.  Q: How can I edit the readln input stream?
  371.  
  372.  A: In practice, if you wish to use anything beyond simple the
  373. BackSpace deleting, you'll have to build your own line editing
  374. routines expanding on the code in the previous item. It is quite a
  375. task, and you can alternatively find the preprogrammed routines in
  376. my Turbo Pascal units ftp://garbo.uwasa.fi/pc/ts/tspa3470.zip (or
  377. whatever version number is current).
  378.  EDRDEBLN Editable Readln with ctrl-c, break trapping, pre-fill etc
  379.  EDRDEFLN Editable Readln with recall, pre-fill, and insert toggle
  380.  EDRDLN   Readln with line-editing potential (the simplest)
  381.  EDREABLN Edreadln with ctrl-c and break trapping
  382.  EDREADLN Editable Readln with recall, and insert toggle
  383. --------------------------------------------------------------------
  384.  
  385. From ts@uwasa.fi Wed Jul 5 00:00:40 1995
  386. Subject: Executable branding
  387.  
  388. 40. *****
  389.  Q: How can I write (brand) something into my executables?
  390.     Here is the actual question that led me to writing this item: 'I
  391.     am very interested in the .EXE "branding" techniques you use in
  392.     your TSUNTI unit. Would it be possible to get hold of the source
  393.     code for that unit, as it would save me from having to re-invent
  394.     the wheel?'
  395.  
  396.  A: What you are referring to is
  397.  BRANDEXE Store information within your program's .exe file (MS-DOS 3.0+)
  398.  CHKSUMFN Checksum self-test to detect any tampering (MS-DOS 3.0+)
  399.  USECOUNT Get the number of times the program has been used
  400. Sorry no, I don't want to distribute my source codes from
  401. ftp://garbo.uwasa.fi/pc/turbopas/ts/tspa3470.zip. Besides they would
  402. be less useful to you than you may think because internally my
  403. programs are in Finnish, comments, variable and procedure names, and
  404. all. But I can hopefully help you by giving a reference to a similar
  405. code.  Please see Ohlsen & Stoker, Turbo Pascal Advanced Techniques,
  406. Que, 1989, p. 420.
  407. --------------------------------------------------------------------
  408.  
  409. From ts@uwasa.fi Wed Jul 5 00:00:41 1995
  410. Subject: Elusive, inconsistent errors
  411.  
  412. 41. *****
  413.  Q: What is wrong with my program? It hangs without a clear pattern?
  414.  
  415.  A: With experience one learns that some programming errors are very
  416. elusive. I have many times seen users declaring that they have found
  417. a bug in Turbo Pascal, but in the overwhelming majority of cases it
  418. still is just a programming error, which just is more difficult to
  419. find than the more clear-cut cases. When you have symptoms like your
  420. program crashing from within the IDE, but working seemingly all
  421. right when called as stand-alone, or something equally strange, you
  422. might have one of the following problems.
  423. - A variable or some variables in your code are uninitialized thus
  424.   getting random values, which differ depending on your environment.
  425. - Your indexes are overflowing. Set on the range check {$R+}
  426.   directive for testing.
  427. - An error in the pointer logic.
  428. Normal debugging does not necessarily help in locating these errors
  429. because one is easily led to debugging the wrong parts of one's
  430. program. Especially the latter two reasons can cause errors which
  431. seemingly have nothing to do with the actual cause. This results
  432. from the fact that indexing and pointer errors can overwrite parts
  433. of memory causing strange quirks in your program. If you have used
  434. indexing with {$R-} or if you use pointer operations, sooner or
  435. later you are bound to have these problems in developing your
  436. applications.
  437.    See Edward Mitchell (1993), Borland Pascal Developer's Guide,
  438. 275-288 for common programming errors and especially the information
  439. on memory clobbering, in a useful chapter on debugging Turbo Pascal
  440. programs. You might also take a look at your Turbo Pascal User's
  441. Guide. At least version 7.0 has an instructive general
  442. categorization of errors on pages 76-77.
  443. --------------------------------------------------------------------
  444.  
  445. From ts@uwasa.fi Wed Jul 5 00:00:42 1995
  446. Subject: Converting the number base
  447.  
  448. 42. *****
  449.  Q: How do I convert a decimal word into a hexadecimal string, etc?
  450.  
  451.  A: Here is one possibility
  452.   function HEXFN (decimal : word) : string;
  453.   const hexDigit : array [0..15] of char = '0123456789ABCDEF';
  454.   begin
  455.     hexfn := hexDigit[(decimal shr 12)]
  456.           + hexDigit[(decimal shr 8) and $0F]
  457.           + hexDigit[(decimal shr 4) and $0F]
  458.           + hexDigit[(decimal and $0F)];
  459.   end;  (* hexfn *)
  460. Here is another conversion example (from longint to binary string)
  461.   function LBINFN (decimal : longint) : string;
  462.   const BinDigit : array [0..1] of char = '01';
  463.   var i     : byte;
  464.       binar : string;
  465.   begin
  466.     FillChar (binar, SizeOf(binar), ' ');
  467.     binar[0] := chr(32);
  468.     for i := 0 to 31 do
  469.       binar[32-i] := BinDigit[(decimal shr i) and 1];
  470.     lbinfn := binar;
  471.   end;  (* lbinfn *)
  472. For a full set of conversions, both from and to decimal, apply
  473. TSUTNTB.TPU from ftp://garbo.uwasa.fi/pc/ts/tspa3470.zip.
  474. --------------------------------------------------------------------
  475.  
  476. From ts@uwasa.fi Wed Jul 5 00:00:43 1995
  477. Subject: Identifying the last drive
  478.  
  479. 43. *****
  480.  Q: How to determine the last drive?
  481.  
  482.  A: One way of doing that is utilizing the information in DPB, that
  483. is the Drive Parameter Block, but that is rather complicated, so you
  484. can find that without source code in the TSUNTH unit in
  485. ftp://garbo.uwasa.fi/pc/ts/tspa3470.zip .
  486.  Another way is using interrupt 21H, function 36H to detect if a
  487. drive exists starting from the first drive letter. The code is given
  488. below. The disadvantage of this method is that it does not
  489. distinguish between real and substituted drives.
  490.   uses Dos;
  491.   function LASTDFN : char;  (* Detect last harddisk letter *)
  492.   var regs : registers;
  493.       i    : byte;
  494.   begin
  495.     i := 2;
  496.     repeat
  497.       Inc(i);
  498.       FillChar (regs, SizeOf(regs), 0);
  499.       regs.ah := $36;
  500.       regs.dl := i;
  501.       MsDos(regs);
  502.     until (regs.ax = $FFFF);
  503.     lastdfn := chr(i+63);
  504.   end;  (* lastdfn *)
  505. --------------------------------------------------------------------
  506.  
  507. From ts@uwasa.fi Wed Jul 5 00:00:44 1995
  508. Subject: Clock display in a TP program
  509.  
  510. 44. *****
  511.  Q: How can I put a running clock into my Turbo Pascal program?
  512.  
  513.  A: We are not speaking of a stand-alone TSR-clock (which is a
  514. different task), but considering a clock that continuously displays
  515. the time in some part of the output screen of your Turbo Pascal
  516. program.
  517.     You might first want to read the earlier items about ReadKey
  518. usages if you are not familiar with it (you probably are, because
  519. you would not pose this advanced question if you were a novice). The
  520. items are the unlikely "How do I disable or capture the break key in
  521. Turbo Pascal?" and "How can I read input without echoing to the
  522. screen?"
  523.    The general idea is to make the body of the program a repeat
  524. until loop using ReadKey for input and updating the clock display
  525. at suitable junctions within the loop. The scheme is thus something
  526. like the following.
  527.   procedure showtime;
  528.     begin
  529.       { if the second has changed, write the time }
  530.     end;
  531.   :
  532.   repeat
  533.     { do whatever }
  534.     showtime;
  535.     if KeyPressed then
  536.       case ReadKey of
  537.         { whatever }
  538.         { exit rules }
  539.       end;
  540.     showtime;
  541.     :
  542.     showtime;
  543.   until false;
  544.    One trick of the trade is that you must not update your clock
  545. each time the clock routine is encountered. You should test if the
  546. second has changed, and update only then. Else you are liable to get
  547. an annoying flicker in your clock.
  548. --------------------------------------------------------------------
  549.  
  550. From ts@uwasa.fi Wed Jul 5 00:00:45 1995
  551. Subject: Is a name a directory
  552.  
  553. 45. *****
  554.  Q: How to establish if a name refers to a directory or not?
  555.  
  556.  A: This question has turned out a bit more complicated than I first
  557. thought. There are several methods, each with some catch. The first
  558. is trying to open the name as a file and observing the IOResult. The
  559. ISDIRFN function in ftp://garbo.uwasa.fi/pc/ts/tspa3470.zip TPU unit
  560. TSUNTJ.TPU is based on this method. Unfortunately it is not always
  561. stable. I have been reported problems in connection with DRDOS by
  562. Richard Breuer (ricki@pool.informatik.rwth-aachen.de) who has
  563. tested these routines.
  564.   The second method (ISDIR2FN) is based on the fact that the file
  565. NUL exists in a directory if the directory exists.
  566.   The thrid method (ISDIR3FN) is a brute force method. It is given
  567. below, since it is quite an instructive little exercise of Turbo
  568. Pascal programming.
  569.   (* Search recursively through a drive's directories.
  570.      Auxiliary, recursive procedure for ISDIR3FN *)
  571.   procedure SEARCHDR (Path, FileSpec : string;
  572.                       name           : string;
  573.                       var found      : boolean);
  574.   var FileInfo : SearchRec;
  575.   begin
  576.     FindFirst (Path + '*.*', Directory, FileInfo);
  577.     while DosError = 0 do
  578.       begin
  579.         if ((FileInfo.Attr and Directory) > 0) and
  580.             (FileInfo.Name <> '.') and
  581.             (FileInfo.Name <> '..') then
  582.               begin
  583.                 SEARCHDR (Path + FileInfo.Name + '\',
  584.                           FileSpec,
  585.                           name,
  586.                           found);
  587.                 if Path + FileInfo.Name + '\' = name then
  588.                   found := true;
  589.               end;
  590.         FindNext (FileInfo);
  591.       end; {while}
  592.   end;  (* searchdr *)
  593.  
  594.   (* Does a name refer to a directory *)
  595.   function ISDIR3FN (name : string) : boolean;
  596.   var drive : char;
  597.       found : boolean;
  598.   begin
  599.     {... Default value ...}
  600.     isdir3fn := false;
  601.     {... Discard empty names ...}
  602.     if name = '' then exit;
  603.     {... Expand into a fully qualified name, makes it uppercase ...}
  604.     name := FExpand (name);
  605.     if name[Length(name)] <> '\' then name := name + '\';
  606.     {... Extract the drive letter from the name ...}
  607.     drive := UpCase (name[1]);
  608.     {... Check first for the root ...}
  609.     if drive + ':\' = name then
  610.       begin isdir3fn := true; exit; end;
  611.     {... Check the rest of the directories recursively ...}
  612.     found := false;
  613.     SEARCHDR (drive + ':\', '*.*', name, found);
  614.     isdir3fn := found;
  615.   end;  (* isdir3fn *)
  616.  
  617. -Date: Mon, 13 Jun 1994 00:13:05 +0000 (GMT)
  618. -From: JEROEN SCHIPPER <JSCHIPPER@HUT.NL>
  619. -To: ts@uwasa.fi (Timo Salmi)
  620. -Subject: Is a name a directory in TP
  621.  
  622. The method I use is simply checking the attribute bit, as this small
  623. program will demonstrate:
  624.   program isdir;
  625.   uses dos;
  626.   var s:string;
  627.       attr:word;
  628.       f:file;
  629.   begin
  630.     repeat
  631.       readln(s);
  632.       if s = '' then break;
  633.       assign(f,s);
  634.       getfattr(f,attr);
  635.       if doserror <> 0 then
  636.         writeln('DOS error code = ', doserror)
  637.       else
  638.       begin
  639.         if attr and directory <> 0 then
  640.           writeln(S,' is a directory')
  641.         else
  642.           writeln(S,' is a not directory')
  643.       end;
  644.     until false;
  645.   end.
  646. The methods you mention in your faq are far more complicated, but
  647. why? Is there are catch why the method above won't work? I guess
  648. don't really understand the problem here.
  649. Jeroen.
  650.  
  651.  A2: This has turned out to be a tricky FAQ. There are some
  652. additional suggestions and comments from the gentle readers. You can
  653. track them from ftp://garbo.uwasa.fi/pc/ts/tspost00.zip index.
  654. --------------------------------------------------------------------
  655.  
  656. From ts@uwasa.fi Wed Jul 5 00:00:46 1995
  657. Subject: Disabling alt-ctrl-del
  658.  
  659. 46. *****
  660.  Q: How does one disable alt-ctrl-del?
  661.  
  662.  A: I can only give a pointer to source code. Take a look at
  663.  4067 Jul 1 1993 ftp://garbo.uwasa.fi/pc/turbopa7/cadthf10.zip
  664.  cadthf10.zip CadThief TP6+ unit for trapping ctrl+alt+del, M.Hanninen
  665. and
  666.  30673 Oct 13 1987 ftp://garbo.uwasa.fi/pc/turbopas/keyint.zip
  667.  keyint.zip Disable alt-ctrl-del + other int09h TP tricks, N.Rubenking
  668. and
  669.  7105 Apr 19 10:51 ftp://garbo.uwasa.fi/pc/turbopa7/cad_int9.zip
  670.  cad_int9.zip Disable Ctrl-Alt-Del via new TP kb interrupt, J.Robertson
  671.    I have utilized alt-ctrl-del disabling at least in one of my own
  672. programs (PESTIKID.EXE). The code is not available, but the general
  673. idea is replacing the old keyboard interrupt ($09) with a handler of
  674. one's own. If the handler detects alt-ctrl-del, the keyboard is
  675. reset, else the handler is chained back to the original interrupt.
  676. The chaining requires a rather complicated inline procedure provided
  677. in TurboPower Software's kit. An additional complication is that the
  678. del keypress must be intercepted already at the relevant port $60,
  679. and the alt and ctrl status must be tested, so that the rebooting
  680. will not be invoked. Resetting the keyboard requires accessing the
  681. $20 and $61 ports.
  682. --------------------------------------------------------------------
  683.  
  684. From ts@uwasa.fi Wed Jul 5 00:00:47 1995
  685. Subject: Does a file exist
  686.  
  687. 47. *****
  688.  Q: How can I test whether a file exists?
  689.  
  690.  A: There are several alternatives. Here is the most common with
  691. example code. It recognizes also read-only, hidden and system files.
  692.   function FILEXIST (name : string) : boolean;
  693.   var fm : byte;
  694.       f  : file;
  695.       b  : boolean;
  696.   begin
  697.     fm := FileMode;
  698.     FileMode := 0;
  699.     assign (f, name);
  700.     {$I-} reset(f); {$I+}
  701.     b := IOResult = 0;
  702.     if b then close(f);
  703.     filexist := b;
  704.     FileMode := fm;
  705.   end;
  706.  
  707. A second alternative is
  708.   Uses Dos;
  709.   function FILEXIST (name : string) : boolean;
  710.   var f  : file;
  711.       a  : word;
  712.   begin
  713.     assign (f, name);
  714.     GetFAttr (f, a);
  715.     filexist := false;
  716.     if DosError = 0 then
  717.       if ((a and Directory) = 0) and ((a and VolumeId) = 0) then
  718.         filexist := true;
  719.   end;
  720.  
  721. A third alternative is
  722.   Uses Dos;
  723.   function FILEXIST (name : PathStr) : boolean;
  724.   begin
  725.     filexist := FSearch (name, '') <> '';
  726.   end;
  727.  
  728. A fourth alternative is the following. Be careful with this option,
  729. since it works a bit differently from the others. It accepts wild
  730. cards. Thus, for example FILEXIST('c:\autoexec.*') would be TRUE in
  731. this method, while FALSE in all the above.
  732.   Uses Dos;
  733.   function FILEXIST (name : string) : boolean;
  734.   var f : SearchRec;
  735.   begin
  736.     filexist := false;
  737.     FindFirst (name, AnyFile, f);
  738.     if DosError = 0 then
  739.       if (f.attr <> Directory) and (f.attr <> VolumeId) then
  740.         filexist := true;
  741.   end;
  742. A good variation from KDT@newton.national-physical-lab.co.uk of this
  743. theme, disallowing wildcards:
  744.   function file_exists (fname :string) :boolean;
  745.   var f :searchrec;
  746.   begin
  747.     findfirst (fname, anyfile - directory - volumeid, f);
  748.     file_exists := (doserror + pos('*',fname) + pos('?',fname) = 0);
  749.   end;
  750.  
  751. --------------------------------------------------------------------
  752.  
  753. From ts@uwasa.fi Wed Jul 5 00:00:48 1995
  754. Subject: The current program name
  755.  
  756. 48. *****
  757.  Q: What is the name of the current Turbo Pascal program?
  758.  
  759.  A: The name of the currently executing Turbo Pascal program is in
  760. ParamStr(0).
  761.    This was introduced in TP version 5.0, and as far as I recall at
  762. least MS-DOS version 3.0 is required. For TP 4.0 you can use
  763. "ParamStr0 The name of the program" from TSUNT45 in
  764. ftp://garbo.uwasa.fi/pc/ts/tspa3440.zip (or whatever the version
  765. number is the latest).
  766.    It is advisable to put the value into a string variable at be
  767. beginning of the program before eny I/O takes place. Thus you might
  768. wish to use:
  769.   var progname : string;
  770.   begin  { the main program }
  771.     progname := ParamStr(0);
  772.     :
  773. A bonus of this method is that you can access the individual
  774. characters of progname (e.g. progname[1] for the drive) while that
  775. is not possible to do for the ParamStr keyword.
  776. --------------------------------------------------------------------
  777.  
  778. From ts@uwasa.fi Wed Jul 5 00:00:49 1995
  779. Subject: How can a program reboot my PC?
  780.  
  781. 49. *****
  782.  Q: How is the code for rebooting the PC written in Turbo Pascal?
  783.  
  784.  A: This item draws from the information and the C-code example in
  785. Stan Brown's, later J.Carlyle's comp.os.msdos.programmer FAQ,
  786. ftp://garbo.uwasa.fi/pc/doc-net/dosfv204.zip (at the time of
  787. updating this), from memory.lst and interrup.b in
  788. ftp://garbo.uwasa.fi/pc/programming/inter46b.zip, and from
  789. ftp://garbo.uwasa.fi/pc/programming/helppc21.zip. The Turbo Pascal
  790. code is my adaptation of the C-code. It is not a one-to-one port.
  791.    The usually advocated warm-boot method is storing $1234 in the
  792. word at $0040:$0072 and jumping to address $FFFF:$0000. The problem
  793. with this approach is that files must first be closed, potential
  794. caches flushed. This is how to do this
  795.   procedure REBOOT;
  796.   label next;
  797.   var regs  : registers;
  798.       i     : byte;
  799.       ticks : longint;
  800.   begin
  801.     {... "press" alt-ctrl ...}
  802.     mem[$0040:$0017] := mem[$0040:$0017] or $0C;  { 00001100 }
  803.     {... "press" del, try a few times ...}
  804.     for i := 1 to 10 do
  805.       begin
  806.         FillChar (regs, sizeOf(regs), 0);  { initialize }
  807.         regs.ah := $4F;  { service number }
  808.         regs.al := $53;  { del key's scan code }
  809.         regs.flags := FCarry;  { "sentinel for ignoring key" }
  810.         Intr ($15, regs);
  811.         {... check if the del key registered, if not retry ...}
  812.         if regs.flags and Fcarry > 0 then goto next;
  813.         {... waste some time, watch out for midnight ...}
  814.         ticks := MemL [$0040:$006C];
  815.         repeat until (MemL[$0040:$006C] - ticks > 3) or
  816.                      (MemL[$0040:$006C] - ticks < 0)
  817.     end; {for}
  818.     exit;
  819.   next:
  820.     {... disk reset: writes all modified disk buffers to disk ...}
  821.     FillChar (regs, sizeOf(regs), 0);
  822.     regs.ah := $0D;
  823.     MsDos (regs);
  824.     {... set post-reset flag, use $0000 instead of $1234 for coldboot ...}
  825.     memW[$0040:$0072] := $1234;
  826.     {... jump to $FFFF:0000 BIOS reset ...}
  827.     Inline($EA/$00/$00/$FF/$FF);
  828.   end;  (* reboot *)
  829. One slight problem with this approach is that the keyboard intercept
  830. interrupt $15 service $4F requires at least an AT according to
  831. ftp://garbo.uwasa.fi/pc/programming/inter46b.zip. A simple test
  832. based on "FFFF:E byte ROM machine id" (the previous definition is
  833. from ftp://garbo.uwasa.fi/pc/programming/helppc21.zip) is:
  834.   function ISATFN : boolean;
  835.   begin
  836.      case Mem[$F000:$FFFE] of
  837.        $FC, $FA, $F8 : isatfn := true;
  838.        else isatfn := false;
  839.      end; {case}
  840.   end;  (* isatfn *)
  841. For a more comprehensive test use CPUFN "Get the type of the
  842. processor chip" from TSUNTH in ftp://garbo.uwasa.fi/pc/ts/tspa3470.zip
  843. or see the TP + ASM code in Michael Ticher (1992), PC Intern System
  844. Programming, pp. 725-727.
  845.  
  846.    An addition by Per Bergland (d6caps@dtek.chalmers.se): I recently
  847. downloaded the FAQ for this newsgroup, and studied the code for
  848. rebooting a PC. The problem with that code (calling FFFF:0000) is
  849. that it will not work in protected mode programs such as those
  850. compiled for Windows or BP7 DPMI, or even in a DOS program run in a
  851. Windows DOS session. The solution provided has been tested on
  852. various COMPAQ PC:s, but I think it will work on any AT-class
  853. machine. It involves using the 8042 keyboard controller chip output
  854. pin 0, which is physically connected to the reset pin of the CPU.
  855. There is unfortunately no way to perform a "warm" reboot this way,
  856. and the warnings about disk caches etc apply to this code, too (see
  857. FAQ). The code is written in BP7 assembly lingo, because that's what
  858. I normally write code in, but anyone could rewrite it in C or high
  859. level Pascal.
  860.   UNIT Reboot;
  861.   INTERFACE
  862.     procedure DoReboot;
  863.   IMPLEMENTATION
  864.     procedure DoReboot;assembler;
  865.     asm
  866.       cli
  867.   @@WaitOutReady:       { Busy-wait until 8042 is ready for new command}
  868.       in al,64h         { read 8042 status byte}
  869.       test al,00000010b { Bit 1 of status indicates input buffer full }
  870.       jnz @@WaitOutReady
  871.       mov al,0FEh       { Pulse "reset" = 8042 pin 0 }
  872.       out 64h,al
  873.       { The PC will reboot now }
  874.     end;
  875.   END.
  876. --------------------------------------------------------------------
  877.  
  878. From ts@uwasa.fi Wed Jul 5 00:00:50 1995
  879. Subject: Writing inline code
  880.  
  881. 50. *****
  882.  Q: How can I write inline code?
  883.  
  884.  A: In Turbo Pascal versions prior 6.0 assembler code could not be
  885. directly included in the code. Instead one had to assemble the code
  886. into inline statements. Consider the task of rebooting the PC
  887. (without disk closing and cache flushing).  The assembler code for
  888. this is
  889.   mov ax,$40
  890.   mov ds,ax
  891.   mov wo [$72],$1234
  892.   jmp $FFFF:$0000
  893. To assemble this code into an inline statement write the following
  894. file calling it e.g. debug.in.  The empty line is important. Also
  895. carefully note that debug assumes hexadecimal notation. Do not use
  896. the $ designator in debug.in.
  897.   .... begin debug.in, cut here ....
  898.   a 100
  899.   mov ax,40
  900.   mov ds,ax
  901.   mov wo [72],1234
  902.   jmp FFFF:0000
  903.  
  904.   u 100
  905.   q
  906.   .... end debug.in, cut here ....
  907. Give the following command
  908.   debug < debug.in
  909. You'll get
  910.   0E9E:0100 B84000        MOV     AX,0040
  911.   0E9E:0103 8ED8          MOV     DS,AX
  912.   0E9E:0105 C70672003412  MOV     WORD PTR [0072],1234
  913.   0E9E:010B EA0000FFFF    JMP     FFFF:0000
  914. This translates into
  915.     Inline ($B8/$40/$00/
  916.             $8E/$D8/
  917.             $C7/$06/$72/$00/$34/$12/
  918.             $EA/$00/$00/$FF/$FF);
  919.  
  920.  A2: You can also utilize an inline <--> asm converter called
  921.   36337 Apr 26 1988 ftp://garbo.uwasa.fi/pub/pc/turbopas/inlin219.zip
  922.   inlin219.zip Inline assembler for Turbo Pascal, w/src, D.Baldwin
  923. It has two sources, inline.pas and uninline.pas which you can
  924. compile to do the conversions in both directions for you. For
  925. example, if you have a file test.asm containing the
  926.   mov ax,$0040
  927.   mov ds,ax
  928.   mov word ptr [$72],$1234
  929.   jmp far $FFFF:$0000
  930. then "inline test.asm" will produce test.obj with the following,
  931. expected contents
  932.   Inline(
  933.     $B8/$40/$00/           {mov ax,$0040}
  934.     $8E/$D8/               {mov ds,ax}
  935.     $C7/$06/$72/$00/$34/$12/ {mov word ptr [$72],$1234}
  936.     $EA/$00/$00/$FF/$FF);  {jmp far $FFFF:$0000}
  937. --------------------------------------------------------------------
  938.  
  939. From ts@uwasa.fi Wed Jul 5 00:00:51 1995
  940. Subject: Out of memory in compiling
  941.  
  942. 51. *****
  943.  Q: I am running out of memory when compiling my large program. What
  944.     can I do?
  945.  
  946.  A: If you are compiling your program from within the IDE (the
  947. Integrated Development Environment) then select the Option from the
  948. main menu, choose the Compiler item and set the Link buffer to
  949. Disk. (Also make the Compile option Destination to be Disk).
  950.    If this is not sufficient, next resort to using the TPC command
  951. line version of the Turbo Pascal compiler instead of the IDE.  Use
  952. the "Link buffer on disk" option.
  953.    Divide your program into units. It is advisable anyway for
  954. modularity when your program size grows.
  955.    If you have extended memory, instead of TURBO.EXE use TPX.EXE, if
  956. you have TP 7.0. If you are into protected mode programming then use
  957. Borland Pascal BP 7.0.
  958.  
  959.  A2: If you would prefer compiling your program from within the IDE
  960. but cannot do it for the above reason (or if you would prefer to
  961. compile your program from within your favorite editor instead of the
  962. TP IDE) you can use the following trick. If your editor has a macro
  963. language like most good editors do, the assign a hotkey macro that
  964. compiles the current file with the TPC. If you are using SemWare's
  965. QEdit editor you'll find such a macro in ("Macros and configurations
  966. for QEdit text-editor") ftp://garbo.uwasa.fi/pc/ts/tsqed17.zip.
  967.    Also your editor must be swapped to disk during the compilation
  968. if memory is critical. There is a very good program for doing that:
  969. ftp://garbo.uwasa.fi/pc/sysutil/shrom24b.zip ("Shell Room, Swap to
  970. disk when shelling to application"). For example I invoke the QEdit
  971. editor with using the following batch:
  972.  c:\tools\shroom -s r:\cmand -z 1024 c:\qedit\q %1 %2 %3 %4 %5 %6 %7
  973. You'll find more about the switches in the Shell Room documentation.
  974. The -s switch designates the swap destination (my r:\cmand directory
  975. is on my ramdisk). The -z switch sets the shell environment size.
  976.   An unfortunate part is that the TP 5.0 Turbo Pascal IDE is about
  977. the only program I know that is not amenable the to Shell Room
  978. utility, so you cannot utilize Shell Room to swap the TP IDE to
  979. disk. Blessfully, at least TP 7.0 no more has this problem.
  980. --------------------------------------------------------------------
  981.  
  982. From ts@uwasa.fi Wed Jul 5 00:00:52 1995
  983. Subject: Last position write woes
  984.  
  985. 52. *****
  986.  Q: How do I avoid scrolling in the last column of the last row?
  987.  
  988.  A: If you use write or writeln at the last column of the last row
  989. (usually 80,25) the screen will scroll. If you wish to avoid the
  990. scrolling you'll have to use an alternative write that does not move
  991. the cursor. Here is a procedure to write without moving the cursor
  992.   uses Dos;
  993.   procedure WriteChar (Character : char; fgColor, bgColor : byte);
  994.   var r : registers;
  995.   begin
  996.     FillChar (r, SizeOf(r), 0);
  997.     r.ah := $09;
  998.     r.al := ord(Character);
  999.     r.bl := (bgColor shl 4) or fgColor;
  1000.     r.cx := 1;    { Number of repeats }
  1001.     Intr ($10, r);
  1002.   end;  (* writechar *)
  1003. Thus, if you wish to write to the last column of the last row, you
  1004. must first move the cursor to that position. That can be done in
  1005. alternative ways. One might get there by having written previously
  1006. on the screen (with writeln and write routines) until one is in that
  1007. position. Another alternative is using GoToXY(80,20), but then you
  1008. have to use the Crt unit. If you don't want to use it, then you can
  1009. move the cursor by employing "GOATXY As the ordinary GoToXY but no
  1010. Crt unit required" from ftp://garbo.uwasa.fi/pc/ts/tspa3470.zip.
  1011.    There is an alternative interrupt service ($0A) which does the
  1012. same as service $09, but uses the default colors instead. Just
  1013. substitute $0A for $09, and leave the r.bl assignment out of the
  1014. WriteChar routine.
  1015.    Another option for writing anyhere on the screen without
  1016. affecting the cursor is using direct screen writes:
  1017.   uses Dos;
  1018.   procedure WriteChar (c : char; x, y : byte; fg, bg : byte);
  1019.   var vidstart : word;
  1020.       regs     : registers;
  1021.   begin
  1022.     FillChar (regs, SizeOf(regs), 0);
  1023.     regs.ah := $0F;
  1024.     Intr ($10, regs);  { Color or MonoChrome video adapter }
  1025.     if regs.al = 7 then vidstart := $B000 else vidstart := $B800;
  1026.     mem[vidstart:((y-1)*80+x-1)*2] := ord(c);
  1027.     mem[vidstart:((y-1)*80+x-1)*2+1] := (bg shl 4) or fg;
  1028.   end;
  1029. To write to the last position simply apply e.g.
  1030.   WriteChar ('X', 80, 25, 14, 0);  { Yellow on black }
  1031. The foreground (fg) and the background (bg) color codes are
  1032.   Black        =   0
  1033.   Blue         =   1
  1034.   Green        =   2
  1035.   Cyan         =   3
  1036.   Red          =   4
  1037.   Magenta      =   5
  1038.   Brown        =   6
  1039.   LightGray    =   7
  1040.   DarkGray     =   8
  1041.   LightBlue    =   9
  1042.   LightGreen   =  10
  1043.   LightCyan    =  11
  1044.   LightRed     =  12
  1045.   LightMagenta =  13
  1046.   Yellow       =  14
  1047.   White        =  15
  1048.   Blink        = 128
  1049. Yet another option is the following, but it needs the Crt unit. On
  1050. the other hand, it uses the default color. This is quite a good and
  1051. easy solution. I captured this fairly frequent idea from a posting
  1052. by Robert Buergi (nbuero@hslrswi.hasler.ascom.ch).
  1053.   uses Crt;
  1054.   procedure WriteToCorner (c : char);
  1055.   begin
  1056.     Inc (WindMax);
  1057.     GotoXY (80, 25);
  1058.     write (c);
  1059.     Dec (WindMax);
  1060.   end;  (* writeToCorner *)
  1061. --------------------------------------------------------------------
  1062.  
  1063. From ts@uwasa.fi Wed Jul 5 00:00:53 1995
  1064. Subject: Hiding a directory
  1065.  
  1066. 53. *****
  1067.  Q: How can one hide (or unhide) a directory using a TP program?
  1068.  
  1069.  A: Here is the code using interrupt programming. Incidentally,
  1070. since MS-DOS 5.0 the attrib command can be used to hide and unhide
  1071. directories.
  1072. (* Hide a directory. Before using it would be prudent to check
  1073.    that the directory exists, and that it is a directory.
  1074.    With a contribution from Jan Nielsen jak@hdc.hha.dk
  1075.    Based on information from Duncan (1986), p. 410 *)
  1076. procedure HIDE (dirname : string);
  1077. var regs : registers;
  1078. begin
  1079.   FillChar (regs, SizeOf(regs), 0);  { standard precaution }
  1080.   dirname := dirname + #0;           { requires ASCII strings }
  1081.   regs.ah := $43;                    { function }
  1082.   regs.al := $01;                    { subfunction }
  1083.   regs.ds := Seg(dirname[1]);        { point to the name }
  1084.   regs.dx := Ofs(dirname[1]);
  1085.   regs.cx := 2; { set bit 1 on }     { to unhide set regs.cx := 0 }
  1086.   Intr ($21, regs);                  { call the interrupt }
  1087.   if regs.Flags and FCarry <> 0 then { were we successful }
  1088.     writeln ('Failed to hide');
  1089. end;  (* hide *)
  1090.  
  1091.  A2: An alternative method by Dr. Abimbola Olowofoyeku
  1092. laa12@seq1.keele.ac.uk. No paths.
  1093.   procedure HIDE (dirname : string);
  1094.   var FileInfo : searchRec;
  1095.       f        : file;
  1096.   begin
  1097.     FindFirst (dirname, Directory, FileInfo);
  1098.     while DosError = 0 do
  1099.       begin
  1100.         assign (f, FileInfo.Name);
  1101.         SetFAttr (f, Hidden);
  1102.         FindNext (FileInfo);
  1103.       end;
  1104.   end;  (* hide *)
  1105.   {}
  1106.   procedure UNHIDE (dirname : string);
  1107.   var FileInfo : searchRec;
  1108.       f        : file;
  1109.   begin
  1110.     FindFirst (dirname, AnyFile, FileInfo);
  1111.     while DosError = 0 do
  1112.       begin
  1113.         assign (f, FileInfo.Name);
  1114.         SetFAttr (f, Archive);
  1115.         FindNext (FileInfo);
  1116.       end;
  1117.   end;  (* unhide *)
  1118. --------------------------------------------------------------------
  1119.  
  1120. From ts@uwasa.fi Wed Jul 5 00:00:54 1995
  1121. Subject: Testing file opened status
  1122.  
  1123. 54. *****
  1124.  Q: How do I test whether a file is already open in a TP program?
  1125.  
  1126.  A: This question is best answered by providing the code:
  1127.   uses Dos;
  1128.   {... for non-text files ...}
  1129.   function ISFOPEN (var filePointer : file) : boolean;
  1130.   begin
  1131.     isfopen := FileRec(filePointer).mode <> FmClosed;
  1132.   end;
  1133.   {}
  1134.   {... for text files ...}
  1135.   function ISTOPEN (var filePointer : text) : boolean;
  1136.   begin
  1137.     istopen := TextRec(filePointer).mode <> FmClosed;
  1138.   end;
  1139.   {}
  1140.   procedure TEST;          { Testing a non-text file }
  1141.   const name = 'R:\TMP';
  1142.   var f  : file;
  1143.   begin
  1144.     Assign (f, name);
  1145.     writeln ('File ', name, ' is open is ', ISFOPEN(f));
  1146.     {$I-} rewrite (f); {$I+}
  1147.     if IOResult <> 0 then
  1148.       begin
  1149.         writeln ('Failed to open ', name);
  1150.         exit;
  1151.       end;
  1152.     writeln ('File ', name, ' is open is ', ISFOPEN(f));
  1153.     close(f);
  1154.     writeln ('File ', name, ' is open is ', ISFOPEN(f));
  1155.   end;
  1156. --------------------------------------------------------------------
  1157.  
  1158. From ts@uwasa.fi Wed Jul 5 00:00:55 1995
  1159. Subject: From string to real
  1160.  
  1161. 55. *****
  1162.  Q: How can I test and convert a numerical string into a real?
  1163.  
  1164.  A1: An easy task in Turbo Pascal but in standard Pascal this
  1165. frequent task is much trickier. Here are both the Turbo Pascal and
  1166. Standard Pascal versions for general edification :-).
  1167.   (* Convert and test a numerical string with Turbo Pascal *)
  1168.   function DIGVALFN (mj : string; var ok : boolean) : real;
  1169.   var k : integer;
  1170.       x : real;
  1171.   begin
  1172.     Val (mj, x, k);
  1173.     ok := k = 0;
  1174.     if ok then digvalfn := x else digvalfn := 0;
  1175.   end;  (* digvalfn *)
  1176.   {}
  1177.   (* Convert and test a numerical string with standard Pascal routines only *)
  1178.   procedure DIGVAL (mj : string; var number : real; var ok : boolean);
  1179.   label 1;
  1180.   var il, lenl, pl, kl1, kl2 : integer;
  1181.       nrol                   : boolean;
  1182.       numberdl               : real;
  1183.   begin
  1184.     ok := true; lenl := Length (mj); nrol := false; pl := 0; number := 0.0;
  1185.     if lenl = 0 then ok := false;
  1186.     for il:=2 to lenl do if (mj[il]='-') or (mj[il]='+') then ok := false;
  1187.     for il:=1 to lenl do
  1188.       case mj[il] of
  1189.         '0'..'9','+','-','.' : ; else ok := false;
  1190.       end;
  1191.     for il:=1 to lenl do
  1192.       case mj[il] of '0'..'9' : begin nrol := true; goto 1; end; end;
  1193.     1: if nrol = false then ok := false;
  1194.     for il:=1 to lenl do if mj[il] = '.' then pl := pl + 1;
  1195.     if pl > 1 then ok := false;
  1196.     kl1:=1; kl2:=lenl+1; if (mj[1]='-') or (mj[1]='+') then kl1 := 2;
  1197.     for il:=1 to lenl do if mj[il] = '.' then kl2 := il;
  1198.     if kl2-kl1 > 38 then ok := false;
  1199.     if ok then
  1200.       begin
  1201.         number:=0; numberdl:=0;
  1202.         for il:=kl1 to kl2-1 do number := (ord(mj[il])-48)+10*number;
  1203.         if kl2 < lenl+1 then
  1204.           for il:=lenl downto kl2+1 do
  1205.             numberdl := (ord(mj[il])-48)/10+numberdl/10;
  1206.         number := number + numberdl;
  1207.         if mj[1]='-' then number := -number;
  1208.       end; {if ok}
  1209.   end;  (* digval *)
  1210.   {}
  1211.   procedure TEST;
  1212.   var s : string; r : real; ok : boolean;
  1213.   begin
  1214.     s := '123.41';
  1215.     r := DIGVALFN (s, ok);
  1216.     if ok then writeln (r) else writeln ('Error in ', s);
  1217.     DIGVAL (s, r, ok);
  1218.     if ok then writeln (r) else writeln ('Error in ', s);
  1219.   end;
  1220.  
  1221. A2: The conversion can be in the other directorion as well. Here is
  1222. how to convert an integer into a string with standard Pascal
  1223. routines only.
  1224.   function CHRIVLFN (number : integer) : string;
  1225.   var il, pl, al : integer;
  1226.   cl, mj : string;
  1227.   isNeg : boolean;
  1228.   begin
  1229.     if number < 0 then begin
  1230.       isNeg := true; number := -number; end
  1231.       else isNeg := false;
  1232.     pl := 0; mj := ''; cl := '';
  1233.     repeat
  1234.       pl := pl + 1;
  1235.       al := number mod 10;
  1236.       cl := cl + chr(al+48);
  1237.       number := number div 10;
  1238.     until number = 0;
  1239.     if isNeg then begin pl := pl + 1; cl[pl] := '-'; end;
  1240.     for il := 1 to pl do mj := mj + cl[pl+1-il];
  1241.     chrivlfn := mj;
  1242.   end;  (* chrivlfn *)
  1243.   {}
  1244.   procedure TEST;
  1245.   var s : string; j : integer;
  1246.   begin
  1247.     j := 12341;
  1248.     s := CHRIVLFN (j);
  1249.     writeln (s);
  1250.   end;
  1251. --------------------------------------------------------------------
  1252.  
  1253. From ts@uwasa.fi Wed Jul 5 00:00:56 1995
  1254. Subject: Decompiling a TP .EXE
  1255.  
  1256. 56. *****
  1257.  Q: How can I reverse a TP .EXE or .TPU back into source code?
  1258.  
  1259.  A: This is simply asking too much. You cannot decompile a TP
  1260. program in a manner that would give you back the original source.
  1261. This method of reverse engineering is not on in actual practice.
  1262. Quoting Jeroen Pluimers (jeroenp@dragons.nest.nl) "During the
  1263. compilation, important information get's lost about variables,
  1264. types, identifiers etc. Writing a Pascal Decompiler is impossible.
  1265. The best you can achieve is a disassembler that can help you
  1266. recognize some Pascal statements."
  1267.    You might note that this question somewhat resembles another
  1268. frequent question "How can I convert a TPU unit of one TP version to
  1269. another?" which cannot be solved without the original source code.
  1270. --------------------------------------------------------------------
  1271.  
  1272. From ts@uwasa.fi Wed Jul 5 00:00:57 1995
  1273. Subject: Calculating date/time differences
  1274.  
  1275. 57. *****
  1276.  Q: How can I calculate the difference between two points of time?
  1277.  
  1278.  A: This is an unconfirmed answer so be a little careful with it.
  1279. But at the very least it shows some interesting information about
  1280. Turbo Pascal date/time conventions and how to declare and initialize
  1281. typed constants if they are records.
  1282.   program TimDifTest;
  1283.   uses Dos;
  1284.   const a : DateTime
  1285.           = (year:1992; month:10; day:24; hour:5; min:29; sec:38);
  1286.         b : DateTime
  1287.           = (year:1993; month:11; day:23; hour:6; min:30; sec:51);
  1288.   var aLong, bLong, cLong : longint;
  1289.       c : DateTime;
  1290.   begin
  1291.     PackTime (a, aLong);
  1292.     PackTime (b, bLong);
  1293.     cLong := bLong - aLong;
  1294.     UnpackTime (cLong, c);
  1295.     writeln (c.year-1980, ' ', c.month, ' ', c.day, ' ',
  1296.              c.hour, ' ', c.min, ' ', c.sec);
  1297.   end.
  1298. More generally than for dates between 1980 and 2079, or for more
  1299. accurate results, the difference between two date/times can be
  1300. calculated using Zeller's congruence (see the item "I want code that
  1301. gives the weekday of the given date"). First calculate Zeller's for
  1302. both the dates, convert them, and the hour, min, and sec into
  1303. seconds, subtract, and convert back.
  1304. --------------------------------------------------------------------
  1305.  
  1306. From ts@uwasa.fi Wed Jul 5 00:00:58 1995
  1307. Subject: Stand-alone or from IDE
  1308.  
  1309. 58. *****
  1310.  Q: Is a program running stand-alone or from within the IDE?
  1311.  
  1312.  A: Not all questions have an answer yet. I posed this question to
  1313. the the late UseNet newsgroup comp.lang.pascal, but we have not
  1314. found an answer that would be general for all MS-DOS versions. The
  1315. closest we have comes from dmurdoch@mast.queensu.ca Duncan Murdoch
  1316. (naturally :-). I have done some slight editing of Duncan's
  1317. solution.
  1318.   uses Dos;
  1319.   type Pchar = ^Char;
  1320.   function Asciiz2Str (p : Pchar) : string;
  1321.   var
  1322.     result : string;
  1323.     len : byte;
  1324.   begin
  1325.     len := 0;
  1326.     while (p^ <> #0) and (len < 255) do
  1327.     begin
  1328.       inc(len);
  1329.       result[len] := p^;
  1330.       inc(longint(p));
  1331.     end;
  1332.     result[0] := chr(len);
  1333.     Asciiz2Str := result;
  1334.   end;
  1335.   {}
  1336.   var parentSeg : ^word;
  1337.       p         : pchar;
  1338.   begin
  1339.     if swap(DosVersion) < $0400 then
  1340.       writeln ('Requires Dos 4.0+')
  1341.     else begin
  1342.       parentSeg := ptr (prefixSeg, $16);
  1343.       p := ptr (ParentSeg^-1, 8);
  1344.       writeln ('I was launched by ', Asciiz2Str(p));
  1345.     end;
  1346.   end.
  1347. Another suggestion has been that the contents of ParamStr(0) would
  1348. show the launching program. I tested this on several configurations
  1349. and TP versions and found no evidence that the contention would
  1350. hold.
  1351. --------------------------------------------------------------------
  1352.  
  1353. From ts@uwasa.fi Wed Jul 5 00:00:59 1995
  1354. Subject: Memory Addressing
  1355.  
  1356. 59. *****
  1357.  Q: Please explain Turbo Pascal memory addressing to me.
  1358.  
  1359.  A: This is far from an easy question, but let's see what we can do.
  1360. The origins of the difficulties are in the design of the 8086 chip
  1361. which still restricts all Turbo Pascal applications (which contrary
  1362. to Borland Pascal use the original real mode). The 8086 (aka real
  1363. mode) addressing is based on 16-bit registers. As you probably know
  1364. 2^16 is 65536 which means that you cannot directly point to all
  1365. addresses of the lower and upper memory, which ranges from 0 to
  1366. 1048575 (2^20-1). Thus all the memory addresses are pointed to into
  1367. two parts in TP programs, the segment and the offset. The following
  1368. example of the PC's memory illustrates.
  1369.  
  1370.   Decimal  Hexa-
  1371.   address  decimal  Segment  Offset  What
  1372.         0   $00000    $0000   $0000  Conventional memory starts
  1373.      1043   $00413    $0040   $0013  Base memory in Kb, a word
  1374.    655359   $9FFFF    $9000   $FFFF  Conventional memory ends
  1375.    655360   $A0000    $A000   $0000  Upper memory begins
  1376.   1048575   $FFFFF    $F000   $FFFF  Upper memory ends
  1377.  
  1378. To exemplify, let's look at some alternative ways we could access
  1379. the information about the amount of the base memory. It is very
  1380. straight-forward, since in a PC that information is at the fixed
  1381. memory location show by the above table. We know this in advance.
  1382. Using direct memory accessing we could write
  1383.   var memsize : word;
  1384.   memsize := MemW [$0040:$0013];
  1385.   writeln (memsize);
  1386.   {.. or ..}
  1387.   var memsize : word absolute $0040:$0013;
  1388.   writeln (memsize);
  1389. If you are not familiar with the true meaning of pointers, they may
  1390. feel confusing, but what they basically are is just what the name
  1391. indicates, pointers to memory locations. Study the following
  1392. example.
  1393.   var memsizePtr : ^word;           { A pointer to a word }
  1394.   begin
  1395.     memsizePtr := ptr ($40, $13);   { Assign the pointer a value }
  1396.     writeln (memsizePtr^);          { Write what is in the address }
  1397.   end.                              { that was pointed to }
  1398. This was relatively simple, since we knew in advance the location of
  1399. the information. Lets look at a case where we do not know that.
  1400. Consider
  1401.   var x : word;
  1402.   begin
  1403.     x := 1223;
  1404.     writeln (x);
  1405.   end.
  1406. We have a variable x somewhere in the memory, and we can refer to it
  1407. without ever needing to know where the variable actually is in the
  1408. memory. But how does one find out if one for some reason wants or
  1409. needs to know?
  1410.   var x       : word;
  1411.       Segment : word;
  1412.       Offset  : word;
  1413.       y       : ^word;
  1414.   begin
  1415.     x := 1223;
  1416.     writeln (x);
  1417.     Segment := Seg(x);
  1418.     Offset  := Ofs(x);
  1419.     writeln ('Variable x is at $', HEXFN(Segment), ':$', HEXFN(Offset));
  1420.     {... one test to ensure that the value really is in there ...}
  1421.     writeln (MemW [Segment:Offset]);
  1422.     {... another test to demonstrate that the value really is in there ...}
  1423.     y := Addr(x);
  1424.     writeln (y^);
  1425.   end.
  1426. Next consider
  1427.   var xPtr    : ^word;
  1428.       Segment : word;
  1429.       Offset  : word;
  1430.       yPtr    : ^word;
  1431.   begin
  1432.     xPtr^ := 1223;
  1433.     writeln (xPtr^);
  1434.     Segment := Seg(xPtr^);
  1435.     Offset  := Ofs(xPtr^);
  1436.     writeln ('$', HEXFN(Segment), ':$', HEXFN(Offset));
  1437.     {... a test to ensure that the value really is in there ...}
  1438.     yPtr := Ptr (Segment, Offset);
  1439.     writeln (yPtr^);
  1440.   end.
  1441. A further aspect of pointers is that you can utilize them to put a
  1442. variables onto the heap instead of the data segment so that you
  1443. won't run so easily out of space.
  1444.   var xPtr : ^word;
  1445.   begin
  1446.     { Put it onto the heap }
  1447.     New (xPtr);
  1448.     xPtr^ := 1223;
  1449.     writeln (xPtr^);
  1450.     { Get rid of it }
  1451.     Dispose (xPtr); xPtr := nil;
  1452.     readln;
  1453.   end.
  1454. Let us return to the addressing. The formulas for converting between
  1455. the addresses (sent in by Duncan Murdoch) are
  1456.   Physical := longint(segment)*16 + offset;
  1457.   {}
  1458.   Segment  := Physical div 16;
  1459.   Offset   := Physical mod 16; { This gives the normalized form }
  1460. There are multiple Segment:Offset pairs that refer to the same
  1461. address, e.g. $0000:$0413 and $0040:$0013. The normalized addresses,
  1462. with the offset in the range of 0 to $F, are, however, unique. An
  1463. example $0041:$0003.
  1464. --------------------------------------------------------------------
  1465.  
  1466. From ts@uwasa.fi Wed Jul 5 00:00:60 1995
  1467. Subject: Getting a bit from a byte
  1468.  
  1469. 60. *****
  1470.  Q: How do I obtain a bit or bits from a byte, a word or a longint?
  1471.  
  1472.  A: For bit operations think of the variable as a binary number
  1473. instead of a decimal. Consider for example
  1474.   var x : word;
  1475.   x := 219;
  1476. In binary presentation it is
  1477.   The word                  0000 0000 1101 1011
  1478.   Position in the word      FEDC BA98 7654 3210
  1479.  
  1480. Say you need the value of bit 6 (the seventh bit) in the word. You
  1481. can "and" the following words
  1482.   0000 0000 1101 1011    (219)
  1483.   0000 0000 0100 0000    ( 64)
  1484. In decimal TP notation this amounts to
  1485.   var b : word;
  1486.   b := x and 64;
  1487. The value of b is now
  1488.   0000 0000 0100 0000    ( 64)
  1489. To get the bit value (0 or 1) you need to shift the result right by
  1490. six steps, that this the expression becomes the often seen but
  1491. cryptic
  1492.   b := (x and 64) shr 6;
  1493. which means that the value of b is finally 1 in this example.
  1494.  
  1495. Ok, but what then if you need the combined value of bits six and
  1496. seven. The answer is evident if you consider the binary presentation
  1497.   0000 0000 1101 1011    (219)
  1498.   0000 0000 1100 0000    (192)
  1499. hence
  1500.   b := (x and 192) shr 6;
  1501. which will give 3 as it should.
  1502.  
  1503. So far, so good. What if you need to turn on bit nine in a word
  1504. without interfering with the other bits. The binary presentation,
  1505. again, is the key. You'll have to "or" the following
  1506.   0000 0000 1101 1011    (219)
  1507.   0000 0010 0000 0000    (512)
  1508. that is
  1509.   x := x or 512;
  1510. This results to
  1511.   0000 0010 1101 1011    (731)
  1512.  
  1513. What if you wish to turn off, say bit 6, in
  1514.   0000 0000 1101 1011    (219)
  1515.   1111 1111 1011 1111  (65471)
  1516. This is achieved by
  1517.   x := 219;
  1518.   x := x and 65471;
  1519. This results to
  1520.   0000 0000 1001 1011    (155)
  1521.  
  1522. Consider the following application as an example. The number of a
  1523. PC's floppy disk drives (minus one) is stored in bits 6 and 7 in a
  1524. word returned by interrupt $11. This is the code to find out how
  1525. many disk drives a PC has.
  1526.   uses Dos;
  1527.   function NrOfFDiskDrives : byte;
  1528.   var regs : registers;
  1529.   begin
  1530.     Intr ($11, regs);
  1531.     NrOfFDiskDrives := ((regs.ax and 192) shr 6) + 1;
  1532.   end;
  1533.  
  1534. A tip from Duncan Murdoch.  You might wish to predefine the
  1535. following constants for easier handling
  1536.   const bit0  = 1;
  1537.         bit1  = 2;
  1538.         bit2  = 4;
  1539.         :
  1540.         bit15 = 32768;
  1541.         :
  1542.         bit31 = 2147483648;
  1543. Or to put it slightly differently as Dr John Stockton
  1544. jrs@dclf.npl.co.uk suggests
  1545.   const
  1546.   bit00=$00000001; bit01=$00000002; bit02=$00000004; bit03=$00000008;
  1547.   bit04=$00000010; bit05=$00000020; bit06=$00000040; bit07=$00000080;
  1548.   :
  1549.   bit28=$10000000; bit29=$20000000; bit30=$40000000; bit31=$80000000;
  1550. Finally, you also might want to look at the item "Getting a nybble
  1551. from a byte".
  1552. --------------------------------------------------------------------
  1553.