Solved

Delphi  UTF-8 strings inside records

Posted on 2013-05-27
4
676 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 27

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 27

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…

738 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