RBA
RBA

Reputation: 12584

Delphi - get encoding for a given file

I have read this question which I thought would give me what I was after:

How Can I Best Guess the Encoding when the BOM (Byte Order Mark) is Missing?

I would like to know if there is another way to get the file encoding, without using Mozilla's i18n component in D2006? I can not use other 3d party components.

I have read the all the answers from original question, and I can not use the interface provided because the client doesn't accept the deployment of that dll:

Some of the links provided in the original question are dead, and none address my problem, which is:
How to get the file encoding without using 3rd party components?

Upvotes: 2

Views: 3706

Answers (2)

bobonwhidbey
bobonwhidbey

Reputation: 505

Determining the encoding of a file seems to be problematic. It appears that some of the UTF8 files do not have a BOM. This appears to work:

InputData.LoadFromFile(f,TEncoding.UTF8);
if InputData.count=0 then
  InputData.LoadFromFile(f);

Is there a better approach. I know this solution isn't very elegant.

Upvotes: 1

David Heffernan
David Heffernan

Reputation: 613531

I would look for a BOM first and if one is not found call IsTextUnicode. But beware that no method is foolproof.

Upvotes: 4

Related Questions