Recent

Author Topic: Is there a function to convert text with control characters to a single line?  (Read 13209 times)

vfclists

  • Hero Member
  • *****
  • Posts: 1013
    • HowTos Considered Harmful?
Is there a method that allows multiline text with control characters etc to be converted to a single line, with the ability to convert it back?

It seems that some of functions which export databases as raw SQL for inserting have that ability, as they are able to convert memos to a form that allows them to be inserted as SQL. I know that base64 can do that, but I am looking for something more compact and readable.

Are the some standard methods for doing that with their Pascal equivalents?
Lazarus 3.0/FPC 3.2.2

taazz

  • Hero Member
  • *****
  • Posts: 5368
Is there a method that allows multiline text with control characters etc to be converted to a single line, with the ability to convert it back?

It seems that some of functions which export databases as raw SQL for inserting have that ability, as they are able to convert memos to a form that allows them to be inserted as SQL. I know that base64 can do that, but I am looking for something more compact and readable.

Are the some standard methods for doing that with their Pascal equivalents?

What is your problem exactly? line break and line feed characters are not breaking any sql execution. The only character that might break the sql execution is the single quote character that is used to enclose string and that can be overcome by using the quotedStr function.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

vfclists

  • Hero Member
  • *****
  • Posts: 1013
    • HowTos Considered Harmful?

What is your problem exactly? line break and line feed characters are not breaking any sql execution. The only character that might break the sql execution is the single quote character that is used to enclose string and that can be overcome by using the quotedStr function.

I need to store multiple line text on a single line, basically store the contents of memo fields on a single line, as Name=Value pairs, with Value being the whole of the memo on a single line.
But I also need to convert the Value back to its original memo form. It is not a SQL issue, I was just using the way some database exporting routines are able to code memos on a single line as an example.
Lazarus 3.0/FPC 3.2.2

mangakissa

  • Hero Member
  • *****
  • Posts: 1131
You can use TStringlist, but might be overdone.
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

minesadorada

  • Sr. Member
  • ****
  • Posts: 452
  • Retired
memo.lines.text?
GPL Apps: Health MonitorRetro Ski Run
OnlinePackageManager Components: LazAutoUpdate, LongTimer, PoweredBy, ScrollText, PlaySound, CryptINI

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1260
memo.lines.text?
Minor modification to this.  I think that's break the Name=Value requirement if there are indeed linebreaks somewhere.  Instead try
Code: [Select]
memo.lines.Delimiter := ' ';  // note the space
MyStringToStore := memo.lines.delimitedtext;

I just can't remember if memo.lines is a TStrings or a TStringList.  You may need to create a TStringList and assign the contents of memo.lines to it first...
Lazarus Trunk/FPC Trunk on Windows [7, 10]
  Have you tried searching this forum or the wiki?:   http://wiki.lazarus.freepascal.org/Alternative_Main_Page
  BOOKS! (Free and otherwise): http://wiki.lazarus.freepascal.org/Pascal_and_Lazarus_Books_and_Magazines

vfclists

  • Hero Member
  • *****
  • Posts: 1013
    • HowTos Considered Harmful?
The right terminology slipped my mind, but what I am looking for ASCII escaped text or HTML escaped text, but in a form where the non printing characters are not expressed as numeric codes. But  I will settle for that if that is what is available.
Lazarus 3.0/FPC 3.2.2

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1260
Ahh, the importance of correct terminology :-)

