Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Replace AnsiChar



Permlink Replies: 0
Michael Rabatsc...

Posts: 125
Registered: 1/22/07
Replace AnsiChar
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 6, 2017 6:32 AM
Hi All!

I already asked the question on how to port my math lib to Linux and now
it's actually coming (in a limited edition). There are some obstacles
to overcome so I hoped one can help me out here:

My main problem is that for some parsing - especially in the persistence
I need AnsiChars (like in my json parser). But afaik there is no
Ansichar any more - according to some research one should use byte
instead (but how should one check against a string name??).

What needs one to use here?

Actually there are a few structs Like this:

cBinaryReaderHeader : Array[0..4] of AnsiChar = 'RMCLS';

the json string encoding:

function TJsonReaderWriter.EncodeJSONString(const s: string): UTF8String;
var counter: Integer;
resIdx : integer;
res : UTF8String;
hexStr : UTF8String;
procedure AddChar(const c : AnsiChar);
begin
res[resIdx] := c;
inc(resIdx);
end;
begin
resIdx := 1;
// reserve the largest possible encoded string as memory once
SetLength(res, 6*Length(s));

for counter := 1 to Length(s) do
begin
// check if the char needs to be encoded
case s[counter] of
'"',
'\',
'/': begin
AddChar('\');
AddChar( AnsiChar( s[counter]) );
end;
#08: begin // backspace
AddChar('\');
AddChar('b');
end;
#09: begin // tab
AddChar('\');
AddChar('t');
end;
#10: begin // carrige return
AddChar('\');
AddChar('r');
end;
#12: begin // form feed
AddChar('\');
AddChar('f');
end;
#13: begin // line feed
AddChar('\');
AddChar('n');
end;
else
// check for: numeric, letter (a..z) and white space
// all others encode!
if ((Ord(s[counter]) >= Ord('a')) and (ord(s[counter]) <=
Ord('z'))) or
((Ord(s[counter]) >= Ord('A')) and (ord(s[counter]) <=
Ord('Z'))) or
(s[counter] = ' ') or ((Ord(s[counter]) >= Ord('0'))
and (Ord(s[counter]) <= Ord('9')))
then
AddChar(AnsiChar(s[counter]))
else
begin
AddChar('\');
AddChar('u');

hexStr := Utf8String( IntToHex(ord(s[counter]), 4) );
assert(Length(hexStr) = 4, 'IntToHex failed');
AddChar(hexStr[1]);
AddChar(hexStr[2]);
AddChar(hexStr[3]);
AddChar(hexStr[4]);
end;
end;
end;

Result := Copy(res, 1, resIdx - 1);
end;

or

function TJsonReaderWriter.LoadFromStream(
aStream: TStream): TBaseMathPersistence;
var c : AnsiChar;
begin
// state machine to decode json:
aStream.ReadBuffer(c, sizeof(c));

if c <> '{' then
raise EReadError.Create('No valid JSON format - stream must
start with "{"');

Result := InternalLoadFromStream(aStream);
end;

king regards
Mike

Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02