Posted on Sunday, January 27, 2008 at 6:39 PM
In his infamous rant
Why Pascal Is Not My Favorite Programming Language Brian Kernighan says
"There is a paucity of operators ... In particular, there are no bit-manipulation operators (AND, OR, XOR, etc.). I simply gave up trying to write the following trivial encryption program in Pascal:
i := 1;
while getc(c) <> ENDFILE do begin
putc(xor(c, key[i]));
i := i mod keylen + 1
end
because I couldn't write a sensible 'xor' function."
The amazing part of this statement is that I cannot fathom why he found it so hard to write an xor function. It's simply not that hard. In this post we take up the challenge.
We will assume that Kernighan's compiler did not have the type Byte and this may one source of his problem. However, Byte is simply a subrange of the integers (0 .. 255) so this is easily surmounted. We can also safely assume that he had access to the Boolean operators NOT, OR and AND...
Posted on Saturday, January 19, 2008 at 5:42 PM
The next program is one which appeared in the original
Software Tools but not in
Software Tools in Pascal. The program is
Crypt which encrypts and decrypts a text file.
The algorithm for the encryption is what cryptologists call a "one time pad" (google it). When properly used it is theoretically unbreakable. What we are doing here does not include all the steps entailed in properly using the algorithm but it should prevent the casual snoop from decoding your files.
The algorithm consists of merging the input stream of data with a second stream, the key, to produce an output stream of encrypted data. We extract the ascii codes from each char of input and from each char of the key. A bitwise
XOR operation gives us the ascii code for the output which is converted back to a char. Here is the code for
Crypt.
Program Crypt ;
{
Crypt -- encrypt and decrypt
}
var
Key : String ;
KeyLen : Byte ;...
Posted on Sunday, January 13, 2008 at 1:39 PM
The Turbo Pascal functions
ParamStr and
ParamCount give us access to command line arguments.
ParamCount returns the number of command line arguments while
ParamStr(i : Word) returns the individual parameter strings.
Ekho illustrates the use of these two functions. We use the spelling Ekho instead of Echo to avoid colliding with the DOS command ECHO.
Program Ekho ;
{
echo command line arguments to output.
}
Uses
Tools ;
Var
i : Byte ;
begin
for i := 0 to ParamCount do
Write (ParamStr(i), BLANK) ;
WriteLn
end.
Note that even if no parameters are included on the command line we still get output. ParamStr(0) is the executable file itself. Thus:
c:\pascal\tools\ekho
C:\PASCAL\TOOLS\EKHO.EXE
The next few programs we write will use command line parameters.
Posted on Wednesday, January 02, 2008 at 2:43 AM
Here, for completeness without further comment, is the code for
RTrim,
LTrim and
Trim which are included in the unit
Tools.
Function RTrim (Var S : String) : String ;
{
RTrim -- remove trailing blanks
}
Var
i : Byte ;
begin { RTrim }
for i := Length(S) downto 1 do
if S[i] > BLANK then begin
S := Copy(S,1,i) ;
RTrim := S ;
Exit
end ;
{
if we reach this point then S contains only BLANKS
so we return a null string
}
S := '' ;
RTrim := S
end ; { RTrim }
Function LTrim (Var S : String) : String ;
{
LTrim -- remove leading blanks
}
Var
i : Byte ;
begin { LTrim }
for i := 1 to Length(S) do
if S[i] > BLANK then begin
S := Copy(S,i,MAXSTR) ;...
Comments:
2
Tags:
String,
Trim
Posted on Tuesday, January 01, 2008 at 9:08 AM
DeTab is the inverse of EnTab. Here is the specification:
{
PROGRAM
DeTab -- convert tabs to blanks
USAGE
DeTab
FUNCTION
DeTab copies its input to its output, expanding horizontal tabs to
blanks along the way, so that the output is visually the same as the
input, but contains no tab chars. Tab stops are assumed to be set
every three columns (i.e., 1, 4, 7, 10, ...), so that each tab char
is replaced by from one to three blanks.
BUGS
1. DeTab is naive about backspace, vertical motions, and
non-printing chars.
2. Each line of output will be truncated at MAXSTR chars.
3. Trailing BLANKS will be trimmed from each line of output.
}
DeTab's main routine echos EnTab's, reducing the problem to one of detabbing a single string. To detab the string we could try to simply apply EnTab's strategy in reverse, however, this is another chance to apply the "how would a human do it?" approach...