Recent

Author Topic: Where is the implementation of WriteLn()  (Read 16264 times)

rvk

  • Hero Member
  • *****
  • Posts: 6111
Re: Where is the implementation of WriteLn()
« Reply #15 on: January 16, 2018, 12:07:52 am »
Say, we had a function with all (or as much/many as possible) features and frills of WriteLn() but one that returned a string. Lets call it WriteLnAsString().
Well, there is. WriteStr() :) You'll have to ask the Delphi gurus why this can't be done in Delphi.

Even if we could assemble some source code for you (which would do compiler magic), which we can't, it wouldn't be compatible with Delphi. It can't because it's too much up to the compiler (which is not open source in Delphi). And for FPC you've already have writestr().

Quote
Yet, WriteLn is blocking me --or, bothering me-- as the current proposals will only add further complexity to the code.
I have to say, I have come to hate WriteLn with an immesurable passion...
I have never used it. But the danm thing comes up in legacy code.
And that's why you need to REWRITE all those calls. No modern function exists with those exact possibilities. Using a function like it... You can just as well capture the standard output or use writestr. Capturing the standard output is the easiest and most invisible to the original code. (You don't need something like writeln(f,...). But if you are going to put every string in a different editbox, writestr would be your best bet.

Making it compatible with Delphi would.mean you need to ask there for a writestr or rewrite all the calls to modern commands.
« Last Edit: January 16, 2018, 12:10:07 am by rvk »

kupferstecher

  • Hero Member
  • *****
  • Posts: 583
Re: Where is the implementation of WriteLn()
« Reply #16 on: January 16, 2018, 02:36:57 am »
And that's why you need to REWRITE all those calls.

@adem0x: You could write a little precompiler.
Translating
Code: Pascal  [Select][+][-]
  1. writeln('hello', ' ', 'world', 2018);
to
Code: Pascal  [Select][+][-]
  1. adem0xwriteln('hello' + ' ' + 'world' + inttostr(2018));

You only have to run it once when you do the porting of third party code.

adem0x

  • New Member
  • *
  • Posts: 17
Re: Where is the implementation of WriteLn()
« Reply #17 on: January 16, 2018, 06:46:13 am »
which we can't, it wouldn't be compatible with Delphi. It can't because it's too much up to the compiler (which is not open source in Delphi).

I am not sure if this would be the case. It seems no one has tried but we all assume that.

I admit, it would be beyond my capabilities; that's why I would gladly pay someone to do it for all of us.

Delphi compatibility would be desired but those parts that cannot be can be excluded using compiler directives.

adem0x

  • New Member
  • *
  • Posts: 17
Re: Where is the implementation of WriteLn()
« Reply #18 on: January 16, 2018, 06:50:46 am »
@adem0x: You could write a little precompiler.

 :o

It's a little too much for me.

Thaddy

  • Hero Member
  • *****
  • Posts: 14204
  • Probably until I exterminate Putin.
