uses sysutils, windows;
const Ccol = 24;
function ansiToWide(s: String): pWideChar;
var
wideStrSize: integer;
j: integer;
p: pChar absolute result;
begin
wideStrSize := 2*(length(s)+1);
getmem(result, wideStrSize);
stringToWideChar(s, result, wideStrSize);
end;
function wideAsString(p: pWideChar): String;
var
wideStrSize: integer;
j: integer;
begin
wideStrSize := 2*lstrlenw(p);
setLength(result, wideStrSize);
move(p[0], result[1], wideStrSize);
end;
function widePos(sKey, sTarget: String): integer;
begin
result := (pos(sKey, sTarget) + 1) div 2;
end;
procedure checkMargin(j: integer);
const
Cline = (80-Ccol) div 3;
begin
if j mod Cline = 0 then
begin
writeln;
write(' ': Ccol);
end;
end;
procedure showAnsi(s: String);
var
j: integer;
begin
writeln('Ansi string: ':Ccol, s);
write('Ansi bytes: ':Ccol);
for j := 1 to length(s) do
begin
checkMargin(j);
write (ord(s[j]), ' ');
end;
writeln;
end;
Procedure showUnicode(p: PwideChar);
Var
j: integer;
Begin
write('Unicode bytes: ':Ccol);
for j:=0 to 2*lstrLenw(p)+1 do begin
checkMargin(j+1);
write (ord(pchar(p)[j]), ' ');
end;
writeln;
End;
Procedure showWideString(s: string);
Var
j: integer;
Begin
write('Wide-as-string bytes: ':Ccol);
for j:=1 to length(s) do begin
checkMargin(j);
write (ord(s[j]), ' ');
end;
writeln;
End;
Var
s1, s2, s1w, s2w: string;
p1, p2: pWideChar;
n: integer;
f: file;
Begin
s1:='abcdefghijklm';
s2:='ef';
p1:=ansiToWide(s1);
p2:=ansiToWide(s2);
s1w:=wideAsString(p1);
s2w:=wideAsString(p2);
showAnsi(s1);
showUnicode(p1);
showWideString(s1w);
writeln;
showAnsi(s2);
showUnicode(p2);
showWideString(s2w);
writeln;
writeln('String pos: ':Ccol, pos(s2, s1));
writeln('Wide-string pos: ':Ccol, widePos(s2w, s1w));
writeln;
freemem(p1);
freemem(p2);
write('String to put in Test.txt: ');
readln(s2);
p2:=ansiToWide(s2);
s2w:=wideAsString(p2);
write('Position to put it at (from 0): ');
readln(n);
assignFile(f, 'test.txt');
reset(f, 1);
seek(f, n);
blockWrite(f, p2[0], 2*lstrLenw(p2));
close(f);
reset(f, 1);
setLength(s1, fileSize(f));
blockRead(f, s1[1], length(s1));
close(f);
writeln('Pos of ', s2, ' in Text.txt = ', pos(s2w, s1)-1);
freemem(p2);
end. |
|