Pascal Misc


Home

About the Author

General Programming

Pascal

Delphi

C&C++

Visual Basic

SQL

JAVA Script

Links

 

Pascal Tutor ] Pascal Graphics ] Pascal Math ] [ Pascal Misc ]

  1. How to use a mouse
  2. How to use a SoundBlaster or compatible
  3. How to write a unit
  4. How to execute dos commands in TP
  5. How to convert a string to text and back
  6. A function to convert letters to lower case
  7. How to change the text cursor size
  8. How to get and use command line parameters
  9. How to establish if a name refers to a directory or not
  10. How to find the files in a directory and sub directories
  11. How to create arrays that are larger than 64 kilobytes

Any questions send them to pascalmisc@teentwo.8m.com


1. How to use a mouse:

To use a mouse you will need the unit, mouselib(you can download it from our download menu). To get the mouse cursor visible in text you must enter INITMOUSE; and then SHOWMOUSECURSOR;. To get it visible in graphics just type SHOWMOUSECURSOR;.
To hide the mouse cursor type HIDEMOUSECURSOR;.
IF BUTTONPRESSED THEN will determine if any of the mouse buttons was pressed.
GETMOUSEX and GETMOUSEY is integer functions witch will determine the x and y position of the cursor. at the moment of test.

EXAMPLE:

Uses
   Crt,Mouselib;

Var
   mousex,mousey:integer;

Begin
    InitMouse;
    If ButtonPressed then
    begin
      mousex := getmousex;
      mousey := getmousey;
      writeln('XPos: ',mousex,' YPos: ',mousey);
    end;
end.

When in graphics you can change the mouse pointer. There are 10 preset mouse pointers, to use them type:
 SetLeftArrowCursor;
 SetCheckMarkCursor;
 SetPointingHandCursor;
 SetDiagonalCrossCursor;
 SetRectangularCrossCursor;
 SetHourGlassCursor;
 SetNewWatchCursor;
For it to change you must first hide the cursor, change it and the show it again. You can also draw your own 6 X 6 cursor and use the GraphicMouseCursor(6, 6, Pointer).

Download the unit here, mouse.zip

Any questions send them to pascalmisc@teentwo.8m.com


2. How to use a sounblaster or compatible

To use a sound blaster you will need the SMIX121 units witch is available at our download menu. Before I continue just a special thanks to ETHANE BRODSKY for the unit.

The sounds you are going to use must be 22MHZ, mono 8 bit, wave file. You must then convert it to raw. A wave 2 raw converter is included with the units. At dos just type wav2raw teen2.wav teen2.raw. Once you've got the raw file you must put it in a library file containing all the sounds you want to use. sndlib.exe is also included with the units. To use it type sndlib teen2.lib + sound.raw sound, at the dos prompt. The second sound is the key witch will be used to load the sound from the file.

The Procedure to load the SoundBlaster is very long so I put in a text file:INITSB.ZIP


EXAMPLE:


Uses
    Crt,Smix,Detect;

const
    SharedEMB = False;

var
    BaseIO: word; IRQ, DMA, DMA16 : byte;
    Sound : Psound;

begin
    Init;
    OpenSoundResourceFile('TEEN2.SND');
    LoadSound(Sound, 'TEEN2');
    CloseSoundResourceFile;
    Startsound(Sound,0,false); {To play the sound continuasly turn the false to true}

    FreeSound(Sound);
    StopSound(0);
    ShutdownSB;
    ShutdownMixing;
    ShutdownSharing;
end.

PSound is a unit declared variable of type ^Tsoud, Tsound is a record.
You can put multiple sounds in the sound library and just read them out into an array.

Loadsound(Sound[1],'PLAY');
Loadsound(Sound[2],'GO');

Click here to download the sound libraries, smix121.zip

Any questions send them to pascalmisc@teentwo.8m.com


3. How to write a unit:

To write a unit is very simple, first you must name all the procedures you are going to use and then you must write them out.

EXAMPLE:

Unit Teen2;

Interface

Uses
    Crt;

Const
    time = 50;

Var
    s : string

Procedure Example();

Implementation

Procedure Example();
begin
    {Type info here}
