Saving TreeView to UTF8 file problem

Hello!

I have the following problem:

While

M:TStrings;
M.SaveToFile(filename,TEncoding.UTF8);

and 

M:TListBox;
M.SaveToFile(filename,TEncoding.UTF8);

work fine - they produce UTF8 files, on the other hand

M:TTreeView;
M.SaveToFile(filename,TEncoding.UTF8);

does not produce UTF8 file, it produces plain ASCII file
(it produces file with correnct contents but format is not the right one).

Does anybody have any idea why saving treeview to file does not produce UTF8 file as demanded,
but saving strings and listbox do it ok?

I am using XE6.

Thanks in advance.
0
Andrej
7/30/2015 7:59:54 AM
embarcadero.delphi.general 4258 articles. 0 followers. Follow

6 Replies
975 Views

Similar Articles

[PageSpeed] 51

Andrej wrote:

> Does anybody have any idea why saving treeview to file does not
> produce UTF8 file as demanded

There is a bug in the TCustomTreeView.SaveToStream() method:

{code}
procedure TCustomTreeView.SaveToStream(Stream: TStream; Encoding: TEncoding);
begin
  with TTreeStrings.Create(Items) do
    try
      SaveTreeToStream(Stream, Encoding); // <--- bug here, do you see it?
    finally
      Free;
  end;
end;
{code}

Which Encoding object do you think is getting passed to SaveTreeToStream()? 
 Hint: it is NOT the one you are expecting!

TTreeStrings derives from TStrings.  In XE, a new Encoding property was added 
to TStrings (so when you load a file/stream, you can save the strings to 
another file/stream using the original encoding).

The 'with' statement is causing the compiler to pass the TTreeStrings.Encoding 
property, not the Encoding given to SaveToStream().

The TTreeStrings.Encodings property is initially nil, so that tells SaveTreeToStream() 
to use TEncoding.Default (not ASCII) for its encoding:

{code}
procedure TTreeStrings.SaveTreeToStream(Stream: TStream; Encoding: TEncoding);
const
  ...
var
  ...
begin
  ...
    if Encoding = nil then
      Encoding := TEncoding.Default; // <-- this gets used in your case!
  ...
end;
{code}

This bug was introduced in XE (when TStrings.Encoding was first added), and 
was finally fixed in XE8 by renaming the Encoding parameter of SaveToStream():

{code}
procedure TCustomTreeView.SaveToStream(Stream: TStream; AEncoding: TEncoding);
begin
  with TTreeStrings.Create(Items) do
    try
      SaveTreeToStream(Stream, AEncoding);
    finally
      Free;
  end;
end;
{code}

> but saving strings and listbox do it ok?

Because they are not affected by this bug.

-- 
Remy Lebeau (TeamB)
0
Remy
7/30/2015 5:07:23 PM
Dear Remy, thanks for exact explanation again.
Then I will wait with solving this problem till I move to a newer Delphi version in the future.
I do not like to change original Delphi files ;)
Best wishes.
0
Andrej
7/30/2015 5:41:16 PM
Andrej wrote:

> Then I will wait with solving this problem till I move to
> a newer Delphi version in the future.

FYI, this bug also affects the TCustomTreeView.LoadFrom...() methods as well.

-- 
Remy Lebeau (TeamB)
0
Remy
7/30/2015 6:25:15 PM
At the moment I use only  SaveToFile,but I will have this in mind too ;)
Thanks.


> {quote:title=Remy Lebeau (TeamB) wrote:}{quote}
> Andrej wrote:
> 
> > Then I will wait with solving this problem till I move to
> > a newer Delphi version in the future.
> 
> FYI, this bug also affects the TCustomTreeView.LoadFrom...() methods as well.
> 
> -- 
> Remy Lebeau (TeamB)
0
Andrej
7/31/2015 6:50:12 AM
On Thu, 30 Jul 2015 10:07:23 -0700, Remy Lebeau (TeamB)
<no.spam@no.spam.com> wrote:

>This bug was introduced in XE (when TStrings.Encoding was first added), and 
>was finally fixed in XE8 by renaming the Encoding parameter of SaveToStream():
>
>{code}
>procedure TCustomTreeView.SaveToStream(Stream: TStream; AEncoding: TEncoding);
>begin
>  with TTreeStrings.Create(Items) do
>    try
>      SaveTreeToStream(Stream, AEncoding);
>    finally
>      Free;
>  end;
>end;
>{code}
>