Re: Where is the implementation of WriteLn()
« Reply #19 on: January 16, 2018, 07:07:36 am »
A string type has build in format functions. E.g.
Code: Pascal  [Select][+][-]
  1. uses sysutils;
  2. var
  3.   s string; ='%s, %d';
  4.   sf:string;
  5. begin
  6.   sf:=s.Format(['Hello, World',1024]);
  7.   //or
  8.   sf:=s.format('%s, %d',[Hello,World',1024]);
  9. end;


But you can also hook into textrec and redirect stdout: in that case a derived memo  control works with simple writeln w/o the file handle...I did that once for Delphi.
« Last Edit: January 16, 2018, 07:12:07 am by Thaddy »
Specialize a type, not a var.

adem0x

  • New Member
  • *
  • Posts: 17
Re: Where is the implementation of WriteLn()
« Reply #20 on: January 16, 2018, 07:47:28 am »
I went through the sources and these are the patterns that I need to take care of:
Code: Pascal  [Select][+][-]
  1. WriteLn(ATextFile, ANumber:AInteger)
  2. WriteLn(ATextFile, ANumber:AInteger, AString)
  3. WriteLn(ATextFile, AString)
  4. WriteLn(ATextFile, AString, ANumber)
  5. WriteLn(ATextFile, AString, AString)
  6. WriteLn(ATextFile, AString, AString, AString)
  7. WriteLn(ATextFile, AString:AInteger)
  8. WriteLn(ATextFile, AString:AInteger, AString)

Being a lazy person I am, I hate having to do this; but it seems I have to write a few functions (I am not sure overloading will work) and replace each WriteLn with a corresponding one in the sources manually.

But it appears to be doable.

Did I mention that I hate WriteLn?  :P >:D

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Where is the implementation of WriteLn()
« Reply #21 on: January 16, 2018, 07:52:01 am »
I went through the sources and these are the patterns that I need to take care of:
Code: Pascal  [Select][+][-]
  1. WriteLn(ATextFile, ANumber:AInteger)
  2. WriteLn(ATextFile, ANumber:AInteger, AString)
  3. WriteLn(ATextFile, AString)
  4. WriteLn(ATextFile, AString, ANumber)
  5. WriteLn(ATextFile, AString, AString)
  6. WriteLn(ATextFile, AString, AString, AString)
  7. WriteLn(ATextFile, AString:AInteger)
  8. WriteLn(ATextFile, AString:AInteger, AString)

Being a lazy person I am, I hate having to do this; but it seems I have to write a few functions (I am not sure overloading will work) and replace each WriteLn with a corresponding one in the sources manually.

But it appears to be doable.

Did I mention that I hate WriteLn?  :P >:D
those are file based calls and have nothing to do with the console output they should be compilable as is between all versions of delphi and fpc/lazarus. I see no problem keeping them unless you want to replace the complete text "export" process with some other type of file access then you should not touch it. Keep in mind that if you change the mechanism you need to make sure that the text written is in the correct encoding (Ascii, dos, ansi etc) as well.
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

Thaddy

  • Hero Member
  • *****
  • Posts: 14204
  • Probably until I exterminate Putin.
Re: Where is the implementation of WriteLn()
« Reply #22 on: January 16, 2018, 08:44:32 am »
Apart from the syntax errors ( ':' instead of ' ,') that use of writeln is fully Delphi compatible.
Specialize a type, not a var.

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Where is the implementation of WriteLn()
« Reply #23 on: January 16, 2018, 08:48:32 am »
Apart from the syntax errors ( ':' instead of ' ,') that use of writeln is fully Delphi compatible.
its not an error its the digit precision modifier without the scale modifier. that forces a right aligned output with space on the empty digits on the left of the number, if memory serves me right.
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

Thaddy

  • Hero Member
  • *****
  • Posts: 14204
  • Probably until I exterminate Putin.
Re: Where is the implementation of WriteLn()
« Reply #24 on: January 16, 2018, 08:51:14 am »
Do yo believe that? Then he should simply read the manual on writeln. Either Delphi's or Freepascal's. Not write/writeln is compiler magic and not approachable for outside code unless you adapt the compiler itself.... They are not simple functions and the formatting features are not exported. And the implementation detail between FreePascal and Delphi likely is considerably different, though the results are exactly the same.
« Last Edit: January 16, 2018, 08:53:30 am by Thaddy »
Specialize a type, not a var.

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Where is the implementation of WriteLn()
« Reply #25 on: January 16, 2018, 08:56:48 am »
Do yo believe that? Then he should simply read the manual on writeln. Either Delphi's or Freepascal's. Not write/writeln is compiler magic and not approachable for outside code unless you adapt the compiler itself.... They are not simple functions and the formatting features are not exported.
Those use cases of writeln (as you said already) are compatible between FPC and delphi and should not need any change at all. But yes I agree with you he must read delphi manual for writeln, fpc manual does not have any information about the syntax of the command it self.
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

Thaddy

  • Hero Member
  • *****
  • Posts: 14204
  • Probably until I exterminate Putin.
Re: Where is the implementation of WriteLn()
« Reply #26 on: January 16, 2018, 10:21:06 am »
Do yo believe that? Then he should simply read the manual on writeln. Either Delphi's or Freepascal's. Not write/writeln is compiler magic and not approachable for outside code unless you adapt the compiler itself.... They are not simple functions and the formatting features are not exported.
Those use cases of writeln (as you said already) are compatible between FPC and delphi and should not need any change at all. But yes I agree with you he must read delphi manual for writeln, fpc manual does not have any information about the syntax of the command it self.
It has... under write.... and writeln refers to write... Heavy users should read ff'ing manuals.... Which is indeed not complete https://www.freepascal.org/docs-html/rtl/system/write.html
Plz file a bug against documentation. The Delphi documentation is also not complete, but better. Code is 100% interchangeable.
[edit]
Just because I happen to know all these things, doesn't mean they are documented correctly. Most of the time FPC documentation is better than Delphi.
« Last Edit: January 16, 2018, 10:26:48 am by Thaddy »
Specialize a type, not a var.

adem0x

  • New Member
  • *
  • Posts: 17
Re: Where is the implementation of WriteLn()
« Reply #27 on: January 16, 2018, 05:13:30 pm »
The Delphi documentation is also not complete, but better.

But, at times only marginally.

I couldn't, for example, find out how this works:

WriteLn('text1', 'text2');

Are they concatenated with a space between them or a tab?

rvk

  • Hero Member
  • *****
  • Posts: 6111
Re: Where is the implementation of WriteLn()
« Reply #28 on: January 16, 2018, 05:18:40 pm »
Are they concatenated with a space between them or a tab?
No space, no tab. They are just printed after each other.

Thaddy

  • Hero Member
  • *****
  • Posts: 14204
  • Probably until I exterminate Putin.
Re: Where is the implementation of WriteLn()
« Reply #29 on: January 16, 2018, 06:46:24 pm »
Same as in his Delphi code...... :D >:( Am I allowed to get grumpy?
Formatting algorithms do not tend or try to second guess the user's intention. They do what they are told.

(But:let me guess.... we have a font issue? as usual...)
« Last Edit: January 16, 2018, 06:54:39 pm by Thaddy »
Specialize a type, not a var.

 

TinyPortal © 2005-2018