1

I am assigning a string to a custom type I have declared, which I Read/Write using the TTreeViews Node.Data property. I read and write to and from the node, something like this:

Read: RichEdit1.Lines.Text := TMyData(TreeView1.Selected.Data).MyString;

Write: TMyData(TreeView1.Selected.Data).MyString := RichEdit1.Lines.Text;

This works perfect for plain strings, I want to allow Rich Formatted text to be stored in the string, without losing the formatting. I managed to do this by using Streams on the RichEdit, because I am saving my database using the Freeware Zeos Lib (SQL) I get Unknown Token errors (likely from the RTF tags). How can I save without the errors?

UPDATE

I have managed to get it saving correctly without erroring now, using Base64 Encoding/Decoding as suggested by Sylverdrag. This encodes my strings removing the bad characters.

  • @Craig - so you just need to save the RTF text into database –  May 15 '11 at 18:14
  • @daemon_x yes, I have managed to save/view the RTF text at runtime, but I cannot save the database without getting Tag or Token errors, which is not use at all if I cannot save/open the file. –  May 15 '11 at 18:24
  • @Craig - and what error message you're getting ? –  May 15 '11 at 18:29
  • @daemon_x, the error I get when I try to save is something like this: SQL Error: unrecognized token: "'????????????????%???????????????..'" –  May 15 '11 at 18:37
  • It looks like unicode problem. Which version of Delphi and ZEOS Lib you have ? –  May 15 '11 at 18:43
  • Delphi XE, and using the latest Zeos Lib (7.0 Alpha I think) –  May 15 '11 at 18:56
  • They're both unicode. What says debugger to the Snippet property when saving it ? –  May 15 '11 at 19:02
  • I caught the output in a messagebox, the snippet now returns as ???c??, i tried implementing Base64 as advised by Sylverdrag so now the error is shorted, but still there. –  May 15 '11 at 19:12
  • 1
    I don't know ZEOS Lib but try to to check [this link.](http://stackoverflow.com/questions/4074383/loading-rtf-text-from-database-into-trichedit/4084140#4084140) If there is a conversion AsAnsiString try to use it this way. –  May 15 '11 at 21:45
  • Thanks for the tip. I have it all working now, except each time I click on a snippet it shows the raw RTF text and it is not formatted. I am converting the snippet code to RTF and base encoding it, when I want to view it I am base decoding it and displaying the snippet, but the rtf code is not shown formatted but as text, like so: {\rtf1\ansi\ansicpg1252\deff0\deflang2057{\fonttbl{\f0\fnil\fcharset0 Tahoma;}} \viewkind4\uc1\pard\f0\fs16 ABC\par } –  May 15 '11 at 22:24

1 Answers1

0

Check out http://delphi.about.com/od/adptips2003/a/bltip1203_5.htm

(My original answer was for C# - misread your question)

Sylverdrag
  • 8,898
  • 5
  • 37
  • 54
  • Ok, I should of added that I have tried this, but when saving my database the Zeos Lib does not like the tags that the RTF have saved with it, I kept getting Unknown Token errors etc. The funny thing is if i hard typed a rtf code into the editor, the save does not complain, but then the formatting is not stored anyway. –  May 15 '11 at 18:02
  • 2
    @Craig: I don't normally work in Delphi, so no code suggestion, but can't you base 64 encode the RTF string before storing it and decode it afterwards? That should get you out of the "unknown token" errors. – Sylverdrag May 15 '11 at 18:08
  • 1
    @Sylverdrag, I have edited my question as I forgot to mention the token errors. I have posted some code too that works with keeping the richedit formatting, but as I said the Unknown Token errors arise. I will have to read about the Base64 Encode you mentioned, if I can use that to get past the token errors then my problem is answered, so thanks for mentioning that. Perhaps a Delphi programmer can come along and advise further on what you posted. I have up voted your answer too, thanks. –  May 15 '11 at 18:21
  • @Sylverdrag, I implemented a Base64 Encode when saving the snippet, and Base64 Decode when reading the snippet, that worked but it did not defeat the saving error. I tried creating a new snippet but not entering any text, i can save no problems there, it seems to be the RTF tags are conflicting with the SQL or something. –  May 15 '11 at 18:45
  • @Craig: With base 64 (A-Z, a-z, +/=), there should not be any character able to interfere with a SQL operation, so I am not sure what the problem is. Since RTF contains only ASCII chars to start with, encoding should not be an issue. When debugging, can you check what the content of the strings are? If so you should be able to see which characters are causing troubles. – Sylverdrag May 15 '11 at 18:59
  • I am using the Base64 found from here: http://www.swissdelphicenter.ch/torry/showcode.php?id=1524 It might be that I need to make it Unicode friendly, ie AnsiString or some other changes, let me try to change it.. –  May 15 '11 at 19:04
  • @Craig - it already is. That `string` is Unicode in your Delphi XE. –  May 15 '11 at 19:28
  • I have it working, sort of. Using a Base64 code I found here: http://strcpy.com/294 I no longer get any errors, but now my Snippets wont decode correctly, they return as ??????? –  May 15 '11 at 19:33
  • Must be a problem in my code somewhere, I just created a test application using two TEdits to Base64 Encode/Decode a string and it works perfect. So somewhere in my code it doesnt like, it also seems to be a problem encoding not just decoding, so I will need to look further into this. @Sylverdrag you were right to suggest this, and I realise now it only accepts characters and numerical values, hence I can now save my database, I just need to find out why it is not encoding/decoding correctly. –  May 15 '11 at 20:02
  • Ok, I dont want too many comments confusing everyone, but I have discovered why it appears to be a unicode problem especially with the Base64, it is the stream I am using. For some reason the TMemoryStream is making the Snippet string undreadable, so when Ive been encoding/decoding it the string was corrupt to start with. I either need to rewrite the stream somehow, or find an alternative method to get the code from the richedit and also display the richedit from the code. –  May 15 '11 at 20:50
  • @Sylverdrag I have accepted your answer, I managed to fix it today (must have been having a bad day yesterday). What I now do is this, when saving the snippets, save the richedit to a temp file, load the file into a stringlist then Base64 Encode it (the rtf tags). When viewing a snippet I Base64 Decode it, assign the rtf codes into a new stringlist, save it as a temp.rtf file then reload it in the snippet viewer. I admit its not the cleanest way, but it works perfect. –  May 16 '11 at 09:05
  • 1
    @Craig; Thanks. Glad you have it working. Sometimes we have to do things the ugly way. Just don't forget to document it because it's gonna be hell to understand it if someone else ever needs to maintain your codebase. – Sylverdrag May 16 '11 at 12:20
  • @Sylverdrag, thankfully I comment most of my code. it comes in handy if say opening an old project and you need refreshing on a few things etc. I will try and do a better way than currently, as you say sometimes you have to do things that are maybe not the best. So long as it works, you can always go back and make any improvements to it. –  May 18 '11 at 00:07