Code: Select all
;-TOP
; By mk-soft
; v1.04
; Update 20.07.2018
EnableExplicit
DeclareModule MyPrintf
; Dummy Variables (EnableExplizit)
Global _arg1, _arg2, _arg3, _arg4, _arg5, _arg6, _arg7, _arg8
Threaded *__Result
*__Result = AllocateMemory(4096)
Macro Printf(Result, sFormat, _arg1, _arg2=_arg2, _arg3=_arg3, _arg4=_arg4, _arg5=_arg5, _arg6=_arg6, _arg7=_arg7, _arg8=_arg8)
CompilerSelect #PB_Compiler_OS
CompilerCase #PB_OS_Windows
ImportC ""
CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
__Printf_#MacroExpandedCount(*pResult, nBytes, sFMT.p-UTF8, sBugfix.s, _arg1, _arg2, _arg3, _arg4, _arg5, _arg6, _arg7, _arg8) As "_snprintf"
CompilerElse
__Printf_#MacroExpandedCount(*pResult, nBytes, sFMT.p-UTF8, sBugfix.s, _arg1, _arg2, _arg3, _arg4, _arg5, _arg6, _arg7, _arg8) As "__snprintf"
CompilerEndIf
EndImport
CompilerCase #PB_OS_MacOS
ImportC ""
__Printf_#MacroExpandedCount(*pResult, nBytes, sFMT.p-UTF8, sBugfix.s, _arg1, _arg2, _arg3, _arg4, _arg5, _arg6, _arg7, _arg8) As "_snprintf"
EndImport
CompilerCase #PB_OS_Linux
ImportC ""
__Printf_#MacroExpandedCount(*pResult, nBytes, sFMT.p-UTF8, sBugfix.s, _arg1, _arg2, _arg3, _arg4, _arg5, _arg6, _arg7, _arg8) As "snprintf"
EndImport
CompilerEndSelect
__Printf_#MacroExpandedCount(*__Result, MemorySize(*__Result) - 1, "%s" + sFormat, #Empty$, _arg1, _arg2, _arg3, _arg4, _arg5, _arg6, _arg7, _arg8)
Result = PeekS(*__Result, -1, #PB_UTF8)
EndMacro
EndDeclareModule
Module MyPrintf
; Nothing
EndModule
UseModule MyPrintf
Define t1.s = "Hello World! (Only Windows)"
Define t2 = UTF8("Hello World!")
Define i1.i = 1234567
Define d1.d = #PI
Define d2.d = 12345.6789
Define Result.s
Debug "Only Window support '%ls'"
Printf(Result, "Result: %ls / i1 = %i / d1 = %f / d2 = %f", t1, i1, d1.d, d2.d)
Debug Result
Debug "MacOS support UTF8"
Printf(Result, "Result: %s / d1 = %f / i1 = %i / d2 = %f", t2, d1.d, i1, d2.d)
Debug Result
FreeMemory(t2)
Debug "Float Not support, only double on x86 And x64"
Define f1.f = 123.45
Printf(Result, "Result: f1 = %f", f1.f)
Debug Result