Recent

Author Topic: Does exist a Function in the Libs to generate a C escape string from Pascal str?  (Read 3317 times)

jamie

  • Hero Member
  • *****
  • Posts: 6077
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?

The only true wisdom is knowing you know nothing

Thaddy

  • Hero Member
  • *****
  • Posts: 14166
  • Probably until I exterminate Putin.
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.
« Last Edit: June 16, 2019, 04:54:39 pm by Thaddy »
Specialize a type, not a var.

jamie

  • Hero Member
  • *****
  • Posts: 6077
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 .
The only true wisdom is knowing you know nothing

engkin

  • Hero Member
  • *****
  • Posts: 3112
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.

jamie

  • Hero Member
  • *****
  • Posts: 6077
That is absolutely perfect, Thank You very much..

Just hopes it handles UTF8 code.  :)
The only true wisdom is knowing you know nothing

Bart

  • Hero Member
  • *****
  • Posts: 5265
    • Bart en Mariska's Webstek
Utf8EscapeControlChars with EscapeMode set to emC?
It's in LazUtf8 unit.

Bart

jamie

  • Hero Member
  • *****
  • Posts: 6077
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.
The only true wisdom is knowing you know nothing

Bart

  • Hero Member
  • *****
  • Posts: 5265
    • Bart en Mariska's Webstek
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

jamie

  • Hero Member
  • *****
  • Posts: 6077
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..?
The only true wisdom is knowing you know nothing

Thaddy

  • Hero Member
  • *****
  • Posts: 14166
  • Probably until I exterminate Putin.
No, there's no unescape there, I just checked. But not too difficult to add.
Specialize a type, not a var.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
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.
« Last Edit: June 18, 2019, 03:23:55 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Bart

  • Hero Member
  • *****
  • Posts: 5265
    • Bart en Mariska's Webstek
No, there's no unescape there, I just checked. But not too difficult to add.

I think there is one in Translations unit.

Bart

Thaddy

  • Hero Member
  • *****
  • Posts: 14166
  • Probably until I exterminate Putin.
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?
Specialize a type, not a var.

Bart

  • Hero Member
  • *****
  • Posts: 5265
    • Bart en Mariska's Webstek
That's probably the wrong place then?
Not if that is the only place it is needed in the entire LCL.

Bart

Thaddy

  • Hero Member
  • *****
  • Posts: 14166
  • Probably until I exterminate Putin.
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.
« Last Edit: June 19, 2019, 11:14:36 am by Thaddy »
Specialize a type, not a var.

 

TinyPortal © 2005-2018