Recent

Author Topic: Do any Lazarus libraries contain html and url decode/encode functions?  (Read 13956 times)

vfclists

  • Hero Member
  • *****
  • Posts: 1165
    • HowTos Considered Harmful?
Do any Lazarus libraries contain html and url decode/encode functions?

I have searched the Lazarus directories but haven't found any.
Lazarus 3.0/FPC 3.2.2

vfclists

  • Hero Member
  • *****
  • Posts: 1165
    • HowTos Considered Harmful?
I used the TiIdURI.urldecode function from Indy. More than I needed, but I am using Indy in the project anyway.

Do any Lazarus libraries contain html and url decode/encode functions?

I have searched the Lazarus directories but haven't found any.
Lazarus 3.0/FPC 3.2.2

fabienwang

  • Sr. Member
  • ****
  • Posts: 449
  • Lazarus is the best
    • My blog
This post is old, but if anyone needs URLDecode without searching or installing Indy, here you go:

Code: Pascal  [Select][+][-]
  1. // Convert URLEncoded string to utf8 string
  2. function URLDecode(const s: String): String;
  3. var
  4.    sAnsi: String;
  5.    sUtf8: String;
  6.    sWide: WideString;
  7.  
  8.    i, len: Cardinal;
  9.    ESC: string[2];
  10.    CharCode: integer;
  11.    c: char;
  12. begin
  13.    sAnsi := PChar(s);
  14.    SetLength(sUtf8, Length(sAnsi));
  15.    i := 1;
  16.    len := 1;
  17.    while (i <= Cardinal(Length(sAnsi))) do begin
  18.       if (sAnsi[i] <> '%') then begin
  19.          if (sAnsi[i] = '+') then begin
  20.             c := ' ';
  21.          end else begin
  22.             c := sAnsi[i];
  23.          end;
  24.          sUtf8[len] := c;
  25.          Inc(len);
  26.       end else begin
  27.          Inc(i);
  28.          ESC := Copy(sAnsi, i, 2);
  29.          Inc(i, 1);
  30.          try
  31.             CharCode := StrToInt('$' + ESC);
  32.             c := Char(CharCode);
  33.             sUtf8[len] := c;
  34.             Inc(len);
  35.          except end;
  36.       end;
  37.       Inc(i);
  38.    end;
  39.    Dec(len);
  40.    SetLength(sUtf8, len);
  41.  
  42.    sWide := UTF8Decode(sUtf8);
  43.    len := Length(sWide);
  44.  
  45.    Result := sWide;
  46. end;
  47.  

PS: why there are </i> at the end? ^^
I'm using Arch Linux.
Known for: CPickSniff, OpenGrabby
Contributed to: LazPaint

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12776
  • FPC developer.

PS: why there are </i> at the end? ^^

It seems in your post some array's were translated to <i>'s, and forum software tries to close the tags. Maybe it is a result of how you created your post.

ThierryJ

  • New Member
  • *
  • Posts: 21
Re: Do any Lazarus libraries contain html and url decode/encode functions?
« Reply #4 on: August 02, 2019, 05:21:54 pm »
Thank you Fabien for this little piece of code that works perfectly ! I was fighting with the URLs pointing to the audio files of my iTunes library, almost desesperated. Now, everything is perfect.

You have made my day better !

Thierry

lucamar

  • Hero Member
  • *****
  • Posts: 4217
Re: Do any Lazarus libraries contain html and url decode/encode functions?
« Reply #5 on: August 02, 2019, 06:10:47 pm »
For completeness sake, there are URI and HTML parsers in fcl-base and fcl-xml, which are included with the base Free Pascal, and (IIRC) in Lazarus's LazUtils package.
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.

m.abudrais

  • Jr. Member
  • **
  • Posts: 62
@fabienwang thank you
that function is helpful :)

Leledumbo

  • Hero Member
  • *****
  • Posts: 8836
  • Programming + Glam Metal + Tae Kwon Do = Me
@fabienwang thank you
that function is helpful :)
Back in the day, the functionality might be missing. Today, you have httpprotocol.HTTPEncode and httpprotocol.HTTPDecode, no need to code yourself.

cfriisha

  • Newbie
  • Posts: 1
Thanks a lot Leledumbo. Nice collection of functions in httpprotocol.

Just became a member of this forum.
It is 35 year ago I last worked with Pascal.
I must admit I love the clarity and usability of Pascal.It doesn't pretend to be smart and invent more and more techniques to the language.

 

TinyPortal © 2005-2018