end;

Begin
    {type anything the unit must do on start up}
end;

You can add ass many procedure ass you want. I advice that you add all the procedure that you use very often.

Any questions send them to pascalmisc@teentwo.8m.com


4. How to execute dos commands in TP:

It is quite simple to execute an dos command or external program while running your program, but there are a few catches. Before and after running the command you must type SWAPVECTORS; this swaps the contents of the SaveIntXX pointers in the system with the current contents of the interrupt vectors. By doing this you are preventing the program to use any of the interrupt's currently in use.
You must also reduce the heap size by typing {$M $4000,0,0 } right at the top of your program, to ensure that there is enough memory for the command to run.

EXAMPLE:

{$M $4000,0,0 }
uses Dos;

var
    Command: string;

begin
    Write('Enter command to execute: ');
    ReadLn(ProgramName);
    Command := '/C' + Command;

    SwapVectors;
    Exec(GetEnv('COMSPEC'),Command);
    SwapVectors;

    if DosError <> 0 then{ Error? }
      WriteLn('Dos error #', DosError)
end.

The GetEnv('COMSPEC') commands determines the location of your command com and uses it to create a shell to run the command or program in.

Any questions send them to pascalmisc@teentwo.8m.com


5. How to convert a string to text and back

Turbo pascal has procedures to convert string into an number, VAL(string,number,number) and integer to string STR(number,string). Because these two are procedures that are quite hard to use because they do not send an obvious variable back.

EXAMPLE:

Uses
   CRT;

Var
   s:string;
   r:real;
   n:integer;

Begin
   s := '10';
   val(s,r,n);
      {OR}
   n := 10;
   str(n,s);
end.

To convert real numbers to string it is advisable to ad a : with the amount of numbers to use and another : with the amount of numbers after the , .

Any questions send them to pascalmisc@teentwo.8m.com


6. A function to convert letters to lower case.

To understand how this works you must have a good knowledge of the ASCII table. The ASCII table contains all the characters a computer can recognize. Capital letter a is located at 56 and lower case a at 97, so all you have to do to convert a capital letter to lower case is to add 32 to it's ASCII value.

EXAMPLE:

Function LowerCase(let:char) : char;
Begin
   if let in['A'..'Z'] then
      then Lowercase := chr(ord(let) + 32)
   else LowerCase := let;
end;

ORD determines where the character is located on the ASCII table.

Any questions send them to pascalmisc@teentwo.8m.com


7. How to change the text cursor size.

This procedure is based on assembler usage of bios interrupts to change stuff on the screen, if you understand this good for you but if you don't just copy it out and use it.

To change the cursor size the values of the new size must be stored in the CH and CL registers. Once these values has been entered the 10H bios interrupt must be initialized. Giving the AH register the value of 01 you are requesting to set the cursor size.

EXAMPLE:

Procedure CursorSize(beg,en:byte);
Var
   Regs : Registers;

begin
   Regs.AH := $01;
   Regs.CH := beg;
   Regs.CL := en;
   Regs.CH := Regs.CH and not $20;
   Regs.CH := Regs.CH or $40;
   Regs.AL := LastMode;
   Intr($10,Regs);
end;

beg is where on the line the cursor will start and en where is will end, both must have a value between 1 and 16. To make the cursor disappear both must be 16, and to make it normal a gain both must have the value of 15.

Any questions send them to pascalmisc@teentwo.8m.com


8.How to get and use command line parameters

To determine how many parameters there are you use PARAMCOUNT.
To find out what the parameters are you use PARAMSTR(index). PARAMSTR(1) is the first parameters, PARAMSTR(2) is the second parameters, etc.

EXAMPLE:

VAR
   i : byte;

Begin
   for i := 1 to PARAMCOUNT do
      Writeln(Paramstr(i));
end.

To determine the name of the program that is running use PARAMSTR(0);

I want to get the whole command line and someone types "hello               my name is" you will get a whole lot of blank spaces. So here is a nifty little function to get the whole command line for you.

type CommandLines = string[127];

function CommandLine: CommandLines;

type
   CommandLinePtr = ^CommandLines;
