Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2012-09

Charset in TSdfDataSet

11 messages · 5 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Roberto P.
to GLBX

Hi,

I have recently dealt with a CSV file and accents. I concluded that the TSdfDataSet had no problem with reading the file; I was using a TMemo to show the strings and to see them correctly I had to write something like this:

   Memo.Lines.Text := UTF8Encode(SdfDataSet.Fields[0].AsString)

I'm no expert at all, especially about string encoding, so maybe someone else could better explain what we should with the string format in the DataSet and in the LCL components, but that should make you work in the meantime.

R#

2012/9/6 GLBX

Show quoted text 19 lines
** Hi ! First time I am here. Hope I do well with posting my question here. Working with a CSV file encoded with accents in French (?, ?, ?, etc...), how can I change the charset for the TSdfDataSet in order to see those accents and not the ? replacing char? Thanks for your answer ! -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20120906/7cad7655/attachment.html>

Reply by email

GLBX
to Roberto P.

Thanks !

I try and let know about

GLBX

Earlier message 36 lines
----- Original Message ----- From: Roberto P. To: Lazarus mailing list Sent: Thursday, September 06, 2012 9:22 AM Subject: Re: [Lazarus] Charset in TSdfDataSet Hi, I have recently dealt with a CSV file and accents. I concluded that the TSdfDataSet had no problem with reading the file; I was using a TMemo to show the strings and to see them correctly I had to write something like this: Memo.Lines.Text := UTF8Encode(SdfDataSet.Fields[0].AsString) I'm no expert at all, especially about string encoding, so maybe someone else could better explain what we should with the string format in the DataSet and in the LCL components, but that should make you work in the meantime. R# 2012/9/6 GLBX Hi ! First time I am here. Hope I do well with posting my question here. Working with a CSV file encoded with accents in French (?, ?, ?, etc...), how can I change the charset for the TSdfDataSet in order to see those accents and not the ? replacing char? Thanks for your answer ! --

Reply by email

Mattias Gaertner
to Roberto P.: I have recently dealt with a CSV file and accents. I concluded that the TSdfDataSet had…
Show quoted text 8 lines
Hi, I have recently dealt with a CSV file and accents. I concluded that the TSdfDataSet had no problem with reading the file; I was using a TMemo to show the strings and to see them correctly I had to write something like this: Memo.Lines.Text := UTF8Encode(SdfDataSet.Fields[0].AsString)

UTF8Encode converts a widestring to an UTF8 string. AsString returns an ansistring. So the above first converts from system codepage to widestring (UTF16) and then to UTF8.

I'm no expert at all, especially about string encoding, so maybe someone else could better explain what we should with the string format in the DataSet and in the LCL components, but that should make you work in the meantime.

I'm no sdf expert, so I will assume it does no conversion like most LCL code:

If the csv file is in UTF8 and sdf does no conversion, then you can simply load the csv file and don't need any the csv file is in the french windows code page 1252 then you can use the function ConvertEncoding of the LazUtils unit lconvencoding to convert its content to UTF8 before loading it into sdf.

Mattias

Reply by email

GLBX
to Mattias Gaertner

Hi,

I checked the encodings and got :

- GuessEncoding(dataset .fields[1].asString) and got UTF8
- [application]defaultTextEncoding = cp1252

