Solved

Delphi  UTF-8 strings inside records

Posted on 2013-05-27
4
648 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 25

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 25

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now