begin
   CommandLine := CommandLinePtr( Ptr(PrefixSeg, $80) )^;
end;

Any questions send them to pascalmisc@teentwo.8m.com


9.How to establish if a name refers to a directory or not

All the file attributes can be established by using GETFATTR and GETFTIME. To find the files you use FINDFIRST and FINDNEXT. Once you've got the file you must assign it to a file variable ex. TEXT, FILE OF etc. When the file is assigned you use GETFATTR(file,attr) to get the file attributes. ATTR must be of type integer or word. If ATTR has a value of 16 it is a directory, but just to be sure it can be crossed checked with DIRECTORY wich is a variable that is declared in the dos unit and wich value changes when the name is found with findfirst or findnext.

EXAMPLE:

Uses
   Dos;

Type
   files = record
            name : string[12];
            dir : boolean;
         end;
   fils = array[1..512] of files;

Procedure FindFiles(var filese:fils; var count:word);
var
   F : SearchRec;
   fi : file;
   attr : word;

Procedure FindFiles(var filese:fils; var count:word);
var
   F : SearchRec;
   fi : file;
   attr : word;

begin
   Count := 1;
   FindFirst('*.*',ReadOnly + Directory + Archive, f);
   while (DosError = 0) and (Count < 512) do
   begin
      with filese[count] do
      begin
         name := f.name;
         assign(fi,name);
         getfattr(fi,attr);
         if attr and directory <> 0 then
            dir := true;
      end;
      Inc(Count);
      FindNext(F);
   end;
end;

var
   i,count : word;
   filese : fils;

begin
   Findfiles(filese,count);
   for i := 1 to count - 1 do
   with filese[I] do
      writeln(name:15,dir:10);
end.

ATTR Values:
1        READ ONLY
2        HIDDEN
4        SYSFILE
8        VOLUME ID
16      DIRECTORY
32      ANY FILE

If you want to get the Date and Time the file was last edited or created you must use GETFTIME. The time you get will be a integer and you must use UNPACKTIME to make it understandable.

EXAMPLE:

Const
   MonthStr: array[1..12] of string[3] = (
       'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
       'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');

............

Type
   files = record
            name : string[12];
            time : integer;
            dir : boolean;
         end;

............

Var
   tim : datetime;

............

   GETFTIME(fi,fil[i].time);

............

   Unpacktime(fil[i].time,tim);
   Write(fil[i].name:20,T.Day: 4, '-',MonthStr[T.Month], '-',T.Year,T.Hour: 4,
   ':',T.Min);

............

Any questions send them to pascalmisc@teentwo.8m.com


10.How to find the files in a directory and sub directories

The Pascal DOS unit has a function FSEARCH. The only problem with this function is that it stops once it has found the file. Usage FSEARCH(file,drive&directories);

EXAMPLE:

Uses
   DOS;

Var
   S : String;

Begin
   s := FSearch('COMMAND.COM','C:\');
   Writeln(s);
end.

For a more comprehensive search you can use this in conjunction with FindFirst and FindNext.

Any questions send them to pascalmisc@teentwo.8m.com


11.How to create arrays that are larger than 64 kilobytes.

Break the array into two (types of) pieces: an array of row ptrs, and a set of rows on the heap. That is, replace

type BigArrayType = array[0..Rows, 0..Cols] of DataType;
var BigArray: BigArrayType;

with

type BigArrayRow = array[0..Cols] of DataType;
BigArrayType = array[0..Rows] of ^ BigArrayRow;
var BigArray: BigArrayType

and replace any references to BigArray[Row, Col] with references to BigArray[Row]^[Col]. You will also have to allocate memory for your array by using NEW(BigArray); Once you have finished using your array you must dispose the memory you allocated so another application can use it. You do this by typing DISPOSE(BigArray);

Any questions send them to pascalmisc@teentwo.8m.com

Pascal Tutor ] Pascal Graphics ] Pascal Math ] [ Pascal Misc ]

Link of the week 
http://www.cpp-
programming.
com/
Newsgroups
comp.lang.c
comp.lang.c++
comp.programming

This page is best viewed in 800x600x256. This page was last edited Tuesday, June 27, 2000

1