Lazarus

Programming => General => Topic started by: jamie on June 16, 2019, 04:08:29 pm

Title: Does exist a Function in the Libs to generate a C escape string from Pascal str?
Post by: jamie on June 16, 2019, 04:08:29 pm
Before I write a Read and Write Pascal to C escape string functions I would like to know if
one already exists ?
 
 I have created a class derived from TiniFiles to Write out Tpoint, TRect as comma delimited
numbers and the required Reader to resolve them. I also have put in a WriteBinString and
ReadBinString to dump the complete list of a TlistBox, Combo Box ect, into a Binary block
so that I can also save the CR/LF and any special chars..

 I like using a Text based file for parameters , setup etc and  its nice to be able to save the
complete contents of a Memo,STringlist under a single key. I  would like it to be human
readable and the C style syntax fits the bill perfectly to allow me to write out the
\n\l, or any other special values. This can be edited later to make corrections using notepad if
needed and it also uses less space, which is a factor, too.

 Any one know of ready made functions/Procedures in the stock libs for this?

Title: Re: Does exist a Function in the Libs to generate a C escape string from Pascal str?
Post by: Thaddy on June 16, 2019, 04:52:51 pm
and it also uses less space, which is a factor, too.
Such function results do take more space, not less, you want escapes.....which will add to the string length....
If you want to interface with real C/C++, keep the format as is. The C stdlib is able to escape such things at the C side.
Title: Re: Does exist a Function in the Libs to generate a C escape string from Pascal str?
Post by: jamie on June 16, 2019, 05:05:12 pm
I know it will take a little more space but currently I am using the BinaryStream method which takes 2 * the total count of the characters and its not human readable.
 
 using a C escape string will only grow the string where chars are below 32 in value using a
two character notation and a \Xnnn where needed..

 I looked using the EncodeString64/DecodeString64 but does not make it human readable.

My idea is to simply write out a UTF8 string converted to the C escape syntax so that I can
have all the CR/LF,TAB, BELL etc in it along with any UTF8 codes., I May even need to put the
BOM at the start of the block if things get kinky .
Title: Re: Does exist a Function in the Libs to generate a C escape string from Pascal str?
Post by: engkin on June 16, 2019, 09:14:02 pm
See if StringToJSONString from unit fpjson suits your needs:
Code: Pascal  [Select]
  1. uses fpjson...
  2.  
  3. var
  4.   s: string;
  5. begin
  6.   s := 'ABC'#9'def'#$0D#$0A#$00#$01#$29'ghi';
  7.   WriteLn(StringToJSONString(s));
  8.  

gives:
Quote
ABC\tdef\r\n\u0000\u0001)ghi

You also have opposite function JSONStringToString.
Title: Re: Does exist a Function in the Libs to generate a C escape string from Pascal str?
Post by: jamie on June 17, 2019, 12:02:06 am
That is absolutely perfect, Thank You very much..

Just hopes it handles UTF8 code.  :)
Title: Re: Does exist a Function in the Libs to generate a C escape string from Pascal str?
Post by: Bart on June 17, 2019, 08:40:45 pm
Utf8EscapeControlChars with EscapeMode set to emC?
It's in LazUtf8 unit.

Bart
Title: Re: Does exist a Function in the Libs to generate a C escape string from Pascal str?
Post by: jamie on June 17, 2019, 11:07:23 pm
The JSON string seems to work with UTF8, it writes and reads the strings find.

Besides I didn't see a corresponding function for Utf8EscapeControlChars in the file..

Thanks.
Title: Re: Does exist a Function in the Libs to generate a C escape string from Pascal str?
Post by: Bart on June 17, 2019, 11:18:43 pm
Besides I didn't see a corresponding function for Utf8EscapeControlChars in the file..

I don't know what you mean by that.
The function is inside the LazUtf8 unit appr around line 2880:
Code: Pascal  [Select]
  1. {
  2.   Translates escape characters inside an UTF8 encoded string into
  3.   human readable format.
  4.   Mainly used for logging purposes.
  5.   Parameters:
  6.     S         : Input string. Must be UTF8 encoded.
  7.     EscapeMode: controls the human readable format for escape characters.
  8. }
  9. function Utf8EscapeControlChars(S: String; EscapeMode: TEscapeMode = emPascal): String;

Bart
Title: Re: Does exist a Function in the Libs to generate a C escape string from Pascal str?
Post by: jamie on June 17, 2019, 11:21:11 pm
I mean is I need it to work both directions..

To a C escape string and back again..

That function seems to only vert to..?
Title: Re: Does exist a Function in the Libs to generate a C escape string from Pascal str?
Post by: Thaddy on June 18, 2019, 09:11:27 am
No, there's no unescape there, I just checked. But not too difficult to add.
Title: Re: Does exist a Function in the Libs to generate a C escape string from Pascal str?
Post by: lucamar on June 18, 2019, 03:19:11 pm
No, there's no unescape there, I just checked. But not too difficult to add.

Indeed. :)
This is a quick implementation of an "unscape" function I use sometimes. Not, maybe, very elegant but it works:

Code: Pascal  [Select]
  1. type
  2.   TScape = packed record
  3.     Scaped: String[2];
  4.     Unscaped: String[2];
  5.   end;
  6.  
  7. const
  8.   {Default replacement table for C-style scape strings}
  9.   DefScapes: array [0..8] of TScape = (
  10.       (Scaped: '\\'; Unscaped: #01),
  11.       (Scaped: '\a'; Unscaped: #07),
  12.       (Scaped: '\b'; Unscaped: #08),
  13.       (Scaped: '\t'; Unscaped: #09),
  14.       (Scaped: '\v'; Unscaped: #11),
  15.       (Scaped: '\f'; Unscaped: #12),
  16.       (Scaped: '\n'; Unscaped: LineEnding),
  17.       (Scaped: '\r'; Unscaped: #13),
  18.       (Scaped: #01; Unscaped: '\'));
  19.  
  20. function Unscape(Source: String): String;
  21. var
  22.   i: Integer;
  23. begin
  24.   Result := Source;
  25.   for i := 0 to High(DefScapes) do
  26.       Result := StringReplace(Result,
  27.                     DefScapes[i].Scaped,
  28.                     DefScapes[i].Unscaped,
  29.                     [rfIgnoreCase,rfReplaceAll]);
  30.   end;
  31. end;

It lacks processing of "\x00" and such, but that isn't too difficult to add either.
Title: Re: Does exist a Function in the Libs to generate a C escape string from Pascal str?
Post by: Bart on June 18, 2019, 05:02:22 pm
No, there's no unescape there, I just checked. But not too difficult to add.

I think there is one in Translations unit.

Bart
Title: Re: Does exist a Function in the Libs to generate a C escape string from Pascal str?
Post by: Thaddy on June 18, 2019, 05:32:45 pm
No, there's no unescape there, I just checked. But not too difficult to add.

I think there is one in Translations unit.

Bart
That's probably the wrong place then?
Title: Re: Does exist a Function in the Libs to generate a C escape string from Pascal str?
Post by: Bart on June 19, 2019, 10:55:53 am
That's probably the wrong place then?
Not if that is the only place it is needed in the entire LCL.

Bart
Title: Re: Does exist a Function in the Libs to generate a C escape string from Pascal str?
Post by: Thaddy on June 19, 2019, 11:12:39 am
Not if that is the only place it is needed in the entire LCL.

Bart
It looks like a generic utility function, just like the reverse, so I still think it is the wrong place.
And it is good practice to combine functions and its reverse in the same unit.
I would say that if a reverse exists, you would put it in the same unit.