Solved

Delphi  UTF-8 strings inside records

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Print Graphic and Text to Epson TM-T88v 12 182
Magic Software info 18 125
Is anyone willing to have a look through this code and help debug? 25 58
Delphi and Access based Enumeration 9 47
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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

911 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

23 Experts available now in Live!

Get 1:1 Help Now