In htmlelements.pas, there are two routines.  EscapeHTML and UnescapeHTML.  Will either of these cover your needs?   Hmmm, they don't cover all non-printing characters :-(
Code: [Select]
function EscapeHTML ( const S : String ) : String;
begin
  // ' does not work on all versions of ie, so do not use it.
  Result := StringsReplace(s,['&','<','>','"',#39],['&amp;','&lt;','&gt;','&quot;','''],[rfReplaceAll]);
end;

function UnescapeHTML ( const S : String ) : String;
begin
  Result := StringsReplace(result,['&amp;','&lt;','&gt;','&quot;','&apos;','''],['&','<','>','"',#39,#39],[rfReplaceAll]);
end;   

I'll keep looking...
Lazarus Trunk/FPC Trunk on Windows [7, 10]
  Have you tried searching this forum or the wiki?:   http://wiki.lazarus.freepascal.org/Alternative_Main_Page
  BOOKS! (Free and otherwise): http://wiki.lazarus.freepascal.org/Pascal_and_Lazarus_Books_and_Magazines

Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Genrally (if you really need to have it on a single line and keep it readable) you will have to convert a LineEndig with something that will not occur in the text. This then depends on the context of your application.
E.g. you might want to replace LineEnding with '\n' or '<br>'or whatever you can think of.
The problem lies in "escaping" this sequnce if it occurrs in the text to be converted.

Bart

taazz

  • Hero Member
  • *****
  • Posts: 5368
Genrally (if you really need to have it on a single line and keep it readable) you will have to convert a LineEndig with something that will not occur in the text. This then depends on the context of your application.
E.g. you might want to replace LineEnding with '\n' or '<br>'or whatever you can think of.
The problem lies in "escaping" this sequnce if it occurrs in the text to be converted.

Bart

How about with #3 instead of a readable sequence?
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

vfclists

  • Hero Member
  • *****
  • Posts: 1013
    • HowTos Considered Harmful?
Re: Is there a function to convert text with control characters to a single line?
« Reply #10 on: September 18, 2014, 10:17:52 pm »
Searching stackoverflow gave me two options.

The first one, which is the second answer in this question, http://stackoverflow.com/questions/25904302/is-there-a-standard-function-that-converts-multi-line-text-into-ascii-escaped-fo,  which seems much easier, involves storing the text as the whole section in a ini file., and reading that section. Does Lazarus's TIniFile support it?

The second one involves an HTMLescape function similar to what is described above by mike, and it seems that adapting it for ASCII is also an option.
http://stackoverflow.com/questions/2968082/is-there-a-delphi-standard-function-for-escaping-html
Lazarus 3.0/FPC 3.2.2

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Is there a function to convert text with control characters to a single line?
« Reply #11 on: September 18, 2014, 10:47:03 pm »
Searching stackoverflow gave me two options.

The first one, which is the second answer in this question, http://stackoverflow.com/questions/25904302/is-there-a-standard-function-that-converts-multi-line-text-into-ascii-escaped-fo,  which seems much easier, involves storing the text as the whole section in a ini file., and reading that section. Does Lazarus's TIniFile support it?

Lazarus TIniFile has the property EscapeLineFeeds which is set from the constructor only, the problem is that the property seems to exist only for compatibility reasons, having no support code behind it, it makes it a bit useless.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Never

  • Sr. Member
  • ****
  • Posts: 409
  • OS:Win7 64bit / Lazarus 1.4
Re: Is there a function to convert text with control characters to a single line?
« Reply #12 on: September 18, 2014, 11:01:44 pm »
Code: [Select]
function func_Split1013String(param_String: String): String;
var
   loc_Original, loc_New : String;
const con_Sep10='[/]';
  const con_Sep13='[\]';
begin
  loc_Original:=param_String;
   loc_New := StringReplace(StringReplace(loc_Original, con_Sep10, #10, [rfReplaceAll]), con_Sep13, #13, [rfReplaceAll]);


    Result:= loc_New;

end;

Code: [Select]
function  func_Join1013String(param_String: String): String;
var
   loc_Original, loc_New : String;
  const con_Sep10='[/]';
  const con_Sep13='[\]';
begin
  loc_Original:=param_String;

   loc_New := StringReplace(StringReplace(loc_Original, #10, con_Sep10, [rfReplaceAll]), #13,con_Sep13, [rfReplaceAll]);

    Result:= loc_New;


end;

Code: [Select]
procedure TForm1.Button3Click(Sender: TObject);
var loc_str:String;
begin
   loc_str:=' text with ' + #10#13 + 'line break'+'text with ' + #10#13 +'line break';
    ShowMessage(func_Join1013String(loc_str));

    ShowMessage(func_Split1013String(func_Join1013String(loc_str)));
end;
Is this what are you looking for?
« Last Edit: September 18, 2014, 11:23:10 pm by Never »
Νέπε Λάζαρε λάγγεψων οξωκά ο φίλοσ'ς αραεύσε

derek.john.evans

  • Guest
Re: Is there a function to convert text with control characters to a single line?
« Reply #13 on: September 19, 2014, 08:11:34 am »
Note: UnescapeHTML still has a bug in it. Dang!

Result := StringsReplace(result,['&amp;','&lt;','&gt;','&quot;','&apos;','''],['&','<','>','"',#39,#39],[rfReplaceAll]);
-----------------------------------^ BUG

I use HTTPDefs:
Function HTTPDecode(const AStr: String): String;
Function HTTPEncode(const AStr: String): String; 

You can also use FpJsOn:
Function StringToJSONString(const S : TJSONStringType) : TJSONStringType;
Function JSONStringToString(const S : TJSONStringType) : TJSONStringType;   

Or, Base64:
function EncodeStringBase64(const s:string):String;
function DecodeStringBase64(const s:string;strict:boolean=false):String;     

These functions are related, but dont encode CR/LF's

If you really need HTML/XML encoding, I use IdStrings (from the Indy library. Note included in Lazarus)
function  StrHtmlEncode (const AStr: String): String;
function  StrHtmlDecode (const AStr: String): String;
function StrXHtmlEncode(const ASource: String): String;
function StrXHtmlDecode(const ASource: String): String;   

There are some functions in URIParser which aren't made public, but you can rip the code:
function Escape(const s: String; const Allowed: TSysCharSet): String;   
function Unescape(const s: String): String;

In Laz2_DOM, you have:
function StrToXMLValue(const s: string): string; // removes #0, encodes <>&'"
function XMLValueToStr(const s: string): string; // reverse of StrToXMLValue (except for invalid #0)   

« Last Edit: September 19, 2014, 08:18:34 am by derek.john.evans »

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1260
Re: Is there a function to convert text with control characters to a single line?
« Reply #14 on: September 19, 2014, 12:17:46 pm »
Note: UnescapeHTML still has a bug in it. Dang!

Result := StringsReplace(result,['&amp;','&lt;','&gt;','&quot;','&apos;','''],['&','<','>','"',#39,#39],[rfReplaceAll]);
-----------------------------------^ BUG

Whats the bug?
Lazarus Trunk/FPC Trunk on Windows [7, 10]
  Have you tried searching this forum or the wiki?:   http://wiki.lazarus.freepascal.org/Alternative_Main_Page
  BOOKS! (Free and otherwise): http://wiki.lazarus.freepascal.org/Pascal_and_Lazarus_Books_and_Magazines

 

TinyPortal © 2005-2018