Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Delphi  UTF-8 strings inside records

Posted on 2013-05-27
4
Medium Priority
?
701 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 2000 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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

705 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