Author Topic: SScanF  (Read 795 times)

Zvoni

  • Hero Member
  • *****
  • Posts: 3574
Re: SScanF
« Reply #15 on: August 26, 2026, 12:27:50 pm »
I do not see that as an improvement. One space extra and the array length becomes three, not two. (unless TStringsplitoptions.Exclide empty is specified.
If the second string needs to be quoted, you can requote with s.QuotedString('"');

Improvement as in: I don't care if there are double quotes in the source-string or not.
My Split-Delimiter is SPACE, not Doublequote, so i don't have to juggle ExcludeEmpty and Trimming any resulting strings.
Yes, "requoting" the remaining String is easy enough.
1) Check with "Pos"-Function if source contains Doublequotes
2) Execute my algorithm
3) Requote the Join-Result if 1) was true

I could even leave out the "Replace"-call to remove doublequotes and requote later.
The Doublequotes would just be a part of the Strings

I just used the StartingIndex-Argument of the Join-Function as in:
Take Member 0 of the Array. That's your Errorcode. Everything else from StartingIndex 1 on is the remaining String.
« Last Edit: August 26, 2026, 12:34:26 pm by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Thaddy

  • Hero Member
  • *****
  • Posts: 19950
  • Pointless static linking author
Re: SScanF
« Reply #16 on: August 26, 2026, 02:52:10 pm »
Isn't that overkill for the original question + its fixed format? I also do overkill sometimes, as you know.
Given a fixed format, which it likely is, my second example should be correct.
Let's see what OP thinks...
Let's agree that sscanf is not the solution. He can pick and choose a solution by now.
Any "programmer" that knows only one programming language is not a programmer

ginoo

  • Full Member
  • ***
  • Posts: 169
Re: SScanF
« Reply #17 on: August 26, 2026, 05:15:23 pm »
I solved it by calling sscanf from libc

sscanf(PAnsiChar(esitoInvioMail), '%d "%[^"]"', @codiceErr, @Msg);

Thaddy

  • Hero Member
  • *****
  • Posts: 19950
  • Pointless static linking author
Re: SScanF
« Reply #18 on: August 26, 2026, 06:30:37 pm »
I don't think that is a wise decision (you introduced a dependency on external code), but hey, if it works, fine with me.

Just curious: why do you think my code is not enough? Did you even test Zvoni's code and my code?
Because both solutions are more lightweight and more cross-platform than linking in a dangerous! C library for no obvious reason.

If the format is like I suspected: a fixed format, you are making a mistake.
Even if it works: Freepascal's sscanf is the same as C, which means you did not pay attention.
And certainly my solution is more safe and cross-platform.

If the C version works, the Pascal version works too, so in that case you have been wasting Zvoni's and my time.

Zvoni's solution just looks more complex because he tried to solve a problem that isn't there.
My solution just "works" for the format you gave us and is a one liner.

Don't dismiss my code because it is simple: I am known to write good code that is simple. See cdbc's comment.. and it is production ready, while avoiding the pitfalls of sscanf....
« Last Edit: August 26, 2026, 06:52:51 pm by Thaddy »
Any "programmer" that knows only one programming language is not a programmer

Zvoni

  • Hero Member
  • *****
  • Posts: 3574
Re: SScanF
« Reply #19 on: August 27, 2026, 08:22:41 am »
I solved it by calling sscanf from libc

sscanf(PAnsiChar(esitoInvioMail), '%d "%[^"]"', @codiceErr, @Msg);

Dear Lord........
what Thaddy said....Not our monkeys.....

Quote
Zvoni's solution just looks more complex because he tried to solve a problem that isn't there.
Yes and no.
The "Source" he wants to parse is an answer of a mailer daemon.
This time it's invalid recipients, next time it's "Attachment too big", "Mailbox full", or whatever else....

My Approach was more of a generic way, not relying on the fact, that the daemon is configured correctly.

One malformed answer (e.g. missing double quotes) and it stops working
« Last Edit: August 27, 2026, 08:26:08 am by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Thaddy

  • Hero Member
  • *****
  • Posts: 19950
  • Pointless static linking author
Re: SScanF
« Reply #20 on: August 27, 2026, 08:40:27 am »
The "Source" he wants to parse is an answer of a mailer daemon.
Ah, I missed that. That makes your efforts clear and explains why you took the trouble.
I withdraw the isn't part. I have seen the light  8-)
« Last Edit: August 27, 2026, 08:43:32 am by Thaddy »
Any "programmer" that knows only one programming language is not a programmer

Zvoni

  • Hero Member
  • *****
  • Posts: 3574
Re: SScanF
« Reply #21 on: August 27, 2026, 09:58:04 am »
*snip*
I withdraw the isn't part. I have seen the light  8-)
"Blinded by the Light"...*sing*.....
No....wait...hang on......
 :P :P :P :P
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

ginoo

  • Full Member
  • ***
  • Posts: 169
Re: SScanF
« Reply #22 on: August 27, 2026, 10:43:12 am »
Thank you all, and I'm looking at your solutions. I just suggested another one before.
I'm looking at yours.
Thanks again.

Thaddy

  • Hero Member
  • *****
  • Posts: 19950
  • Pointless static linking author
Re: SScanF
« Reply #23 on: August 28, 2026, 02:46:15 pm »
Above solutions are better but:
Another solution returns the string quoted and protects against tabs and space errors:
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}{$H+}
  2. uses classes;
  3. var
  4.   s:string ='554 "554 5.5.1 Error: no valid recipients"';
  5.   L:TStringlist;
  6. begin
  7.   L:= TStringlist.create;
  8.   try
  9.     ExtractStrings([#9,#32,#37,#39],[#9,#32],PAnsiChar(s),L);
  10.     writeln(L.text);
  11.   finally
  12.     L.Free;
  13.   end;
  14. end.
This outputs:
Code: [Select]
554
"554 5.5.1 Error: no valid recipients"
« Last Edit: August 28, 2026, 02:59:01 pm by Thaddy »
Any "programmer" that knows only one programming language is not a programmer

 

TinyPortal © 2005-2018