Now in DBGrid I wrote in DrawColumnCell :
...
ExtTextOut( .... UTF8ToISO_8859_1(dataCSV.Fields[datacol].AsString......) and.....
Now accents are correctly displayed...

But I will get the problem back when validating changes in fields !!! (I Tried with OnSetText : but inefficient !)

I go on looking for solution....

GLBX

Earlier message 46 lines
----- Original Message ----- From: "Mattias Gaertner" To: Sent: Thursday, September 06, 2012 9:45 AM Subject: Re: [Lazarus] Charset in TSdfDataSet > On Thu, 6 Sep 2012 09:22:57 +0200 > "Roberto P." wrote: > >> Hi, >> >> I have recently dealt with a CSV file and accents. >> I concluded that the TSdfDataSet had no problem with reading the file; I >> was using a TMemo to show the strings and to see them correctly I had to >> write something like this: >> >> Memo.Lines.Text := UTF8Encode(SdfDataSet.Fields[0].AsString) > > UTF8Encode converts a widestring to an UTF8 string. AsString returns an > ansistring. So the above first converts from system codepage to > widestring (UTF16) and then to UTF8. > > >> I'm no expert at all, especially about string encoding, so maybe someone >> else could better explain what we should with the string format in the >> DataSet and in the LCL components, but that should make you work in the >> meantime. > > I'm no sdf expert, so I will assume it does no conversion like most > LCL code: > > If the csv file is in UTF8 and sdf does no conversion, then you can > simply load the csv file and don't need any conversion at all. > > If the csv file is in the french windows code page 1252 then you can > use the function ConvertEncoding of the LazUtils unit lconvencoding to > convert its content to UTF8 before loading it into sdf. > > Mattias > > -- > _______________________________________________ > Lazarus mailing list > > http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

Reply by email

Mattias Gaertner
to GLBX

GLBX hat am 6. September 2012 um 11:36 geschrieben:

Show quoted text 5 lines
Hi, I checked the encodings and got : - GuessEncoding(dataset .fields[1].asString) and got UTF8

Keep in mind that it is impossible to reliably get the codepage of a text, unless the text begins with a BOM. GuessEncoding simply checks if the string validates some UTF-8 rules. If it is valid it returns UTF-8 otherwise the system code page.

Show quoted text 7 lines
- [application]defaultTextEncoding = cp1252 Now in DBGrid I wrote in DrawColumnCell : ... ExtTextOut( .... UTF8ToISO_8859_1(dataCSV.Fields[datacol].AsString......) and..... Now accents are correctly displayed...

It seems you are calling the windows API function ExtTextOut directly instead of the x-platform LCL one. The LCL one expects UTF-8, so you don't need the conversion.

But I will get the problem back when validating changes in fields !!! (I Tried with OnSetText : but inefficient !)

Mattias
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20120906/eb4e52bf/attachment.html>

Reply by email

GLBX
to Mattias Gaertner

Right !

with Textout, I get accents correctly

but... when I post the changes, now, I get the problem

Originally DAMMANS

Changed to D?MMANS

Display after editing
D?MMANS (coorect)

Saved in the CSV file as !!!!!
D??MMANS
and...

when I display after new loading :
D?MMANS (!!!!?!?!?!)

Oups !!

GLBX

Earlier message 40 lines
----- Original Message ----- From: Mattias Gaertner To: Lazarus mailing list Sent: Thursday, September 06, 2012 12:04 PM Subject: Re: [Lazarus] Charset in TSdfDataSet GLBX hat am 6. September 2012 um 11:36 geschrieben: > Hi, > > I checked the encodings and got : > > - GuessEncoding(dataset .fields[1].asString) and got UTF8 Keep in mind that it is impossible to reliably get the codepage of a text, unless the text begins with a BOM. GuessEncoding simply checks if the string validates some UTF-8 rules. If it is valid it returns UTF-8 otherwise the system code page. > - [application]defaultTextEncoding = cp1252 > > Now in DBGrid I wrote in DrawColumnCell : > ... > ExtTextOut( .... UTF8ToISO_8859_1(dataCSV.Fields[datacol].AsString......) > and..... > Now accents are correctly displayed... It seems you are calling the windows API function ExtTextOut directly instead of the x-platform LCL one. The LCL one expects UTF-8, so you don't need the conversion. > But I will get the problem back when validating changes in fields !!! > (I Tried with OnSetText : but inefficient !) Mattias ------------------------------------------------------------------------------ --

Reply by email

Roberto P.
to GLBX

2012/9/6 GLBX

Show quoted text 23 lines
** Right ! with Textout, I get accents correctly but... when I post the changes, now, I get the problem Originally DAMMANS Changed to D?MMANS Display after editing D?MMANS (coorect) Saved in the CSV file as !!!!! D??MMANS and... when I display after new loading : D?MMANS (!!!!?!?!?!) Oups !!

when working on that project of mine, I was in a hurry and I couldn't go deeper in understanding things (like the double conversion through UTF16, thanks Mattias).
At that time I was also UNABLE to save back to csv through the sdf dataset, even with readonly = false, putting the db in edit/append and posting the changes.... it looked somehow a reader-only dataset.

A quick (and maybe unefficient or dirty) trick to get around it has been to use:
- a TSDFDataset to read the CSV file (my separtor was non-standard)
- copying it to a TMemDataset which can be manipulated really fast
- place a TCSVExporter component on the form (install in the palette from the dbexport package) connected to the TMemDataset When you're done with editing and posting, you can simply call TCSVExporter.Execute to save it to CSV.

An alternative (that I couldn't explore due to the limited amount of time I had) might be this one: http://wiki.freepascal.org/CsvDocument which supposedly reads and writes, either randomly (in memory) or sequentially, a CSV file.

R#

Show quoted text 53 lines
GLBX ----- Original Message ----- *From:* Mattias Gaertner *To:* Lazarus mailing list *Sent:* Thursday, September 06, 2012 12:04 PM *Subject:* Re: [Lazarus] Charset in TSdfDataSet GLBX hat am 6. September 2012 um 11:36 geschrieben: > Hi, > > I checked the encodings and got : > > - GuessEncoding(dataset .fields[1].asString) and got UTF8 Keep in mind that it is impossible to reliably get the codepage of a text, unless the text begins with a BOM. GuessEncoding simply checks if the string validates some UTF-8 rules. If it is valid it returns UTF-8 otherwise the system code page. > - [application]defaultTextEncoding = cp1252 > > Now in DBGrid I wrote in DrawColumnCell : > ... > ExtTextOut( .... UTF8ToISO_8859_1(dataCSV.Fields[datacol].AsString......) > and..... > Now accents are correctly displayed... It seems you are calling the windows API function ExtTextOut directly instead of the x-platform LCL one. The LCL one expects UTF-8, so you don't need the conversion. > But I will get the problem back when validating changes in fields !!! > (I Tried with OnSetText : but inefficient !) Mattias ------------------------------ -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20120906/1e14a75a/attachment.html>

Reply by email

Unknown author
to Roberto P.
Roberto P. wrote
2012/9/6 GLBX

[snip]

Show quoted text 21 lines
when working on that project of mine, I was in a hurry and I couldn't go deeper in understanding things (like the double conversion through UTF16, thanks Mattias). At that time I was also UNABLE to save back to csv through the sdf dataset, even with readonly = false, putting the db in edit/append and posting the changes.... it looked somehow a reader-only dataset. A quick (and maybe unefficient or dirty) trick to get around it has been to use: - a TSDFDataset to read the CSV file (my separtor was non-standard) - copying it to a TMemDataset which can be manipulated really fast - place a TCSVExporter component on the form (install in the palette from the dbexport package) connected to the TMemDataset When you're done with editing and posting, you can simply call TCSVExporter.Execute to save it to CSV. An alternative (that I couldn't explore due to the limited amount of time I had) might be this one: http://wiki.freepascal.org/CsvDocument which supposedly reads and writes, either randomly (in memory) or sequentially, a CSV file.

Yes, but if you read that page carefully, you will see that

"Both UTF-8 encoding and windows-xxx codepages can be used with CsvDocument library. The library uses string type for all string operations and will not do any encoding conversion for you. Keep in mind though that Excel does not support CSV files in UTF-8."

So you will experience the same problems.

Michael.

Reply by email

waldo kitty
to GLBX: Right ! with Textout, I get accents correctly but... when I post the changes, now, I get…
GLBX wrote 7 lines
Right ! with Textout, I get accents correctly but... when I post the changes, now, I get the problem Originally DAMMANS Changed to D?MMANS Display after editing D?MMANS (coorect)

ok... looks fine...

Saved in the CSV file as !!!!! D??MMANS

that is UTF8 encoding of the "?" character...

and... when I display after new loading : D?MMANS (!!!!?!?!?!)

that is proper...

Oups !!

can't you convert the text back to CP1252 before writing it to the CSV?? it would seem that that's what you are wanting to do...

Reply by email

Mattias Gaertner
to Unknown author: [snip] Yes, but if you read that page carefully, you will see that "Both UTF-8 encoding…

hat am 6. September 2012 um 14:32 geschrieben:

Show quoted text 5 lines
[...] "Both UTF-8 encoding and windows-xxx codepages can be used with CsvDocument library. The library uses string type for all string operations and will not do any encoding conversion for you. Keep in mind though that Excel does not support CSV files in UTF-8."

Excel supports CSV with UTF-8 BOM.

Mattias
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20120906/22835eac/attachment.html>

Reply by email