Solved

Delphi  UTF-8 strings inside records

Posted on 2013-05-27
4
667 Views
1 Endorsement
Last Modified: 2013-05-27
I use a record like this...

type
  TNameRec = record
    Nam: string[maxNameLength];
    Var : real;
    Siz : integer;
  end;

to store and access data to files of class TRecordStream = class(TFileStream)

using functions like...

var
    fileStream: TRecordStream;
begin
    fileStream.ReadNameRec(NameRec);
    fileStream.writeNameRec(NameRec);
end;

declared as...

function TRecordStream.WriteNameRec (Rec: TNameRec  ): Longint;
begin
  // This function writes the record Rec to the stream
  Result := Write(Rec, sizeOf(Rec));
end;

function TRecordStream.ReadNameRec (var Rec: TNameRec  ): Longint;
begin
  // This function reads the record Rec from the stream
  Result := Read(Rec, sizeOf(Rec));
end;

This works fine for me. The problem rises when i want to save utf-8 strings.
In this case the strings cannot be stored properly (uknown characters), unless I remove the declaration of constant length for the strings as below:

type
  TNameRec = record
    Nam: string;    //[maxNameLength];
    Var : real;
    Siz : integer;
  end;

if i do that, the utf-8 strings can be saved and then be accessed keeping their actual characters. Unfortunately, when i try to read these records from the filestream, sometimes the process works fine, and sometimes there is access failure, obviously due to miscalculation of the record length.

Is there a way to save and read these utf-8 strings properly, but by keeping this record structure? Of course, if it's not possible, any solution close to this is welcomed.
1
Comment
Question by:Stelios_Antoniou
  • 2
  • 2
4 Comments
 
LVL 26

Accepted Solution

by:
Sinisa Vuk earned 500 total points
ID: 39198952
I propose you to use generic Byte array instead of String type. Also recommend to not use reserved word as variable/property/record names like Var.

const
  maxNameLength = 255;

Type
  PRecString = ^TRecString;
  TRecString = array[0..maxNameLength] of Byte;

  TNameRec = record
    CustomerName: TRecString;
    VarValue : Real;
    ValueSize : Integer;
  end;

....

procedure SetRecordValue_Name(var r: TNameRec; sText: String); overload;
var
  n: Integer;
begin
  n := Min(Length(sText), maxNameLength);
  r.CustomerName[0] := n; //set length
  Move(sText[1], r.CustomerName[1], n);
end;

procedure SetRecordValue_Name(var r: TNameRec; sText: Utf8String); overload;
var
  n: Integer;
begin
  n := Min(Length(sText), maxNameLength);
  r.CustomerName[0] := n; //set length
  Move(sText[1], r.CustomerName[1], n);
end;

function GetRecordValue_Name(const r: TNameRec): String; overload;
var
  n: Integer;
begin
  n := r.CustomerName[0];
  SetLength(Result, n);
  if n>0 then
  begin
    Move(r.CustomerName[1], Result[1], n);
  end;
end;

function GetRecordValue_Name(const r: TNameRec): Utf8String; overload;
var
  n: Integer;
begin
  n := r.CustomerName[0];
  SetLength(Result, n);
  if n>0 then
  begin
    Move(r.CustomerName[1], Result[1], n);
  end;
end;

//set Name value
  ZeroMemory(@rName, SizeOf(TNameRec));
  SetRecordValue_Name(rName, UTF8Encode(Edit1.Text));

//get value
  Edit2.Text := UTF8Decode(GetRecordValue_Name(rName));

Open in new window


... operations with stream is the same as you do. Difference is on how to set and get Name value of the record.
0
 

Author Comment

by:Stelios_Antoniou
ID: 39199136
Thank you for your answer

I have compile problem with the line

ZeroMemory(@rName, SizeOf(TNameRec));

Shouldn't I define this procedure? And what is '@rName'?
0
 
LVL 26

Expert Comment

by:Sinisa Vuk
ID: 39199144
Sorry, this is part of my global test variable. You use own rec. var. ZeroMemory is useful to sell all bytes/record to zero. Just remove this.
0
 

Author Comment

by:Stelios_Antoniou
ID: 39199424
It's working !

Thanks
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question