home *** CD-ROM | disk | FTP | other *** search
- Uses Crt;
-
-
- Type
- VarBuffer = Array[0..255] of Byte;
- VarType = (SInt,Int,LInt,RealType,StringType,WordType,Charac,
- Bool,Bite,SingleP,DoubleP,CompType,ExtendP,Enum,Other);
-
- (*═══════════════════════════════════════════════════════════════════════════*)
- Procedure AllSwap(Var X,Y; FormalType:VarType);
-
- (* Copyright 1988
- By MorganSoft
- All rights reserved.
-
- Last Modified 3-15-88
-
- This program is NOT released to the public domain, and you may use
- it (for now) with the sole proviso that you do not remove the comments
- or this notification.
-
- Purpose:
- To use one procedure to swap two variables of almost any type
- (pointers and compound types excluded) without running afoul
- of type checking by the compiler.
-
- Requirements:
- Two Global Types
-
- Type
- VarBuffer = Array[0..255] of Byte;
- VarType = (SInt,Int,LInt,RealType,StringType,WordType,Charac,
- Bool,Bite,SingleP,DoubleP,CompType,ExtendP,Enum,Other);
-
- Sample Call:
- AllSwap(X,Y,RealType);
-
- NOTES:
- All user defined enumerated types can be switched as well as
- the standard Turbo Pascal types available.
-
- Since I do not have a 8087 coprocessor, I have been unable to test
- run some of the types--they may or may not work though I suspect that
- they will. The basic logic of the program is such that I strongly
- doubt that a problem would occurr. But I still have NOT checked those
- types.
-
- *)
-
- Var
- P1 : VarBuffer Absolute X; (* Get access to untyped var X *)
- P2 : VarBuffer Absolute Y; (* Get access to untyped var Y *)
- B : VarBuffer; (* This is our holding variable *)
- J, K, I : Integer;
-
- Begin
-
- (* The case statement allows us to input the various byte lengths taken
- up by each supported type *)
-
- Case FormalType of
- Bite,Charac,
- Bool, SInt,Enum : I := 1;
- Int, WordType : I := 2;
- LInt, SingleP : I := 4;
- RealType : I := 6;
- DoubleP, CompType : I := 8;
- ExtendP : I := 10;
-
- (* Type String already contains the length of the actual variable;
- we just need to access it and adjust for the index length. *)
-
- StringType : Begin
- I := P1[0] + 1;
- J := P2[0] + 1;
- If (I < J) then
- I := J (* must use the longer
- of the two *)
- End;
- Other : Exit
- End; (* End Case *)
-
- (* This as you will no doubt recognize, it the usual swap routine,
- done byte by byte *)
-
- For K := 0 To I-1 do
- Begin
- B[K] := P1[K];
- P1[K] := P2[K];
- P2[K] := B[K]
- End (* End For Loop *)
- End; (* End AllSwap *)
- (*═══════════════════════════════════════════════════════════════════════════*)
-
- Var
- X,Y:String;
- J,K:Integer;
- L,M:Real;
- Begin
- Clrscr;
- X := 'This is the variable x, initially.';
- Y := 'And this, Y.';
- J := 255;
- K := 9;
- L := 1.004;
- M := 2.678E+6;
- Writeln('X = ',X);
- Writeln('Y = ', Y);
- AllSwap(X,Y,StringType);
- Writeln('X = ',X);
- Writeln('Y = ', Y);
- Writeln;
-
- Writeln('J = ',J);
- Writeln('K = ',K);
- AllSwap(J,K,Int);
- Writeln('J = ',J);
- Writeln('K = ',K);
- Writeln;
-
- Writeln('L = ',L);
- Writeln('M = ', M);
- AllSwap(L,M,RealType);
- Writeln('L = ',L);
- Writeln('M = ', M);
-
- Writeln;
- Writeln('The above three swap examples (String swap, Integer swap,');
- Writeln('and Real swap, were all perfomred by the same procedure.')
- End.
-
-
-