I am using XE5. I bought XE7 7 months ago but never got around to
using it and now that too is outdated... :(

Anyway, could the problem described in this thread be fixed locally by
editing the VCL source file in question also for XE5?


---
Bo Berglund
Sweden & Texas
Newsreader: Forte Free Agent 1.92/32.572
0
Bo
8/1/2015 7:24:09 AM
Bo wrote:

> Anyway, could the problem described in this thread be fixed
> locally by editing the VCL source file in question also for XE5?

Yes.  You can edit Vcl.ComCtrls.pas to remove the offending 'with' statement 
to remove any ambiguity over which Encoding object to use, and then you can 
add the modified .pas file to your projects when needed:

{code}
procedure TCustomTreeView.SaveToStream(Stream: TStream; Encoding: TEncoding);
var
  LStrings: TTreeStrings;
begin
  LStrings := TTreeStrings.Create(Items);
  try
    LStrings.SaveTreeToStream(Stream, Encoding);
  finally
    LStrings.Free;
  end;
end;
{code}

If you were to just rename the Encoding parameter instead (as Embarcadero 
did in XE8), that would require a corresponding rename in the interface section 
as well, and that is not allowed when editing VCL source code, unless you 
are prepared to recompile the entire VCL library.

-- 
Remy Lebeau (TeamB)
0
Remy
8/3/2015 11:10:13 PM
Reply:

Similar Artilces:

Saving TreeView to UTF8 file problem #2
Hello! I have the following problem: While M:TStrings; M.SaveToFile(filename,TEncoding.UTF8); and M:TListBox; M.SaveToFile(filename,TEncoding.UTF8); work fine - they produce UTF8 files, on the other hand M:TTreeView; M.SaveToFile(filename,TEncoding.UTF8); does not produce UTF8 file, it produces plain ASCII file (it produces file with correnct contents but format is not the right one). Does anybody have any idea why saving treeview to file does not produce UTF8 file as demanded, but saving strings and listbox do it ok? I am using XE6. Thanks in advance. ...

superreview denied: [Bug 279070] File
Daniel Veditz <dveditz@cruzio.com> has denied Daniel Veditz <dveditz@cruzio.com>'s request for superreview: Bug 279070: File - save page as overwrites lnk targets https://bugzilla.mozilla.org/show_bug.cgi?id=279070 Attachment 176739: Fixes the problem with overwritten .lnk files in the File Save As-dialog in Firefox 1.0.1 https://bugzilla.mozilla.org/attachment.cgi?id=176739&action=edit ------- Additional Comments from Daniel Veditz <dveditz@cruzio.com> sr- This patch simply backs out the fix for bug 271732 (http://www.mozilla.org/security/announce/mfsa20...

when saving a file (save page as) please have option of saving file the title and not the actual file name
Name: Dan Kelleher Product: Firefox Summary: when saving a file (save page as) please have option of saving file the title and not the actual file name Comments: I like you browser better than windows explorer but windows has one feature that I wish you had. When saving a file (save page as) please have option of saving file the title and not the actual file name. Many files have number names that have no relationship to the web page. Saving as the page title gives an exact indication of the topic of the page saved. Keep up the good work. see http://www.sciencedaily.com/news/...

Problems saving excel files and opening word files with cifs
I have a NW6SP4 cluster with cifs. Users can access shares but they are not able to open word files nor saving excel files (word and excel simply stuck!). It doesn't ahppens with other kind of files (text only, power point, acess databases). I guess it has something to do with the way excel and world work with files (using temporary files). I tried to apply the latest cifs patches available for nw6 (cifs.nlm 1.60 oct 5 2004), but it didn't solve the problem. I'm using word and excel 2000. Can someone help me ? Thanks in davance. You're having this problem when...

superreview granted: [Bug 409796] No pictures shown in saved file (file name and folder name, containing that file, is in cyrillic) : [Attachment 301753] fixes the problem. try1.
Christian :Biesinger <cbiesinger@gmx.at> has granted Evgeniy Ivanov (powerfox) <lolkaantimat@gmail.com>'s request for superreview: Bug 409796: No pictures shown in saved file (file name and folder name, containing that file, is in cyrillic) https://bugzilla.mozilla.org/show_bug.cgi?id=409796 Attachment 301753: fixes the problem. try1. https://bugzilla.mozilla.org/attachment.cgi?id=301753&action=edit ...

File Upload ASP.NET
Question For Everyone, I recently added an image upload to the member profile page of my site.  Everything works fine, users are only allowed to upload GIFS or JPEGS.  The problem is, once I upload the file, if I try to upload it again(and overwrite the current file), it gives an error: Error saving c:\websites\testwebsite\images\401.jpgSystem.UnauthorizedAccessException: Access to the path "c:\websites\testwebsite\images\401.jpg" is denied. at System.IO.__Error.WinIOError(Int32 errorCode, String str) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, Fi...

problems in saving a file
    hi all, i have upload a file to a webform. but i can't save it to the server. how can i save it? i am getting an error message like this .Access to the path "D:\Inetpub\wwwroot\Infomart\VnillaQuestions.rtf" is denied. please answer me soon.                                  raji HI ..     Just give the full permissions to the folder 'Informart" .... Right click the folder and give the folder sha...

File saving problem....
Hi All, I have licence.exe file i want to save that file on runtime with different name. i mean to say that i want to make copy of that file on run time. how can i achieve that ? is anybody have some idea ? Thanks -Nelly      If the File is placed in the main websiet folder, System.IO.File.Copy(Server.MapPath("~/licence.exe "), Server.MapPath("~/licenceCopy.exe"))  Regards,Anas Ghanem.Note:Please Don't hesitate to click "Report Abuse" link if you noticed something wrong on the forums (like duplicate ,Off-topic,offensive...

Saving the file problem
Hi! Does anybody know how to save the file in Excel format but difference worksheet? That mean the same file name but difference worksheet in a file? Please kindly help me solve the problem. Thank you very much first. ...

File-Save As Problems
When using SQL Advantage, I am not always able to write the entire file to disk when I do a File-Save As. I write the file out as a text file and have increased the file size to 512K. Does anyone have any idea what might be going on. This doesn't happen all of the time but I have a critical application and can't afford for it to happen any of the time. The particular file in question should be about 100k but only 9k is being written. I have written other files that are 10-35k in size. The version that I have is SQL Advantage 11.5/P/PC. Help, Billy Vick bvick@us.ibm.com ...

File saving problems
When working on documents in Word 2003 user saves the document and all is well. That is until they go back the next day and discover there is nothing in the document and the value displaed in properties is 0 KB > When working on documents in Word 2003 user saves the document and all is > well. That is until they go back the next day and discover there is > nothing in the document and the value displaed in properties is 0 KB Sorry for lack of info NW6.5 sp3 XP PRO sp2 mschaefe@roxanaschools.org wrote: > > When working on documents in Word 2003 user saves the do...

Ide temp file problem - .$$$ file
Hi i have problem with BDS IDE /Code editor all units i can change and save but one i can not if i have option in Tools->Options->editor options - Crate back up files chcked i get error "Unable to rename 'C:\KarolWin\DMZestawienia.pas' to 'C:\KarolWin\DMZestawienia.pas.~1~' when i uncheck this option i get i get error "Unable to rename 'C:\KarolWin\DMZestawienia.$$$' to 'C:\KarolWin\DMZestawienia.pas' i delete this temp file .$$$ and i delete __history dir and i restart BDS but can not save any changes to this file what c...

File Saving Problem
Name: William Wilson Email: grendelttrollatverizondotnet Product: Firefox Summary: File Saving Problem Comments: I use Firefox version 2.0.0.6 On MandrivaLinux 2007 Spring Free edition. It works great...until I want to save a file. It loads up the "Save To.." window, then eats up all my ram and freezes for about 5 or 6 seconds. Then it functions normally. Has anyone had a problem like this? ...

How disable save file popup error message: file "could not be saved, because the source file could not be read." ; jor
When I save web pages (complete, not merely html, in order to save images) to my disk, while offline, I get about 10 pop up messages saying it can't load .js & .php files. I have to close each one of those 10 windows individually - a big waste of human time. Firefox 3.6.10 on Ubuntu Linux 10.4 Is there a way to 1) disable those info messages so they don't pop up, or 2) have ff put all the messages into merely one window, so I only have to close one window per web page. Thanks. :) = Popup window error message: {filename path to folder on disk} "could not...

