Macro for Print + Print #

New to FreeBASIC? Post your questions here.
Post Reply
fzabkar
Posts: 154
Joined: Sep 29, 2018 2:52
Location: Australia

Macro for Print + Print #

Post by fzabkar »

Would it be possible to define a macro, PrintC, such that this ...

Code: Select all

PrintC #f, "text"; "text2" & "text3", data
... resolves to ...

Code: Select all

Print #f, "text"; "text2" & "text3", data
Print "text"; "text2" & "text3", data
Would this do it? Is there a better way?

Code: Select all

#macro PrintC( a, b... )

 Print a, b
 Print b

#end macro
fzabkar
Posts: 154
Joined: Sep 29, 2018 2:52
Location: Australia

Re: Macro for Print + Print #

Post by fzabkar »

This works:

Code: Select all

#macro PrintC( a, b... )

 Print a, b
 Print b

#endmacro

Dim bdata as byte = 41
dim f as long

f = freefile

open "c:\temp\junk.txt" for output as #f

PrintC( #f, "text"; "text2" & "text3", bdata )

close #f

sleep
dodicat
Posts: 7993
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Macro for Print + Print #

Post by dodicat »

You can do it in one line

Code: Select all

#include "file.bi"

#define printC( stuff... ) print stuff 

Dim bdata as byte = 41
dim f as long

f = freefile

open "junk.txt" for output as #f

PrintC( #f, "text "; "text2" & "text3", bdata,chr(10)+"File status:" )

close #f
shell "type junk.txt"
kill "junk.txt"

printc (iif(fileexists("junk.txt"),"Goodbye, press any key, delete file manually","Goodbye, press any key, file deleted"))

sleep 
fzabkar
Posts: 154
Joined: Sep 29, 2018 2:52
Location: Australia

Re: Macro for Print + Print #

Post by fzabkar »

I want to write the same "stuff" to both the console and the specified file. I have a program which writes updates to a log file, and I want these updates to be echoed on the screen while the program is running.
coderJeff
Site Admin
Posts: 4354
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: Macro for Print + Print #

Post by coderJeff »

Here's a variation. Personally, I would probably use a different name like 'printC' as suggested, or whatever. But completely replacing the behaviour of print was fun to play with.

Code: Select all

'' replace 'print' behaviour so that any print to a file will also print same text to screen

'' undefine 'print' so we can replace it 
'' trick is: we can still use '?' for the built-in print.
#undef print

'' ignore the file number and print to console
#macro print_skip_filenum( filenum, stuff... )
    ? stuff
#endmacro 

'' PRINT [#filenum,] ...
'' perform 'print' as-is or if it writes to a file number, then also print to console
'' in this context, the '?' makes the parens '(' and ')' optional to use 
#macro print ? ( stuff... )
    '' could be to console or file so print as-is
    ? stuff
    '' starts with #filenum? then also print to console 
    #if (__fb_arg_count__(stuff) > 1) andalso (asc(__fb_quote__((stuff)),2) = asc("#"))
        print_skip_filenum( stuff )
    #endif
#endmacro 

dim bdata as byte = 41
dim f as long = freefile

print "TEST:"        '' screen only
print "------------" 
open "junk.txt" for output as #f
print #f, "START"  '' screen and file
print #f, "print to both file and screen"
print #f,  '' blank line
print #f,, '' tab indent
print #f, "tab indent"
print #f, "text1 "; "text2 " & "text3 ", bdata, chr(10) + "File status: ";
print #f, "DONE"
print #f, "END"
close #f
print "------------"
print
print
print "RESULTS:"
print "------------"
shell "type junk.txt"

kill "junk.txt"
print "------------"

sleep
fzabkar
Posts: 154
Joined: Sep 29, 2018 2:52
Location: Australia

Re: Macro for Print + Print #

Post by fzabkar »

Thanks. Very clever.

Actually, redefining the Print command will be very useful to me. That's because my software usually parses firmware modules, and the structure of these modules changes as new versions emerge. These new versions will usually break my code. Unfortunately, if my program crashes, the log file is not closed, so I don't see where the crash occurs (because the output is not echoed to the console). I can now add your macro to my existing programs as a debugging aid, without needing to make any other changes.

Thanks again.
Last edited by fzabkar on May 28, 2023 19:50, edited 1 time in total.
dodicat
Posts: 7993
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Macro for Print + Print #

Post by dodicat »

Just one liner again

Code: Select all

#include "file.bi"

#define printC( filenumber,stuff... ) print filenumber,stuff: print stuff

Dim bdata As Byte = 41
Dim f As Long
color 4
f = Freefile
Open "junk.txt" For Output As #f
PrintC( #f, "text "; "text2" & "text3", bdata)
Close #f

Color 15
Shell "type junk.txt"
Kill "junk.txt"

Print Iif(Fileexists("junk.txt"),"Goodbye, press any key, delete file manually","Goodbye, press any key, file deleted")

Sleep 
fzabkar
Posts: 154
Joined: Sep 29, 2018 2:52
Location: Australia

Re: Macro for Print + Print #

Post by fzabkar »

Just FYI, this macro trick doesn't work in earlier versions of the compiler, so I expect that it is not guaranteed to work in future versions.
coderJeff
Site Admin
Posts: 4354
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: Macro for Print + Print #

Post by coderJeff »

fzabkar wrote: May 31, 2023 21:45 Just FYI, this macro trick doesn't work in earlier versions of the compiler, so I expect that it is not guaranteed to work in future versions.
I don't understand. Which trick? And why the expectation it won't work in future? If we intentionally break user code in a future release it should be justified - of course opinions vary.
fzabkar
Posts: 154
Joined: Sep 29, 2018 2:52
Location: Australia

Re: Macro for Print + Print #

Post by fzabkar »

I was originally using Version 1.07.1.

Code: Select all

Command executed:
"C:\FreeBASIC\fbc.exe" "C:\FreeBASIC\FBIDE\FBIDETEMP.bas"

Compiler output:
C:\FreeBASIC\FBIDE\FBIDETEMP.bas(27) error 42: Variable not declared, stuff in 'print "TEST:"        '' screen only'
C:\FreeBASIC\FBIDE\FBIDETEMP.bas(27) error 3: Expected End-of-Line, found 'stuff' in 'print "TEST:"        '' screen only'
C:\FreeBASIC\FBIDE\FBIDETEMP.bas(27) error 7: Expected ')', found '(' in 'print "TEST:"        '' screen only'
C:\FreeBASIC\FBIDE\FBIDETEMP.bas(28) error 9: Expected expression, found 'stuff' in 'print "------------"'
C:\FreeBASIC\FBIDE\FBIDETEMP.bas(28) error 3: Expected End-of-Line, found 'stuff' in 'print "------------"'
C:\FreeBASIC\FBIDE\FBIDETEMP.bas(28) error 7: Expected ')', found '(' in 'print "------------"'
C:\FreeBASIC\FBIDE\FBIDETEMP.bas(30) error 9: Expected expression, found 'stuff' in 'print #f, "START"  '' screen and file'
C:\FreeBASIC\FBIDE\FBIDETEMP.bas(30) error 3: Expected End-of-Line, found 'stuff' in 'print #f, "START"  '' screen and file'
C:\FreeBASIC\FBIDE\FBIDETEMP.bas(30) error 7: Expected ')', found '(' in 'print #f, "START"  '' screen and file'
C:\FreeBASIC\FBIDE\FBIDETEMP.bas(31) error 9: Expected expression, found 'stuff' in 'print #f, "print to both file and screen"'
C:\FreeBASIC\FBIDE\FBIDETEMP.bas(31) error 133: Too many errors, exiting

Results:
Compilation failed

System:
FBIde: 0.4.6
fbc:   FreeBASIC Compiler - Version 1.07.1 (2019-09-27), built for win32 (32bit)
OS:    Windows NT 6.2 (build 9200)
I'm now using Version 1.10.0.

Code: Select all

C:\FreeBASIC>fbc32 -version
FreeBASIC Compiler - Version 1.10.0 (2023-05-14), built for win32 (32bit)
Copyright (C) 2004-2023 The FreeBASIC development team.
standalone
Post Reply