Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

string problems migrating Delphi 3 to Delphi 2010

I got the source of an older project and have to change little things but I got in big trouble because of having only delphi 2010 to do that.

There is an record defined :

bbil = record
  path : string;
  pos: byte;
  nr: Word;
end;

later this definition is used to read from file :

b_bil: file of bbil;
pbbil: ^bbil;
l_bil : tlist;

while not(eof(b_bil)) do
  begin
    new(pbbil);
    read(b_bil, pbbil^);
    l_bil.add(pbbil);
  end

The primary problem is, the compiler does not accept the type "string" in the record because he wants a "finalization". So I tried to change "string" to "string[255]" or "shortstring". Doing this the app is reading the file but with wrong content.

My question is how to convert the old "string" type with which the files were written to the "new" types in Delphi 2010.

I already tried a lot e.g. "{$H-}". Adding only one char more in the record shows, the file is correct, because file is read nearly correct but truncated one char more each dataset - the length of lengthbyte+255chars seems to be correct fpr the definition but shortstring is not matching.

like image 440
rseffner Avatar asked Jan 16 '12 17:01

rseffner


People also ask

Is it difficult to migrate from Delphi 7 to XE2?

We have not had many problems migrating from 7 to 2010 to XE2. The main issue was components, as many have mentioned. The strategy we used was to change to new components in the old delphi version first, then migrate up versions.

Can I switch from Delphi 5 to Delphi 10?

The conversion from Delphi 5, 6 or 7 to Delphi 10 is done very quickly. Because of this, developments to your application do not come to a standstill. It is also possible to switch from Delphi or an outdated PHP application to Mendix.

What is an example of migration in Delphi?

An example is a project migrated from an old Delphi version (Delphi 2010) that used Rave reports with the Rave (.rav) file in the Delphi project folder which was the same for all build configurations.

Why can't I use the Delphi developers built or received code?

Existing compiled code (DCU, BPL) Delphi developers built or received (in case of third party components) cannot be used in newer versions of the product. All code needs to be recompiled from the original sources


2 Answers

Eek! It looks like your code either pre-dates or does not use long strings. If you want to get the same behaviour as in your old Delphi then you need to replace string with ShortString.

I see that you've tried that already and report that it fails. It's really the only explanation that makes any sense to me because all other string types are essentially pointers and so the only way the read could ever have worked is with a ShortString. The migration you are attempting is immense and you probably have huge numbers of confounding problems.

@LU RD makes a good point in the comments that the record layout may differ between Delphi versions since you are not using a packed array. You can investigate the record layout using the two Delphi versions that you have at hand. You will need to arrange that the size of the records match between versions, and that the offsets to the fields also match.

Based on the comments below, adding a padding byte between pos and nr will resolve your problems.

bbil = record
  path : string;
  pos: byte;
  _pad: byte;
  nr: Word;
end;

You could also achieve the same effect by setting the $ALIGN compiler option to {$ALIGN ON} which would be how I think I would go about things.

In the long run you really ought to get away from short strings, ANSI encoding, direct mapping between your internal records and your data files and so on. In the short run you may be better off getting hold of the same version of Delphi as was used to build this code and using that. I'd expect this issue to be just the tip of the iceberg.

like image 114
David Heffernan Avatar answered Nov 14 '22 23:11

David Heffernan


Just remember:

"string" <> "string[255]" <> "shortstring" <> AnsiString

Back in old DOS/Turbo Pascal days, "strings" were indeed limited to 255 characters. In large part because the 1st byte contained the string length, and a byte can only have a value between 0 and 255.

That is no longer an issue in contemporary versions of Delphi.

"ShortString" is the type for the old DOS/Pascal string type.

"LongString" has been the default string type for a long time (including the Borland Delphi 2006 I currently use for most production work). From Delphi 3 .. Delphi 2009, LongStrings held 8-bit characters, and were limited only by available memory. From Delphi 3 .. Delphi 2009, "LongStrings" were synonymous with "AnsiStrings".

Recent versions of Delphi (Delphi 2009 and higher, including the new Delphi XE2) all now default to multi-byte Unicode "WideString" strings. WideStrings, like AnsiStrings, are also effectively "unlimited" in maximum length.

This article explains in more detail:

http://delphi.about.com/od/beginners/l/aa071800a.htm

PS: Consider using "sizeof(bbil)" and "Packed" for binary records.

like image 41
paulsm4 Avatar answered Nov 14 '22 23:11

paulsm4