Web resources about - Saving TreeView to UTF8 file problem - embarcadero.delphi.general

Live-Beta TreeView Demo: Frameless Layout with Highlighting TreeView Demo: Frameless Layout with Highlighting ...
Welcome to our beta site for CBC Live mp3 streams! This is a form of online broadcast we are exploring right now, both from a technical best-practices ...

Visual Basic for Applications - Wikipedia, the free encyclopedia
Visual Basic for Applications ( VBA ) is an implementation of Microsoft 's event-driven programming language Visual Basic 6 and its associated ...

From the Saint
It's full steam ahead for events in the Saint today; although some have been postponed, others are getting bigger and better.

HOME - St. Catharines
St. Catharines is a city that offers many ammenities and opportunities to its residents. It is a place known for its friendliness, charm and ...

CrossFont Help
CrossFont Help

Examples - AlloyUI
AlloyUI is a framework built on top of YUI3 that provides a simple API for building high scalable applications.

allworldsoft.com — all software.
Xara X1 1.1 DL Xara X is an advanced graphic illustration package for Windows, that offers unrivaled redraw speed and a user interface which ...

CrossFont Help
CrossFont Help

Evolution of the Windows Interface
... a 3D button, comboboxes. Windows 95 introduced a slew of new 3D controls, such as rich text edit, toolbars/statusbar, progressbar, treeview/listview, ...

Silverlight - Blackboard
Silverlight is a development platform for creating rich media applications and business applications for the Web, desktop, and mobile devices.Microsoft ...

Resources last updated: 12/8/2015 2:37:06 AM