ch: Char; begi n for ch in ListBoxl.Items.Text do if Ord (ch) >= 256 then begi n encodingl := TEncoding.UTF8; break; end;

Using similar code you could decide which format to use, depending on the situation. It might be a better idea, though, to move all of your files to Unicode encoding (UTF-8 or UTF-16), regardless of the actual data. Using UTF-16 will make the files bigger, but will also reduce the conversions when saving and loading.

However, since there is no way to specify a default conversion, going for Unicode encoding of your files would mean the need to change each and every file save operation... unless we use a trick, changing the standard

49 Checking if ch >= 256 does not work if the default code page is something other than Windows 1252. For example "Cantu" does not have any characters >= 256, but it cannot be represented in code page 1251.

behavior of the class. Such a hack could comes in the form of a class helper50. Consider the following code: type

TStringsHelper = class helper for TStrings procedure SaveToFile (const strFileName: string); end ;

procedure TStringsHe1per.SaveToFi1e(

const strFileName: string); begi n inherited SaveToFile (strFileName, TEncoding.UTF8); end ;

Notice that inherited here doesn't mean to call a base class but the class helped by the class helper. Now you simply write (or keep your code as): | ListBoxl.Items.SaveToFile(strFileName);

to save it as UTF-8 (or any other encoding of your choice). You'll find this code in the StreamEncoding example.

Was this article helpful?

0 0
Project Management Made Easy

Project Management Made Easy

What you need to know about… Project Management Made Easy! Project management consists of more than just a large building project and can encompass small projects as well. No matter what the size of your project, you need to have some sort of project management. How you manage your project has everything to do with its outcome.

Get My Free Ebook

Post a comment